Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/gamepad/data_fetcher_mac.h" | |
| 6 | |
| 7 #include "base/mac/foundation_util.h" | |
| 8 #include "base/memory/scoped_nsobject.h" | |
| 9 #include "base/string16.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 | |
| 13 #include <IOKit/hid/IOHIDKeys.h> | |
| 14 #import <Foundation/NSArray.h> | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
#import <Foundation/Foundation.h> if not #import <
scottmg
2011/12/07 04:39:36
Done.
| |
| 15 #import <Foundation/NSDictionary.h> | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 NSDictionary* DeviceMatching(unsigned usage_page, unsigned usage) { | |
| 22 return [NSDictionary dictionaryWithObjectsAndKeys: | |
| 23 [NSNumber numberWithInt: usage_page], | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
no space after : here, and on line 25.
scottmg
2011/12/07 04:39:36
Done.
| |
| 24 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsagePageKey)), | |
| 25 [NSNumber numberWithInt: usage], | |
| 26 base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsageKey)), | |
| 27 nil]; | |
| 28 } | |
| 29 | |
| 30 float NormalizeAxis(CFIndex value, CFIndex min, CFIndex max) { | |
| 31 return (2.f * (value - min) / static_cast<float>(max - min)) - 1.f; | |
| 32 } | |
| 33 | |
| 34 // http://www.usb.org/developers/hidpage | |
| 35 const uint32_t kGenericDesktopUsagePage = 0x01; | |
| 36 const uint32_t kButtonUsagePage = 0x09; | |
| 37 const uint32_t kJoystickUsageNumber = 0x04; | |
| 38 const uint32_t kGameUsageNumber = 0x05; | |
| 39 const uint32_t kMultiAxisUsageNumber = 0x08; | |
| 40 const uint32_t kAxisMinimumUsageNumber = 0x30; | |
| 41 const uint32_t kAxisMaximumUsageNumber = 0x35; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 GamepadDataFetcherMac::GamepadDataFetcherMac() : enabled_(true) { | |
| 46 hid_manager_ref_.reset(IOHIDManagerCreate(kCFAllocatorDefault, | |
| 47 kIOHIDOptionsTypeNone)); | |
| 48 if (CFGetTypeID(hid_manager_ref_) != IOHIDManagerGetTypeID()) { | |
| 49 enabled_ = false; | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects: | |
| 54 DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber), | |
| 55 DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber), | |
| 56 DeviceMatching(kGenericDesktopUsagePage, kMultiAxisUsageNumber), | |
| 57 nil]); | |
| 58 IOHIDManagerSetDeviceMatchingMultiple( | |
| 59 hid_manager_ref_, | |
| 60 base::mac::NSToCFCast(criteria)); | |
| 61 | |
| 62 RegisterForNotifications(); | |
| 63 } | |
| 64 | |
| 65 void GamepadDataFetcherMac::RegisterForNotifications() { | |
| 66 // Register for plug/unplug notifications | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
full sentences end with periods .
scottmg
2011/12/07 04:39:36
Done.
| |
| 67 IOHIDManagerRegisterDeviceMatchingCallback( | |
| 68 hid_manager_ref_, | |
| 69 &DeviceAddCallback, | |
| 70 this); | |
| 71 IOHIDManagerRegisterDeviceRemovalCallback( | |
| 72 hid_manager_ref_, | |
| 73 DeviceRemoveCallback, | |
| 74 this); | |
| 75 | |
| 76 // Register for value change notifications | |
| 77 IOHIDManagerRegisterInputValueCallback( | |
| 78 hid_manager_ref_, | |
| 79 ValueChangedCallback, | |
| 80 this); | |
| 81 | |
| 82 IOHIDManagerScheduleWithRunLoop( | |
| 83 hid_manager_ref_, | |
| 84 CFRunLoopGetMain(), | |
| 85 kCFRunLoopDefaultMode); | |
| 86 | |
| 87 enabled_ = IOHIDManagerOpen(hid_manager_ref_, | |
| 88 kIOHIDOptionsTypeSeizeDevice) == kIOReturnSuccess; | |
| 89 } | |
| 90 | |
| 91 void GamepadDataFetcherMac::UnregisterFromNotifications() { | |
| 92 IOHIDManagerUnscheduleFromRunLoop( | |
| 93 hid_manager_ref_, | |
| 94 CFRunLoopGetCurrent(), | |
| 95 kCFRunLoopDefaultMode); | |
| 96 IOHIDManagerClose(hid_manager_ref_, kIOHIDOptionsTypeNone); | |
| 97 } | |
| 98 | |
| 99 void GamepadDataFetcherMac::PauseHint(bool pause) { | |
| 100 if (pause) | |
| 101 UnregisterFromNotifications(); | |
| 102 else | |
| 103 RegisterForNotifications(); | |
| 104 } | |
| 105 | |
| 106 GamepadDataFetcherMac::~GamepadDataFetcherMac() { | |
| 107 UnregisterFromNotifications(); | |
| 108 } | |
| 109 | |
| 110 GamepadDataFetcherMac* GamepadDataFetcherMac::InstanceFromContext( | |
| 111 void* context) { | |
| 112 return reinterpret_cast<GamepadDataFetcherMac*>(context); | |
| 113 } | |
| 114 | |
| 115 void GamepadDataFetcherMac::DeviceAddCallback(void* context, | |
| 116 IOReturn result, | |
| 117 void* sender, | |
| 118 IOHIDDeviceRef ref) { | |
| 119 InstanceFromContext(context)->DeviceAdd(ref); | |
| 120 } | |
| 121 | |
| 122 void GamepadDataFetcherMac::DeviceRemoveCallback(void* context, | |
| 123 IOReturn result, | |
| 124 void* sender, | |
| 125 IOHIDDeviceRef ref) { | |
| 126 InstanceFromContext(context)->DeviceRemove(ref); | |
| 127 } | |
| 128 | |
| 129 void GamepadDataFetcherMac::ValueChangedCallback(void* context, | |
| 130 IOReturn result, | |
| 131 void* sender, | |
| 132 IOHIDValueRef ref) { | |
| 133 InstanceFromContext(context)->ValueChanged(ref); | |
| 134 } | |
| 135 | |
| 136 void GamepadDataFetcherMac::AddButtonsAndAxes(NSArray* elements, | |
| 137 unsigned slot) { | |
| 138 WebKit::WebGamepad& pad = data_.items[slot]; | |
| 139 AssociatedData& associated = associated_[slot]; | |
| 140 | |
| 141 pad.axesLength = 0; | |
| 142 pad.buttonsLength = 0; | |
| 143 memset(pad.axes, 0, sizeof(pad.axes)); | |
| 144 memset(pad.buttons, 0, sizeof(pad.buttons)); | |
| 145 | |
| 146 for (id elem in elements) { | |
| 147 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); | |
| 148 uint32_t usagePage = IOHIDElementGetUsagePage(element); | |
| 149 uint32_t usage = IOHIDElementGetUsage(element); | |
| 150 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button && | |
| 151 usagePage == kButtonUsagePage) { | |
| 152 uint32_t button_index = usage - 1; | |
| 153 if (button_index < WebKit::WebGamepad::buttonsLengthCap) { | |
| 154 associated.button_elements[button_index] = element; | |
| 155 pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1); | |
| 156 } | |
| 157 } | |
| 158 else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) { | |
| 159 uint32_t axis_index = usage - kAxisMinimumUsageNumber; | |
| 160 if (axis_index < WebKit::WebGamepad::axesLengthCap) { | |
| 161 associated.axis_minimums[axis_index] = | |
| 162 IOHIDElementGetLogicalMin(element); | |
| 163 associated.axis_maximums[axis_index] = | |
| 164 IOHIDElementGetLogicalMax(element); | |
| 165 associated.axis_elements[axis_index] = element; | |
| 166 pad.axesLength = std::max(pad.axesLength, axis_index + 1); | |
| 167 } | |
| 168 } | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void GamepadDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) { | |
| 173 using WebKit::WebGamepad; | |
| 174 using WebKit::WebGamepads; | |
| 175 using base::mac::CFToNSCast; | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
I was not aware that the style guide permitted thi
scottmg
2011/12/07 04:39:36
Darin schooled me on that last review. :)
| |
| 176 unsigned slot; | |
| 177 | |
| 178 if (!enabled_) | |
| 179 return; | |
| 180 | |
| 181 // Find an index for this device. | |
| 182 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | |
| 183 // If we already have this device, and it's already connected, don't do | |
| 184 // anything now. | |
| 185 if (associated_[slot].device_ref == device && data_.items[slot].connected) | |
| 186 return; | |
| 187 if (!data_.items[slot].connected) | |
| 188 break; | |
| 189 } | |
| 190 | |
| 191 // We can't handle this many connected devices. | |
| 192 if (slot == WebGamepads::itemsLengthCap) | |
| 193 return; | |
| 194 | |
| 195 NSNumber* vendor_id = CFToNSCast(base::mac::CFCastStrict<CFNumberRef>( | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
We do "using base::mac::CFToNSCast" to cut clutter
scottmg
2011/12/07 04:39:36
Done.
| |
| 196 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)))); | |
| 197 NSNumber* product_id = CFToNSCast(base::mac::CFCastStrict<CFNumberRef>( | |
| 198 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)))); | |
| 199 NSString* product = CFToNSCast(base::mac::CFCastStrict<CFStringRef>( | |
| 200 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); | |
| 201 | |
| 202 NSString* ident([NSString stringWithFormat: | |
| 203 @"%s (Vendor: %04x Product: %04x)", | |
| 204 [product UTF8String], | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
Nope. NSObjects have a formatting code of %@; use
scottmg
2011/12/07 04:39:36
Done. (Magic!)
| |
| 205 [vendor_id intValue], | |
| 206 [product_id intValue]]); | |
| 207 NSData* as16 = [ident dataUsingEncoding: NSUTF16StringEncoding]; | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
no space after :
scottmg
2011/12/07 04:39:36
Done.
| |
| 208 | |
| 209 const unsigned kOutputLengthBytes = sizeof(data_.items[slot].id); | |
| 210 memset(&data_.items[slot].id, 0, kOutputLengthBytes); | |
| 211 [as16 | |
| 212 getBytes: data_.items[slot].id | |
| 213 length: kOutputLengthBytes - sizeof(WebKit::WebUChar)]; | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
Bad style. Besides no space after :, put getBytes
scottmg
2011/12/07 04:39:36
Done.
| |
| 214 | |
| 215 NSArray* elements = CFToNSCast(IOHIDDeviceCopyMatchingElements( | |
|
Avi (use Gerrit)
2011/12/07 04:12:46
Nope. CF-style functions that have "Copy" in their
scottmg
2011/12/07 04:39:36
Is scoped_nsobject<NSArray> ok, or are the retain/
Avi (use Gerrit)
2011/12/07 05:26:06
Wellllll.... they used to be the same until they w
| |
| 216 device, | |
| 217 NULL, | |
| 218 kIOHIDOptionsTypeNone)); | |
| 219 AddButtonsAndAxes(elements, slot); | |
| 220 | |
| 221 associated_[slot].device_ref = device; | |
| 222 data_.items[slot].connected = true; | |
| 223 data_.length = std::max(slot + 1, data_.length); | |
| 224 } | |
| 225 | |
| 226 void GamepadDataFetcherMac::DeviceRemove(IOHIDDeviceRef device) { | |
| 227 using WebKit::WebGamepads; | |
| 228 unsigned slot; | |
| 229 if (!enabled_) | |
| 230 return; | |
| 231 | |
| 232 // Find the index for this device. | |
| 233 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | |
| 234 if (associated_[slot].device_ref == device && data_.items[slot].connected) | |
| 235 break; | |
| 236 } | |
| 237 DCHECK(slot < WebGamepads::itemsLengthCap); | |
| 238 // Leave associated device_ref so that it will be reconnected in the same | |
| 239 // location. Simply mark it as disconnected. | |
| 240 data_.items[slot].connected = false; | |
| 241 } | |
| 242 | |
| 243 void GamepadDataFetcherMac::ValueChanged(IOHIDValueRef value) { | |
| 244 if (!enabled_) | |
| 245 return; | |
| 246 | |
| 247 IOHIDElementRef element = IOHIDValueGetElement(value); | |
| 248 IOHIDDeviceRef device = IOHIDElementGetDevice(element); | |
| 249 | |
| 250 // Find device slot | |
| 251 unsigned slot; | |
| 252 for (slot = 0; slot < data_.length; ++slot) { | |
| 253 if (data_.items[slot].connected && associated_[slot].device_ref == device) | |
| 254 break; | |
| 255 } | |
| 256 if (slot == data_.length) | |
| 257 return; | |
| 258 | |
| 259 WebKit::WebGamepad& pad = data_.items[slot]; | |
| 260 AssociatedData& associated = associated_[slot]; | |
| 261 | |
| 262 // Find and fill in the associated button event, if any. | |
| 263 for (unsigned i = 0; i < pad.buttonsLength; ++i) { | |
| 264 if (associated.button_elements[i] == element) { | |
| 265 pad.buttons[i] = IOHIDValueGetIntegerValue(value) ? 1.f : 0.f; | |
| 266 return; | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 // Find and fill in the associated axis event, if any. | |
| 271 for (unsigned i = 0; i < pad.axesLength; ++i) { | |
| 272 if (associated.axis_elements[i] == element) { | |
| 273 pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value), | |
| 274 associated.axis_minimums[i], | |
| 275 associated.axis_maximums[i]); | |
| 276 return; | |
| 277 } | |
| 278 } | |
| 279 } | |
| 280 | |
| 281 void GamepadDataFetcherMac::GetGamepadData(WebKit::WebGamepads* pads, bool) { | |
| 282 if (!enabled_) { | |
| 283 pads->length = 0; | |
| 284 return; | |
| 285 } | |
| 286 memcpy(pads, &data_, sizeof(WebKit::WebGamepads)); | |
| 287 } | |
| 288 | |
| 289 } // namespace content | |
| OLD | NEW |