Chromium Code Reviews| Index: content/browser/gamepad/data_fetcher_mac.mm |
| diff --git a/content/browser/gamepad/data_fetcher_mac.mm b/content/browser/gamepad/data_fetcher_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b976569fa120a431a31b04461fc8b247a9bf46d9 |
| --- /dev/null |
| +++ b/content/browser/gamepad/data_fetcher_mac.mm |
| @@ -0,0 +1,295 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/gamepad/data_fetcher_mac.h" |
| + |
| +#include "base/string16.h" |
| +#include "base/string_util.h" |
| +#include "base/utf_string_conversions.h" |
| + |
| +#include <IOKit/hid/IOHIDKeys.h> |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +CFMutableDictionaryRef CreateDeviceMatchingDictionary( |
| + unsigned usage_page, |
| + unsigned usage) { |
| + CFMutableDictionaryRef dict = CFDictionaryCreateMutable( |
| + kCFAllocatorDefault, |
| + 0, |
| + &kCFTypeDictionaryKeyCallBacks, |
| + &kCFTypeDictionaryValueCallBacks); |
| + |
| + CFNumberRef page_number_ref = CFNumberCreate( |
| + kCFAllocatorDefault, |
| + kCFNumberIntType, |
| + &usage_page); |
| + CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), page_number_ref); |
| + CFRelease(page_number_ref); |
|
Avi (use Gerrit)
2011/12/06 04:54:19
Rather than create/release, prefer scoped_cftypere
scottmg
2011/12/07 01:20:50
Done.
|
| + |
| + CFNumberRef usage_number_ref = CFNumberCreate( |
| + kCFAllocatorDefault, |
| + kCFNumberIntType, |
| + &usage); |
| + CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), usage_number_ref); |
| + CFRelease(usage_number_ref); |
| + |
| + return dict; |
| +} |
| + |
| +float NormalizeAxis(CFIndex value, CFIndex min, CFIndex max) { |
| + return (2.f * (value - min) / static_cast<float>(max - min)) - 1.f; |
| +} |
| + |
| +// http://www.usb.org/developers/hidpage |
| +const uint32_t kGenericDesktopUsagePage = 0x01; |
| +const uint32_t kButtonUsagePage = 0x09; |
| +const uint32_t kJoystickUsageNumber = 0x04; |
| +const uint32_t kGameUsageNumber = 0x05; |
| +const uint32_t kAxisMinimumUsageNumber = 0x30; |
| +const uint32_t kAxisMaximumUsageNumber = 0x35; |
| + |
| +} // namespace |
| + |
| +GamepadDataFetcherMac::GamepadDataFetcherMac() : enabled_(true) { |
| + hid_manager_ref_ = IOHIDManagerCreate(kCFAllocatorDefault, |
| + kIOHIDOptionsTypeNone); |
| + if (CFGetTypeID(hid_manager_ref_) != IOHIDManagerGetTypeID()) { |
| + enabled_ = false; |
| + return; |
| + } |
| + |
| + CFMutableDictionaryRef joystick_criterion = CreateDeviceMatchingDictionary( |
|
Avi (use Gerrit)
2011/12/06 04:54:19
Make this a scoped_cftyperef...
scottmg
2011/12/07 01:20:50
Done.
|
| + kGenericDesktopUsagePage, |
| + kJoystickUsageNumber); |
| + CFMutableDictionaryRef game_criterion = CreateDeviceMatchingDictionary( |
|
Avi (use Gerrit)
2011/12/06 04:54:19
...and this...
scottmg
2011/12/07 01:20:50
Done.
|
| + kGenericDesktopUsagePage, |
| + kGameUsageNumber); |
| + CFMutableDictionaryRef criteria_list[] = { |
| + joystick_criterion, |
| + game_criterion |
| + }; |
| + CFArrayRef criteria = CFArrayCreate( |
|
Avi (use Gerrit)
2011/12/06 04:54:19
...and this should be held in a scoped_cftyperef (
scottmg
2011/12/07 01:20:50
Done.
|
| + kCFAllocatorDefault, |
| + const_cast<const void**>( |
| + reinterpret_cast<const void* const* const>(criteria_list)), |
| + 2, |
| + NULL); |
|
Avi (use Gerrit)
2011/12/06 04:54:19
...and this should be kCFTypeArrayCallBacks, to al
scottmg
2011/12/07 01:20:50
Done.
|
| + IOHIDManagerSetDeviceMatchingMultiple(hid_manager_ref_, criteria); |
| + CFRelease(criteria); |
| + |
| + // Register for plug/unplug notifications |
| + IOHIDManagerRegisterDeviceMatchingCallback( |
| + hid_manager_ref_, |
| + &DeviceAddCallback, |
| + this); |
| + IOHIDManagerRegisterDeviceRemovalCallback( |
| + hid_manager_ref_, |
| + DeviceRemoveCallback, |
| + this); |
| + |
| + // Register for value change notifications |
| + IOHIDManagerRegisterInputValueCallback( |
| + hid_manager_ref_, |
| + ValueChangedCallback, |
| + this); |
| + |
| + IOHIDManagerScheduleWithRunLoop( |
| + hid_manager_ref_, |
| + CFRunLoopGetMain(), |
| + kCFRunLoopDefaultMode); |
| + |
| + IOReturn ret = IOHIDManagerOpen(hid_manager_ref_, |
| + kIOHIDOptionsTypeSeizeDevice); |
| + if (ret != kIOReturnSuccess) { |
| + enabled_ = false; |
| + return; |
| + } |
| +} |
| + |
| +GamepadDataFetcherMac::~GamepadDataFetcherMac() { |
| + IOHIDManagerUnscheduleFromRunLoop( |
| + hid_manager_ref_, |
| + CFRunLoopGetCurrent(), |
| + kCFRunLoopDefaultMode); |
| + IOHIDManagerClose(hid_manager_ref_, kIOHIDOptionsTypeNone); |
| + CFRelease(hid_manager_ref_); |
|
Avi (use Gerrit)
2011/12/06 04:54:19
Make this member variable a scoped_cftyperef and y
scottmg
2011/12/07 01:20:50
Done.
|
| +} |
| + |
| +GamepadDataFetcherMac* GamepadDataFetcherMac::InstanceFromContext( |
| + void* context) { |
| + return reinterpret_cast<GamepadDataFetcherMac*>(context); |
| +} |
| + |
| +void GamepadDataFetcherMac::DeviceAddCallback(void* context, |
| + IOReturn result, |
| + void* sender, |
| + IOHIDDeviceRef ref) { |
| + InstanceFromContext(context)->DeviceAdd(ref); |
| +} |
| + |
| +void GamepadDataFetcherMac::DeviceRemoveCallback(void* context, |
| + IOReturn result, |
| + void* sender, |
| + IOHIDDeviceRef ref) { |
| + InstanceFromContext(context)->DeviceRemove(ref); |
| +} |
| + |
| +void GamepadDataFetcherMac::ValueChangedCallback(void* context, |
| + IOReturn result, |
| + void* sender, |
| + IOHIDValueRef ref) { |
| + InstanceFromContext(context)->ValueChanged(ref); |
| +} |
| + |
| +void GamepadDataFetcherMac::AddButtonsAndAxes(CFArrayRef elements, |
| + unsigned slot) { |
| + WebKit::WebGamepad& pad = data_.items[slot]; |
| + AssociatedData& associated = associated_[slot]; |
| + |
| + pad.axesLength = 0; |
| + pad.buttonsLength = 0; |
| + memset(pad.axes, 0, sizeof(pad.axes)); |
| + memset(pad.buttons, 0, sizeof(pad.buttons)); |
| + |
| + for (CFIndex i = 0; i < CFArrayGetCount(elements); ++i) { |
| + IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>( |
| + const_cast<void*>(CFArrayGetValueAtIndex(elements, i))); |
|
Avi (use Gerrit)
2011/12/06 04:54:19
This screams for CFCast from foundation_util.
scottmg
2011/12/07 01:20:50
I mucked around for a bit here, but I'm not clear
|
| + uint32_t usagePage = IOHIDElementGetUsagePage(element); |
| + uint32_t usage = IOHIDElementGetUsage(element); |
| + if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button && |
| + usagePage == kButtonUsagePage) { |
| + uint32_t button_index = usage - 1; |
| + if (button_index < WebKit::WebGamepad::buttonsLengthCap) { |
| + associated.button_elements[button_index] = element; |
| + pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1); |
| + } |
| + } |
| + else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) { |
| + uint32_t axis_index = usage - kAxisMinimumUsageNumber; |
| + if (axis_index < WebKit::WebGamepad::axesLengthCap) { |
| + associated.axis_minimums[axis_index] = |
| + IOHIDElementGetLogicalMin(element); |
| + associated.axis_maximums[axis_index] = |
| + IOHIDElementGetLogicalMax(element); |
| + associated.axis_elements[axis_index] = element; |
| + pad.axesLength = std::max(pad.axesLength, axis_index + 1); |
| + } |
| + } |
| + } |
| +} |
| + |
| +void GamepadDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) { |
| + using WebKit::WebGamepad; |
| + using WebKit::WebGamepads; |
| + unsigned slot; |
| + |
| + if (!enabled_) |
| + return; |
| + |
| + // Find an index for this device. |
| + for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { |
| + if (associated_[slot].device_ref == device) |
| + return; |
| + if (!data_.items[slot].connected) |
| + break; |
| + } |
| + |
| + // We can't handle this many connected devices. |
| + if (slot == WebGamepads::itemsLengthCap) |
| + return; |
| + |
| + CFNumberRef vendor_id_ref = reinterpret_cast<CFNumberRef>( |
|
Avi (use Gerrit)
2011/12/06 04:54:19
CFCast? (and the two others below)
scottmg
2011/12/07 01:20:50
Done.
|
| + IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey))); |
| + CFNumberRef product_id_ref = reinterpret_cast<CFNumberRef>( |
| + IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey))); |
| + CFStringRef product_ref = reinterpret_cast<CFStringRef>( |
| + IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey))); |
| + |
| + int vendor_id, product_id; |
| + char product_name[WebGamepad::idLengthCap]; |
| + CFNumberGetValue(vendor_id_ref, kCFNumberIntType, &vendor_id); |
| + CFNumberGetValue(product_id_ref, kCFNumberIntType, &product_id); |
| + CFStringGetCString(product_ref, |
| + product_name, |
| + sizeof(product_name), |
| + kCFStringEncodingASCII); |
| + |
| + char into[WebGamepad::idLengthCap]; |
| + base::snprintf(into, |
| + WebGamepad::idLengthCap, |
| + "%s (Vendor: %04x Product: %04x)", |
| + product_name, |
| + vendor_id, |
| + product_id); |
| + string16 as16 = ASCIIToUTF16(into); |
| + memset(&data_.items[slot].id, 0, sizeof(data_.items[slot].id)); |
| + for (unsigned i = 0; i < as16.size() && i < WebGamepad::idLengthCap - 1; ++i) |
| + data_.items[slot].id[i] = as16[i]; |
| + |
| + CFArrayRef elements = IOHIDDeviceCopyMatchingElements(device, |
| + NULL, |
| + kIOHIDOptionsTypeNone); |
| + AddButtonsAndAxes(elements, slot); |
| + |
| + associated_[slot].device_ref = device; |
| + data_.items[slot].connected = true; |
| + data_.length = std::max(slot + 1, data_.length); |
| + |
| +} |
| + |
| +void GamepadDataFetcherMac::DeviceRemove(IOHIDDeviceRef device) { |
| + if (!enabled_) |
| + return; |
| +} |
| + |
| +void GamepadDataFetcherMac::ValueChanged(IOHIDValueRef value) { |
| + if (!enabled_) |
| + return; |
| + |
| + IOHIDElementRef element = IOHIDValueGetElement(value); |
| + IOHIDDeviceRef device = IOHIDElementGetDevice(element); |
| + |
| + // Find device slot |
| + unsigned slot; |
| + for (slot = 0; slot < data_.length; ++slot) { |
| + if (data_.items[slot].connected && associated_[slot].device_ref == device) |
| + break; |
| + } |
| + if (slot == data_.length) |
| + return; |
| + |
| + WebKit::WebGamepad& pad = data_.items[slot]; |
| + AssociatedData& associated = associated_[slot]; |
| + |
| + // Find and fill in the associated button event, if any. |
| + for (unsigned i = 0; i < pad.buttonsLength; ++i) { |
| + if (associated.button_elements[i] == element) { |
| + pad.buttons[i] = IOHIDValueGetIntegerValue(value) ? 1.f : 0.f; |
| + return; |
| + } |
| + } |
| + |
| + // Find and fill in the associated axis event, if any. |
| + for (unsigned i = 0; i < pad.axesLength; ++i) { |
| + if (associated.axis_elements[i] == element) { |
| + pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value), |
| + associated.axis_minimums[i], |
| + associated.axis_maximums[i]); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +void GamepadDataFetcherMac::GetGamepadData(WebKit::WebGamepads* pads, bool) { |
| + if (!enabled_) { |
| + pads->length = 0; |
| + return; |
| + } |
| + memcpy(pads, &data_, sizeof(WebKit::WebGamepads)); |
| +} |
| + |
| +} // namespace content |