OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/gamepad/raw_input_data_fetcher_win.h" | 5 #include "device/gamepad/raw_input_data_fetcher_win.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
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 | 12 |
13 namespace device { | 13 namespace device { |
14 | 14 |
15 using namespace blink; | |
16 | |
17 namespace { | 15 namespace { |
18 | 16 |
19 float NormalizeAxis(long value, long min, long max) { | 17 float NormalizeAxis(long value, long min, long max) { |
20 return (2.f * (value - min) / static_cast<float>(max - min)) - 1.f; | 18 return (2.f * (value - min) / static_cast<float>(max - min)) - 1.f; |
21 } | 19 } |
22 | 20 |
23 unsigned long GetBitmask(unsigned short bits) { | 21 unsigned long GetBitmask(unsigned short bits) { |
24 return (1 << bits) - 1; | 22 return (1 << bits) - 1; |
25 } | 23 } |
26 | 24 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 151 |
154 if (devices_changed_hint) | 152 if (devices_changed_hint) |
155 EnumerateDevices(); | 153 EnumerateDevices(); |
156 | 154 |
157 for (const auto& controller : controllers_) { | 155 for (const auto& controller : controllers_) { |
158 RawGamepadInfo* gamepad = controller.second; | 156 RawGamepadInfo* gamepad = controller.second; |
159 PadState* state = GetPadState(gamepad->source_id); | 157 PadState* state = GetPadState(gamepad->source_id); |
160 if (!state) | 158 if (!state) |
161 continue; | 159 continue; |
162 | 160 |
163 WebGamepad& pad = state->data; | 161 Gamepad& pad = state->data; |
164 | 162 |
165 pad.timestamp = gamepad->report_id; | 163 pad.timestamp = gamepad->report_id; |
166 pad.buttons_length = gamepad->buttons_length; | 164 pad.buttons_length = gamepad->buttons_length; |
167 pad.axes_length = gamepad->axes_length; | 165 pad.axes_length = gamepad->axes_length; |
168 | 166 |
169 for (unsigned int i = 0; i < pad.buttons_length; i++) { | 167 for (unsigned int i = 0; i < pad.buttons_length; i++) { |
170 pad.buttons[i].pressed = gamepad->buttons[i]; | 168 pad.buttons[i].pressed = gamepad->buttons[i]; |
171 pad.buttons[i].value = gamepad->buttons[i] ? 1.0 : 0.0; | 169 pad.buttons[i].value = gamepad->buttons[i] ? 1.0 : 0.0; |
172 } | 170 } |
173 | 171 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 gamepad = ParseGamepadInfo(device_handle); | 207 gamepad = ParseGamepadInfo(device_handle); |
210 if (!gamepad) | 208 if (!gamepad) |
211 continue; | 209 continue; |
212 | 210 |
213 PadState* state = GetPadState(gamepad->source_id); | 211 PadState* state = GetPadState(gamepad->source_id); |
214 if (!state) | 212 if (!state) |
215 continue; // No slot available for this gamepad. | 213 continue; // No slot available for this gamepad. |
216 | 214 |
217 controllers_[device_handle] = gamepad; | 215 controllers_[device_handle] = gamepad; |
218 | 216 |
219 WebGamepad& pad = state->data; | 217 Gamepad& pad = state->data; |
220 pad.connected = true; | 218 pad.connected = true; |
221 | 219 |
222 std::string vendor = base::StringPrintf("%04x", gamepad->vendor_id); | 220 std::string vendor = base::StringPrintf("%04x", gamepad->vendor_id); |
223 std::string product = base::StringPrintf("%04x", gamepad->product_id); | 221 std::string product = base::StringPrintf("%04x", gamepad->product_id); |
224 state->mapper = GetGamepadStandardMappingFunction(vendor, product); | 222 state->mapper = GetGamepadStandardMappingFunction(vendor, product); |
225 state->axis_mask = 0; | 223 state->axis_mask = 0; |
226 state->button_mask = 0; | 224 state->button_mask = 0; |
227 | 225 |
228 swprintf(pad.id, WebGamepad::kIdLengthCap, | 226 swprintf(pad.id, Gamepad::kIdLengthCap, |
229 L"%ls (%lsVendor: %04x Product: %04x)", gamepad->id, | 227 L"%ls (%lsVendor: %04x Product: %04x)", gamepad->id, |
230 state->mapper ? L"STANDARD GAMEPAD " : L"", gamepad->vendor_id, | 228 state->mapper ? L"STANDARD GAMEPAD " : L"", gamepad->vendor_id, |
231 gamepad->product_id); | 229 gamepad->product_id); |
232 | 230 |
233 if (state->mapper) | 231 if (state->mapper) |
234 swprintf(pad.mapping, WebGamepad::kMappingLengthCap, L"standard"); | 232 swprintf(pad.mapping, Gamepad::kMappingLengthCap, L"standard"); |
235 else | 233 else |
236 pad.mapping[0] = 0; | 234 pad.mapping[0] = 0; |
237 } | 235 } |
238 | 236 |
239 gamepad->enumeration_id = last_enumeration_id_; | 237 gamepad->enumeration_id = last_enumeration_id_; |
240 } | 238 } |
241 } | 239 } |
242 | 240 |
243 // Clear out old controllers that weren't part of this enumeration pass. | 241 // Clear out old controllers that weren't part of this enumeration pass. |
244 for (const auto& controller : controllers_) { | 242 for (const auto& controller : controllers_) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 HANDLE hid_handle = CreateFile( | 327 HANDLE hid_handle = CreateFile( |
330 name_buffer.get(), GENERIC_READ | GENERIC_WRITE, | 328 name_buffer.get(), GENERIC_READ | GENERIC_WRITE, |
331 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL); | 329 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL); |
332 if (hid_handle) { | 330 if (hid_handle) { |
333 got_product_string = hidd_get_product_string_(hid_handle, gamepad_info->id, | 331 got_product_string = hidd_get_product_string_(hid_handle, gamepad_info->id, |
334 sizeof(gamepad_info->id)); | 332 sizeof(gamepad_info->id)); |
335 CloseHandle(hid_handle); | 333 CloseHandle(hid_handle); |
336 } | 334 } |
337 | 335 |
338 if (!got_product_string) | 336 if (!got_product_string) |
339 swprintf(gamepad_info->id, WebGamepad::kIdLengthCap, L"Unknown Gamepad"); | 337 swprintf(gamepad_info->id, Gamepad::kIdLengthCap, L"Unknown Gamepad"); |
340 | 338 |
341 // Query device capabilities. | 339 // Query device capabilities. |
342 result = GetRawInputDeviceInfo(hDevice, RIDI_PREPARSEDDATA, NULL, &size); | 340 result = GetRawInputDeviceInfo(hDevice, RIDI_PREPARSEDDATA, NULL, &size); |
343 if (result == static_cast<UINT>(-1)) { | 341 if (result == static_cast<UINT>(-1)) { |
344 PLOG(ERROR) << "GetRawInputDeviceInfo() failed"; | 342 PLOG(ERROR) << "GetRawInputDeviceInfo() failed"; |
345 return NULL; | 343 return NULL; |
346 } | 344 } |
347 DCHECK_EQ(0u, result); | 345 DCHECK_EQ(0u, result); |
348 | 346 |
349 gamepad_info->ppd_buffer.reset(new uint8_t[size]); | 347 gamepad_info->ppd_buffer.reset(new uint8_t[size]); |
(...skipping 14 matching lines...) Expand all Loading... |
364 // Query button information. | 362 // Query button information. |
365 USHORT count = caps.NumberInputButtonCaps; | 363 USHORT count = caps.NumberInputButtonCaps; |
366 if (count > 0) { | 364 if (count > 0) { |
367 std::unique_ptr<HIDP_BUTTON_CAPS[]> button_caps( | 365 std::unique_ptr<HIDP_BUTTON_CAPS[]> button_caps( |
368 new HIDP_BUTTON_CAPS[count]); | 366 new HIDP_BUTTON_CAPS[count]); |
369 status = hidp_get_button_caps_(HidP_Input, button_caps.get(), &count, | 367 status = hidp_get_button_caps_(HidP_Input, button_caps.get(), &count, |
370 gamepad_info->preparsed_data); | 368 gamepad_info->preparsed_data); |
371 DCHECK_EQ(HIDP_STATUS_SUCCESS, status); | 369 DCHECK_EQ(HIDP_STATUS_SUCCESS, status); |
372 | 370 |
373 for (uint32_t i = 0; i < count; ++i) { | 371 for (uint32_t i = 0; i < count; ++i) { |
374 if (button_caps[i].Range.UsageMin <= WebGamepad::kButtonsLengthCap && | 372 if (button_caps[i].Range.UsageMin <= Gamepad::kButtonsLengthCap && |
375 button_caps[i].UsagePage == kButtonUsagePage) { | 373 button_caps[i].UsagePage == kButtonUsagePage) { |
376 uint32_t max_index = | 374 uint32_t max_index = |
377 std::min(WebGamepad::kButtonsLengthCap, | 375 std::min(Gamepad::kButtonsLengthCap, |
378 static_cast<size_t>(button_caps[i].Range.UsageMax)); | 376 static_cast<size_t>(button_caps[i].Range.UsageMax)); |
379 gamepad_info->buttons_length = | 377 gamepad_info->buttons_length = |
380 std::max(gamepad_info->buttons_length, max_index); | 378 std::max(gamepad_info->buttons_length, max_index); |
381 } | 379 } |
382 } | 380 } |
383 } | 381 } |
384 | 382 |
385 // Query axis information. | 383 // Query axis information. |
386 count = caps.NumberInputValueCaps; | 384 count = caps.NumberInputValueCaps; |
387 std::unique_ptr<HIDP_VALUE_CAPS[]> axes_caps(new HIDP_VALUE_CAPS[count]); | 385 std::unique_ptr<HIDP_VALUE_CAPS[]> axes_caps(new HIDP_VALUE_CAPS[count]); |
388 status = hidp_get_value_caps_(HidP_Input, axes_caps.get(), &count, | 386 status = hidp_get_value_caps_(HidP_Input, axes_caps.get(), &count, |
389 gamepad_info->preparsed_data); | 387 gamepad_info->preparsed_data); |
390 | 388 |
391 bool mapped_all_axes = true; | 389 bool mapped_all_axes = true; |
392 | 390 |
393 for (UINT i = 0; i < count; i++) { | 391 for (UINT i = 0; i < count; i++) { |
394 uint32_t axis_index = axes_caps[i].Range.UsageMin - kAxisMinimumUsageNumber; | 392 uint32_t axis_index = axes_caps[i].Range.UsageMin - kAxisMinimumUsageNumber; |
395 if (axis_index < WebGamepad::kAxesLengthCap) { | 393 if (axis_index < Gamepad::kAxesLengthCap) { |
396 gamepad_info->axes[axis_index].caps = axes_caps[i]; | 394 gamepad_info->axes[axis_index].caps = axes_caps[i]; |
397 gamepad_info->axes[axis_index].value = 0; | 395 gamepad_info->axes[axis_index].value = 0; |
398 gamepad_info->axes[axis_index].active = true; | 396 gamepad_info->axes[axis_index].active = true; |
399 gamepad_info->axes[axis_index].bitmask = GetBitmask(axes_caps[i].BitSize); | 397 gamepad_info->axes[axis_index].bitmask = GetBitmask(axes_caps[i].BitSize); |
400 gamepad_info->axes_length = | 398 gamepad_info->axes_length = |
401 std::max(gamepad_info->axes_length, axis_index + 1); | 399 std::max(gamepad_info->axes_length, axis_index + 1); |
402 } else { | 400 } else { |
403 mapped_all_axes = false; | 401 mapped_all_axes = false; |
404 } | 402 } |
405 } | 403 } |
406 | 404 |
407 if (!mapped_all_axes) { | 405 if (!mapped_all_axes) { |
408 // For axes who's usage puts them outside the standard axesLengthCap range. | 406 // For axes who's usage puts them outside the standard axesLengthCap range. |
409 uint32_t next_index = 0; | 407 uint32_t next_index = 0; |
410 for (UINT i = 0; i < count; i++) { | 408 for (UINT i = 0; i < count; i++) { |
411 uint32_t usage = axes_caps[i].Range.UsageMin - kAxisMinimumUsageNumber; | 409 uint32_t usage = axes_caps[i].Range.UsageMin - kAxisMinimumUsageNumber; |
412 if (usage >= WebGamepad::kAxesLengthCap && | 410 if (usage >= Gamepad::kAxesLengthCap && |
413 axes_caps[i].UsagePage <= kGameControlsUsagePage) { | 411 axes_caps[i].UsagePage <= kGameControlsUsagePage) { |
414 for (; next_index < WebGamepad::kAxesLengthCap; ++next_index) { | 412 for (; next_index < Gamepad::kAxesLengthCap; ++next_index) { |
415 if (!gamepad_info->axes[next_index].active) | 413 if (!gamepad_info->axes[next_index].active) |
416 break; | 414 break; |
417 } | 415 } |
418 if (next_index < WebGamepad::kAxesLengthCap) { | 416 if (next_index < Gamepad::kAxesLengthCap) { |
419 gamepad_info->axes[next_index].caps = axes_caps[i]; | 417 gamepad_info->axes[next_index].caps = axes_caps[i]; |
420 gamepad_info->axes[next_index].value = 0; | 418 gamepad_info->axes[next_index].value = 0; |
421 gamepad_info->axes[next_index].active = true; | 419 gamepad_info->axes[next_index].active = true; |
422 gamepad_info->axes[next_index].bitmask = | 420 gamepad_info->axes[next_index].bitmask = |
423 GetBitmask(axes_caps[i].BitSize); | 421 GetBitmask(axes_caps[i].BitSize); |
424 gamepad_info->axes_length = | 422 gamepad_info->axes_length = |
425 std::max(gamepad_info->axes_length, next_index + 1); | 423 std::max(gamepad_info->axes_length, next_index + 1); |
426 } | 424 } |
427 } | 425 } |
428 | 426 |
429 if (next_index >= WebGamepad::kAxesLengthCap) | 427 if (next_index >= Gamepad::kAxesLengthCap) |
430 break; | 428 break; |
431 } | 429 } |
432 } | 430 } |
433 | 431 |
434 // Sometimes devices show up with no buttons or axes. Don't return these. | 432 // Sometimes devices show up with no buttons or axes. Don't return these. |
435 if (gamepad_info->buttons_length == 0 && gamepad_info->axes_length == 0) | 433 if (gamepad_info->buttons_length == 0 && gamepad_info->axes_length == 0) |
436 return nullptr; | 434 return nullptr; |
437 | 435 |
438 return gamepad_info.release(); | 436 return gamepad_info.release(); |
439 } | 437 } |
(...skipping 21 matching lines...) Expand all Loading... |
461 hidp_get_usages_ex_(HidP_Input, 0, usages.get(), &buttons_length, | 459 hidp_get_usages_ex_(HidP_Input, 0, usages.get(), &buttons_length, |
462 gamepad_info->preparsed_data, | 460 gamepad_info->preparsed_data, |
463 reinterpret_cast<PCHAR>(input->data.hid.bRawData), | 461 reinterpret_cast<PCHAR>(input->data.hid.bRawData), |
464 input->data.hid.dwSizeHid); | 462 input->data.hid.dwSizeHid); |
465 | 463 |
466 if (status == HIDP_STATUS_SUCCESS) { | 464 if (status == HIDP_STATUS_SUCCESS) { |
467 // Set each reported button to true. | 465 // Set each reported button to true. |
468 for (uint32_t j = 0; j < buttons_length; j++) { | 466 for (uint32_t j = 0; j < buttons_length; j++) { |
469 int32_t button_index = usages[j].Usage - 1; | 467 int32_t button_index = usages[j].Usage - 1; |
470 if (usages[j].UsagePage == kButtonUsagePage && button_index >= 0 && | 468 if (usages[j].UsagePage == kButtonUsagePage && button_index >= 0 && |
471 button_index < | 469 button_index < static_cast<int>(Gamepad::kButtonsLengthCap)) { |
472 static_cast<int>(blink::WebGamepad::kButtonsLengthCap)) { | |
473 gamepad_info->buttons[button_index] = true; | 470 gamepad_info->buttons[button_index] = true; |
474 } | 471 } |
475 } | 472 } |
476 } | 473 } |
477 } | 474 } |
478 | 475 |
479 // Query axis state. | 476 // Query axis state. |
480 ULONG axis_value = 0; | 477 ULONG axis_value = 0; |
481 LONG scaled_axis_value = 0; | 478 LONG scaled_axis_value = 0; |
482 for (uint32_t i = 0; i < gamepad_info->axes_length; i++) { | 479 for (uint32_t i = 0; i < gamepad_info->axes_length; i++) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 return false; | 590 return false; |
594 hidd_get_product_string_ = reinterpret_cast<HidDGetStringFunc>( | 591 hidd_get_product_string_ = reinterpret_cast<HidDGetStringFunc>( |
595 hid_dll_.GetFunctionPointer("HidD_GetProductString")); | 592 hid_dll_.GetFunctionPointer("HidD_GetProductString")); |
596 if (!hidd_get_product_string_) | 593 if (!hidd_get_product_string_) |
597 return false; | 594 return false; |
598 | 595 |
599 return true; | 596 return true; |
600 } | 597 } |
601 | 598 |
602 } // namespace device | 599 } // namespace device |
OLD | NEW |