OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/browser/gamepad/gamepad_standard_mappings.h" | 5 #include "content/browser/gamepad/gamepad_standard_mappings.h" |
6 | 6 |
7 #include "content/common/gamepad_hardware_buffer.h" | 7 #include "content/common/gamepad_hardware_buffer.h" |
8 | 8 |
9 namespace content { | 9 namespace content { |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 float AxisToButton(float input) { | 13 float AxisToButton(float input) { |
14 return (input + 1.f) / 2.f; | 14 return (input + 1.f) / 2.f; |
15 } | 15 } |
16 | 16 |
| 17 void DpadFromAxis(WebKit::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 |
17 void MapperXbox360Gamepad( | 36 void MapperXbox360Gamepad( |
18 const WebKit::WebGamepad& input, | 37 const WebKit::WebGamepad& input, |
19 WebKit::WebGamepad* mapped) { | 38 WebKit::WebGamepad* mapped) { |
20 *mapped = input; | 39 *mapped = input; |
21 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]); | 40 mapped->buttons[kButtonLeftTrigger] = AxisToButton(input.axes[2]); |
22 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]); | 41 mapped->buttons[kButtonRightTrigger] = AxisToButton(input.axes[5]); |
23 mapped->buttons[kButtonBackSelect] = input.buttons[9]; | 42 mapped->buttons[kButtonBackSelect] = input.buttons[9]; |
24 mapped->buttons[kButtonStart] = input.buttons[8]; | 43 mapped->buttons[kButtonStart] = input.buttons[8]; |
25 mapped->buttons[kButtonLeftThumbstick] = input.buttons[6]; | 44 mapped->buttons[kButtonLeftThumbstick] = input.buttons[6]; |
26 mapped->buttons[kButtonRightThumbstick] = input.buttons[7]; | 45 mapped->buttons[kButtonRightThumbstick] = input.buttons[7]; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 } | 82 } |
64 | 83 |
65 void MapperDirectInputStyle( | 84 void MapperDirectInputStyle( |
66 const WebKit::WebGamepad& input, | 85 const WebKit::WebGamepad& input, |
67 WebKit::WebGamepad* mapped) { | 86 WebKit::WebGamepad* mapped) { |
68 *mapped = input; | 87 *mapped = input; |
69 mapped->buttons[kButtonPrimary] = input.buttons[1]; | 88 mapped->buttons[kButtonPrimary] = input.buttons[1]; |
70 mapped->buttons[kButtonSecondary] = input.buttons[2]; | 89 mapped->buttons[kButtonSecondary] = input.buttons[2]; |
71 mapped->buttons[kButtonTertiary] = input.buttons[0]; | 90 mapped->buttons[kButtonTertiary] = input.buttons[0]; |
72 mapped->axes[kAxisRightStickY] = input.axes[5]; | 91 mapped->axes[kAxisRightStickY] = input.axes[5]; |
73 | 92 DpadFromAxis(mapped, input.axes[9]); |
74 // Dpad is mapped as a direction on one axis, where -1 is up and it | |
75 // increases clockwise to 1, which is up + left. It's set to a large (> 1.f) | |
76 // number when nothing is depressed, except on start up, sometimes it's 0.0 | |
77 // for no data, rather than the large number. | |
78 float dir = input.axes[9]; | |
79 if (dir == 0.0f) { | |
80 mapped->buttons[kButtonDpadUp] = 0.f; | |
81 mapped->buttons[kButtonDpadDown] = 0.f; | |
82 mapped->buttons[kButtonDpadLeft] = 0.f; | |
83 mapped->buttons[kButtonDpadRight] = 0.f; | |
84 } else { | |
85 mapped->buttons[kButtonDpadUp] = (dir >= -1.f && dir < -0.7f) || | |
86 (dir >= .95f && dir <= 1.f); | |
87 mapped->buttons[kButtonDpadRight] = dir >= -.75f && dir < -.1f; | |
88 mapped->buttons[kButtonDpadDown] = dir >= -.2f && dir < .45f; | |
89 mapped->buttons[kButtonDpadLeft] = dir >= .4f && dir <= 1.f; | |
90 } | |
91 | |
92 mapped->buttonsLength = kNumButtons - 1; /* no meta */ | 93 mapped->buttonsLength = kNumButtons - 1; /* no meta */ |
93 mapped->axesLength = kNumAxes; | 94 mapped->axesLength = kNumAxes; |
94 } | 95 } |
95 | 96 |
96 void MapperMacallyIShock( | 97 void MapperMacallyIShock( |
97 const WebKit::WebGamepad& input, | 98 const WebKit::WebGamepad& input, |
98 WebKit::WebGamepad* mapped) { | 99 WebKit::WebGamepad* mapped) { |
99 enum IShockButtons { | 100 enum IShockButtons { |
100 kButtonC = kNumButtons, | 101 kButtonC = kNumButtons, |
101 kButtonD, | 102 kButtonD, |
(...skipping 24 matching lines...) Expand all Loading... |
126 mapped->buttons[kButtonE] = input.buttons[19]; | 127 mapped->buttons[kButtonE] = input.buttons[19]; |
127 mapped->axes[kAxisLeftStickX] = input.axes[0]; | 128 mapped->axes[kAxisLeftStickX] = input.axes[0]; |
128 mapped->axes[kAxisLeftStickY] = input.axes[1]; | 129 mapped->axes[kAxisLeftStickY] = input.axes[1]; |
129 mapped->axes[kAxisRightStickX] = -input.axes[5]; | 130 mapped->axes[kAxisRightStickX] = -input.axes[5]; |
130 mapped->axes[kAxisRightStickY] = input.axes[6]; | 131 mapped->axes[kAxisRightStickY] = input.axes[6]; |
131 | 132 |
132 mapped->buttonsLength = kNumIShockButtons; | 133 mapped->buttonsLength = kNumIShockButtons; |
133 mapped->axesLength = kNumAxes; | 134 mapped->axesLength = kNumAxes; |
134 } | 135 } |
135 | 136 |
| 137 void MapperXGEAR( |
| 138 const WebKit::WebGamepad& input, |
| 139 WebKit::WebGamepad* mapped) { |
| 140 *mapped = input; |
| 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; |
| 153 } |
| 154 |
136 struct MappingData { | 155 struct MappingData { |
137 const char* const vendor_id; | 156 const char* const vendor_id; |
138 const char* const product_id; | 157 const char* const product_id; |
139 GamepadStandardMappingFunction function; | 158 GamepadStandardMappingFunction function; |
140 } AvailableMappings[] = { | 159 } AvailableMappings[] = { |
141 // http://www.linux-usb.org/usb.ids | 160 // http://www.linux-usb.org/usb.ids |
142 { "045e", "028e", MapperXbox360Gamepad }, // Xbox 360 Controller | 161 { "045e", "028e", MapperXbox360Gamepad }, // Xbox 360 Controller |
143 { "045e", "028f", MapperXbox360Gamepad }, // Xbox 360 Wireless Controller | 162 { "045e", "028f", MapperXbox360Gamepad }, // Xbox 360 Wireless Controller |
144 { "046d", "c216", MapperDirectInputStyle }, // Logitech F310, D mode | 163 { "046d", "c216", MapperDirectInputStyle }, // Logitech F310, D mode |
145 { "046d", "c218", MapperDirectInputStyle }, // Logitech F510, D mode | 164 { "046d", "c218", MapperDirectInputStyle }, // Logitech F510, D mode |
146 { "046d", "c219", MapperDirectInputStyle }, // Logitech F710, D mode | 165 { "046d", "c219", MapperDirectInputStyle }, // Logitech F710, D mode |
147 { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS | 166 { "054c", "0268", MapperPlaystationSixAxis }, // Playstation SIXAXIS |
| 167 { "0e8f", "0003", MapperXGEAR }, // XFXforce XGEAR PS2 Controller |
148 { "2222", "0060", MapperDirectInputStyle }, // Macally iShockX, analog mode | 168 { "2222", "0060", MapperDirectInputStyle }, // Macally iShockX, analog mode |
149 { "2222", "4010", MapperMacallyIShock }, // Macally iShock | 169 { "2222", "4010", MapperMacallyIShock }, // Macally iShock |
150 }; | 170 }; |
151 | 171 |
152 } // namespace | 172 } // namespace |
153 | 173 |
154 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( | 174 GamepadStandardMappingFunction GetGamepadStandardMappingFunction( |
155 const base::StringPiece& vendor_id, | 175 const base::StringPiece& vendor_id, |
156 const base::StringPiece& product_id) { | 176 const base::StringPiece& product_id) { |
157 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { | 177 for (size_t i = 0; i < arraysize(AvailableMappings); ++i) { |
158 MappingData& item = AvailableMappings[i]; | 178 MappingData& item = AvailableMappings[i]; |
159 if (vendor_id == item.vendor_id && product_id == item.product_id) | 179 if (vendor_id == item.vendor_id && product_id == item.product_id) |
160 return item.function; | 180 return item.function; |
161 } | 181 } |
162 return NULL; | 182 return NULL; |
163 } | 183 } |
164 | 184 |
165 } // namespace content | 185 } // namespace content |
OLD | NEW |