Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/input/GamepadMappings.java

Issue 307413004: Gamepad: cleanup mappings and add support for Samsung and PS3 gamepads (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 PS3_SIXAXIS_DEVICE_NAME = "Sony PLAYSTATION(R)3 Controller";
18 private static final String SAMSUNG_EI_GP20_DEVICE_NAME = "Samsung Game Pad EI-GP20";
19
20 public static boolean mapToStandardGamepad(float[] mappedAxes, float[] mappe dButtons,
14 float[] rawAxes, float[] rawButtons, String deviceName) { 21 float[] rawAxes, float[] rawButtons, String deviceName) {
22 // TODO(b.kelemen): we should check for strict equality in all cases.
15 if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) { 23 if (deviceName.contains("NVIDIA Corporation NVIDIA Controller")) {
16 mapShieldGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); 24 mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
17 return true; 25 return true;
18 } else if (deviceName.contains("Microsoft X-Box 360 pad")) { 26 } else if (deviceName.contains("Microsoft X-Box 360 pad")) {
jdduke (slow) 2014/06/03 22:10:46 We might as well move treat all constant strings h
kbalazs 2014/06/03 22:25:11 I don't think we need to be case insensitive if we
kbalazs 2014/06/05 17:59:36 I moved the constants but it seems like there is s
19 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); 27 mapXBox360Gamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
28 return true;
29 } else if (deviceName.equals(PS3_SIXAXIS_DEVICE_NAME)) {
30 mapPS3SixAxisGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes) ;
31 return true;
32 } else if (deviceName.equals(SAMSUNG_EI_GP20_DEVICE_NAME)) {
33 mapSamsungEIGP20Gamepad(mappedButtons, rawButtons, mappedAxes, rawAx es);
20 return true; 34 return true;
21 } 35 }
22 36
23 mapUnknownGamepad(mappedButtons, rawButtons, mappedAxis, rawAxes); 37 mapUnknownGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
24 return false; 38 return false;
25 } 39 }
26 40
27 private static void mapCommonButtons(float[] mappedButtons, float[] rawButto ns) { 41 private static void mapCommonXYABButtons(float[] mappedButtons, float[] rawB uttons) {
28 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = rawButtons[KeyEvent .KEYCODE_BUTTON_A]; 42 float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A];
jdduke (slow) 2014/06/03 22:10:46 Are the temporaries here just a stylistic preferen
kbalazs 2014/06/03 22:25:11 Kind of, I prefer avoiding multiline statements wh
jdduke (slow) 2014/06/05 15:22:21 OK, but this actually makes inspection more diffic
kbalazs 2014/06/05 17:59:36 I know. I tried to make it a bit more review frien
29 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = 43 float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B];
30 rawButtons[KeyEvent.KEYCODE_BUTTON_B]; 44 float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X];
31 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = 45 float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y];
32 rawButtons[KeyEvent.KEYCODE_BUTTON_X]; 46 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = a;
33 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = 47 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = b;
34 rawButtons[KeyEvent.KEYCODE_BUTTON_Y]; 48 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = x;
35 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = 49 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 } 50 }
49 51
50 private static void mapDpadButtonsToAxes(float[] mappedButtons, float[] rawA xes) { 52 private static void mapCommonStartSelectMetaButtons(
51 // Negative value indicates dpad up. 53 float[] mappedButtons, float[] rawButtons) {
52 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs Button(rawAxes[9]); 54 float start = rawButtons[KeyEvent.KEYCODE_BUTTON_START];
53 // Positive axis value indicates dpad down. 55 float select = rawButtons[KeyEvent.KEYCODE_BUTTON_SELECT];
54 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = 56 float mode = rawButtons[KeyEvent.KEYCODE_BUTTON_MODE];
55 positiveAxisValueAsButton(rawAxes[9]); 57 mappedButtons[CanonicalButtonIndex.BUTTON_START] = start;
56 // Positive axis value indicates dpad right. 58 mappedButtons[CanonicalButtonIndex.BUTTON_BACK_SELECT] = select;
57 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = 59 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 } 60 }
63 61
64 private static void mapAxes(float[] mappedAxis, float[] rawAxes) { 62 private static void mapCommonThumbstickButtons(float[] mappedButtons, float[ ] rawButtons) {
65 // Standard gamepad can have only four axes. 63 float thumbL = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL];
66 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[0]; 64 float thumbR = rawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR];
67 mappedAxis[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[1]; 65 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_THUMBSTICK] = thumbL;
68 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[4]; 66 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_THUMBSTICK] = thumbR;
69 mappedAxis[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[5]; 67 }
68
69 private static void mapCommonTriggerButtons(float[] mappedButtons, float[] r awButtons) {
70 float l1 = rawButtons[KeyEvent.KEYCODE_BUTTON_L1];
71 float r1 = rawButtons[KeyEvent.KEYCODE_BUTTON_R1];
72 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = l1;
73 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = r1;
74 }
75
76 private static void mapCommonDpadButtons(float[] mappedButtons, float[] rawB uttons) {
77 float dpadDown = rawButtons[KeyEvent.KEYCODE_DPAD_DOWN];
78 float dpadUp = rawButtons[KeyEvent.KEYCODE_DPAD_UP];
79 float dpadLeft = rawButtons[KeyEvent.KEYCODE_DPAD_LEFT];
80 float dpadRight = rawButtons[KeyEvent.KEYCODE_DPAD_RIGHT];
81 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = dpadDown;
82 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = dpadUp;
83 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = dpadLeft;
84 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = dpadRight;
85 }
86
87 private static void mapXYAxes(float[] mappedAxes, float[] rawAxes) {
88 mappedAxes[CanonicalAxisIndex.AXIS_LEFT_STICK_X] = rawAxes[MotionEvent.A XIS_X];
89 mappedAxes[CanonicalAxisIndex.AXIS_LEFT_STICK_Y] = rawAxes[MotionEvent.A XIS_Y];
90 }
91
92 private static void mapRXAndRYAxesToRightStick(float[] mappedAxes, float[] r awAxes) {
93 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[MotionEvent. AXIS_RX];
94 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[MotionEvent. AXIS_RY];
95 }
96
97 private static void mapZAndRZAxesToRightStick(float[] mappedAxes, float[] ra wAxes) {
98 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_X] = rawAxes[MotionEvent. AXIS_Z];
99 mappedAxes[CanonicalAxisIndex.AXIS_RIGHT_STICK_Y] = rawAxes[MotionEvent. AXIS_RZ];
100 }
101
102 private static void mapTriggerAxexToShoulderButtons(float[] mappedButtons, f loat[] rawAxes) {
103 float lTrigger = rawAxes[MotionEvent.AXIS_LTRIGGER];
104 float rTrigger = rawAxes[MotionEvent.AXIS_RTRIGGER];
105 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_SHOULDER] = lTrigger;
106 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_SHOULDER] = rTrigger;
70 } 107 }
71 108
72 private static float negativeAxisValueAsButton(float input) { 109 private static float negativeAxisValueAsButton(float input) {
73 return (input < -0.5f) ? 1.f : 0.f; 110 return (input < -0.5f) ? 1.f : 0.f;
74 } 111 }
75 112
76 private static float positiveAxisValueAsButton(float input) { 113 private static float positiveAxisValueAsButton(float input) {
77 return (input > 0.5f) ? 1.f : 0.f; 114 return (input > 0.5f) ? 1.f : 0.f;
78 } 115 }
79 116
117 private static void mapHatAxisToDpadButtons(float[] mappedButtons, float[] r awAxes) {
118 float hatX = rawAxes[MotionEvent.AXIS_HAT_X];
119 float hatY = rawAxes[MotionEvent.AXIS_HAT_Y];
120 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_LEFT] = negativeAxisValue AsButton(hatX);
121 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_RIGHT] = positiveAxisValu eAsButton(hatX);
122 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_UP] = negativeAxisValueAs Button(hatY);
123 mappedButtons[CanonicalButtonIndex.BUTTON_DPAD_DOWN] = positiveAxisValue AsButton(hatY);
124 }
125
80 /** 126 /**
81 * Method for mapping Nvidia gamepad axis and button values 127 * Method for mapping Nvidia gamepad axis and button values
82 * to standard gamepad button and axes values. 128 * to standard gamepad button and axes values.
83 */ 129 */
84 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto ns, 130 private static void mapShieldGamepad(float[] mappedButtons, float[] rawButto ns,
85 float[] mappedAxis, float[] rawAxes) { 131 float[] mappedAxes, float[] rawAxes) {
86 mapCommonButtons(mappedButtons, rawButtons); 132 mapCommonXYABButtons(mappedButtons, rawButtons);
133 mapCommonTriggerButtons(mappedButtons, rawButtons);
134 mapCommonThumbstickButtons(mappedButtons, rawButtons);
135 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
136 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes);
137 mapHatAxisToDpadButtons(mappedButtons, rawAxes);
87 138
88 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; 139 mapXYAxes(mappedAxes, rawAxes);
89 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; 140 mapRXAndRYAxesToRightStick(mappedAxes, rawAxes);
SaurabhK 2014/06/05 15:20:26 SHield maps Z and RZ axes. Please use mapZAndRZAxe
kbalazs 2014/06/05 17:59:36 Great, thanks for testing. I assumed that this is
90
91 mapDpadButtonsToAxes(mappedButtons, rawAxes);
92 mapAxes(mappedAxis, rawAxes);
93 } 141 }
94 142
95 /** 143 /**
96 * Method for mapping Microsoft XBox 360 gamepad axis and button values 144 * Method for mapping Microsoft XBox 360 gamepad axis and button values
97 * to standard gamepad button and axes values. 145 * to standard gamepad button and axes values.
98 */ 146 */
99 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt ons, 147 private static void mapXBox360Gamepad(float[] mappedButtons, float[] rawButt ons,
100 float[] mappedAxis, float[] rawAxes) { 148 float[] mappedAxes, float[] rawAxes) {
101 mapCommonButtons(mappedButtons, rawButtons); 149 // Actually these are the same.
150 mapShieldGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
151 }
102 152
103 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = rawAxes[2]; 153 private static void mapPS3SixAxisGamepad(float[] mappedButtons, float[] rawB uttons,
104 mappedButtons[CanonicalButtonIndex.BUTTON_RIGHT_TRIGGER] = rawAxes[6]; 154 float[] mappedAxes, float[] rawAxes) {
155 // On PS3 X/Y has higher priority.
156 float a = rawButtons[KeyEvent.KEYCODE_BUTTON_A];
157 float b = rawButtons[KeyEvent.KEYCODE_BUTTON_B];
158 float x = rawButtons[KeyEvent.KEYCODE_BUTTON_X];
159 float y = rawButtons[KeyEvent.KEYCODE_BUTTON_Y];
160 mappedButtons[CanonicalButtonIndex.BUTTON_PRIMARY] = x;
161 mappedButtons[CanonicalButtonIndex.BUTTON_SECONDARY] = y;
162 mappedButtons[CanonicalButtonIndex.BUTTON_TERTIARY] = a;
163 mappedButtons[CanonicalButtonIndex.BUTTON_QUATERNARY] = b;
105 164
106 mapDpadButtonsToAxes(mappedButtons, rawAxes); 165 mapCommonTriggerButtons(mappedButtons, rawButtons);
107 mapAxes(mappedAxis, rawAxes); 166 mapCommonThumbstickButtons(mappedButtons, rawButtons);
167 mapCommonDpadButtons(mappedButtons, rawButtons);
168 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
169 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes);
170
171 mapXYAxes(mappedAxes, rawAxes);
172 mapZAndRZAxesToRightStick(mappedAxes, rawAxes);
173 }
174
175 private static void mapSamsungEIGP20Gamepad(float[] mappedButtons, float[] r awButtons,
176 float[] mappedAxes, float[] rawAxes) {
177 mapCommonXYABButtons(mappedButtons, rawButtons);
178 mapCommonTriggerButtons(mappedButtons, rawButtons);
179 mapCommonThumbstickButtons(mappedButtons, rawButtons);
180 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
181 mapHatAxisToDpadButtons(mappedButtons, rawAxes);
182
183 mapXYAxes(mappedAxes, rawAxes);
184 mapRXAndRYAxesToRightStick(mappedAxes, rawAxes);
108 } 185 }
109 186
110 /** 187 /**
111 * Method for mapping Unkown gamepad axis and button values 188 * Method for mapping Unkown gamepad axis and button values
112 * to standard gamepad button and axes values. 189 * to standard gamepad button and axes values.
113 */ 190 */
114 private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButt ons, 191 private static void mapUnknownGamepad(float[] mappedButtons, float[] rawButt ons,
115 float[] mappedAxis, float[] rawAxes) { 192 float[] mappedAxes, float[] rawAxes) {
116 mapCommonButtons(mappedButtons, rawButtons); 193 mapCommonXYABButtons(mappedButtons, rawButtons);
194 mapCommonTriggerButtons(mappedButtons, rawButtons);
195 mapCommonThumbstickButtons(mappedButtons, rawButtons);
196 mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
197 mapTriggerAxexToShoulderButtons(mappedButtons, rawAxes);
198 mapCommonDpadButtons(mappedButtons, rawButtons);
117 199
118 mappedButtons[CanonicalButtonIndex.BUTTON_LEFT_TRIGGER] = 200 mapXYAxes(mappedAxes, rawAxes);
119 rawButtons[KeyEvent.KEYCODE_BUTTON_L2]; 201 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 } 202 }
133 203 }
134 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698