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..a0c8c5607302e229bb7170baabb15065e70d8516 |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
@@ -0,0 +1,227 @@ |
+// 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 org.chromium.content.browser.input.CanonicalAxisIndex; |
+import org.chromium.content.browser.input.CanonicalButtonIndex; |
+ |
+import java.util.Arrays; |
+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. |
+ private int mDeviceId; |
+ // The index of the gamepad in the Navigator. |
+ private int mDeviceIndex; |
+ // Last time the data for this gamepad was updated. |
+ private long mTimestamp; |
+ // If this gamepad is mapped to standard gamepad? |
+ private String mMapping; |
+ |
+ // 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". |
+ private float[] mAxisValues; |
+ |
+ private float[] mButtonsValues; |
+ |
+ // 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. Therefore we must |
+ // pass the raw Button and raw Axis values. |
+ private float[] mRawButtons; |
+ private float[] mRawAxes; |
+ |
+ // An identification string for the gamepad. |
+ private String mDeviceName; |
+ |
+ // Array of axes ids. |
+ private int[] mAxes; |
+ |
+ // 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 = { |
+ 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. |
+ private LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap<Integer, Float>(); |
+ |
+ GamepadDevice(int index, InputDevice inputDevice) { |
+ mDeviceIndex = index; |
+ mDeviceId = inputDevice.getId(); |
+ mDeviceName = inputDevice.getName(); |
+ mTimestamp = SystemClock.uptimeMillis(); |
+ mMapping = " "; |
+ mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS]; |
+ mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES]; |
+ // 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]; |
+ mRawAxes = new float[totalAxes]; |
+ int i = 0; |
+ for (MotionRange range : inputDevice.getMotionRanges()) { |
+ if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
+ mAxes[i] = range.getAxis(); |
+ mRawAxes[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); |
+ } |
+ } |
+ mRawButtons = new float[mKeyCodeValueMap.size()]; |
+ } |
+ |
+ GamepadDevice() {} |
+ |
+ /** |
+ * Map the axes and buttons of a gamepad device to a standard gamepad format. |
+ */ |
+ public void mapButtonsAndAxes() { |
+ int i = 0; |
+ Iterator itr = mKeyCodeValueMap.values().iterator(); |
+ while (itr.hasNext()) { |
+ mRawButtons[i++] = (Float)itr.next(); |
+ } |
+ mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsValues, mRawAxes, |
jdduke (slow)
2014/04/04 15:10:17
I still don't understand why we're assigning |mMap
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 15:10:17, jdduke wrote:
As earlier
|
+ mRawButtons, |
+ mDeviceName) ? "standard" : " "; |
+ } |
+ |
+ /** |
+ * @return Device Id of the gamepad device. |
+ */ |
+ public int getId() { return mDeviceId; } |
+ |
+ /** |
+ * @return Mapping status of the gamepad device. |
+ */ |
+ public String getMapping() { return mMapping; } |
+ |
+ /** |
+ * @return Device name of the gamepad device. |
+ */ |
+ public String getName() { return mDeviceName; } |
+ |
+ /** |
+ * @return Device index of the gamepad device. |
+ */ |
+ public int getIndex() { return mDeviceIndex; } |
+ |
+ /** |
+ * @return The timestamp when the gamepad device was last interacted. |
+ */ |
+ public long getTimestamp() { return mTimestamp; } |
+ |
+ /** |
+ * @return The axes state of the gamepad device. |
+ */ |
+ public float[] getAxes() { |
+ if (mMapping.equals("standard")) |
+ return mAxisValues; |
+ else |
+ return mRawAxes; |
+ } |
+ |
+ /** |
+ * @return The buttons state of the gamepad device. |
+ */ |
+ public float[] getButtons() { |
+ if (mMapping.equals("standard")) |
+ return mButtonsValues; |
+ else |
+ return mRawButtons; |
+ } |
+ |
+ /** |
+ * Reset the axes and buttons data of the gamepad device everytime gamepad data access is |
+ * paused. |
+ */ |
+ public void clearData() { |
+ Arrays.fill(mAxisValues, 0); |
+ Arrays.fill(mRawAxes, 0); |
+ Arrays.fill(mButtonsValues, 0); |
+ Arrays.fill(mRawButtons, 0); |
+ } |
+ |
+ /** |
+ * Handles key event from the gamepad device. |
+ * @return True if the key event from the gamepad device has been consumed. |
+ */ |
+ public boolean updateForEvent(KeyEvent event) { |
+ // Ignore event if it is not for standard gamepad key. |
+ int keyCode = event.getKeyCode(); |
+ if (mKeyCodeValueMap.get(keyCode) == null) { |
+ return false; |
+ } |
+ // 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); |
+ } |
+ mTimestamp = event.getEventTime(); |
+ |
+ return true; |
+ } |
+ |
+ /** |
+ * Handles motion event from the gamepad device. |
+ * @return True if the motion event from the gamepad device has been consumed. |
+ */ |
+ public boolean updateForEvent(MotionEvent event) { |
+ // Update axes values. |
+ for (int i = 0; i < mAxes.length; ++i) { |
+ mRawAxes[i] = event.getAxisValue(mAxes[i]); |
+ } |
+ mTimestamp = event.getEventTime(); |
+ return true; |
+ } |
+} |