Upstream version 5.34.104.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 #include "content/common/gamepad_hardware_buffer.h"
8
9 namespace content {
10
11 namespace {
12
13 float AxisToButton(float input) {
14   return (input + 1.f) / 2.f;
15 }
16
17 void DpadFromAxis(blink::WebGamepad* mapped, float dir) {
18   // Dpad is mapped as a direction on one axis, where -1 is up and it
19   // increases clockwise to 1, which is up + left. It's set to a large (> 1.f)
20   // number when nothing is depressed, except on start up, sometimes it's 0.0
21   // for no data, rather than the large number.
22   if (dir == 0.0f) {
23     mapped->buttons[kButtonDpadUp] = 0.f;
24     mapped->buttons[kButtonDpadDown] = 0.f;
25     mapped->buttons[kButtonDpadLeft] = 0.f;
26     mapped->buttons[kButtonDpadRight] = 0.f;
27   } else {
28     mapped->buttons[kButtonDpadUp] = (dir >= -1.f && dir < -0.7f) ||
29                                      (dir >= .95f && dir <= 1.f);
30     mapped->buttons[kButtonDpadRight] = dir >= -.75f && dir < -.1f;
31     mapped->buttons[kButtonDpadDown] = dir >= -.2f && dir < .45f;
32     mapped->buttons[kButtonDpadLeft] = dir >= .4f && dir <= 1.f;
33   }
34 }
35
36 void MapperXbox360Gamepad(
37     const blink::WebGamepad& input,
38     blink::WebGamepad* mapped) {
39   *mapped = input;
40   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]);
41   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]);
42   mapped->buttons[kButtonBackSelect] = input.buttons[9];
43   mapped->buttons[kButtonStart] = input.buttons[8];
44   mapped->buttons[kButtonLeftThumbstick] = input.buttons[6];
45   mapped->buttons[kButtonRightThumbstick] = input.buttons[7];
46   mapped->buttons[kButtonDpadUp] = input.buttons[11];
47   mapped->buttons[kButtonDpadDown] = input.buttons[12];
48   mapped->buttons[kButtonDpadLeft] = input.buttons[13];
49   mapped->buttons[kButtonDpadRight] = input.buttons[14];
50   mapped->buttons[kButtonMeta] = input.buttons[10];
51   mapped->axes[kAxisRightStickX] = input.axes[3];
52   mapped->axes[kAxisRightStickY] = input.axes[4];
53   mapped->buttonsLength = kNumButtons;
54   mapped->axesLength = kNumAxes;
55 }
56
57 void MapperPlaystationSixAxis(
58     const blink::WebGamepad& input,
59     blink::WebGamepad* mapped) {
60   *mapped = input;
61   mapped->buttons[kButtonPrimary] = input.buttons[14];
62   mapped->buttons[kButtonSecondary] = input.buttons[13];
63   mapped->buttons[kButtonTertiary] = input.buttons[15];
64   mapped->buttons[kButtonQuaternary] = input.buttons[12];
65   mapped->buttons[kButtonLeftShoulder] = input.buttons[10];
66   mapped->buttons[kButtonRightShoulder] = input.buttons[11];
67   mapped->buttons[kButtonLeftTrigger] = input.buttons[8];
68   mapped->buttons[kButtonRightTrigger] = input.buttons[9];
69   mapped->buttons[kButtonBackSelect] = input.buttons[0];
70   mapped->buttons[kButtonStart] = input.buttons[3];
71   mapped->buttons[kButtonLeftThumbstick] = input.buttons[1];
72   mapped->buttons[kButtonRightThumbstick] = input.buttons[2];
73   mapped->buttons[kButtonDpadUp] = input.buttons[4];
74   mapped->buttons[kButtonDpadDown] = input.buttons[6];
75   mapped->buttons[kButtonDpadLeft] = input.buttons[7];
76   mapped->buttons[kButtonDpadRight] = input.buttons[5];
77   mapped->buttons[kButtonMeta] = input.buttons[16];
78   mapped->axes[kAxisRightStickY] = input.axes[5];
79
80   mapped->buttonsLength = kNumButtons;
81   mapped->axesLength = kNumAxes;
82 }
83
84 void MapperDualshock4(
85     const blink::WebGamepad& input,
86     blink::WebGamepad* mapped) {
87   enum Dualshock4Buttons {
88     kTouchpadButton = kNumButtons,
89     kNumDualshock4Buttons
90   };
91
92   *mapped = input;
93   mapped->buttons[kButtonPrimary] = input.buttons[1];
94   mapped->buttons[kButtonSecondary] = input.buttons[2];
95   mapped->buttons[kButtonTertiary] = input.buttons[0];
96   mapped->buttons[kButtonQuaternary] = input.buttons[3];
97   mapped->buttons[kButtonLeftShoulder] = input.buttons[4];
98   mapped->buttons[kButtonRightShoulder] = input.buttons[5];
99   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[3]);
100   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[4]);
101   mapped->buttons[kButtonBackSelect] = input.buttons[8];
102   mapped->buttons[kButtonStart] = input.buttons[9];
103   mapped->buttons[kButtonLeftThumbstick] = input.buttons[10];
104   mapped->buttons[kButtonRightThumbstick] = input.buttons[11];
105   mapped->buttons[kButtonMeta] = input.buttons[12];
106   mapped->buttons[kTouchpadButton] = input.buttons[13];
107   mapped->axes[kAxisRightStickY] = input.axes[5];
108   DpadFromAxis(mapped, input.axes[9]);
109
110   mapped->buttonsLength = kNumDualshock4Buttons;
111   mapped->axesLength = kNumAxes;
112 }
113
114 void MapperDirectInputStyle(
115     const blink::WebGamepad& input,
116     blink::WebGamepad* mapped) {
117   *mapped = input;
118   mapped->buttons[kButtonPrimary] = input.buttons[1];
119   mapped->buttons[kButtonSecondary] = input.buttons[2];
120   mapped->buttons[kButtonTertiary] = input.buttons[0];
121   mapped->axes[kAxisRightStickY] = input.axes[5];
122   DpadFromAxis(mapped, input.axes[9]);
123   mapped->buttonsLength = kNumButtons - 1; /* no meta */
124   mapped->axesLength = kNumAxes;
125 }
126
127 void MapperMacallyIShock(
128     const blink::WebGamepad& input,
129     blink::WebGamepad* mapped) {
130   enum IShockButtons {
131     kButtonC = kNumButtons,
132     kButtonD,
133     kButtonE,
134     kNumIShockButtons
135   };
136
137   *mapped = input;
138   mapped->buttons[kButtonPrimary] = input.buttons[6];
139   mapped->buttons[kButtonSecondary] = input.buttons[5];
140   mapped->buttons[kButtonTertiary] = input.buttons[7];
141   mapped->buttons[kButtonQuaternary] = input.buttons[4];
142   mapped->buttons[kButtonLeftShoulder] = input.buttons[14];
143   mapped->buttons[kButtonRightShoulder] = input.buttons[12];
144   mapped->buttons[kButtonLeftTrigger] = input.buttons[15];
145   mapped->buttons[kButtonRightTrigger] = input.buttons[13];
146   mapped->buttons[kButtonBackSelect] = input.buttons[9];
147   mapped->buttons[kButtonStart] = input.buttons[10];
148   mapped->buttons[kButtonLeftThumbstick] = input.buttons[16];
149   mapped->buttons[kButtonRightThumbstick] = input.buttons[17];
150   mapped->buttons[kButtonDpadUp] = input.buttons[0];
151   mapped->buttons[kButtonDpadDown] = input.buttons[1];
152   mapped->buttons[kButtonDpadLeft] = input.buttons[2];
153   mapped->buttons[kButtonDpadRight] = input.buttons[3];
154   mapped->buttons[kButtonMeta] = input.buttons[11];
155   mapped->buttons[kButtonC] = input.buttons[8];
156   mapped->buttons[kButtonD] = input.buttons[18];
157   mapped->buttons[kButtonE] = input.buttons[19];
158   mapped->axes[kAxisLeftStickX] = input.axes[0];
159   mapped->axes[kAxisLeftStickY] = input.axes[1];
160   mapped->axes[kAxisRightStickX] = -input.axes[5];
161   mapped->axes[kAxisRightStickY] = input.axes[6];
162
163   mapped->buttonsLength = kNumIShockButtons;
164   mapped->axesLength = kNumAxes;
165 }
166
167 void MapperXGEAR(
168     const blink::WebGamepad& input,
169     blink::WebGamepad* mapped) {
170   *mapped = input;
171   mapped->buttons[kButtonPrimary] = input.buttons[2];
172   mapped->buttons[kButtonTertiary] = input.buttons[3];
173   mapped->buttons[kButtonQuaternary] = input.buttons[0];
174   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
175   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
176   mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
177   mapped->buttons[kButtonRightTrigger] = input.buttons[5];
178   DpadFromAxis(mapped, input.axes[9]);
179   mapped->axes[kAxisRightStickX] = input.axes[5];
180   mapped->axes[kAxisRightStickY] = input.axes[2];
181   mapped->buttonsLength = kNumButtons - 1; /* no meta */
182   mapped->axesLength = kNumAxes;
183 }
184
185 void MapperSmartJoyPLUS(
186     const blink::WebGamepad& input,
187     blink::WebGamepad* mapped) {
188   *mapped = input;
189   mapped->buttons[kButtonPrimary] = input.buttons[2];
190   mapped->buttons[kButtonTertiary] = input.buttons[3];
191   mapped->buttons[kButtonQuaternary] = input.buttons[0];
192   mapped->buttons[kButtonStart] = input.buttons[8];
193   mapped->buttons[kButtonBackSelect] = input.buttons[9];
194   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
195   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
196   mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
197   mapped->buttons[kButtonRightTrigger] = input.buttons[5];
198   DpadFromAxis(mapped, input.axes[9]);
199   mapped->axes[kAxisRightStickY] = input.axes[5];
200   mapped->buttonsLength = kNumButtons - 1; /* no meta */
201   mapped->axesLength = kNumAxes;
202 }
203
204 void MapperDragonRiseGeneric(
205     const blink::WebGamepad& input,
206     blink::WebGamepad* mapped) {
207   *mapped = input;
208   DpadFromAxis(mapped, input.axes[9]);
209   mapped->axes[kAxisLeftStickX] = input.axes[0];
210   mapped->axes[kAxisLeftStickY] = input.axes[1];
211   mapped->axes[kAxisRightStickX] = input.axes[2];
212   mapped->axes[kAxisRightStickY] = input.axes[5];
213   mapped->buttonsLength = kNumButtons - 1; /* no meta */
214   mapped->axesLength = kNumAxes;
215 }
216
217 void MapperOnLiveWireless(
218     const blink::WebGamepad& input,
219     blink::WebGamepad* mapped) {
220   *mapped = input;
221   mapped->buttons[kButtonPrimary] = input.buttons[0];
222   mapped->buttons[kButtonSecondary] = input.buttons[1];
223   mapped->buttons[kButtonTertiary] = input.buttons[3];
224   mapped->buttons[kButtonQuaternary] = input.buttons[4];
225   mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
226   mapped->buttons[kButtonRightShoulder] = input.buttons[7];
227   mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]);
228   mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]);
229   mapped->buttons[kButtonBackSelect] = input.buttons[10];
230   mapped->buttons[kButtonStart] = input.buttons[11];
231   mapped->buttons[kButtonLeftThumbstick] = input.buttons[13];
232   mapped->buttons[kButtonRightThumbstick] = input.buttons[14];
233   mapped->buttons[kButtonMeta] = input.buttons[12];
234   mapped->axes[kAxisRightStickX] = input.axes[3];
235   mapped->axes[kAxisRightStickY] = input.axes[4];
236   DpadFromAxis(mapped, input.axes[9]);
237
238   mapped->buttonsLength = kNumButtons;
239   mapped->axesLength = kNumAxes;
240 }
241
242 struct MappingData {
243   const char* const vendor_id;
244   const char* const product_id;
245   GamepadStandardMappingFunction function;
246 } AvailableMappings[] = {
247   // http://www.linux-usb.org/usb.ids
248   { "0079", "0006", MapperDragonRiseGeneric },  // DragonRise Generic USB
249   { "045e", "028e", MapperXbox360Gamepad },     // Xbox 360 Controller
250   { "045e", "028f", MapperXbox360Gamepad },     // Xbox 360 Wireless Controller
251   { "046d", "c216", MapperDirectInputStyle },   // Logitech F310, D mode
252   { "046d", "c218", MapperDirectInputStyle },   // Logitech F510, D mode
253   { "046d", "c219", MapperDirectInputStyle },   // Logitech F710, D mode
254   { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS
255   { "054c", "05c4", MapperDualshock4 },         // Playstation Dualshock 4
256   { "0925", "0005", MapperSmartJoyPLUS },       // SmartJoy PLUS Adapter
257   { "0e8f", "0003", MapperXGEAR },              // XFXforce XGEAR PS2 Controller
258   { "2222", "0060", MapperDirectInputStyle },   // Macally iShockX, analog mode
259   { "2222", "4010", MapperMacallyIShock },      // Macally iShock
260   { "2378", "1008", MapperOnLiveWireless },     // OnLive Controller (Bluetooth)
261   { "2378", "100a", MapperOnLiveWireless },     // OnLive Controller (Wired)
262 };
263
264 }  // namespace
265
266 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
267     const base::StringPiece& vendor_id,
268     const base::StringPiece& product_id) {
269   for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
270     MappingData& item = AvailableMappings[i];
271     if (vendor_id == item.vendor_id && product_id == item.product_id)
272       return item.function;
273   }
274   return NULL;
275 }
276
277 }  // namespace content