Chromium Code Reviews| 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..11e4416bbff55be083481a77e89e639ed8d26ada 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,105 @@ |
| 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 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) { |
| + // TODO(b.kelemen): we should check for strict equality in all cases. |
| if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) { |
| - mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); |
| return true; |
| } else if (deviceName.contains("Microsoft X-Box 360 pad")) { |
|
jdduke (slow)
2014/06/03 22:10:46
We might as well move treat all constant strings h
kbalazs
2014/06/03 22:25:11
I don't think we need to be case insensitive if we
kbalazs
2014/06/05 17:59:36
I moved the constants but it seems like there is s
|
| - mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + 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.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]; |
|
jdduke (slow)
2014/06/03 22:10:46
Are the temporaries here just a stylistic preferen
kbalazs
2014/06/03 22:25:11
Kind of, I prefer avoiding multiline statements wh
jdduke (slow)
2014/06/05 15:22:21
OK, but this actually makes inspection more diffic
kbalazs
2014/06/05 17:59:36
I know. I tried to make it a bit more review frien
|
| + 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 +114,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); |
| + mapRXAndRYAxesToRightStick(mappedAxes, rawAxes); |
|
SaurabhK
2014/06/05 15:20:26
SHield maps Z and RZ axes. Please use mapZAndRZAxe
kbalazs
2014/06/05 17:59:36
Great, thanks for testing. I assumed that this is
|
| } |
| /** |
| @@ -97,14 +145,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) { |
| + // Actually these are the same. |
| + 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 +189,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); |
| } |
| - |
| -} |
| +} |