Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java

Issue 133943002: Gamepad API support for chrome on android (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..3ba0fe2b11d9b3709e1fd011255caff9cd802f1e
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java
@@ -0,0 +1,409 @@
+/*
+ * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of NVIDIA CORPORATION nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+package org.chromium.content.browser;
+
+import android.os.SystemClock;
+import android.view.InputDevice;
+import android.view.InputDevice.MotionRange;
+import android.view.KeyCharacterMap;
+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;
+
+ // 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??
+ };
+
+ // Standard gamepad buttons.
+ private static class StandardButtons {
+ public static final int BUTTON_A = 0;
+ public static final int BUTTON_B = 1;
+ public static final int BUTTON_X = 2;
+ public static final int BUTTON_Y = 3;
+ public static final int LEFT_TRIGGER = 4;
+ public static final int RIGHT_TRIGGER = 5;
+ public static final int BOTTOM_LEFT_TRIGGER = 6;
+ public static final int BOTTOM_RIGHT_TRIGGER = 7;
+ public static final int SELECT = 8;
+ public static final int START = 9;
+ public static final int LEFT_THUMB = 10;
+ public static final int RIGHT_THUMB = 11;
+ public static final int DPAD_UP = 12;
+ public static final int DPAD_DOWN = 13;
+ public static final int DPAD_LEFT = 14;
+ public static final int DPAD_RIGHT = 15;
+ public static final int MODE = 16;
+ public static final int MAX_BUTTONS = 17;
+ }
+
+ // Standard gamepad axes.
+ private static class StandardAxes {
+ public static final int AXIS_0 = 0;
+ public static final int AXIS_1 = 1;
+ public static final int AXIS_2 = 2;
+ public static final int AXIS_3 = 3;
+ public static final int MAX_AXES = 4;
+ }
+
+ // We guess the gamepad inputDevice type from its name and then map it to the
+ // standard gamepad if it is one of the known gamepads.
+ protected enum GamepadType {
+ Shield,
+ XBox360,
+ Unknown
+ }
+ protected GamepadType mGamepadType;
+
+ // Following values should be changed if the gamepad layout changes
xwang 2014/01/14 03:03:22 Will it better to separate the device specific stu
SaurabhK 2014/01/15 16:57:08 Will add another class in next patch set.
+ // or frameworks exposes different axes / buttons for a particular gamepad.
+ // mRawShieldAxes is the total raw axes on known shield gamepad.
+ private static final int mRawShieldAxes = 10;
+ // mRawShieldButtons is the total raw buttons on known shield gamepad.
+ private static final int mRawShieldButtons = 10;
+ // mRawXBoxAxes is the total raw axes on known XBox gamepad.
+ private static final int mRawXBoxAxes = 10;
+ // mRawXBoxButtons is the total raw buttons on known XBox gamepad.
+ private static final int mRawXBoxButtons = 9;
+
+ // 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.
+ KeyCharacterMap kcm = inputDevice.getKeyCharacterMap();
+ boolean[] deviceKeys = kcm.deviceHasKeys(standardGamepadKeyCodes);
+ int j;
+ for (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() {}
+
+ 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 != GamepadType.Unknown) {
+ return mapToStandardGamepadAxes(mAxisValues);
+ }
+ 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 != GamepadType.Unknown) {
+ return mapToStandardGamepadButtons(values, mAxisValues);
+ }
+ 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 != GamepadType.Unknown;
+ }
+
+ protected void setGamepadType() {
+ if (isKnownShieldGamepad()) {
+ mGamepadType = GamepadType.Shield;
+ } else if (isKnownXBoxGamepad()) {
+ mGamepadType = GamepadType.XBox360;
+ } else {
+ // TODO: Add support for mapping other common gamepads to the standard gamepads.
+ mGamepadType = GamepadType.Unknown;
+ }
+ }
+
+ private boolean isKnownShieldGamepad() {
+ if (mDeviceName.contains("NVIDIA") && mDeviceName.contains("Controller")) {
+ // Known shield gamepad should have mRawShieldAxes and mRawShieldButtons.
+ if (mAxisValues.length == mRawShieldAxes &&
+ mKeyCodeValueMap.size() == 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 == mRawXBoxAxes && mKeyCodeValueMap.size() == mRawXBoxButtons) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private float[] mapToStandardGamepadAxes(float[] rawAxes) {
+ switch (mGamepadType) {
+ case Shield:
+ return mapShieldAxes(rawAxes);
+ case XBox360:
+ return mapXBox360Axes(rawAxes);
+ }
+ return rawAxes;
+ }
+
+ private float[] mapToStandardGamepadButtons(float[] rawButtons, float[] rawAxes) {
+ switch (mGamepadType) {
+ case Shield:
+ return mapShieldButtons(rawButtons, rawAxes);
+ case XBox360:
+ return mapXBox360Buttons(rawButtons, rawAxes);
+ }
+ return rawAxes;
+ }
+
+ private float negativeAxisValueAsButton(float input) {
+ return (input < -0.5f) ? 1.f : 0.f;
+ }
+
+ private float positiveAxisValueAsButton(float input) {
+ return (input > 0.5f) ? 1.f : 0.f;
+ }
+
+ /**
+ * Method for mapping Nvidia gamepad axis values to
+ * standard gamepad axis values.
+ */
+ private float[] mapShieldAxes(float[] rawAxes) {
+ float[] values = new float[StandardAxes.MAX_AXES];
+ // Standard gamepad can have only four axes.
+ values[StandardAxes.AXIS_0] = rawAxes[0];
+ values[StandardAxes.AXIS_1] = rawAxes[1];
+ values[StandardAxes.AXIS_2] = rawAxes[4];
+ values[StandardAxes.AXIS_3] = rawAxes[5];
+ return values;
+ }
+
+ /**
+ * Method for mapping Nvidia gamepad axis and button values
+ * to standard gamepad button values.
+ */
+ public float[] mapShieldButtons(float[] rawButtons, float[] rawAxes) {
+ float[] values = new float[StandardButtons.MAX_BUTTONS];
+ // First four buttons on Nvidia gamepad appear in correct order so no mapping is required.
+ values[StandardButtons.BUTTON_A] = rawButtons[0];
+ values[StandardButtons.BUTTON_B] = rawButtons[1];
+ values[StandardButtons.BUTTON_X] = rawButtons[2];
+ values[StandardButtons.BUTTON_Y] = rawButtons[3];
+ // Third axis on Nvidia gamepad acts as a bottom left trigger button.
+ values[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2];
+ values[StandardButtons.LEFT_TRIGGER] = rawButtons[4];
+ values[StandardButtons.RIGHT_TRIGGER] = rawButtons[5];
+ values[StandardButtons.SELECT] = rawButtons[6];
+ values[StandardButtons.START] = rawButtons[7];
+ values[StandardButtons.LEFT_THUMB] = rawButtons[8];
+ values[StandardButtons.RIGHT_THUMB] = rawButtons[9];
+ // Seventh axis on Nvidia gamepad acts as a bottom right trigger button.
+ values[StandardButtons.BOTTOM_RIGHT_TRIGGER] = rawAxes[6];
+ // Ninth axis on Nvidia gamepad acts as directional pad right and left button.
+ // Positive axis value indicates dpad right.
+ // Negative value indicates dpad left.
+ values[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(rawAxes[8]);
+ values[StandardButtons.DPAD_LEFT] = negativeAxisValueAsButton(rawAxes[8]);
+ // Tenth axis on Nvidia gamepad acts as directional pad up and down button.
+ // Positive axis value indicates dpad down.
+ // Negative value indicates dpad up.
+ values[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(rawAxes[9]);
+ values[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]);
+ // Other standard buttons are either not present on the gamepad or not exposed to an
+ // application.
+ values[StandardButtons.MODE] = 0.0f;
+ return values;
+ }
+
+ /**
+ * Method for mapping Microsoft XBox 360 gamepad axis values to
+ * standard gamepad axis values.
+ */
+ private float[] mapXBox360Axes(float[] rawAxes) {
+ float[] values = new float[StandardAxes.MAX_AXES];
+ // Standard gamepad can have only four axes.
+ values[StandardAxes.AXIS_0] = rawAxes[0];
+ values[StandardAxes.AXIS_1] = rawAxes[1];
+ values[StandardAxes.AXIS_2] = rawAxes[4];
+ values[StandardAxes.AXIS_3] = rawAxes[5];
+ return values;
+ }
+
+ /**
+ * Method for mapping Microsoft XBox 360 gamepad axis and button values
+ * to standard gamepad button values.
+ */
+ private float[] mapXBox360Buttons(float[] rawButtons, float[] rawAxes) {
+ float[] values = new float[StandardButtons.MAX_BUTTONS];
+ // First four buttons on Microsoft XBox 360 gamepad appear in correct order so no mapping
+ // is required.
+ values[StandardButtons.BUTTON_A] = rawButtons[0];
+ values[StandardButtons.BUTTON_B] = rawButtons[1];
+ values[StandardButtons.BUTTON_X] = rawButtons[2];
+ values[StandardButtons.BUTTON_Y] = rawButtons[3];
+ // Third axis on Microsoft XBox 360 gamepad acts as a left bottom shoulder button.
+ values[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2];
+ values[StandardButtons.LEFT_TRIGGER] = rawButtons[4];
+ values[StandardButtons.RIGHT_TRIGGER] = rawButtons[5];
+ values[StandardButtons.START] = rawButtons[6];
+ values[StandardButtons.LEFT_THUMB] = rawButtons[7];
+ values[StandardButtons.RIGHT_THUMB] = rawButtons[8];
+ // Seventh axis on Microsoft XBox 360 gamepad acts as a right bottom shoulder button.
+ values[StandardButtons.BOTTOM_RIGHT_TRIGGER] = rawAxes[6];
+ // Ninth axis on Microsoft XBox 360 gamepad acts as directional pad right and left button.
+ // Positive axis value indicates dpad right.
+ // Negative value indicates dpad left.
+ values[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(rawAxes[8]);
+ values[StandardButtons.DPAD_LEFT] = negativeAxisValueAsButton(rawAxes[8]);
+ // Tenth axis on Microsoft XBox 360 gamepad acts as directional pad up and down button.
+ // Positive axis value indicates dpad down.
+ // Negative value indicates dpad up.
+ values[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(rawAxes[9]);
+ values[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]);
+ // Other standard buttons are either not present on the gamepad or not exposed to an
+ // application.
+ values[StandardButtons.SELECT] = 0.0f;
+ values[StandardButtons.MODE] = 0.0f;
+ return values;
+ }
+
+}

Powered by Google App Engine
This is Rietveld 408576698