OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content.browser.input; | |
6 | |
7 import android.view.KeyEvent; | |
8 import android.view.MotionEvent; | |
9 | |
10 import org.chromium.base.VisibleForTesting; | |
11 import org.chromium.base.annotations.JNINamespace; | |
12 | |
13 /** | |
14 * Class to manage mapping information related to each supported gamepad control
ler device. | |
15 */ | |
16 @JNINamespace("content") | |
17 class GamepadMappings { | |
18 @VisibleForTesting | |
19 static final String NVIDIA_SHIELD_DEVICE_NAME_PREFIX = "NVIDIA Corporation N
VIDIA Controller"; | |
20 @VisibleForTesting | |
21 static final String MICROSOFT_XBOX_PAD_DEVICE_NAME = "Microsoft X-Box 360 pa
d"; | |
22 @VisibleForTesting | |
23 static final String PS3_SIXAXIS_DEVICE_NAME = "Sony PLAYSTATION(R)3 Controll
er"; | |
24 @VisibleForTesting | |
25 static final String SAMSUNG_EI_GP20_DEVICE_NAME = "Samsung Game Pad EI-GP20"
; | |
26 @VisibleForTesting | |
27 static final String AMAZON_FIRE_DEVICE_NAME = "Amazon Fire Game Controller"; | |
28 | |
29 public static boolean mapToStandardGamepad(float[] mappedAxes, float[] mappe
dButtons, | |
30 float[] rawAxes, float[] rawButtons, String deviceName) { | |
31 if (deviceName.startsWith(NVIDIA_SHIELD_DEVICE_NAME_PREFIX)) { | |
32 mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); | |
33 return true; | |
34 } else if (deviceName.equals(MICROSOFT_XBOX_PAD_DEVICE_NAME)) { | |
35 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); | |
36 return true; | |
37 } else if (deviceName.equals(PS3_SIXAXIS_DEVICE_NAME)) { | |
38 mapPS3SixAxisGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes)
; | |
39 return true; | |
40 } else if (deviceName.equals(SAMSUNG_EI_GP20_DEVICE_NAME)) { | |
41 mapSamsungEIGP20Gamepad(mappedButtons, rawButtons, mappedAxes, rawAx
es); | |
42 return true; | |
43 } else if (deviceName.equals(AMAZON_FIRE_DEVICE_NAME)) { | |
44 mapAmazonFireGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes)
; | |
45 return true; | |
46 } | |
47 | |
48 mapUnknownGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); | |
49 return false; | |
50 } | |
51 | |
52 private static void mapCommonXYABButtons(float[] mappedButtons, float[] rawB
uttons) { | |
53 float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A]; | |
54 float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B]; | |
55 float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X]; | |
56 float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; | |
57 mappedButtons[CanonicalButtonIndex.PRIMARY] = a; | |
58 mappedButtons[CanonicalButtonIndex.SECONDARY] = b; | |
59 mappedButtons[CanonicalButtonIndex.TERTIARY] = x; | |
60 mappedButtons[CanonicalButtonIndex.QUATERNARY] = y; | |
61 } | |
62 | |
63 private static void mapCommonStartSelectMetaButtons( | |
64 float[] mappedButtons, float[] rawButtons) { | |
65 float start = rawButtons[KeyEvent.KEYCODE_BUTTON_START]; | |
66 float select = rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT]; | |
67 float mode = rawButtons[KeyEvent.KEYCODE_BUTTON_MODE]; | |
68 mappedButtons[CanonicalButtonIndex.START] = start; | |
69 mappedButtons[CanonicalButtonIndex.BACK_SELECT] = select; | |
70 mappedButtons[CanonicalButtonIndex.META] = mode; | |
71 } | |
72 | |
73 private static void mapCommonThumbstickButtons(float[] mappedButtons, float[
] rawButtons) { | |
74 float thumbL = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL]; | |
75 float thumbR = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR]; | |
76 mappedButtons[CanonicalButtonIndex.LEFT_THUMBSTICK] = thumbL; | |
77 mappedButtons[CanonicalButtonIndex.RIGHT_THUMBSTICK] = thumbR; | |
78 } | |
79 | |
80 private static void mapCommonTriggerButtons(float[] mappedButtons, float[] r
awButtons) { | |
81 float l1 = rawButtons[KeyEvent.KEYCODE_BUTTON_L1]; | |
82 float r1 = rawButtons[KeyEvent.KEYCODE_BUTTON_R1]; | |
83 mappedButtons[CanonicalButtonIndex.LEFT_TRIGGER] = l1; | |
84 mappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER] = r1; | |
85 } | |
86 | |
87 private static void mapTriggerButtonsToTopShoulder(float[] mappedButtons, fl
oat[] rawButtons) { | |
88 float l1 = rawButtons[KeyEvent.KEYCODE_BUTTON_L1]; | |
89 float r1 = rawButtons[KeyEvent.KEYCODE_BUTTON_R1]; | |
90 mappedButtons[CanonicalButtonIndex.LEFT_SHOULDER] = l1; | |
91 mappedButtons[CanonicalButtonIndex.RIGHT_SHOULDER] = r1; | |
92 } | |
93 | |
94 private static void mapCommonDpadButtons(float[] mappedButtons, float[] rawB
uttons) { | |
95 float dpadDown = rawButtons[KeyEvent.KEYCODE_DPAD_DOWN]; | |
96 float dpadUp = rawButtons[KeyEvent.KEYCODE_DPAD_UP]; | |
97 float dpadLeft = rawButtons[KeyEvent.KEYCODE_DPAD_LEFT]; | |
98 float dpadRight = rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT]; | |
99 mappedButtons[CanonicalButtonIndex.DPAD_DOWN] = dpadDown; | |
100 mappedButtons[CanonicalButtonIndex.DPAD_UP] = dpadUp; | |
101 mappedButtons[CanonicalButtonIndex.DPAD_LEFT] = dpadLeft; | |
102 mappedButtons[CanonicalButtonIndex.DPAD_RIGHT] = dpadRight; | |
103 } | |
104 | |
105 private static void mapXYAxes(float[] mappedAxes, float[] rawAxes) { | |
106 mappedAxes[CanonicalAxisIndex.LEFT_STICK_X] = rawAxes[MotionEvent.AXIS_X
]; | |
107 mappedAxes[CanonicalAxisIndex.LEFT_STICK_Y] = rawAxes[MotionEvent.AXIS_Y
]; | |
108 } | |
109 | |
110 private static void mapRXAndRYAxesToRightStick(float[] mappedAxes, float[] r
awAxes) { | |
111 mappedAxes[CanonicalAxisIndex.RIGHT_STICK_X] = rawAxes[MotionEvent.AXIS_
RX]; | |
112 mappedAxes[CanonicalAxisIndex.RIGHT_STICK_Y] = rawAxes[MotionEvent.AXIS_
RY]; | |
113 } | |
114 | |
115 private static void mapZAndRZAxesToRightStick(float[] mappedAxes, float[] ra
wAxes) { | |
116 mappedAxes[CanonicalAxisIndex.RIGHT_STICK_X] = rawAxes[MotionEvent.AXIS_
Z]; | |
117 mappedAxes[CanonicalAxisIndex.RIGHT_STICK_Y] = rawAxes[MotionEvent.AXIS_
RZ]; | |
118 } | |
119 | |
120 private static void mapTriggerAxexToShoulderButtons(float[] mappedButtons, f
loat[] rawAxes) { | |
121 float lTrigger = rawAxes[MotionEvent.AXIS_LTRIGGER]; | |
122 float rTrigger = rawAxes[MotionEvent.AXIS_RTRIGGER]; | |
123 mappedButtons[CanonicalButtonIndex.LEFT_SHOULDER] = lTrigger; | |
124 mappedButtons[CanonicalButtonIndex.RIGHT_SHOULDER] = rTrigger; | |
125 } | |
126 | |
127 private static void mapPedalAxesToBottomShoulder(float[] mappedButtons, floa
t[] rawAxes) { | |
128 float lTrigger = rawAxes[MotionEvent.AXIS_BRAKE]; | |
129 float rTrigger = rawAxes[MotionEvent.AXIS_GAS]; | |
130 mappedButtons[CanonicalButtonIndex.LEFT_TRIGGER] = lTrigger; | |
131 mappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER] = rTrigger; | |
132 } | |
133 | |
134 private static void mapTriggerAxesToBottomShoulder(float[] mappedButtons, fl
oat[] rawAxes) { | |
135 float lTrigger = rawAxes[MotionEvent.AXIS_LTRIGGER]; | |
136 float rTrigger = rawAxes[MotionEvent.AXIS_RTRIGGER]; | |
137 mappedButtons[CanonicalButtonIndex.LEFT_TRIGGER] = lTrigger; | |
138 mappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER] = rTrigger; | |
139 } | |
140 | |
141 @VisibleForTesting | |
142 static float negativeAxisValueAsButton(float input) { | |
143 return (input < -0.5f) ? 1.f : 0.f; | |
144 } | |
145 | |
146 @VisibleForTesting | |
147 static float positiveAxisValueAsButton(float input) { | |
148 return (input > 0.5f) ? 1.f : 0.f; | |
149 } | |
150 | |
151 private static void mapHatAxisToDpadButtons(float[] mappedButtons, float[] r
awAxes) { | |
152 float hatX = rawAxes[MotionEvent.AXIS_HAT_X]; | |
153 float hatY = rawAxes[MotionEvent.AXIS_HAT_Y]; | |
154 mappedButtons[CanonicalButtonIndex.DPAD_LEFT] = negativeAxisValueAsButto
n(hatX); | |
155 mappedButtons[CanonicalButtonIndex.DPAD_RIGHT] = positiveAxisValueAsButt
on(hatX); | |
156 mappedButtons[CanonicalButtonIndex.DPAD_UP] = negativeAxisValueAsButton(
hatY); | |
157 mappedButtons[CanonicalButtonIndex.DPAD_DOWN] = positiveAxisValueAsButto
n(hatY); | |
158 } | |
159 | |
160 /** | |
161 * Method for mapping Amazon Fire gamepad axis and button values | |
162 * to standard gamepad button and axes values. | |
163 */ | |
164 private static void mapAmazonFireGamepad( | |
165 float[] mappedButtons, float[] rawButtons, float[] mappedAxes, float
[] rawAxes) { | |
166 mapCommonXYABButtons(mappedButtons, rawButtons); | |
167 mapTriggerButtonsToTopShoulder(mappedButtons, rawButtons); | |
168 mapCommonThumbstickButtons(mappedButtons, rawButtons); | |
169 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); | |
170 mapPedalAxesToBottomShoulder(mappedButtons, rawAxes); | |
171 mapHatAxisToDpadButtons(mappedButtons, rawAxes); | |
172 | |
173 mapXYAxes(mappedAxes, rawAxes); | |
174 mapZAndRZAxesToRightStick(mappedAxes, rawAxes); | |
175 } | |
176 | |
177 /** | |
178 * Method for mapping Nvidia gamepad axis and button values | |
179 * to standard gamepad button and axes values. | |
180 */ | |
181 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto
ns, | |
182 float[] mappedAxes, float[] rawAxes) { | |
183 mapCommonXYABButtons(mappedButtons, rawButtons); | |
184 mapTriggerButtonsToTopShoulder(mappedButtons, rawButtons); | |
185 mapCommonThumbstickButtons(mappedButtons, rawButtons); | |
186 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); | |
187 mapTriggerAxesToBottomShoulder(mappedButtons, rawAxes); | |
188 mapHatAxisToDpadButtons(mappedButtons, rawAxes); | |
189 | |
190 mapXYAxes(mappedAxes, rawAxes); | |
191 mapZAndRZAxesToRightStick(mappedAxes, rawAxes); | |
192 } | |
193 | |
194 /** | |
195 * Method for mapping Microsoft XBox 360 gamepad axis and button values | |
196 * to standard gamepad button and axes values. | |
197 */ | |
198 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt
ons, | |
199 float[] mappedAxes, float[] rawAxes) { | |
200 // These are actually mapped the same way in Android. | |
201 mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes); | |
202 } | |
203 | |
204 private static void mapPS3SixAxisGamepad(float[] mappedButtons, float[] rawB
uttons, | |
205 float[] mappedAxes, float[] rawAxes) { | |
206 // On PS3 X/Y has higher priority. | |
207 float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A]; | |
208 float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B]; | |
209 float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X]; | |
210 float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; | |
211 mappedButtons[CanonicalButtonIndex.PRIMARY] = x; | |
212 mappedButtons[CanonicalButtonIndex.SECONDARY] = y; | |
213 mappedButtons[CanonicalButtonIndex.TERTIARY] = a; | |
214 mappedButtons[CanonicalButtonIndex.QUATERNARY] = b; | |
215 | |
216 mapCommonTriggerButtons(mappedButtons, rawButtons); | |
217 mapCommonThumbstickButtons(mappedButtons, rawButtons); | |
218 mapCommonDpadButtons(mappedButtons, rawButtons); | |
219 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); | |
220 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes); | |
221 | |
222 mapXYAxes(mappedAxes, rawAxes); | |
223 mapZAndRZAxesToRightStick(mappedAxes, rawAxes); | |
224 } | |
225 | |
226 private static void mapSamsungEIGP20Gamepad(float[] mappedButtons, float[] r
awButtons, | |
227 float[] mappedAxes, float[] rawAxes) { | |
228 mapCommonXYABButtons(mappedButtons, rawButtons); | |
229 mapCommonTriggerButtons(mappedButtons, rawButtons); | |
230 mapCommonThumbstickButtons(mappedButtons, rawButtons); | |
231 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); | |
232 mapHatAxisToDpadButtons(mappedButtons, rawAxes); | |
233 | |
234 mapXYAxes(mappedAxes, rawAxes); | |
235 mapRXAndRYAxesToRightStick(mappedAxes, rawAxes); | |
236 } | |
237 | |
238 /** | |
239 * Method for mapping Unkown gamepad axis and button values | |
240 * to standard gamepad button and axes values. | |
241 */ | |
242 private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButt
ons, | |
243 float[] mappedAxes, float[] rawAxes) { | |
244 mapCommonXYABButtons(mappedButtons, rawButtons); | |
245 mapCommonTriggerButtons(mappedButtons, rawButtons); | |
246 mapCommonThumbstickButtons(mappedButtons, rawButtons); | |
247 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons); | |
248 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes); | |
249 mapCommonDpadButtons(mappedButtons, rawButtons); | |
250 | |
251 mapXYAxes(mappedAxes, rawAxes); | |
252 mapRXAndRYAxesToRightStick(mappedAxes, rawAxes); | |
253 } | |
254 } | |
OLD | NEW |