Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ae34d48e9b5fa22873c157938cf32f2f8b217289 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| @@ -0,0 +1,202 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.os.SystemClock; |
| +import android.view.InputDevice; |
| +import android.view.InputDevice.MotionRange; |
| +import android.view.KeyEvent; |
| +import android.view.MotionEvent; |
| + |
| +import java.util.Iterator; |
| +import java.util.LinkedHashMap; |
| + |
| +/* |
| + * Class to manage information related to each connected gamepad device. |
| + */ |
| + |
| +class GamepadDevice { |
| + // An id for the gamepad. |
| + protected int mDeviceId; |
| + // The index of the gamepad in the Navigator. |
| + protected int mDeviceIndex; |
| + // An identification string for the gamepad. |
| + protected String mDeviceName; |
| + // Last time the data for this gamepad was updated. |
| + protected long mTimestamp; |
| + |
| + // Array of values for all axes of the gamepad. |
| + // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. |
| + // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 |
| + // should correspond to "down" or "right". |
| + protected float[] mAxisValues; |
| + // Array of axes ids. |
| + private int[] mAxes; |
| + |
| + protected GamepadMappings.GamepadType mGamepadType; |
| + |
| + // List of keycodes that can be sent by a standard gamepad. Standard gamepad can have |
| + // buttons as per the spec: |
| + // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping |
| + // Need to maintain this list to get input device capabilities. We query |
| + // input devices with this list and device returns list mentioning which |
| + // Keycodes out of these can be sent by a gamepad device. |
| + private static final int[] standardGamepadKeyCodes = { |
|
bajones
2014/02/28 23:56:11
This is more-or-less a duplicate of the StandardBu
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
I have to
|
| + KeyEvent.KEYCODE_BUTTON_A, // Button 0 |
| + KeyEvent.KEYCODE_BUTTON_B, // Button 1 |
| + KeyEvent.KEYCODE_BUTTON_X, // Button 2 |
| + KeyEvent.KEYCODE_BUTTON_Y, // Button 3 |
| + KeyEvent.KEYCODE_BUTTON_L1, // Button 4 |
| + KeyEvent.KEYCODE_BUTTON_R1, // Button 5 |
| + KeyEvent.KEYCODE_BUTTON_L2, // Button 6 |
| + KeyEvent.KEYCODE_BUTTON_R2, // Button 7 |
| + KeyEvent.KEYCODE_BUTTON_SELECT, // Button 8 |
| + KeyEvent.KEYCODE_BUTTON_START, // Button 9 |
| + KeyEvent.KEYCODE_BUTTON_THUMBL, // Button 10 |
| + KeyEvent.KEYCODE_BUTTON_THUMBR, // Button 11 |
| + KeyEvent.KEYCODE_DPAD_UP, // Button 12 |
| + KeyEvent.KEYCODE_DPAD_DOWN, // Button 13 |
| + KeyEvent.KEYCODE_DPAD_LEFT, // Button 14 |
| + KeyEvent.KEYCODE_DPAD_RIGHT, // Button 15 |
| + KeyEvent.KEYCODE_BUTTON_MODE, // Button 16?? |
| + }; |
| + |
| + // Hash map of supported keycodes and their current value linearly |
| + // normalized to the range [0.0 .. 1.0]. |
| + // 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. |
| + protected LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap<Integer, Float>(); |
| + |
| + GamepadDevice(int index, InputDevice inputDevice) { |
| + mDeviceIndex = index; |
| + mDeviceId = inputDevice.getId(); |
| + mDeviceName = inputDevice.getName(); |
| + mTimestamp = SystemClock.uptimeMillis(); |
| + // Get the total number of axes supported by gamepad. |
| + int totalAxes = 0; |
| + for (MotionRange range : inputDevice.getMotionRanges()) { |
| + if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
| + totalAxes++; |
| + } |
| + } |
| + // Get axis ids and initialize axes values. |
| + mAxes = new int[totalAxes]; |
| + mAxisValues = new float[totalAxes]; |
| + int i = 0; |
| + for (MotionRange range : inputDevice.getMotionRanges()) { |
| + if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
| + mAxes[i] = range.getAxis(); |
| + mAxisValues[i] = 0.0f; |
| + i++; |
| + } |
| + } |
| + // Update supported buttons list. |
| + boolean[] deviceKeys = inputDevice.hasKeys(standardGamepadKeyCodes); |
| + for (int j = 0; j < standardGamepadKeyCodes.length; ++j) { |
| + if (deviceKeys[j]) { |
| + mKeyCodeValueMap.put(standardGamepadKeyCodes[j], 0.0f); |
| + } |
| + } |
| + |
| + // When the user agent recognizes the attached inputDevice, it is recommended |
| + // that it be remapped to a canonical ordering when possible. Devices |
| + // that are not recognized should still be exposed in their raw form. |
| + setGamepadType(); |
| + } |
| + |
| + GamepadDevice() {} |
| + |
| + protected void setGamepadType() { |
|
bajones
2014/02/28 23:56:11
I'd rather this logic occur in the GamepadMappings
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
Done.
|
| + if (isKnownShieldGamepad()) { |
| + mGamepadType = GamepadMappings.GamepadType.Shield; |
| + } else if (isKnownXBoxGamepad()) { |
| + mGamepadType = GamepadMappings.GamepadType.XBox360; |
| + } else { |
| + // TODO: Add support for mapping other common gamepads to the standard gamepads. |
| + mGamepadType = GamepadMappings.GamepadType.Unknown; |
| + } |
| + } |
| + |
| + private boolean isKnownShieldGamepad() { |
| + if (mDeviceName.contains("NVIDIA") && mDeviceName.contains("Controller")) { |
|
bajones
2014/02/28 23:56:11
This feels imprecise. If NVIDIA ever makes another
SaurabhK
2014/03/11 17:39:27
On 2014/02/28 23:56:11, bajones wrote:
Done.
|
| + // Known shield gamepad should have mRawShieldAxes and mRawShieldButtons. |
| + if (mAxisValues.length == GamepadMappings.mRawShieldAxes && |
| + mKeyCodeValueMap.size() == GamepadMappings.mRawShieldButtons) { |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + private boolean isKnownXBoxGamepad() { |
| + if (mDeviceName.contains("Microsoft") && mDeviceName.contains("X-Box") && |
| + mDeviceName.contains("360") && mDeviceName.contains("pad")) { |
| + // Known XBox gamepad should have mRawXBoxAxes and mRawXBoxButtons. |
| + if (mAxisValues.length == GamepadMappings.mRawXBoxAxes && |
| + mKeyCodeValueMap.size() == GamepadMappings.mRawXBoxButtons) { |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + public int getId() { return mDeviceId; } |
| + |
| + public String getName() { return mDeviceName; } |
| + |
| + public int getIndex() { return mDeviceIndex; } |
| + |
| + public long getTimestamp() { return mTimestamp; } |
| + |
| + public float[] getAxes() { |
| + if ( mGamepadType != GamepadMappings.GamepadType.Unknown) { |
| + return GamepadMappings.mapToStandardGamepadAxes(mAxisValues, mGamepadType); |
| + } |
| + return mAxisValues; |
| + } |
| + |
| + public float[] getButtons() { |
| + int size = mKeyCodeValueMap.size(); |
| + float[] values = new float[size]; |
| + int i = 0; |
| + Iterator itr = mKeyCodeValueMap.values().iterator(); |
| + while (itr.hasNext()) { |
| + values[i++] = (Float)itr.next(); |
| + } |
| + if ( mGamepadType != GamepadMappings.GamepadType.Unknown) { |
| + return GamepadMappings.mapToStandardGamepadButtons(values, mAxisValues, mGamepadType); |
| + } |
| + return values; |
| + } |
| + |
| + public boolean updateForEvent(KeyEvent event) { |
| + mTimestamp = SystemClock.uptimeMillis(); |
| + // Ignore event if it is not for standard gamepad key. |
| + int keyCode = event.getKeyCode(); |
| + if (mKeyCodeValueMap.get(keyCode) == null) { |
| + return true; |
| + } |
| + // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. |
| + if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| + mKeyCodeValueMap.put(keyCode, 1.0f); |
| + } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| + mKeyCodeValueMap.put(keyCode, 0.0f); |
| + } |
| + |
| + return true; |
| + } |
| + |
| + public boolean updateForEvent(MotionEvent event) { |
| + mTimestamp = SystemClock.uptimeMillis(); |
| + // Update axes values. |
| + for (int i = 0; i < mAxes.length; ++i) { |
| + mAxisValues[i] = event.getAxisValue(mAxes[i]); |
| + } |
| + return true; |
| + } |
| + |
| + public boolean isKnownGamepadLayout() { |
| + return mGamepadType != GamepadMappings.GamepadType.Unknown; |
| + } |
| +} |