Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / gamepad / gamepad_standard_mappings_mac.mm
blob4377bd2af114ae2250e6cceb47261fba6f3fe1e9
1 // Copyright (c) 2012 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.
5 #include "content/browser/gamepad/gamepad_standard_mappings.h"
7 namespace content {
9 namespace {
11 void MapperXbox360Gamepad(const blink::WebGamepad& input,
12                           blink::WebGamepad* mapped) {
13   *mapped = input;
14   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
15   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[5]);
16   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[9];
17   mapped->buttons[BUTTON_INDEX_START] = input.buttons[8];
18   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[6];
19   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[7];
20   mapped->buttons[BUTTON_INDEX_DPAD_UP] = input.buttons[11];
21   mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = input.buttons[12];
22   mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = input.buttons[13];
23   mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] = input.buttons[14];
24   mapped->buttons[BUTTON_INDEX_META] = input.buttons[10];
25   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[3];
26   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[4];
27   mapped->buttonsLength = BUTTON_INDEX_COUNT;
28   mapped->axesLength = AXIS_INDEX_COUNT;
31 void MapperPlaystationSixAxis(const blink::WebGamepad& input,
32                               blink::WebGamepad* mapped) {
33   *mapped = input;
34   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[14];
35   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[13];
36   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[15];
37   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[12];
38   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[10];
39   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[11];
41   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] =
42       ButtonFromButtonAndAxis(input.buttons[8], input.axes[14]);
43   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] =
44       ButtonFromButtonAndAxis(input.buttons[9], input.axes[15]);
46   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[0];
47   mapped->buttons[BUTTON_INDEX_START] = input.buttons[3];
48   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[1];
49   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[2];
51   // The SixAxis Dpad is pressure sensitive.
52   mapped->buttons[BUTTON_INDEX_DPAD_UP] =
53       ButtonFromButtonAndAxis(input.buttons[4], input.axes[10]);
54   mapped->buttons[BUTTON_INDEX_DPAD_DOWN] =
55       ButtonFromButtonAndAxis(input.buttons[6], input.axes[12]);
56   mapped->buttons[BUTTON_INDEX_DPAD_LEFT] =
57       ButtonFromButtonAndAxis(input.buttons[7], input.axes[13]);
58   mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] =
59       ButtonFromButtonAndAxis(input.buttons[5], input.axes[11]);
61   mapped->buttons[BUTTON_INDEX_META] = input.buttons[16];
62   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
64   mapped->buttonsLength = BUTTON_INDEX_COUNT;
65   mapped->axesLength = AXIS_INDEX_COUNT;
68 void MapperDualshock4(const blink::WebGamepad& input,
69                       blink::WebGamepad* mapped) {
70   enum Dualshock4Buttons {
71     DUALSHOCK_BUTTON_TOUCHPAD = BUTTON_INDEX_COUNT,
72     DUALSHOCK_BUTTON_COUNT
73   };
75   *mapped = input;
76   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
77   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
78   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
79   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[3];
80   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[4];
81   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[5];
82   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[3]);
83   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[4]);
84   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[8];
85   mapped->buttons[BUTTON_INDEX_START] = input.buttons[9];
86   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[10];
87   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[11];
88   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
89   mapped->buttons[DUALSHOCK_BUTTON_TOUCHPAD] = input.buttons[13];
90   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
91   DpadFromAxis(mapped, input.axes[9]);
93   mapped->buttonsLength = DUALSHOCK_BUTTON_COUNT;
94   mapped->axesLength = AXIS_INDEX_COUNT;
97 void MapperIBuffalo(const blink::WebGamepad& input, blink::WebGamepad* mapped) {
98   *mapped = input;
99   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
100   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[0];
101   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
102   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[2];
103   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[6];
104   mapped->buttons[BUTTON_INDEX_START] = input.buttons[7];
105   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[4];
106   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[5];
107   mapped->buttons[BUTTON_INDEX_DPAD_UP] = AxisNegativeAsButton(input.axes[1]);
108   mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = AxisPositiveAsButton(input.axes[1]);
109   mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = AxisNegativeAsButton(input.axes[0]);
110   mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] =
111       AxisPositiveAsButton(input.axes[0]);
112   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
113   mapped->axesLength = 2;
116 void MapperDirectInputStyle(const blink::WebGamepad& input,
117                             blink::WebGamepad* mapped) {
118   *mapped = input;
119   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
120   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
121   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
122   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
123   DpadFromAxis(mapped, input.axes[9]);
124   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
125   mapped->axesLength = AXIS_INDEX_COUNT;
128 void MapperMacallyIShock(const blink::WebGamepad& input,
129                          blink::WebGamepad* mapped) {
130   enum IShockButtons {
131     ISHOCK_BUTTON_C = BUTTON_INDEX_COUNT,
132     ISHOCK_BUTTON_D,
133     ISHOCK_BUTTON_E,
134     ISHOCK_BUTTON_COUNT,
135   };
137   *mapped = input;
138   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[6];
139   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[5];
140   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[7];
141   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
142   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[14];
143   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[12];
144   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[15];
145   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[13];
146   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[9];
147   mapped->buttons[BUTTON_INDEX_START] = input.buttons[10];
148   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[16];
149   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[17];
150   mapped->buttons[BUTTON_INDEX_DPAD_UP] = input.buttons[0];
151   mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = input.buttons[1];
152   mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = input.buttons[2];
153   mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] = input.buttons[3];
154   mapped->buttons[BUTTON_INDEX_META] = input.buttons[11];
155   mapped->buttons[ISHOCK_BUTTON_C] = input.buttons[8];
156   mapped->buttons[ISHOCK_BUTTON_D] = input.buttons[18];
157   mapped->buttons[ISHOCK_BUTTON_E] = input.buttons[19];
158   mapped->axes[AXIS_INDEX_LEFT_STICK_X] = input.axes[0];
159   mapped->axes[AXIS_INDEX_LEFT_STICK_Y] = input.axes[1];
160   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = -input.axes[5];
161   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[6];
163   mapped->buttonsLength = ISHOCK_BUTTON_COUNT;
164   mapped->axesLength = AXIS_INDEX_COUNT;
167 void MapperXGEAR(const blink::WebGamepad& input, blink::WebGamepad* mapped) {
168   *mapped = input;
169   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[2];
170   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
171   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[0];
172   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
173   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
174   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[4];
175   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[5];
176   DpadFromAxis(mapped, input.axes[9]);
177   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[5];
178   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[2];
179   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
180   mapped->axesLength = AXIS_INDEX_COUNT;
183 void MapperSmartJoyPLUS(const blink::WebGamepad& input,
184                         blink::WebGamepad* mapped) {
185   *mapped = input;
186   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[2];
187   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
188   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[0];
189   mapped->buttons[BUTTON_INDEX_START] = input.buttons[8];
190   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[9];
191   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
192   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
193   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[4];
194   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[5];
195   DpadFromAxis(mapped, input.axes[9]);
196   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
197   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
198   mapped->axesLength = AXIS_INDEX_COUNT;
201 void MapperDragonRiseGeneric(const blink::WebGamepad& input,
202                              blink::WebGamepad* mapped) {
203   *mapped = input;
204   DpadFromAxis(mapped, input.axes[9]);
205   mapped->axes[AXIS_INDEX_LEFT_STICK_X] = input.axes[0];
206   mapped->axes[AXIS_INDEX_LEFT_STICK_Y] = input.axes[1];
207   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[2];
208   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
209   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
210   mapped->axesLength = AXIS_INDEX_COUNT;
213 void MapperOnLiveWireless(const blink::WebGamepad& input,
214                           blink::WebGamepad* mapped) {
215   *mapped = input;
216   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
217   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
218   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
219   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
220   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
221   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
222   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
223   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[5]);
224   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[10];
225   mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
226   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
227   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
228   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
229   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[3];
230   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[4];
231   DpadFromAxis(mapped, input.axes[9]);
233   mapped->buttonsLength = BUTTON_INDEX_COUNT;
234   mapped->axesLength = AXIS_INDEX_COUNT;
237 void MapperADT1(const blink::WebGamepad& input, blink::WebGamepad* mapped) {
238   *mapped = input;
239   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
240   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
241   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
242   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
243   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
244   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
245   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[3]);
246   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[4]);
247   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = NullButton();
248   mapped->buttons[BUTTON_INDEX_START] = NullButton();
249   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
250   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
251   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
252   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
253   DpadFromAxis(mapped, input.axes[9]);
255   mapped->buttonsLength = BUTTON_INDEX_COUNT;
256   mapped->axesLength = AXIS_INDEX_COUNT;
259 struct MappingData {
260   const char* const vendor_id;
261   const char* const product_id;
262   GamepadStandardMappingFunction function;
263 } AvailableMappings[] = {
264     // http://www.linux-usb.org/usb.ids
265     {"0079", "0006", MapperDragonRiseGeneric},   // DragonRise Generic USB
266     {"045e", "028e", MapperXbox360Gamepad},      // Xbox 360 Controller
267     {"045e", "028f", MapperXbox360Gamepad},      // Xbox 360 Wireless Controller
268     {"046d", "c216", MapperDirectInputStyle},    // Logitech F310, D mode
269     {"046d", "c218", MapperDirectInputStyle},    // Logitech F510, D mode
270     {"046d", "c219", MapperDirectInputStyle},    // Logitech F710, D mode
271     {"054c", "0268", MapperPlaystationSixAxis},  // Playstation SIXAXIS
272     {"054c", "05c4", MapperDualshock4},          // Playstation Dualshock 4
273     {"0583", "2060", MapperIBuffalo},            // iBuffalo Classic
274     {"0925", "0005", MapperSmartJoyPLUS},        // SmartJoy PLUS Adapter
275     {"0e8f", "0003", MapperXGEAR},             // XFXforce XGEAR PS2 Controller
276     {"18d1", "2c40", MapperADT1},              // ADT-1 Controller
277     {"2222", "0060", MapperDirectInputStyle},  // Macally iShockX, analog mode
278     {"2222", "4010", MapperMacallyIShock},     // Macally iShock
279     {"2378", "1008", MapperOnLiveWireless},    // OnLive Controller (Bluetooth)
280     {"2378", "100a", MapperOnLiveWireless},    // OnLive Controller (Wired)
283 }  // namespace
285 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
286     const base::StringPiece& vendor_id,
287     const base::StringPiece& product_id) {
288   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
289     MappingData& item = AvailableMappings[i];
290     if (vendor_id == item.vendor_id && product_id == item.product_id)
291       return item.function;
292   }
293   return NULL;
296 }  // namespace content