OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser.input; | 5 package org.chromium.content.browser.input; |
6 | 6 |
7 import android.view.KeyEvent; | 7 import android.view.KeyEvent; |
| 8 import android.view.MotionEvent; |
| 9 |
| 10 import org.chromium.base.JNINamespace; |
8 | 11 |
9 /** | 12 /** |
10 * Class to manage mapping information related to each supported gamepad control
ler device. | 13 * Class to manage mapping information related to each supported gamepad control
ler device. |
11 */ | 14 */ |
| 15 @JNINamespace("content") |
12 class GamepadMappings { | 16 class GamepadMappings { |
13 public static boolean mapToStandardGamepad(float[] mappedAxis, float[] mappe
dButtons, | 17 private static final String NVIDIA_SHIELD_DEVICE_NAME_PREFIX = |
| 18 "NVIDIA Corporation NVIDIA Controller"; |
| 19 private static final String MICROSOFT_XBOX_PAD_DEVICE_NAME = "Microsoft X-Bo
x 360 pad"; |
| 20 private static final String PS3_SIXAXIS_DEVICE_NAME = "Sony PLAYSTATION(R)3
Controller"; |
| 21 private static final String SAMSUNG_EI_GP20_DEVICE_NAME = "Samsung Game Pad
EI-GP20"; |
| 22 |
| 23 public static boolean mapToStandardGamepad(float[] mappedAxes, float[] mappe
dButtons, |
14 float[] rawAxes, float[] rawButtons, String deviceName) { | 24 float[] rawAxes, float[] rawButtons, String deviceName) { |
15 if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) { | 25 if (deviceName.startsWith(NVIDIA_SHIELD_DEVICE_NAME_PREFIX)) { |
16 mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); | 26 mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); |
17 return true; | 27 return true; |
18 } else if (deviceName.contains("Microsoft X-Box 360 pad")) { | 28 } else if (deviceName.equals(MICROSOFT_XBOX_PAD_DEVICE_NAME)) { |
19 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); | 29 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); |
| 30 return true; |
| 31 } else if (deviceName.equals(PS3_SIXAXIS_DEVICE_NAME)) { |
| 32 mapPS3SixAxisGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes)
; |
| 33 return true; |
| 34 } else if (deviceName.equals(SAMSUNG_EI_GP20_DEVICE_NAME)) { |
| 35 mapSamsungEIGP20Gamepad(mappedButtons, rawButtons, mappedAxes, rawAx
es); |
20 return true; | 36 return true; |
21 } | 37 } |
22 | 38 |
23 mapUnknownGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); | 39 mapUnknownGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); |
24 return false; | 40 return false; |
25 } | 41 } |
26 | 42 |
27 private static void mapCommonButtons(float[] mappedButtons, float[] rawButto
ns) { | 43 private static void mapCommonXYABButtons(float[] mappedButtons, float[] rawB
uttons) { |
28 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[KeyEvent
.KEYCODE_BUTTON_A]; | 44 float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A]; |
29 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = | 45 float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B]; |
30 rawButtons[KeyEvent.KEYCODE_BUTTON_B]; | 46 float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X]; |
31 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = | 47 float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; |
32 rawButtons[KeyEvent.KEYCODE_BUTTON_X]; | 48 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = a; |
33 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = | 49 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = b; |
34 rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; | 50 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = x; |
35 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = | 51 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = y; |
36 rawButtons[KeyEvent.KEYCODE_BUTTON_L1]; | |
37 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = | |
38 rawButtons[KeyEvent.KEYCODE_BUTTON_R1]; | |
39 mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = | |
40 rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT]; | |
41 mappedButtons[CanonicalButtonIndex.BUTTON_START] = | |
42 rawButtons[KeyEvent.KEYCODE_BUTTON_START]; | |
43 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = | |
44 rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL]; | |
45 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = | |
46 rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR]; | |
47 mappedButtons[CanonicalButtonIndex.BUTTON_META] = rawButtons[KeyEvent.KE
YCODE_BUTTON_MODE]; | |
48 } | 52 } |
49 | 53 |
50 private static void mapDpadButtonsToAxes(float[] mappedButtons, float[] rawA
xes) { | 54 private static void mapCommonStartSelectMetaButtons( |
51 // Negative value indicates dpad up. | 55 float[] mappedButtons, float[] rawButtons) { |
52 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs
Button(rawAxes[9]); | 56 float start = rawButtons[KeyEvent.KEYCODE_BUTTON_START]; |
53 // Positive axis value indicates dpad down. | 57 float select = rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT]; |
54 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = | 58 float mode = rawButtons[KeyEvent.KEYCODE_BUTTON_MODE]; |
55 positiveAxisValueAsButton(rawAxes[9]); | 59 mappedButtons[CanonicalButtonIndex.BUTTON_START] = start; |
56 // Positive axis value indicates dpad right. | 60 mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = select; |
57 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = | 61 mappedButtons[CanonicalButtonIndex.BUTTON_META] = mode; |
58 positiveAxisValueAsButton(rawAxes[8]); | |
59 // Negative value indicates dpad left. | |
60 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = | |
61 negativeAxisValueAsButton(rawAxes[8]); | |
62 } | 62 } |
63 | 63 |
64 private static void mapAxes(float[] mappedAxis, float[] rawAxes) { | 64 private static void mapCommonThumbstickButtons(float[] mappedButtons, float[
] rawButtons) { |
65 // Standard gamepad can have only four axes. | 65 float thumbL = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL]; |
66 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0]; | 66 float thumbR = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR]; |
67 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1]; | 67 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = thumbL; |
68 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4]; | 68 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = thumbR; |
69 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5]; | 69 } |
| 70 |
| 71 private static void mapCommonTriggerButtons(float[] mappedButtons, float[] r
awButtons) { |
| 72 float l1 = rawButtons[KeyEvent.KEYCODE_BUTTON_L1]; |
| 73 float r1 = rawButtons[KeyEvent.KEYCODE_BUTTON_R1]; |
| 74 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = l1; |
| 75 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = r1; |
| 76 } |
| 77 |
| 78 private static void mapCommonDpadButtons(float[] mappedButtons, float[] rawB
uttons) { |
| 79 float dpadDown = rawButtons[KeyEvent.KEYCODE_DPAD_DOWN]; |
| 80 float dpadUp = rawButtons[KeyEvent.KEYCODE_DPAD_UP]; |
| 81 float dpadLeft = rawButtons[KeyEvent.KEYCODE_DPAD_LEFT]; |
| 82 float dpadRight = rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT]; |
| 83 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = dpadDown; |
| 84 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = dpadUp; |
| 85 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = dpadLeft; |
| 86 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = dpadRight; |
| 87 } |
| 88 |
| 89 private static void mapXYAxes(float[] mappedAxes, float[] rawAxes) { |
| 90 mappedAxes[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[MotionEvent.A
XIS_X]; |
| 91 mappedAxes[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[MotionEvent.A
XIS_Y]; |
| 92 } |
| 93 |
| 94 private static void mapRXAndRYAxesToRightStick(float[] mappedAxes, float[] r
awAxes) { |
| 95 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[MotionEvent.
AXIS_RX]; |
| 96 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[MotionEvent.
AXIS_RY]; |
| 97 } |
| 98 |
| 99 private static void mapZAndRZAxesToRightStick(float[] mappedAxes, float[] ra
wAxes) { |
| 100 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[MotionEvent.
AXIS_Z]; |
| 101 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[MotionEvent.
AXIS_RZ]; |
| 102 } |
| 103 |
| 104 private static void mapTriggerAxexToShoulderButtons(float[] mappedButtons, f
loat[] rawAxes) { |
| 105 float lTrigger = rawAxes[MotionEvent.AXIS_LTRIGGER]; |
| 106 float rTrigger = rawAxes[MotionEvent.AXIS_RTRIGGER]; |
| 107 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = lTrigger; |
| 108 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rTrigger; |
70 } | 109 } |
71 | 110 |
72 private static float negativeAxisValueAsButton(float input) { | 111 private static float negativeAxisValueAsButton(float input) { |
73 return (input < -0.5f) ? 1.f : 0.f; | 112 return (input < -0.5f) ? 1.f : 0.f; |
74 } | 113 } |
75 | 114 |
76 private static float positiveAxisValueAsButton(float input) { | 115 private static float positiveAxisValueAsButton(float input) { |
77 return (input > 0.5f) ? 1.f : 0.f; | 116 return (input > 0.5f) ? 1.f : 0.f; |
78 } | 117 } |
79 | 118 |
| 119 private static void mapHatAxisToDpadButtons(float[] mappedButtons, float[] r
awAxes) { |
| 120 float hatX = rawAxes[MotionEvent.AXIS_HAT_X]; |
| 121 float hatY = rawAxes[MotionEvent.AXIS_HAT_Y]; |
| 122 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = negativeAxisValue
AsButton(hatX); |
| 123 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = positiveAxisValu
eAsButton(hatX); |
| 124 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs
Button(hatY); |
| 125 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = positiveAxisValue
AsButton(hatY); |
| 126 } |
| 127 |
80 /** | 128 /** |
81 * Method for mapping Nvidia gamepad axis and button values | 129 * Method for mapping Nvidia gamepad axis and button values |
82 * to standard gamepad button and axes values. | 130 * to standard gamepad button and axes values. |
83 */ | 131 */ |
84 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto
ns, | 132 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto
ns, |
85 float[] mappedAxis, float[] rawAxes) { | 133 float[] mappedAxes, float[] rawAxes) { |
86 mapCommonButtons(mappedButtons, rawButtons); | 134 mapCommonXYABButtons(mappedButtons, rawButtons); |
| 135 mapCommonTriggerButtons(mappedButtons, rawButtons); |
| 136 mapCommonThumbstickButtons(mappedButtons, rawButtons); |
| 137 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); |
| 138 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes); |
| 139 mapHatAxisToDpadButtons(mappedButtons, rawAxes); |
87 | 140 |
88 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; | 141 mapXYAxes(mappedAxes, rawAxes); |
89 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; | 142 mapZAndRZAxesToRightStick(mappedAxes, rawAxes); |
90 | |
91 mapDpadButtonsToAxes(mappedButtons, rawAxes); | |
92 mapAxes(mappedAxis, rawAxes); | |
93 } | 143 } |
94 | 144 |
95 /** | 145 /** |
96 * Method for mapping Microsoft XBox 360 gamepad axis and button values | 146 * Method for mapping Microsoft XBox 360 gamepad axis and button values |
97 * to standard gamepad button and axes values. | 147 * to standard gamepad button and axes values. |
98 */ | 148 */ |
99 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt
ons, | 149 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt
ons, |
100 float[] mappedAxis, float[] rawAxes) { | 150 float[] mappedAxes, float[] rawAxes) { |
101 mapCommonButtons(mappedButtons, rawButtons); | 151 // These are actually mapped the same way in Android. |
| 152 mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); |
| 153 } |
102 | 154 |
103 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; | 155 private static void mapPS3SixAxisGamepad(float[] mappedButtons, float[] rawB
uttons, |
104 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; | 156 float[] mappedAxes, float[] rawAxes) { |
| 157 // On PS3 X/Y has higher priority. |
| 158 float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A]; |
| 159 float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B]; |
| 160 float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X]; |
| 161 float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; |
| 162 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = x; |
| 163 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = y; |
| 164 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = a; |
| 165 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = b; |
105 | 166 |
106 mapDpadButtonsToAxes(mappedButtons, rawAxes); | 167 mapCommonTriggerButtons(mappedButtons, rawButtons); |
107 mapAxes(mappedAxis, rawAxes); | 168 mapCommonThumbstickButtons(mappedButtons, rawButtons); |
| 169 mapCommonDpadButtons(mappedButtons, rawButtons); |
| 170 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); |
| 171 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes); |
| 172 |
| 173 mapXYAxes(mappedAxes, rawAxes); |
| 174 mapZAndRZAxesToRightStick(mappedAxes, rawAxes); |
| 175 } |
| 176 |
| 177 private static void mapSamsungEIGP20Gamepad(float[] mappedButtons, float[] r
awButtons, |
| 178 float[] mappedAxes, float[] rawAxes) { |
| 179 mapCommonXYABButtons(mappedButtons, rawButtons); |
| 180 mapCommonTriggerButtons(mappedButtons, rawButtons); |
| 181 mapCommonThumbstickButtons(mappedButtons, rawButtons); |
| 182 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); |
| 183 mapHatAxisToDpadButtons(mappedButtons, rawAxes); |
| 184 |
| 185 mapXYAxes(mappedAxes, rawAxes); |
| 186 mapRXAndRYAxesToRightStick(mappedAxes, rawAxes); |
108 } | 187 } |
109 | 188 |
110 /** | 189 /** |
111 * Method for mapping Unkown gamepad axis and button values | 190 * Method for mapping Unkown gamepad axis and button values |
112 * to standard gamepad button and axes values. | 191 * to standard gamepad button and axes values. |
113 */ | 192 */ |
114 private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButt
ons, | 193 private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButt
ons, |
115 float[] mappedAxis, float[] rawAxes) { | 194 float[] mappedAxes, float[] rawAxes) { |
116 mapCommonButtons(mappedButtons, rawButtons); | 195 mapCommonXYABButtons(mappedButtons, rawButtons); |
| 196 mapCommonTriggerButtons(mappedButtons, rawButtons); |
| 197 mapCommonThumbstickButtons(mappedButtons, rawButtons); |
| 198 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); |
| 199 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes); |
| 200 mapCommonDpadButtons(mappedButtons, rawButtons); |
117 | 201 |
118 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = | 202 mapXYAxes(mappedAxes, rawAxes); |
119 rawButtons[KeyEvent.KEYCODE_BUTTON_L2]; | 203 mapRXAndRYAxesToRightStick(mappedAxes, rawAxes); |
120 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = | |
121 rawButtons[KeyEvent.KEYCODE_BUTTON_R2]; | |
122 | |
123 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = rawButtons[KeyEvent
.KEYCODE_DPAD_UP]; | |
124 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = | |
125 rawButtons[KeyEvent.KEYCODE_DPAD_DOWN]; | |
126 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = | |
127 rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT]; | |
128 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = | |
129 rawButtons[KeyEvent.KEYCODE_DPAD_LEFT]; | |
130 | |
131 mapAxes(mappedAxis, rawAxes); | |
132 } | 204 } |
133 | 205 } |
134 } | |
OLD | NEW |