Chromium Code Reviews| 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..4e4b2fac5896407c6df2439e7395a68fb1a6e9a6 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/GamepadMappings.java |
| @@ -0,0 +1,166 @@ |
| +// 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. |
| + public static class StandardButtons { |
| + public static final int BUTTON_A = 0; |
|
jdduke (slow)
2014/03/18 09:43:15
I believe we're getting these values from gamepad_
SaurabhK
2014/03/18 12:45:30
On 2014/03/18 09:43:15, jdduke wrote:
Incorporati
|
| + 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. |
| + public 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; |
| + } |
| + |
| + // 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[StandardButtons.BUTTON_A] = rawButtons[0]; |
| + mappedButtons[StandardButtons.BUTTON_B] = rawButtons[1]; |
| + mappedButtons[StandardButtons.BUTTON_X] = rawButtons[2]; |
| + mappedButtons[StandardButtons.BUTTON_Y] = rawButtons[3]; |
| + // Third axis on Nvidia gamepad acts as a bottom left trigger button. |
| + mappedButtons[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2]; |
| + mappedButtons[StandardButtons.LEFT_TRIGGER] = rawButtons[4]; |
| + mappedButtons[StandardButtons.RIGHT_TRIGGER] = rawButtons[5]; |
| + mappedButtons[StandardButtons.SELECT] = rawButtons[6]; |
| + mappedButtons[StandardButtons.START] = rawButtons[7]; |
| + mappedButtons[StandardButtons.LEFT_THUMB] = rawButtons[8]; |
| + mappedButtons[StandardButtons.RIGHT_THUMB] = rawButtons[9]; |
| + // Seventh axis on Nvidia gamepad acts as a bottom right trigger button. |
| + mappedButtons[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. |
| + mappedButtons[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(rawAxes[8]); |
| + mappedButtons[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. |
| + mappedButtons[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(rawAxes[9]); |
| + mappedButtons[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); |
| + // Other standard buttons are either not present on the gamepad or not exposed to an |
| + // application. |
| + mappedButtons[StandardButtons.MODE] = 0.0f; |
| + |
| + // Standard gamepad can have only four axes. |
| + mappedAxis[StandardAxes.AXIS_0] = rawAxes[0]; |
| + mappedAxis[StandardAxes.AXIS_1] = rawAxes[1]; |
| + mappedAxis[StandardAxes.AXIS_2] = rawAxes[4]; |
| + mappedAxis[StandardAxes.AXIS_3] = 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[StandardButtons.BUTTON_A] = rawButtons[0]; |
| + mappedButtons[StandardButtons.BUTTON_B] = rawButtons[1]; |
| + mappedButtons[StandardButtons.BUTTON_X] = rawButtons[2]; |
| + mappedButtons[StandardButtons.BUTTON_Y] = rawButtons[3]; |
| + // Third axis on Microsoft XBox 360 gamepad acts as a left bottom shoulder button. |
| + mappedButtons[StandardButtons.BOTTOM_LEFT_TRIGGER] = rawAxes[2]; |
| + mappedButtons[StandardButtons.LEFT_TRIGGER] = rawButtons[4]; |
| + mappedButtons[StandardButtons.RIGHT_TRIGGER] = rawButtons[5]; |
| + mappedButtons[StandardButtons.START] = rawButtons[6]; |
| + mappedButtons[StandardButtons.LEFT_THUMB] = rawButtons[7]; |
| + mappedButtons[StandardButtons.RIGHT_THUMB] = rawButtons[8]; |
| + // Seventh axis on Microsoft XBox 360 gamepad acts as a right bottom shoulder button. |
| + mappedButtons[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. |
| + mappedButtons[StandardButtons.DPAD_RIGHT] = positiveAxisValueAsButton(rawAxes[8]); |
| + mappedButtons[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. |
| + mappedButtons[StandardButtons.DPAD_DOWN] = positiveAxisValueAsButton(rawAxes[9]); |
| + mappedButtons[StandardButtons.DPAD_UP] = negativeAxisValueAsButton(rawAxes[9]); |
| + // Other standard buttons are either not present on the gamepad or not exposed to an |
| + // application. |
| + mappedButtons[StandardButtons.SELECT] = 0.0f; |
| + mappedButtons[StandardButtons.MODE] = 0.0f; |
| + |
| + // Standard gamepad can have only four axes. |
| + mappedAxis[StandardAxes.AXIS_0] = rawAxes[0]; |
| + mappedAxis[StandardAxes.AXIS_1] = rawAxes[1]; |
| + mappedAxis[StandardAxes.AXIS_2] = rawAxes[4]; |
| + mappedAxis[StandardAxes.AXIS_3] = rawAxes[5]; |
| + } |
| + |
| +} |