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_platform_data_fetcher_win.h" | 5 #include "device/gamepad/gamepad_platform_data_fetcher_win.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
13 #include "content/common/gamepad_hardware_buffer.h" | |
14 #include "content/common/gamepad_messages.h" | |
15 | 13 |
16 namespace content { | 14 namespace device { |
17 | 15 |
18 using namespace blink; | 16 using namespace blink; |
19 | 17 |
20 namespace { | 18 namespace { |
21 | 19 |
22 // See http://goo.gl/5VSJR. These are not available in all versions of the | 20 // See http://goo.gl/5VSJR. These are not available in all versions of the |
23 // header, but they can be returned from the driver, so we define our own | 21 // header, but they can be returned from the driver, so we define our own |
24 // versions here. | 22 // versions here. |
25 static const BYTE kDeviceSubTypeGamepad = 1; | 23 static const BYTE kDeviceSubTypeGamepad = 1; |
26 static const BYTE kDeviceSubTypeWheel = 2; | 24 static const BYTE kDeviceSubTypeWheel = 2; |
27 static const BYTE kDeviceSubTypeArcadeStick = 3; | 25 static const BYTE kDeviceSubTypeArcadeStick = 3; |
28 static const BYTE kDeviceSubTypeFlightStick = 4; | 26 static const BYTE kDeviceSubTypeFlightStick = 4; |
29 static const BYTE kDeviceSubTypeDancePad = 5; | 27 static const BYTE kDeviceSubTypeDancePad = 5; |
30 static const BYTE kDeviceSubTypeGuitar = 6; | 28 static const BYTE kDeviceSubTypeGuitar = 6; |
31 static const BYTE kDeviceSubTypeGuitarAlternate = 7; | 29 static const BYTE kDeviceSubTypeGuitarAlternate = 7; |
32 static const BYTE kDeviceSubTypeDrumKit = 8; | 30 static const BYTE kDeviceSubTypeDrumKit = 8; |
33 static const BYTE kDeviceSubTypeGuitarBass = 11; | 31 static const BYTE kDeviceSubTypeGuitarBass = 11; |
34 static const BYTE kDeviceSubTypeArcadePad = 19; | 32 static const BYTE kDeviceSubTypeArcadePad = 19; |
35 | 33 |
36 float NormalizeXInputAxis(SHORT value) { | 34 float NormalizeXInputAxis(SHORT value) { |
37 return ((value + 32768.f) / 32767.5f) - 1.f; | 35 return ((value + 32768.f) / 32767.5f) - 1.f; |
38 } | 36 } |
39 | 37 |
40 const WebUChar* GamepadSubTypeName(BYTE sub_type) { | 38 const WebUChar* GamepadSubTypeName(BYTE sub_type) { |
41 switch (sub_type) { | 39 switch (sub_type) { |
42 case kDeviceSubTypeGamepad: return L"GAMEPAD"; | 40 case kDeviceSubTypeGamepad: |
43 case kDeviceSubTypeWheel: return L"WHEEL"; | 41 return L"GAMEPAD"; |
44 case kDeviceSubTypeArcadeStick: return L"ARCADE_STICK"; | 42 case kDeviceSubTypeWheel: |
45 case kDeviceSubTypeFlightStick: return L"FLIGHT_STICK"; | 43 return L"WHEEL"; |
46 case kDeviceSubTypeDancePad: return L"DANCE_PAD"; | 44 case kDeviceSubTypeArcadeStick: |
47 case kDeviceSubTypeGuitar: return L"GUITAR"; | 45 return L"ARCADE_STICK"; |
48 case kDeviceSubTypeGuitarAlternate: return L"GUITAR_ALTERNATE"; | 46 case kDeviceSubTypeFlightStick: |
49 case kDeviceSubTypeDrumKit: return L"DRUM_KIT"; | 47 return L"FLIGHT_STICK"; |
50 case kDeviceSubTypeGuitarBass: return L"GUITAR_BASS"; | 48 case kDeviceSubTypeDancePad: |
51 case kDeviceSubTypeArcadePad: return L"ARCADE_PAD"; | 49 return L"DANCE_PAD"; |
52 default: return L"<UNKNOWN>"; | 50 case kDeviceSubTypeGuitar: |
| 51 return L"GUITAR"; |
| 52 case kDeviceSubTypeGuitarAlternate: |
| 53 return L"GUITAR_ALTERNATE"; |
| 54 case kDeviceSubTypeDrumKit: |
| 55 return L"DRUM_KIT"; |
| 56 case kDeviceSubTypeGuitarBass: |
| 57 return L"GUITAR_BASS"; |
| 58 case kDeviceSubTypeArcadePad: |
| 59 return L"ARCADE_PAD"; |
| 60 default: |
| 61 return L"<UNKNOWN>"; |
53 } | 62 } |
54 } | 63 } |
55 | 64 |
56 const WebUChar* XInputDllFileName() { | 65 const WebUChar* XInputDllFileName() { |
57 // Xinput.h defines filename (XINPUT_DLL) on different Windows versions, but | 66 // Xinput.h defines filename (XINPUT_DLL) on different Windows versions, but |
58 // Xinput.h specifies it in build time. Approach here uses the same values | 67 // Xinput.h specifies it in build time. Approach here uses the same values |
59 // and it is resolving dll filename based on Windows version it is running on. | 68 // and it is resolving dll filename based on Windows version it is running on. |
60 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | 69 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
61 // For Windows 8 and 10, XINPUT_DLL is xinput1_4.dll. | 70 // For Windows 8 and 10, XINPUT_DLL is xinput1_4.dll. |
62 return FILE_PATH_LITERAL("xinput1_4.dll"); | 71 return FILE_PATH_LITERAL("xinput1_4.dll"); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 platform_state.status = RAWINPUT_CONNECTED; | 168 platform_state.status = RAWINPUT_CONNECTED; |
160 platform_state.raw_input_handle = gamepad->handle; | 169 platform_state.raw_input_handle = gamepad->handle; |
161 | 170 |
162 std::string vendor = base::StringPrintf("%04x", gamepad->vendor_id); | 171 std::string vendor = base::StringPrintf("%04x", gamepad->vendor_id); |
163 std::string product = base::StringPrintf("%04x", gamepad->product_id); | 172 std::string product = base::StringPrintf("%04x", gamepad->product_id); |
164 state.mapper = GetGamepadStandardMappingFunction(vendor, product); | 173 state.mapper = GetGamepadStandardMappingFunction(vendor, product); |
165 state.axis_mask = 0; | 174 state.axis_mask = 0; |
166 state.button_mask = 0; | 175 state.button_mask = 0; |
167 | 176 |
168 swprintf(pad.id, WebGamepad::idLengthCap, | 177 swprintf(pad.id, WebGamepad::idLengthCap, |
169 L"%ls (%lsVendor: %04x Product: %04x)", | 178 L"%ls (%lsVendor: %04x Product: %04x)", gamepad->id, |
170 gamepad->id, state.mapper ? L"STANDARD GAMEPAD " : L"", | 179 state.mapper ? L"STANDARD GAMEPAD " : L"", gamepad->vendor_id, |
171 gamepad->vendor_id, gamepad->product_id); | 180 gamepad->product_id); |
172 | 181 |
173 if (state.mapper) | 182 if (state.mapper) |
174 swprintf(pad.mapping, WebGamepad::mappingLengthCap, L"standard"); | 183 swprintf(pad.mapping, WebGamepad::mappingLengthCap, L"standard"); |
175 else | 184 else |
176 pad.mapping[0] = 0; | 185 pad.mapping[0] = 0; |
177 } | 186 } |
178 } | 187 } |
179 } | 188 } |
180 | 189 |
181 void GamepadPlatformDataFetcherWin::GetGamepadData(WebGamepads* pads, | 190 void GamepadPlatformDataFetcherWin::GetGamepadData(WebGamepads* pads, |
182 bool devices_changed_hint) { | 191 bool devices_changed_hint) { |
183 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); | 192 TRACE_EVENT0("GAMEPAD", "GetGamepadData"); |
184 | 193 |
185 if (!xinput_available_ && | 194 if (!xinput_available_ && !raw_input_fetcher_->Available()) { |
186 !raw_input_fetcher_->Available()) { | |
187 pads->length = 0; | 195 pads->length = 0; |
188 return; | 196 return; |
189 } | 197 } |
190 | 198 |
191 // A note on XInput devices: | 199 // A note on XInput devices: |
192 // If we got notification that system devices have been updated, then | 200 // If we got notification that system devices have been updated, then |
193 // run GetCapabilities to update the connected status and the device | 201 // run GetCapabilities to update the connected status and the device |
194 // identifier. It can be slow to do to both GetCapabilities and | 202 // identifier. It can be slow to do to both GetCapabilities and |
195 // GetState on unconnected devices, so we want to avoid a 2-5ms pause | 203 // GetState on unconnected devices, so we want to avoid a 2-5ms pause |
196 // here by only doing this when the devices are updated (despite | 204 // here by only doing this when the devices are updated (despite |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 WebGamepad* pad) const { | 239 WebGamepad* pad) const { |
232 DCHECK(pad); | 240 DCHECK(pad); |
233 TRACE_EVENT1("GAMEPAD", "GetXInputPadConnectivity", "id", i); | 241 TRACE_EVENT1("GAMEPAD", "GetXInputPadConnectivity", "id", i); |
234 XINPUT_CAPABILITIES caps; | 242 XINPUT_CAPABILITIES caps; |
235 DWORD res = xinput_get_capabilities_(i, XINPUT_FLAG_GAMEPAD, &caps); | 243 DWORD res = xinput_get_capabilities_(i, XINPUT_FLAG_GAMEPAD, &caps); |
236 if (res == ERROR_DEVICE_NOT_CONNECTED) { | 244 if (res == ERROR_DEVICE_NOT_CONNECTED) { |
237 pad->connected = false; | 245 pad->connected = false; |
238 return false; | 246 return false; |
239 } else { | 247 } else { |
240 pad->connected = true; | 248 pad->connected = true; |
241 swprintf(pad->id, | 249 swprintf(pad->id, WebGamepad::idLengthCap, |
242 WebGamepad::idLengthCap, | |
243 L"Xbox 360 Controller (XInput STANDARD %ls)", | 250 L"Xbox 360 Controller (XInput STANDARD %ls)", |
244 GamepadSubTypeName(caps.SubType)); | 251 GamepadSubTypeName(caps.SubType)); |
245 swprintf(pad->mapping, WebGamepad::mappingLengthCap, L"standard"); | 252 swprintf(pad->mapping, WebGamepad::mappingLengthCap, L"standard"); |
246 return true; | 253 return true; |
247 } | 254 } |
248 } | 255 } |
249 | 256 |
250 void GamepadPlatformDataFetcherWin::GetXInputPadData( | 257 void GamepadPlatformDataFetcherWin::GetXInputPadData(int i, WebGamepad* pad) { |
251 int i, | |
252 WebGamepad* pad) { | |
253 XINPUT_STATE state; | 258 XINPUT_STATE state; |
254 memset(&state, 0, sizeof(XINPUT_STATE)); | 259 memset(&state, 0, sizeof(XINPUT_STATE)); |
255 TRACE_EVENT_BEGIN1("GAMEPAD", "XInputGetState", "id", i); | 260 TRACE_EVENT_BEGIN1("GAMEPAD", "XInputGetState", "id", i); |
256 DWORD dwResult = xinput_get_state_(platform_pad_state_[i].xinput_index, | 261 DWORD dwResult = |
257 &state); | 262 xinput_get_state_(platform_pad_state_[i].xinput_index, &state); |
258 TRACE_EVENT_END1("GAMEPAD", "XInputGetState", "id", i); | 263 TRACE_EVENT_END1("GAMEPAD", "XInputGetState", "id", i); |
259 | 264 |
260 if (dwResult == ERROR_SUCCESS) { | 265 if (dwResult == ERROR_SUCCESS) { |
261 pad->timestamp = state.dwPacketNumber; | 266 pad->timestamp = state.dwPacketNumber; |
262 pad->buttonsLength = 0; | 267 pad->buttonsLength = 0; |
263 WORD val = state.Gamepad.wButtons; | 268 WORD val = state.Gamepad.wButtons; |
264 #define ADD(b) pad->buttons[pad->buttonsLength].pressed = (val & (b)) != 0; \ | 269 #define ADD(b) \ |
| 270 pad->buttons[pad->buttonsLength].pressed = (val & (b)) != 0; \ |
265 pad->buttons[pad->buttonsLength++].value = ((val & (b)) ? 1.f : 0.f); | 271 pad->buttons[pad->buttonsLength++].value = ((val & (b)) ? 1.f : 0.f); |
266 ADD(XINPUT_GAMEPAD_A); | 272 ADD(XINPUT_GAMEPAD_A); |
267 ADD(XINPUT_GAMEPAD_B); | 273 ADD(XINPUT_GAMEPAD_B); |
268 ADD(XINPUT_GAMEPAD_X); | 274 ADD(XINPUT_GAMEPAD_X); |
269 ADD(XINPUT_GAMEPAD_Y); | 275 ADD(XINPUT_GAMEPAD_Y); |
270 ADD(XINPUT_GAMEPAD_LEFT_SHOULDER); | 276 ADD(XINPUT_GAMEPAD_LEFT_SHOULDER); |
271 ADD(XINPUT_GAMEPAD_RIGHT_SHOULDER); | 277 ADD(XINPUT_GAMEPAD_RIGHT_SHOULDER); |
272 | 278 |
273 pad->buttons[pad->buttonsLength].pressed = | 279 pad->buttons[pad->buttonsLength].pressed = |
274 state.Gamepad.bLeftTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; | 280 state.Gamepad.bLeftTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; |
275 pad->buttons[pad->buttonsLength++].value = | 281 pad->buttons[pad->buttonsLength++].value = |
276 state.Gamepad.bLeftTrigger / 255.f; | 282 state.Gamepad.bLeftTrigger / 255.f; |
277 | 283 |
278 pad->buttons[pad->buttonsLength].pressed = | 284 pad->buttons[pad->buttonsLength].pressed = |
279 state.Gamepad.bRightTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; | 285 state.Gamepad.bRightTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; |
280 pad->buttons[pad->buttonsLength++].value = | 286 pad->buttons[pad->buttonsLength++].value = |
281 state.Gamepad.bRightTrigger / 255.f; | 287 state.Gamepad.bRightTrigger / 255.f; |
282 | 288 |
283 ADD(XINPUT_GAMEPAD_BACK); | 289 ADD(XINPUT_GAMEPAD_BACK); |
284 ADD(XINPUT_GAMEPAD_START); | 290 ADD(XINPUT_GAMEPAD_START); |
285 ADD(XINPUT_GAMEPAD_LEFT_THUMB); | 291 ADD(XINPUT_GAMEPAD_LEFT_THUMB); |
286 ADD(XINPUT_GAMEPAD_RIGHT_THUMB); | 292 ADD(XINPUT_GAMEPAD_RIGHT_THUMB); |
287 ADD(XINPUT_GAMEPAD_DPAD_UP); | 293 ADD(XINPUT_GAMEPAD_DPAD_UP); |
288 ADD(XINPUT_GAMEPAD_DPAD_DOWN); | 294 ADD(XINPUT_GAMEPAD_DPAD_DOWN); |
289 ADD(XINPUT_GAMEPAD_DPAD_LEFT); | 295 ADD(XINPUT_GAMEPAD_DPAD_LEFT); |
290 ADD(XINPUT_GAMEPAD_DPAD_RIGHT); | 296 ADD(XINPUT_GAMEPAD_DPAD_RIGHT); |
291 #undef ADD | 297 #undef ADD |
292 pad->axesLength = 0; | 298 pad->axesLength = 0; |
293 | 299 |
294 float value = 0.0; | 300 float value = 0.0; |
295 #define ADD(a, factor) value = factor * NormalizeXInputAxis(a); \ | 301 #define ADD(a, factor) \ |
| 302 value = factor * NormalizeXInputAxis(a); \ |
296 pad->axes[pad->axesLength++] = value; | 303 pad->axes[pad->axesLength++] = value; |
297 | 304 |
298 // XInput are +up/+right, -down/-left, we want -up/-left. | 305 // XInput are +up/+right, -down/-left, we want -up/-left. |
299 ADD(state.Gamepad.sThumbLX, 1); | 306 ADD(state.Gamepad.sThumbLX, 1); |
300 ADD(state.Gamepad.sThumbLY, -1); | 307 ADD(state.Gamepad.sThumbLY, -1); |
301 ADD(state.Gamepad.sThumbRX, 1); | 308 ADD(state.Gamepad.sThumbRX, 1); |
302 ADD(state.Gamepad.sThumbRY, -1); | 309 ADD(state.Gamepad.sThumbRY, -1); |
303 #undef ADD | 310 #undef ADD |
304 } else { | 311 } else { |
305 pad->connected = false; | 312 pad->connected = false; |
306 } | 313 } |
307 } | 314 } |
308 | 315 |
309 void GamepadPlatformDataFetcherWin::GetRawInputPadData( | 316 void GamepadPlatformDataFetcherWin::GetRawInputPadData(int index, |
310 int index, | 317 WebGamepad* pad) { |
311 WebGamepad* pad) { | |
312 RawGamepadInfo* gamepad = raw_input_fetcher_->GetGamepadInfo( | 318 RawGamepadInfo* gamepad = raw_input_fetcher_->GetGamepadInfo( |
313 platform_pad_state_[index].raw_input_handle); | 319 platform_pad_state_[index].raw_input_handle); |
314 if (!gamepad) { | 320 if (!gamepad) { |
315 pad->connected = false; | 321 pad->connected = false; |
316 return; | 322 return; |
317 } | 323 } |
318 | 324 |
319 pad->timestamp = gamepad->report_id; | 325 pad->timestamp = gamepad->report_id; |
320 pad->buttonsLength = gamepad->buttons_length; | 326 pad->buttonsLength = gamepad->buttons_length; |
321 pad->axesLength = gamepad->axes_length; | 327 pad->axesLength = gamepad->axes_length; |
322 | 328 |
323 for (unsigned int i = 0; i < pad->buttonsLength; i++) { | 329 for (unsigned int i = 0; i < pad->buttonsLength; i++) { |
324 pad->buttons[i].pressed = gamepad->buttons[i]; | 330 pad->buttons[i].pressed = gamepad->buttons[i]; |
325 pad->buttons[i].value = gamepad->buttons[i] ? 1.0 : 0.0; | 331 pad->buttons[i].value = gamepad->buttons[i] ? 1.0 : 0.0; |
326 } | 332 } |
327 | 333 |
328 for (unsigned int i = 0; i < pad->axesLength; i++) | 334 for (unsigned int i = 0; i < pad->axesLength; i++) |
329 pad->axes[i] = gamepad->axes[i].value; | 335 pad->axes[i] = gamepad->axes[i].value; |
330 } | 336 } |
331 | 337 |
(...skipping 10 matching lines...) Expand all Loading... |
342 xinput_dll_.GetFunctionPointer("XInputGetState")); | 348 xinput_dll_.GetFunctionPointer("XInputGetState")); |
343 if (!xinput_get_state_) | 349 if (!xinput_get_state_) |
344 return false; | 350 return false; |
345 if (xinput_enable) { | 351 if (xinput_enable) { |
346 // XInputEnable is unavailable before Win8 and deprecated in Win10. | 352 // XInputEnable is unavailable before Win8 and deprecated in Win10. |
347 xinput_enable(true); | 353 xinput_enable(true); |
348 } | 354 } |
349 return true; | 355 return true; |
350 } | 356 } |
351 | 357 |
352 } // namespace content | 358 } // namespace device |
OLD | NEW |