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..b1bd9cef0df157da39db8b7b67cab6a5d6e5f8e1 |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
@@ -0,0 +1,195 @@ |
+// 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; |
+ |
+/* |
+ * Class to manage mapping information related to each supported gamepad controller device. |
+ */ |
+ |
+class GamepadMappings { |
+ |
+ // 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 static enum GamepadType { |
+ Shield, |
+ XBox360, |
+ Unknown |
+ } |
+ |
+ // 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 float[] mapToStandardGamepadAxes(float[] rawAxes, GamepadType gamepadType) { |
+ switch (gamepadType) { |
+ case Shield: |
+ return mapShieldAxes(rawAxes); |
+ case XBox360: |
+ return mapXBox360Axes(rawAxes); |
+ } |
+ return rawAxes; |
+ } |
+ |
+ public static float[] mapToStandardGamepadButtons(float[] rawButtons, float[] rawAxes, |
+ GamepadType gamepadType) { |
+ switch (gamepadType) { |
+ case Shield: |
+ return mapShieldButtons(rawButtons, rawAxes); |
+ case XBox360: |
+ return mapXBox360Buttons(rawButtons, rawAxes); |
+ } |
+ return rawAxes; |
+ } |
+ |
+ 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 values to |
+ * standard gamepad axis values. |
+ */ |
+ private static 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. |
+ */ |
+ private static 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 static 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 static 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; |
+ } |
+ |
+} |