Index: content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java b/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0949c6262a0360e5566543db5ad0215f343554eb |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
@@ -0,0 +1,143 @@ |
+// 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 org.chromium.content.browser.input.CanonicalAxisIndex; |
+import org.chromium.content.browser.input.CanonicalButtonIndex; |
+ |
+/* |
+ * Class to manage mapping information related to each supported gamepad controller device. |
+ */ |
+ |
+class GamepadMappings { |
kbalazs
2014/03/26 19:24:53
I would slightly prefer an abstract class implemen
|
+ // Following values should be changed if the gamepad layout changes |
+ // or frameworks exposes different axes / buttons for a particular gamepad. |
+ // mRawShieldAxes is the total raw axes on known shield gamepad. |
+ protected static final int mRawShieldAxes = 10; |
+ // mRawShieldButtons is the total raw buttons on known shield gamepad. |
+ protected static final int mRawShieldButtons = 10; |
+ // mRawXBoxAxes is the total raw axes on known XBox gamepad. |
+ protected static final int mRawXBoxAxes = 10; |
+ // mRawXBoxButtons is the total raw buttons on known XBox gamepad. |
+ protected static final int mRawXBoxButtons = 9; |
+ |
+ public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappedButtons, |
+ float[] rawAxes, float[] rawButtons, |
+ String deviceName) { |
+ boolean isMapped = true; |
+ if (deviceName.contains("NVIDIA Corporation NVIDIA Controller") && |
+ rawAxes.length == mRawShieldAxes && rawButtons.length == mRawShieldButtons) { |
+ mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
+ } |
+ else if (deviceName.contains("Microsoft X-Box 360 pad") && |
+ rawAxes.length == mRawXBoxAxes && rawButtons.length == mRawXBoxButtons) { |
+ mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); |
+ } |
+ else { |
+ isMapped = false; |
+ } |
+ return isMapped; |
+ } |
+ |
+ 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) { |
+ // First four buttons on Nvidia gamepad appear in correct order so no mapping is required. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[0]; |
kbalazs
2014/03/26 19:24:53
I would also prefer if we could use the symbolic c
|
+ mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = rawButtons[1]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = rawButtons[2]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = rawButtons[3]; |
+ // Third axis on Nvidia gamepad acts as a bottom left trigger button. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = rawButtons[4]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rawButtons[5]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = rawButtons[6]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_START] = rawButtons[7]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = rawButtons[8]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = rawButtons[9]; |
+ // Seventh axis on Nvidia gamepad acts as a bottom right trigger button. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_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. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = |
+ positiveAxisValueAsButton(rawAxes[8]); |
+ mappedButtons[CanonicalButtonIndex.BUTTON_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. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = |
+ positiveAxisValueAsButton(rawAxes[9]); |
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); |
+ // Other standard buttons are either not present on the gamepad or not exposed to an |
+ // application. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_META] = 0.0f; |
+ |
+ // 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]; |
+ } |
+ |
+ /** |
+ * 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) { |
+ // First four buttons on Microsoft XBox 360 gamepad appear in correct order so no mapping |
+ // is required. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[0]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = rawButtons[1]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = rawButtons[2]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = rawButtons[3]; |
+ // Third axis on Microsoft XBox 360 gamepad acts as a left bottom shoulder button. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = rawButtons[4]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rawButtons[5]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_START] = rawButtons[6]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = rawButtons[7]; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = rawButtons[8]; |
+ // Seventh axis on Microsoft XBox 360 gamepad acts as a right bottom shoulder button. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_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. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = |
+ positiveAxisValueAsButton(rawAxes[8]); |
+ mappedButtons[CanonicalButtonIndex.BUTTON_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. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = |
+ positiveAxisValueAsButton(rawAxes[9]); |
+ mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); |
+ // Other standard buttons are either not present on the gamepad or not exposed to an |
+ // application. |
+ mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = 0.0f; |
+ mappedButtons[CanonicalButtonIndex.BUTTON_META] = 0.0f; |
+ |
+ // 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]; |
+ } |
+ |
+} |