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 #include "content/common/gamepad_hardware_buffer.h"
13 float AxisToButton(float input) {
14 return (input + 1.f) / 2.f;
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.
23 mapped->buttons[kButtonDpadUp] = 0.f;
24 mapped->buttons[kButtonDpadDown] = 0.f;
25 mapped->buttons[kButtonDpadLeft] = 0.f;
26 mapped->buttons[kButtonDpadRight] = 0.f;
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;
36 void MapperXbox360Gamepad(
37 const blink::WebGamepad& input,
38 blink::WebGamepad* mapped) {
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;
57 void MapperPlaystationSixAxis(
58 const blink::WebGamepad& input,
59 blink::WebGamepad* mapped) {
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];
80 mapped->buttonsLength = kNumButtons;
81 mapped->axesLength = kNumAxes;
84 void MapperDirectInputStyle(
85 const blink::WebGamepad& input,
86 blink::WebGamepad* mapped) {
88 mapped->buttons[kButtonPrimary] = input.buttons[1];
89 mapped->buttons[kButtonSecondary] = input.buttons[2];
90 mapped->buttons[kButtonTertiary] = input.buttons[0];
91 mapped->axes[kAxisRightStickY] = input.axes[5];
92 DpadFromAxis(mapped, input.axes[9]);
93 mapped->buttonsLength = kNumButtons - 1; /* no meta */
94 mapped->axesLength = kNumAxes;
97 void MapperMacallyIShock(
98 const blink::WebGamepad& input,
99 blink::WebGamepad* mapped) {
101 kButtonC = kNumButtons,
108 mapped->buttons[kButtonPrimary] = input.buttons[6];
109 mapped->buttons[kButtonSecondary] = input.buttons[5];
110 mapped->buttons[kButtonTertiary] = input.buttons[7];
111 mapped->buttons[kButtonQuaternary] = input.buttons[4];
112 mapped->buttons[kButtonLeftShoulder] = input.buttons[14];
113 mapped->buttons[kButtonRightShoulder] = input.buttons[12];
114 mapped->buttons[kButtonLeftTrigger] = input.buttons[15];
115 mapped->buttons[kButtonRightTrigger] = input.buttons[13];
116 mapped->buttons[kButtonBackSelect] = input.buttons[9];
117 mapped->buttons[kButtonStart] = input.buttons[10];
118 mapped->buttons[kButtonLeftThumbstick] = input.buttons[16];
119 mapped->buttons[kButtonRightThumbstick] = input.buttons[17];
120 mapped->buttons[kButtonDpadUp] = input.buttons[0];
121 mapped->buttons[kButtonDpadDown] = input.buttons[1];
122 mapped->buttons[kButtonDpadLeft] = input.buttons[2];
123 mapped->buttons[kButtonDpadRight] = input.buttons[3];
124 mapped->buttons[kButtonMeta] = input.buttons[11];
125 mapped->buttons[kButtonC] = input.buttons[8];
126 mapped->buttons[kButtonD] = input.buttons[18];
127 mapped->buttons[kButtonE] = input.buttons[19];
128 mapped->axes[kAxisLeftStickX] = input.axes[0];
129 mapped->axes[kAxisLeftStickY] = input.axes[1];
130 mapped->axes[kAxisRightStickX] = -input.axes[5];
131 mapped->axes[kAxisRightStickY] = input.axes[6];
133 mapped->buttonsLength = kNumIShockButtons;
134 mapped->axesLength = kNumAxes;
138 const blink::WebGamepad& input,
139 blink::WebGamepad* mapped) {
141 mapped->buttons[kButtonPrimary] = input.buttons[2];
142 mapped->buttons[kButtonTertiary] = input.buttons[3];
143 mapped->buttons[kButtonQuaternary] = input.buttons[0];
144 mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
145 mapped->buttons[kButtonRightShoulder] = input.buttons[7];
146 mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
147 mapped->buttons[kButtonRightTrigger] = input.buttons[5];
148 DpadFromAxis(mapped, input.axes[9]);
149 mapped->axes[kAxisRightStickX] = input.axes[5];
150 mapped->axes[kAxisRightStickY] = input.axes[2];
151 mapped->buttonsLength = kNumButtons - 1; /* no meta */
152 mapped->axesLength = kNumAxes;
155 void MapperSmartJoyPLUS(
156 const blink::WebGamepad& input,
157 blink::WebGamepad* mapped) {
159 mapped->buttons[kButtonPrimary] = input.buttons[2];
160 mapped->buttons[kButtonTertiary] = input.buttons[3];
161 mapped->buttons[kButtonQuaternary] = input.buttons[0];
162 mapped->buttons[kButtonStart] = input.buttons[8];
163 mapped->buttons[kButtonBackSelect] = input.buttons[9];
164 mapped->buttons[kButtonLeftShoulder] = input.buttons[6];
165 mapped->buttons[kButtonRightShoulder] = input.buttons[7];
166 mapped->buttons[kButtonLeftTrigger] = input.buttons[4];
167 mapped->buttons[kButtonRightTrigger] = input.buttons[5];
168 DpadFromAxis(mapped, input.axes[9]);
169 mapped->axes[kAxisRightStickY] = input.axes[5];
170 mapped->buttonsLength = kNumButtons - 1; /* no meta */
171 mapped->axesLength = kNumAxes;
174 void MapperDragonRiseGeneric(
175 const blink::WebGamepad& input,
176 blink::WebGamepad* mapped) {
178 DpadFromAxis(mapped, input.axes[9]);
179 mapped->axes[kAxisLeftStickX] = input.axes[0];
180 mapped->axes[kAxisLeftStickY] = input.axes[1];
181 mapped->axes[kAxisRightStickX] = input.axes[2];
182 mapped->axes[kAxisRightStickY] = input.axes[5];
183 mapped->buttonsLength = kNumButtons - 1; /* no meta */
184 mapped->axesLength = kNumAxes;
188 const char* const vendor_id;
189 const char* const product_id;
190 GamepadStandardMappingFunction function;
191 } AvailableMappings[] = {
192 // http://www.linux-usb.org/usb.ids
193 { "0079", "0006", MapperDragonRiseGeneric }, // DragonRise Generic USB
194 { "045e", "028e", MapperXbox360Gamepad }, // Xbox 360 Controller
195 { "045e", "028f", MapperXbox360Gamepad }, // Xbox 360 Wireless Controller
196 { "046d", "c216", MapperDirectInputStyle }, // Logitech F310, D mode
197 { "046d", "c218", MapperDirectInputStyle }, // Logitech F510, D mode
198 { "046d", "c219", MapperDirectInputStyle }, // Logitech F710, D mode
199 { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS
200 { "0925", "0005", MapperSmartJoyPLUS }, // SmartJoy PLUS Adapter
201 { "0e8f", "0003", MapperXGEAR }, // XFXforce XGEAR PS2 Controller
202 { "2222", "0060", MapperDirectInputStyle }, // Macally iShockX, analog mode
203 { "2222", "4010", MapperMacallyIShock }, // Macally iShock
208 GamepadStandardMappingFunction GetGamepadStandardMappingFunction(
209 const base::StringPiece& vendor_id,
210 const base::StringPiece& product_id) {
211 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) {
212 MappingData& item = AvailableMappings[i];
213 if (vendor_id == item.vendor_id && product_id == item.product_id)
214 return item.function;
219 } // namespace content