Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: content/browser/gamepad/gamepad_platform_data_fetcher_mac.mm

Issue 165983005: Updating Gamepad API to match latest spec (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added compile flag to enable new Blink interface Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_mac.h" 5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_mac.h"
6 6
7 #include "base/mac/foundation_util.h" 7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h" 8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/string16.h" 9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 13
14 #import <Foundation/Foundation.h> 14 #import <Foundation/Foundation.h>
15 #include <IOKit/hid/IOHIDKeys.h> 15 #include <IOKit/hid/IOHIDKeys.h>
16 16
17 using blink::WebGamepad; 17 using blink::WebGamepad;
18 using blink::WebGamepads; 18 using blink::WebGamepads;
19 19
20 namespace content { 20 namespace content {
21 21
22 namespace { 22 namespace {
23 23
24 void copyNSStringAsUTF16LittleEndian(
scottmg 2014/02/20 01:15:13 rename to Copy...
25 NSString* src, blink::WebUChar* dest, size_t dest_len) {
26 NSData* as16 = [src dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
27 memset(dest, 0, dest_len);
28 [as16 getBytes:dest length:dest_len - sizeof(blink::WebUChar)];
29 }
30
24 NSDictionary* DeviceMatching(uint32_t usage_page, uint32_t usage) { 31 NSDictionary* DeviceMatching(uint32_t usage_page, uint32_t usage) {
25 return [NSDictionary dictionaryWithObjectsAndKeys: 32 return [NSDictionary dictionaryWithObjectsAndKeys:
26 [NSNumber numberWithUnsignedInt:usage_page], 33 [NSNumber numberWithUnsignedInt:usage_page],
27 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsagePageKey)), 34 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsagePageKey)),
28 [NSNumber numberWithUnsignedInt:usage], 35 [NSNumber numberWithUnsignedInt:usage],
29 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsageKey)), 36 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsageKey)),
30 nil]; 37 nil];
31 } 38 }
32 39
33 float NormalizeAxis(CFIndex value, CFIndex min, CFIndex max) { 40 float NormalizeAxis(CFIndex value, CFIndex min, CFIndex max) {
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 snprintf(product_as_str, sizeof(product_as_str), "%04x", product_int); 261 snprintf(product_as_str, sizeof(product_as_str), "%04x", product_int);
255 associated_[slot].hid.mapper = 262 associated_[slot].hid.mapper =
256 GetGamepadStandardMappingFunction(vendor_as_str, product_as_str); 263 GetGamepadStandardMappingFunction(vendor_as_str, product_as_str);
257 264
258 NSString* ident = [NSString stringWithFormat: 265 NSString* ident = [NSString stringWithFormat:
259 @"%@ (%sVendor: %04x Product: %04x)", 266 @"%@ (%sVendor: %04x Product: %04x)",
260 product, 267 product,
261 associated_[slot].hid.mapper ? "STANDARD GAMEPAD " : "", 268 associated_[slot].hid.mapper ? "STANDARD GAMEPAD " : "",
262 vendor_int, 269 vendor_int,
263 product_int]; 270 product_int];
264 NSData* as16 = [ident dataUsingEncoding:NSUTF16LittleEndianStringEncoding]; 271 copyNSStringAsUTF16LittleEndian(
272 ident,
273 data_.items[slot].id,
274 sizeof(data_.items[slot].id));
265 275
266 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); 276 if (associated_[slot].hid.mapper) {
267 memset(&data_.items[slot].id, 0, kOutputLengthBytes); 277 copyNSStringAsUTF16LittleEndian(
268 [as16 getBytes:data_.items[slot].id 278 @"standard",
269 length:kOutputLengthBytes - sizeof(blink::WebUChar)]; 279 data_.items[slot].mapping,
280 sizeof(data_.items[slot].mapping));
281 } else {
282 data_.items[slot].mapping[0] = 0;
283 }
270 284
271 base::ScopedCFTypeRef<CFArrayRef> elements( 285 base::ScopedCFTypeRef<CFArrayRef> elements(
272 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); 286 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone));
273 AddButtonsAndAxes(CFToNSCast(elements), slot); 287 AddButtonsAndAxes(CFToNSCast(elements), slot);
274 288
275 associated_[slot].hid.device_ref = device; 289 associated_[slot].hid.device_ref = device;
276 data_.items[slot].connected = true; 290 data_.items[slot].connected = true;
277 if (slot >= data_.length) 291 if (slot >= data_.length)
278 data_.length = slot + 1; 292 data_.length = slot + 1;
279 } 293 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 327 }
314 if (slot == data_.length) 328 if (slot == data_.length)
315 return; 329 return;
316 330
317 WebGamepad& pad = data_.items[slot]; 331 WebGamepad& pad = data_.items[slot];
318 AssociatedData& associated = associated_[slot]; 332 AssociatedData& associated = associated_[slot];
319 333
320 // Find and fill in the associated button event, if any. 334 // Find and fill in the associated button event, if any.
321 for (size_t i = 0; i < pad.buttonsLength; ++i) { 335 for (size_t i = 0; i < pad.buttonsLength; ++i) {
322 if (associated.hid.button_elements[i] == element) { 336 if (associated.hid.button_elements[i] == element) {
323 pad.buttons[i] = IOHIDValueGetIntegerValue(value) ? 1.f : 0.f; 337 pad.buttons[i].pressed = IOHIDValueGetIntegerValue(value);
338 pad.buttons[i].value = pad.buttons[i].pressed ? 1.f : 0.f;
324 pad.timestamp = std::max(pad.timestamp, IOHIDValueGetTimeStamp(value)); 339 pad.timestamp = std::max(pad.timestamp, IOHIDValueGetTimeStamp(value));
325 return; 340 return;
326 } 341 }
327 } 342 }
328 343
329 // Find and fill in the associated axis event, if any. 344 // Find and fill in the associated axis event, if any.
330 for (size_t i = 0; i < pad.axesLength; ++i) { 345 for (size_t i = 0; i < pad.axesLength; ++i) {
331 if (associated.hid.axis_elements[i] == element) { 346 if (associated.hid.axis_elements[i] == element) {
332 pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value), 347 pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value),
333 associated.hid.axis_minimums[i], 348 associated.hid.axis_minimums[i],
(...skipping 14 matching lines...) Expand all
348 if (slot == WebGamepads::itemsLengthCap) 363 if (slot == WebGamepads::itemsLengthCap)
349 return; 364 return;
350 365
351 device->SetLEDPattern( 366 device->SetLEDPattern(
352 (XboxController::LEDPattern)(XboxController::LED_FLASH_TOP_LEFT + slot)); 367 (XboxController::LEDPattern)(XboxController::LED_FLASH_TOP_LEFT + slot));
353 368
354 NSString* ident = 369 NSString* ident =
355 [NSString stringWithFormat: 370 [NSString stringWithFormat:
356 @"Xbox 360 Controller (STANDARD GAMEPAD Vendor: %04x Product: %04x)", 371 @"Xbox 360 Controller (STANDARD GAMEPAD Vendor: %04x Product: %04x)",
357 device->GetProductId(), device->GetVendorId()]; 372 device->GetProductId(), device->GetVendorId()];
358 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; 373 copyNSStringAsUTF16LittleEndian(
359 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); 374 ident,
360 memset(&data_.items[slot].id, 0, kOutputLengthBytes); 375 data_.items[slot].id,
361 [as16 getBytes:data_.items[slot].id 376 sizeof(data_.items[slot].id));
362 length:kOutputLengthBytes - sizeof(blink::WebUChar)]; 377
378 copyNSStringAsUTF16LittleEndian(
379 @"standard",
380 data_.items[slot].mapping,
381 sizeof(data_.items[slot].mapping));
363 382
364 associated_[slot].is_xbox = true; 383 associated_[slot].is_xbox = true;
365 associated_[slot].xbox.device = device; 384 associated_[slot].xbox.device = device;
366 associated_[slot].xbox.location_id = device->location_id(); 385 associated_[slot].xbox.location_id = device->location_id();
367 data_.items[slot].connected = true; 386 data_.items[slot].connected = true;
368 data_.items[slot].axesLength = 4; 387 data_.items[slot].axesLength = 4;
369 data_.items[slot].buttonsLength = 17; 388 data_.items[slot].buttonsLength = 17;
370 data_.items[slot].timestamp = 0; 389 data_.items[slot].timestamp = 0;
371 if (slot >= data_.length) 390 if (slot >= data_.length)
372 data_.length = slot + 1; 391 data_.length = slot + 1;
(...skipping 26 matching lines...) Expand all
399 associated_[slot].is_xbox && 418 associated_[slot].is_xbox &&
400 associated_[slot].xbox.device == device) 419 associated_[slot].xbox.device == device)
401 break; 420 break;
402 } 421 }
403 if (slot == data_.length) 422 if (slot == data_.length)
404 return; 423 return;
405 424
406 WebGamepad& pad = data_.items[slot]; 425 WebGamepad& pad = data_.items[slot];
407 426
408 for (size_t i = 0; i < 6; i++) { 427 for (size_t i = 0; i < 6; i++) {
409 pad.buttons[i] = data.buttons[i] ? 1.0f : 0.0f; 428 pad.buttons[i].pressed = data.buttons[i];
429 pad.buttons[i].value = data.buttons[i] ? 1.0f : 0.0f;
410 } 430 }
411 pad.buttons[6] = data.triggers[0]; 431 pad.buttons[6].pressed = data.triggers[0] > 0.1f;
scottmg 2014/02/20 01:15:13 Did the .1 come from anywhere in particular? it'd
412 pad.buttons[7] = data.triggers[1]; 432 pad.buttons[6].value = data.triggers[0];
433 pad.buttons[7].pressed = data.triggers[1] > 0.1f;
434 pad.buttons[7].value = data.triggers[1];
413 for (size_t i = 8; i < 17; i++) { 435 for (size_t i = 8; i < 17; i++) {
414 pad.buttons[i] = data.buttons[i - 2] ? 1.0f : 0.0f; 436 pad.buttons[i].pressed = data.buttons[i - 2];
437 pad.buttons[i].value = data.buttons[i - 2] ? 1.0f : 0.0f;
415 } 438 }
416 for (size_t i = 0; i < arraysize(data.axes); i++) { 439 for (size_t i = 0; i < arraysize(data.axes); i++) {
417 pad.axes[i] = data.axes[i]; 440 pad.axes[i] = data.axes[i];
418 } 441 }
419 442
420 pad.timestamp = base::TimeTicks::Now().ToInternalValue(); 443 pad.timestamp = base::TimeTicks::Now().ToInternalValue();
421 } 444 }
422 445
423 void GamepadPlatformDataFetcherMac::GetGamepadData(WebGamepads* pads, bool) { 446 void GamepadPlatformDataFetcherMac::GetGamepadData(WebGamepads* pads, bool) {
424 if (!enabled_ && !xbox_fetcher_) { 447 if (!enabled_ && !xbox_fetcher_) {
425 pads->length = 0; 448 pads->length = 0;
426 return; 449 return;
427 } 450 }
428 451
429 // Copy to the current state to the output buffer, using the mapping 452 // Copy to the current state to the output buffer, using the mapping
430 // function, if there is one available. 453 // function, if there is one available.
431 pads->length = WebGamepads::itemsLengthCap; 454 pads->length = WebGamepads::itemsLengthCap;
432 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { 455 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) {
433 if (!associated_[i].is_xbox && associated_[i].hid.mapper) 456 if (!associated_[i].is_xbox && associated_[i].hid.mapper)
434 associated_[i].hid.mapper(data_.items[i], &pads->items[i]); 457 associated_[i].hid.mapper(data_.items[i], &pads->items[i]);
435 else 458 else
436 pads->items[i] = data_.items[i]; 459 pads->items[i] = data_.items[i];
437 } 460 }
438 } 461 }
439 462
440 } // namespace content 463 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698