| 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/platform_data_fetcher_mac.h" | 5 #include "content/browser/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/memory/scoped_nsobject.h" | 8 #include "base/memory/scoped_nsobject.h" |
| 9 #include "base/string16.h" | 9 #include "base/string16.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 const uint32_t kJoystickUsageNumber = 0x04; | 36 const uint32_t kJoystickUsageNumber = 0x04; |
| 37 const uint32_t kGameUsageNumber = 0x05; | 37 const uint32_t kGameUsageNumber = 0x05; |
| 38 const uint32_t kMultiAxisUsageNumber = 0x08; | 38 const uint32_t kMultiAxisUsageNumber = 0x08; |
| 39 const uint32_t kAxisMinimumUsageNumber = 0x30; | 39 const uint32_t kAxisMinimumUsageNumber = 0x30; |
| 40 const uint32_t kAxisMaximumUsageNumber = 0x35; | 40 const uint32_t kAxisMaximumUsageNumber = 0x35; |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 GamepadPlatformDataFetcherMac::GamepadPlatformDataFetcherMac() | 44 GamepadPlatformDataFetcherMac::GamepadPlatformDataFetcherMac() |
| 45 : enabled_(true) { | 45 : enabled_(true) { |
| 46 memset(associated_, 0, sizeof(associated_)); |
| 46 hid_manager_ref_.reset(IOHIDManagerCreate(kCFAllocatorDefault, | 47 hid_manager_ref_.reset(IOHIDManagerCreate(kCFAllocatorDefault, |
| 47 kIOHIDOptionsTypeNone)); | 48 kIOHIDOptionsTypeNone)); |
| 48 if (CFGetTypeID(hid_manager_ref_) != IOHIDManagerGetTypeID()) { | 49 if (CFGetTypeID(hid_manager_ref_) != IOHIDManagerGetTypeID()) { |
| 49 enabled_ = false; | 50 enabled_ = false; |
| 50 return; | 51 return; |
| 51 } | 52 } |
| 52 | 53 |
| 53 scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects: | 54 scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects: |
| 54 DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber), | 55 DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber), |
| 55 DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber), | 56 DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber), |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 // We can't handle this many connected devices. | 194 // We can't handle this many connected devices. |
| 194 if (slot == WebGamepads::itemsLengthCap) | 195 if (slot == WebGamepads::itemsLengthCap) |
| 195 return; | 196 return; |
| 196 | 197 |
| 197 NSNumber* vendor_id = CFToNSCast(CFCastStrict<CFNumberRef>( | 198 NSNumber* vendor_id = CFToNSCast(CFCastStrict<CFNumberRef>( |
| 198 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)))); | 199 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)))); |
| 199 NSNumber* product_id = CFToNSCast(CFCastStrict<CFNumberRef>( | 200 NSNumber* product_id = CFToNSCast(CFCastStrict<CFNumberRef>( |
| 200 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)))); | 201 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)))); |
| 201 NSString* product = CFToNSCast(CFCastStrict<CFStringRef>( | 202 NSString* product = CFToNSCast(CFCastStrict<CFStringRef>( |
| 202 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); | 203 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); |
| 204 int vendor_int = [vendor_id intValue]; |
| 205 int product_int = [product_id intValue]; |
| 203 | 206 |
| 204 NSString* ident([NSString stringWithFormat: | 207 char vendor_as_str[5], product_as_str[5]; |
| 205 @"%@ (Vendor: %04x Product: %04x)", | 208 snprintf(vendor_as_str, sizeof(vendor_as_str), "%04x", vendor_int); |
| 209 snprintf(product_as_str, sizeof(product_as_str), "%04x", product_int); |
| 210 associated_[slot].mapper = |
| 211 GetGamepadStandardMappingFunction(vendor_as_str, product_as_str); |
| 212 |
| 213 NSString* ident = [NSString stringWithFormat: |
| 214 @"%@ (%sVendor: %04x Product: %04x)", |
| 206 product, | 215 product, |
| 207 [vendor_id intValue], | 216 associated_[slot].mapper ? "STANDARD GAMEPAD " : "", |
| 208 [product_id intValue]]); | 217 vendor_int, |
| 218 product_int]; |
| 209 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; | 219 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; |
| 210 | 220 |
| 211 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); | 221 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); |
| 212 memset(&data_.items[slot].id, 0, kOutputLengthBytes); | 222 memset(&data_.items[slot].id, 0, kOutputLengthBytes); |
| 213 [as16 getBytes:data_.items[slot].id | 223 [as16 getBytes:data_.items[slot].id |
| 214 length:kOutputLengthBytes - sizeof(WebKit::WebUChar)]; | 224 length:kOutputLengthBytes - sizeof(WebKit::WebUChar)]; |
| 215 | 225 |
| 216 base::mac::ScopedCFTypeRef<CFArrayRef> elements( | 226 base::mac::ScopedCFTypeRef<CFArrayRef> elements( |
| 217 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); | 227 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); |
| 218 AddButtonsAndAxes(CFToNSCast(elements), slot); | 228 AddButtonsAndAxes(CFToNSCast(elements), slot); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 288 } |
| 279 } | 289 } |
| 280 | 290 |
| 281 void GamepadPlatformDataFetcherMac::GetGamepadData( | 291 void GamepadPlatformDataFetcherMac::GetGamepadData( |
| 282 WebKit::WebGamepads* pads, | 292 WebKit::WebGamepads* pads, |
| 283 bool) { | 293 bool) { |
| 284 if (!enabled_) { | 294 if (!enabled_) { |
| 285 pads->length = 0; | 295 pads->length = 0; |
| 286 return; | 296 return; |
| 287 } | 297 } |
| 288 memcpy(pads, &data_, sizeof(WebKit::WebGamepads)); | 298 |
| 299 // Copy to the current state to the output buffer, using the mapping |
| 300 // function, if there is one available. |
| 301 pads->length = data_.length; |
| 302 for (size_t i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { |
| 303 if (associated_[i].mapper) |
| 304 associated_[i].mapper(data_.items[i], &pads->items[i]); |
| 305 else |
| 306 pads->items[i] = data_.items[i]; |
| 307 } |
| 289 } | 308 } |
| 290 | 309 |
| 291 } // namespace content | 310 } // namespace content |
| OLD | NEW |