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_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( |
| 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); | 255 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); |
249 int vendor_int = [vendor_id intValue]; | 256 int vendor_int = [vendor_id intValue]; |
250 int product_int = [product_id intValue]; | 257 int product_int = [product_id intValue]; |
251 | 258 |
252 char vendor_as_str[5], product_as_str[5]; | 259 char vendor_as_str[5], product_as_str[5]; |
253 snprintf(vendor_as_str, sizeof(vendor_as_str), "%04x", vendor_int); | 260 snprintf(vendor_as_str, sizeof(vendor_as_str), "%04x", vendor_int); |
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 |
| 265 #ifdef ENABLE_NEW_GAMEPAD_API |
| 266 NSString* ident = [NSString stringWithFormat: |
| 267 @"%@ (Vendor: %04x Product: %04x)", |
| 268 product, |
| 269 vendor_int, |
| 270 product_int]; |
| 271 copyNSStringAsUTF16LittleEndian( |
| 272 ident, |
| 273 data_.items[slot].id, |
| 274 sizeof(data_.items[slot].id)); |
| 275 |
| 276 if (associated_[slot].hid.mapper) { |
| 277 copyNSStringAsUTF16LittleEndian( |
| 278 @"standard", |
| 279 data_.items[slot].mapping, |
| 280 sizeof(data_.items[slot].mapping)); |
| 281 } else { |
| 282 data_.items[slot].mapping[0] = 0; |
| 283 } |
| 284 #else |
258 NSString* ident = [NSString stringWithFormat: | 285 NSString* ident = [NSString stringWithFormat: |
259 @"%@ (%sVendor: %04x Product: %04x)", | 286 @"%@ (%sVendor: %04x Product: %04x)", |
260 product, | 287 product, |
261 associated_[slot].hid.mapper ? "STANDARD GAMEPAD " : "", | 288 associated_[slot].hid.mapper ? "STANDARD GAMEPAD " : "", |
262 vendor_int, | 289 vendor_int, |
263 product_int]; | 290 product_int]; |
264 NSData* as16 = [ident dataUsingEncoding:NSUTF16LittleEndianStringEncoding]; | 291 copyNSStringAsUTF16LittleEndian( |
265 | 292 ident, |
266 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); | 293 data_.items[slot].id, |
267 memset(&data_.items[slot].id, 0, kOutputLengthBytes); | 294 sizeof(data_.items[slot].id)); |
268 [as16 getBytes:data_.items[slot].id | 295 #endif |
269 length:kOutputLengthBytes - sizeof(blink::WebUChar)]; | |
270 | 296 |
271 base::ScopedCFTypeRef<CFArrayRef> elements( | 297 base::ScopedCFTypeRef<CFArrayRef> elements( |
272 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); | 298 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); |
273 AddButtonsAndAxes(CFToNSCast(elements), slot); | 299 AddButtonsAndAxes(CFToNSCast(elements), slot); |
274 | 300 |
275 associated_[slot].hid.device_ref = device; | 301 associated_[slot].hid.device_ref = device; |
276 data_.items[slot].connected = true; | 302 data_.items[slot].connected = true; |
277 if (slot >= data_.length) | 303 if (slot >= data_.length) |
278 data_.length = slot + 1; | 304 data_.length = slot + 1; |
279 } | 305 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 | 370 |
345 size_t slot = GetSlotForXboxDevice(device); | 371 size_t slot = GetSlotForXboxDevice(device); |
346 | 372 |
347 // We can't handle this many connected devices. | 373 // We can't handle this many connected devices. |
348 if (slot == WebGamepads::itemsLengthCap) | 374 if (slot == WebGamepads::itemsLengthCap) |
349 return; | 375 return; |
350 | 376 |
351 device->SetLEDPattern( | 377 device->SetLEDPattern( |
352 (XboxController::LEDPattern)(XboxController::LED_FLASH_TOP_LEFT + slot)); | 378 (XboxController::LEDPattern)(XboxController::LED_FLASH_TOP_LEFT + slot)); |
353 | 379 |
| 380 #ifdef ENABLE_NEW_GAMEPAD_API |
| 381 NSString* ident = |
| 382 [NSString stringWithFormat: |
| 383 @"Xbox 360 Controller (Vendor: %04x Product: %04x)", |
| 384 device->GetProductId(), device->GetVendorId()]; |
| 385 copyNSStringAsUTF16LittleEndian( |
| 386 ident, |
| 387 data_.items[slot].id, |
| 388 sizeof(data_.items[slot].id)); |
| 389 |
| 390 copyNSStringAsUTF16LittleEndian( |
| 391 @"standard", |
| 392 data_.items[slot].mapping, |
| 393 sizeof(data_.items[slot].mapping)); |
| 394 #else |
354 NSString* ident = | 395 NSString* ident = |
355 [NSString stringWithFormat: | 396 [NSString stringWithFormat: |
356 @"Xbox 360 Controller (STANDARD GAMEPAD Vendor: %04x Product: %04x)", | 397 @"Xbox 360 Controller (STANDARD GAMEPAD Vendor: %04x Product: %04x)", |
357 device->GetProductId(), device->GetVendorId()]; | 398 device->GetProductId(), device->GetVendorId()]; |
358 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; | 399 copyNSStringAsUTF16LittleEndian( |
359 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); | 400 ident, |
360 memset(&data_.items[slot].id, 0, kOutputLengthBytes); | 401 data_.items[slot].id, |
361 [as16 getBytes:data_.items[slot].id | 402 sizeof(data_.items[slot].id)); |
362 length:kOutputLengthBytes - sizeof(blink::WebUChar)]; | 403 #endif |
363 | 404 |
364 associated_[slot].is_xbox = true; | 405 associated_[slot].is_xbox = true; |
365 associated_[slot].xbox.device = device; | 406 associated_[slot].xbox.device = device; |
366 associated_[slot].xbox.location_id = device->location_id(); | 407 associated_[slot].xbox.location_id = device->location_id(); |
367 data_.items[slot].connected = true; | 408 data_.items[slot].connected = true; |
368 data_.items[slot].axesLength = 4; | 409 data_.items[slot].axesLength = 4; |
369 data_.items[slot].buttonsLength = 17; | 410 data_.items[slot].buttonsLength = 17; |
370 data_.items[slot].timestamp = 0; | 411 data_.items[slot].timestamp = 0; |
371 if (slot >= data_.length) | 412 if (slot >= data_.length) |
372 data_.length = slot + 1; | 413 data_.length = slot + 1; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 pads->length = WebGamepads::itemsLengthCap; | 472 pads->length = WebGamepads::itemsLengthCap; |
432 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 473 for (size_t i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
433 if (!associated_[i].is_xbox && associated_[i].hid.mapper) | 474 if (!associated_[i].is_xbox && associated_[i].hid.mapper) |
434 associated_[i].hid.mapper(data_.items[i], &pads->items[i]); | 475 associated_[i].hid.mapper(data_.items[i], &pads->items[i]); |
435 else | 476 else |
436 pads->items[i] = data_.items[i]; | 477 pads->items[i] = data_.items[i]; |
437 } | 478 } |
438 } | 479 } |
439 | 480 |
440 } // namespace content | 481 } // namespace content |
OLD | NEW |