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

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

Issue 307413004: Gamepad: cleanup mappings and add support for Samsung and PS3 gamepads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix axes on Shield and nits Created 6 years, 6 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
« no previous file with comments | « content/public/android/java/src/org/chromium/content/browser/input/GamepadDevice.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java b/content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java
index 7c171ebfcd3c42cab169a8a2dd4b65e6fbd7109d..99e28f9648c0f870a53b0287adb0dc0a684691b5 100644
--- a/content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java
+++ b/content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java
@@ -5,68 +5,107 @@
package org.chromium.content.browser.input;
import android.view.KeyEvent;
+import android.view.MotionEvent;
+
+import org.chromium.base.JNINamespace;
/**
* Class to manage mapping information related to each supported gamepad controller device.
*/
+@JNINamespace("content")
class GamepadMappings {
- public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappedButtons,
+ private static final String NVIDIA_SHIELD_DEVICE_NAME_PREFIX =
+ "NVIDIA Corporation NVIDIA Controller";
+ private static final String MICROSOFT_XBOX_PAD_DEVICE_NAME = "Microsoft X-Box 360 pad";
+ private static final String PS3_SIXAXIS_DEVICE_NAME = "Sony PLAYSTATION(R)3 Controller";
+ private static final String SAMSUNG_EI_GP20_DEVICE_NAME = "Samsung Game Pad EI-GP20";
+
+ public static boolean mapToStandardGamepad(float[] mappedAxes, float[] mappedButtons,
float[] rawAxes, float[] rawButtons, String deviceName) {
- if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) {
- mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
+ if (deviceName.startsWith(NVIDIA_SHIELD_DEVICE_NAME_PREFIX)) {
+ mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
+ return true;
+ } else if (deviceName.equals(MICROSOFT_XBOX_PAD_DEVICE_NAME)) {
+ mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
+ return true;
+ } else if (deviceName.equals(PS3_SIXAXIS_DEVICE_NAME)) {
+ mapPS3SixAxisGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
return true;
- } else if (deviceName.contains("Microsoft X-Box 360 pad")) {
- mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
+ } else if (deviceName.equals(SAMSUNG_EI_GP20_DEVICE_NAME)) {
+ mapSamsungEIGP20Gamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
return true;
}
- mapUnknownGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes);
+ mapUnknownGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
return false;
}
- private static void mapCommonButtons(float[] mappedButtons, float[] rawButtons) {
- mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[KeyEvent.KEYCODE_BUTTON_A];
- mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_B];
- mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_X];
- mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_Y];
- mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_L1];
- mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_R1];
- mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT];
- mappedButtons[CanonicalButtonIndex.BUTTON_START] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_START];
- mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL];
- mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR];
- mappedButtons[CanonicalButtonIndex.BUTTON_META] = rawButtons[KeyEvent.KEYCODE_BUTTON_MODE];
- }
-
- private static void mapDpadButtonsToAxes(float[] mappedButtons, float[] rawAxes) {
- // Negative value indicates dpad up.
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]);
- // Positive axis value indicates dpad down.
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] =
- positiveAxisValueAsButton(rawAxes[9]);
- // Positive axis value indicates dpad right.
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] =
- positiveAxisValueAsButton(rawAxes[8]);
- // Negative value indicates dpad left.
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] =
- negativeAxisValueAsButton(rawAxes[8]);
- }
-
- private static void mapAxes(float[] mappedAxis, float[] rawAxes) {
- // Standard gamepad can have only four axes.
- mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0];
- mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1];
- mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4];
- mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5];
+ private static void mapCommonXYABButtons(float[] mappedButtons, float[] rawButtons) {
+ float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A];
+ float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B];
+ float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X];
+ float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y];
+ mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = a;
+ mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = b;
+ mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = x;
+ mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = y;
+ }
+
+ private static void mapCommonStartSelectMetaButtons(
+ float[] mappedButtons, float[] rawButtons) {
+ float start = rawButtons[KeyEvent.KEYCODE_BUTTON_START];
+ float select = rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT];
+ float mode = rawButtons[KeyEvent.KEYCODE_BUTTON_MODE];
+ mappedButtons[CanonicalButtonIndex.BUTTON_START] = start;
+ mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = select;
+ mappedButtons[CanonicalButtonIndex.BUTTON_META] = mode;
+ }
+
+ private static void mapCommonThumbstickButtons(float[] mappedButtons, float[] rawButtons) {
+ float thumbL = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL];
+ float thumbR = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR];
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = thumbL;
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = thumbR;
+ }
+
+ private static void mapCommonTriggerButtons(float[] mappedButtons, float[] rawButtons) {
+ float l1 = rawButtons[KeyEvent.KEYCODE_BUTTON_L1];
+ float r1 = rawButtons[KeyEvent.KEYCODE_BUTTON_R1];
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = l1;
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = r1;
+ }
+
+ private static void mapCommonDpadButtons(float[] mappedButtons, float[] rawButtons) {
+ float dpadDown = rawButtons[KeyEvent.KEYCODE_DPAD_DOWN];
+ float dpadUp = rawButtons[KeyEvent.KEYCODE_DPAD_UP];
+ float dpadLeft = rawButtons[KeyEvent.KEYCODE_DPAD_LEFT];
+ float dpadRight = rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT];
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = dpadDown;
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = dpadUp;
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = dpadLeft;
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = dpadRight;
+ }
+
+ private static void mapXYAxes(float[] mappedAxes, float[] rawAxes) {
+ mappedAxes[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[MotionEvent.AXIS_X];
+ mappedAxes[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[MotionEvent.AXIS_Y];
+ }
+
+ private static void mapRXAndRYAxesToRightStick(float[] mappedAxes, float[] rawAxes) {
+ mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[MotionEvent.AXIS_RX];
+ mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[MotionEvent.AXIS_RY];
+ }
+
+ private static void mapZAndRZAxesToRightStick(float[] mappedAxes, float[] rawAxes) {
+ mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[MotionEvent.AXIS_Z];
+ mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[MotionEvent.AXIS_RZ];
+ }
+
+ private static void mapTriggerAxexToShoulderButtons(float[] mappedButtons, float[] rawAxes) {
+ float lTrigger = rawAxes[MotionEvent.AXIS_LTRIGGER];
+ float rTrigger = rawAxes[MotionEvent.AXIS_RTRIGGER];
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = lTrigger;
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rTrigger;
}
private static float negativeAxisValueAsButton(float input) {
@@ -77,19 +116,30 @@ class GamepadMappings {
return (input > 0.5f) ? 1.f : 0.f;
}
+ private static void mapHatAxisToDpadButtons(float[] mappedButtons, float[] rawAxes) {
+ float hatX = rawAxes[MotionEvent.AXIS_HAT_X];
+ float hatY = rawAxes[MotionEvent.AXIS_HAT_Y];
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = negativeAxisValueAsButton(hatX);
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = positiveAxisValueAsButton(hatX);
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAsButton(hatY);
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = positiveAxisValueAsButton(hatY);
+ }
+
/**
* Method for mapping Nvidia gamepad axis and button values
* to standard gamepad button and axes values.
*/
private static void mapShieldGamepad(float[] mappedButtons, float[] rawButtons,
- float[] mappedAxis, float[] rawAxes) {
- mapCommonButtons(mappedButtons, rawButtons);
-
- mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2];
- mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6];
-
- mapDpadButtonsToAxes(mappedButtons, rawAxes);
- mapAxes(mappedAxis, rawAxes);
+ float[] mappedAxes, float[] rawAxes) {
+ mapCommonXYABButtons(mappedButtons, rawButtons);
+ mapCommonTriggerButtons(mappedButtons, rawButtons);
+ mapCommonThumbstickButtons(mappedButtons, rawButtons);
+ mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
+ mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes);
+ mapHatAxisToDpadButtons(mappedButtons, rawAxes);
+
+ mapXYAxes(mappedAxes, rawAxes);
+ mapZAndRZAxesToRightStick(mappedAxes, rawAxes);
}
/**
@@ -97,14 +147,43 @@ class GamepadMappings {
* to standard gamepad button and axes values.
*/
private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButtons,
- float[] mappedAxis, float[] rawAxes) {
- mapCommonButtons(mappedButtons, rawButtons);
+ float[] mappedAxes, float[] rawAxes) {
+ // These are actually mapped the same way in Android.
+ mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
+ }
+
+ private static void mapPS3SixAxisGamepad(float[] mappedButtons, float[] rawButtons,
+ float[] mappedAxes, float[] rawAxes) {
+ // On PS3 X/Y has higher priority.
+ float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A];
+ float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B];
+ float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X];
+ float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y];
+ mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = x;
+ mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = y;
+ mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = a;
+ mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = b;
+
+ mapCommonTriggerButtons(mappedButtons, rawButtons);
+ mapCommonThumbstickButtons(mappedButtons, rawButtons);
+ mapCommonDpadButtons(mappedButtons, rawButtons);
+ mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
+ mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes);
+
+ mapXYAxes(mappedAxes, rawAxes);
+ mapZAndRZAxesToRightStick(mappedAxes, rawAxes);
+ }
- mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2];
- mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6];
+ private static void mapSamsungEIGP20Gamepad(float[] mappedButtons, float[] rawButtons,
+ float[] mappedAxes, float[] rawAxes) {
+ mapCommonXYABButtons(mappedButtons, rawButtons);
+ mapCommonTriggerButtons(mappedButtons, rawButtons);
+ mapCommonThumbstickButtons(mappedButtons, rawButtons);
+ mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
+ mapHatAxisToDpadButtons(mappedButtons, rawAxes);
- mapDpadButtonsToAxes(mappedButtons, rawAxes);
- mapAxes(mappedAxis, rawAxes);
+ mapXYAxes(mappedAxes, rawAxes);
+ mapRXAndRYAxesToRightStick(mappedAxes, rawAxes);
}
/**
@@ -112,23 +191,15 @@ class GamepadMappings {
* to standard gamepad button and axes values.
*/
private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButtons,
- float[] mappedAxis, float[] rawAxes) {
- mapCommonButtons(mappedButtons, rawButtons);
-
- mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_L2];
- mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] =
- rawButtons[KeyEvent.KEYCODE_BUTTON_R2];
-
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = rawButtons[KeyEvent.KEYCODE_DPAD_UP];
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] =
- rawButtons[KeyEvent.KEYCODE_DPAD_DOWN];
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] =
- rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT];
- mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] =
- rawButtons[KeyEvent.KEYCODE_DPAD_LEFT];
-
- mapAxes(mappedAxis, rawAxes);
+ float[] mappedAxes, float[] rawAxes) {
+ mapCommonXYABButtons(mappedButtons, rawButtons);
+ mapCommonTriggerButtons(mappedButtons, rawButtons);
+ mapCommonThumbstickButtons(mappedButtons, rawButtons);
+ mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
+ mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes);
+ mapCommonDpadButtons(mappedButtons, rawButtons);
+
+ mapXYAxes(mappedAxes, rawAxes);
+ mapRXAndRYAxesToRightStick(mappedAxes, rawAxes);
}
-
-}
+}
« no previous file with comments | « content/public/android/java/src/org/chromium/content/browser/input/GamepadDevice.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698