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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c1fe930dc3d711daf560493cafcab57214faed8 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java |
| @@ -0,0 +1,134 @@ |
| +// 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.input; |
| + |
| +import android.view.KeyEvent; |
| + |
| +/** |
| + * Class to manage mapping information related to each supported gamepad controller device. |
| + */ |
| +class GamepadMappings { |
| + public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappedButtons, |
| + float[] rawAxes, float[] rawButtons, String deviceName) { |
| + if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) { |
| + mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + return true; |
| + } else if (deviceName.contains("Microsoft X-Box 360 pad")) { |
| + mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
| + return true; |
| + } |
| + |
| + mapUnkownGamepad(mappedButtons, rawButtons, mappedAxis, 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 float negativeAxisValueAsButton(float input) { |
| + return (input < -0.5f) ? 1.f : 0.f; |
| + } |
| + |
| + private static float positiveAxisValueAsButton(float input) { |
| + return (input > 0.5f) ? 1.f : 0.f; |
| + } |
| + |
| + /** |
| + * 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]; // or rawAxes[3]; |
|
Ted C
2014/04/16 23:36:53
I'm confused of what the comment // or rawAxes[3]
SaurabhK
2014/04/18 14:51:57
On 2014/04/16 23:36:53, Ted C wrote:
No there is
kbalazs
2014/04/18 15:21:20
I agree :)
|
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; // or rawAxes[7]; |
| + |
| + mapDpadButtonsToAxes(mappedButtons, rawAxes); |
| + mapAxes(mappedAxis, rawAxes); |
| + } |
| + |
| + /** |
| + * Method for mapping Microsoft XBox 360 gamepad axis and button values |
| + * to standard gamepad button and axes values. |
| + */ |
| + private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButtons, |
| + float[] mappedAxis, float[] rawAxes) { |
| + mapCommonButtons(mappedButtons, rawButtons); |
| + |
| + mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; // or rawAxes[3]; |
| + mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; // or rawAxes[7]; |
| + |
| + mapDpadButtonsToAxes(mappedButtons, rawAxes); |
| + mapAxes(mappedAxis, rawAxes); |
| + } |
| + |
| + /** |
| + * Method for mapping Unkown gamepad axis and button values |
| + * to standard gamepad button and axes values. |
| + */ |
| + private static void mapUnkownGamepad(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); |
| + } |
| + |
| +} |