Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / gamepad / gamepad_standard_mappings_mac.mm
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.
4
5 #include "content/browser/gamepad/gamepad_standard_mappings.h"
6
7 namespace content {
8
9 namespace {
10
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;
29 }
30
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];
40
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]);
45
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];
50
51   // The SixAxis Dpad is pressure sensative
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]);
60
61   mapped->buttons[BUTTON_INDEX_META] = input.buttons[16];
62   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
63
64   mapped->buttonsLength = BUTTON_INDEX_COUNT;
65   mapped->axesLength = AXIS_INDEX_COUNT;
66 }
67
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   };
74
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]);
92
93   mapped->buttonsLength = DUALSHOCK_BUTTON_COUNT;
94   mapped->axesLength = AXIS_INDEX_COUNT;
95 }
96
97 void MapperDirectInputStyle(const blink::WebGamepad& input,
98                             blink::WebGamepad* mapped) {
99   *mapped = input;
100   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[1];
101   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[2];
102   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[0];
103   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
104   DpadFromAxis(mapped, input.axes[9]);
105   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
106   mapped->axesLength = AXIS_INDEX_COUNT;
107 }
108
109 void MapperMacallyIShock(const blink::WebGamepad& input,
110                          blink::WebGamepad* mapped) {
111   enum IShockButtons {
112     ISHOCK_BUTTON_C = BUTTON_INDEX_COUNT,
113     ISHOCK_BUTTON_D,
114     ISHOCK_BUTTON_E,
115     ISHOCK_BUTTON_COUNT,
116   };
117
118   *mapped = input;
119   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[6];
120   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[5];
121   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[7];
122   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
123   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[14];
124   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[12];
125   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[15];
126   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[13];
127   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[9];
128   mapped->buttons[BUTTON_INDEX_START] = input.buttons[10];
129   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[16];
130   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[17];
131   mapped->buttons[BUTTON_INDEX_DPAD_UP] = input.buttons[0];
132   mapped->buttons[BUTTON_INDEX_DPAD_DOWN] = input.buttons[1];
133   mapped->buttons[BUTTON_INDEX_DPAD_LEFT] = input.buttons[2];
134   mapped->buttons[BUTTON_INDEX_DPAD_RIGHT] = input.buttons[3];
135   mapped->buttons[BUTTON_INDEX_META] = input.buttons[11];
136   mapped->buttons[ISHOCK_BUTTON_C] = input.buttons[8];
137   mapped->buttons[ISHOCK_BUTTON_D] = input.buttons[18];
138   mapped->buttons[ISHOCK_BUTTON_E] = input.buttons[19];
139   mapped->axes[AXIS_INDEX_LEFT_STICK_X] = input.axes[0];
140   mapped->axes[AXIS_INDEX_LEFT_STICK_Y] = input.axes[1];
141   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = -input.axes[5];
142   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[6];
143
144   mapped->buttonsLength = ISHOCK_BUTTON_COUNT;
145   mapped->axesLength = AXIS_INDEX_COUNT;
146 }
147
148 void MapperXGEAR(const blink::WebGamepad& input, blink::WebGamepad* mapped) {
149   *mapped = input;
150   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[2];
151   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
152   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[0];
153   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
154   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
155   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = input.buttons[4];
156   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = input.buttons[5];
157   DpadFromAxis(mapped, input.axes[9]);
158   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[5];
159   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[2];
160   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
161   mapped->axesLength = AXIS_INDEX_COUNT;
162 }
163
164 void MapperSmartJoyPLUS(const blink::WebGamepad& input,
165                         blink::WebGamepad* mapped) {
166   *mapped = input;
167   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[2];
168   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
169   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[0];
170   mapped->buttons[BUTTON_INDEX_START] = input.buttons[8];
171   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[9];
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_Y] = input.axes[5];
178   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
179   mapped->axesLength = AXIS_INDEX_COUNT;
180 }
181
182 void MapperDragonRiseGeneric(const blink::WebGamepad& input,
183                              blink::WebGamepad* mapped) {
184   *mapped = input;
185   DpadFromAxis(mapped, input.axes[9]);
186   mapped->axes[AXIS_INDEX_LEFT_STICK_X] = input.axes[0];
187   mapped->axes[AXIS_INDEX_LEFT_STICK_Y] = input.axes[1];
188   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[2];
189   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
190   mapped->buttonsLength = BUTTON_INDEX_COUNT - 1; /* no meta */
191   mapped->axesLength = AXIS_INDEX_COUNT;
192 }
193
194 void MapperOnLiveWireless(const blink::WebGamepad& input,
195                           blink::WebGamepad* mapped) {
196   *mapped = input;
197   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
198   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
199   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
200   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
201   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
202   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
203   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[2]);
204   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[5]);
205   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = input.buttons[10];
206   mapped->buttons[BUTTON_INDEX_START] = input.buttons[11];
207   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
208   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
209   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
210   mapped->axes[AXIS_INDEX_RIGHT_STICK_X] = input.axes[3];
211   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[4];
212   DpadFromAxis(mapped, input.axes[9]);
213
214   mapped->buttonsLength = BUTTON_INDEX_COUNT;
215   mapped->axesLength = AXIS_INDEX_COUNT;
216 }
217
218 void MapperADT1(const blink::WebGamepad& input, blink::WebGamepad* mapped) {
219   *mapped = input;
220   mapped->buttons[BUTTON_INDEX_PRIMARY] = input.buttons[0];
221   mapped->buttons[BUTTON_INDEX_SECONDARY] = input.buttons[1];
222   mapped->buttons[BUTTON_INDEX_TERTIARY] = input.buttons[3];
223   mapped->buttons[BUTTON_INDEX_QUATERNARY] = input.buttons[4];
224   mapped->buttons[BUTTON_INDEX_LEFT_SHOULDER] = input.buttons[6];
225   mapped->buttons[BUTTON_INDEX_RIGHT_SHOULDER] = input.buttons[7];
226   mapped->buttons[BUTTON_INDEX_LEFT_TRIGGER] = AxisToButton(input.axes[3]);
227   mapped->buttons[BUTTON_INDEX_RIGHT_TRIGGER] = AxisToButton(input.axes[4]);
228   mapped->buttons[BUTTON_INDEX_BACK_SELECT] = NullButton();
229   mapped->buttons[BUTTON_INDEX_START] = NullButton();
230   mapped->buttons[BUTTON_INDEX_LEFT_THUMBSTICK] = input.buttons[13];
231   mapped->buttons[BUTTON_INDEX_RIGHT_THUMBSTICK] = input.buttons[14];
232   mapped->buttons[BUTTON_INDEX_META] = input.buttons[12];
233   mapped->axes[AXIS_INDEX_RIGHT_STICK_Y] = input.axes[5];
234   DpadFromAxis(mapped, input.axes[9]);
235
236   mapped->buttonsLength = BUTTON_INDEX_COUNT;
237   mapped->axesLength = AXIS_INDEX_COUNT;
238 }
239
240 struct MappingData {
241   const char* const vendor_id;
242   const char* const product_id;
243   GamepadStandardMappingFunction function;
244 } AvailableMappings[] = {
245       // http://www.linux-usb.org/usb.ids
246       {"0079", "0006", MapperDragonRiseGeneric},  // DragonRise Generic USB
247       {"045e", "028e", MapperXbox360Gamepad},     // Xbox 360 Controller
248       {"045e", "028f", MapperXbox360Gamepad},    // Xbox 360 Wireless Controller
249       {"046d", "c216", MapperDirectInputStyle},  // Logitech F310, D mode
250       {"046d", "c218", MapperDirectInputStyle},  // Logitech F510, D mode
251       {"046d", "c219", MapperDirectInputStyle},  // Logitech F710, D mode
252       {"054c", "0268", MapperPlaystationSixAxis},  // Playstation SIXAXIS
253       {"054c", "05c4", MapperDualshock4},          // Playstation Dualshock 4
254       {"0925", "0005", MapperSmartJoyPLUS},        // SmartJoy PLUS Adapter
255       {"0e8f", "0003", MapperXGEAR},  // XFXforce XGEAR PS2 Controller
256       {"2222", "0060", MapperDirectInputStyle},  // Macally iShockX, analog mode
257       {"2222", "4010", MapperMacallyIShock},     // Macally iShock
258       {"2378", "1008", MapperOnLiveWireless},  // OnLive Controller (Bluetooth)
259       {"2378", "100a", MapperOnLiveWireless},  // OnLive Controller (Wired)
260       {"18d1", "2c40", MapperADT1},            // ADT-1 Controller
261 };
262
263 }  // namespace
264
265 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
266     const base::StringPiece& vendor_id,
267     const base::StringPiece& product_id) {
268   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
269     MappingData& item = AvailableMappings[i];
270     if (vendor_id == item.vendor_id && product_id == item.product_id)
271       return item.function;
272   }
273   return NULL;
274 }
275
276 }  // namespace content