| Index: device/gamepad/gamepad_platform_data_fetcher_mac.mm
|
| diff --git a/content/browser/gamepad/gamepad_platform_data_fetcher_mac.mm b/device/gamepad/gamepad_platform_data_fetcher_mac.mm
|
| similarity index 79%
|
| rename from content/browser/gamepad/gamepad_platform_data_fetcher_mac.mm
|
| rename to device/gamepad/gamepad_platform_data_fetcher_mac.mm
|
| index 38e69b6c3e33d844cdf1390ce26c646d6aab63fe..60c57dd7bb93e7cea95a0dad99657d17d21e5629 100644
|
| --- a/content/browser/gamepad/gamepad_platform_data_fetcher_mac.mm
|
| +++ b/device/gamepad/gamepad_platform_data_fetcher_mac.mm
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "content/browser/gamepad/gamepad_platform_data_fetcher_mac.h"
|
| +#include "device/gamepad/gamepad_platform_data_fetcher_mac.h"
|
|
|
| #include <stdint.h>
|
| #include <string.h>
|
| @@ -21,24 +21,27 @@
|
| using blink::WebGamepad;
|
| using blink::WebGamepads;
|
|
|
| -namespace content {
|
| +namespace device {
|
|
|
| namespace {
|
|
|
| -void CopyNSStringAsUTF16LittleEndian(
|
| - NSString* src, blink::WebUChar* dest, size_t dest_len) {
|
| +void CopyNSStringAsUTF16LittleEndian(NSString* src,
|
| + blink::WebUChar* dest,
|
| + size_t dest_len) {
|
| NSData* as16 = [src dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
|
| memset(dest, 0, dest_len);
|
| [as16 getBytes:dest length:dest_len - sizeof(blink::WebUChar)];
|
| }
|
|
|
| NSDictionary* DeviceMatching(uint32_t usage_page, uint32_t usage) {
|
| - return [NSDictionary dictionaryWithObjectsAndKeys:
|
| - [NSNumber numberWithUnsignedInt:usage_page],
|
| - base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsagePageKey)),
|
| - [NSNumber numberWithUnsignedInt:usage],
|
| - base::mac::CFToNSCast(CFSTR(kIOHIDDeviceUsageKey)),
|
| - nil];
|
| + return [NSDictionary
|
| + dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:usage_page],
|
| + base::mac::CFToNSCast(
|
| + CFSTR(kIOHIDDeviceUsagePageKey)),
|
| + [NSNumber numberWithUnsignedInt:usage],
|
| + base::mac::CFToNSCast(
|
| + CFSTR(kIOHIDDeviceUsageKey)),
|
| + nil];
|
| }
|
|
|
| float NormalizeAxis(CFIndex value, CFIndex min, CFIndex max) {
|
| @@ -76,59 +79,51 @@ GamepadPlatformDataFetcherMac::GamepadPlatformDataFetcherMac()
|
| if (!xbox_fetcher_->RegisterForNotifications())
|
| xbox_fetcher_.reset();
|
|
|
| - hid_manager_ref_.reset(IOHIDManagerCreate(kCFAllocatorDefault,
|
| - kIOHIDOptionsTypeNone));
|
| + hid_manager_ref_.reset(
|
| + IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone));
|
| if (CFGetTypeID(hid_manager_ref_) != IOHIDManagerGetTypeID()) {
|
| enabled_ = false;
|
| return;
|
| }
|
|
|
| - base::scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects:
|
| - DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber),
|
| - DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber),
|
| - DeviceMatching(kGenericDesktopUsagePage, kMultiAxisUsageNumber),
|
| - nil]);
|
| - IOHIDManagerSetDeviceMatchingMultiple(
|
| - hid_manager_ref_,
|
| - base::mac::NSToCFCast(criteria));
|
| + base::scoped_nsobject<NSArray> criteria(
|
| + [[NSArray alloc] initWithObjects:DeviceMatching(kGenericDesktopUsagePage,
|
| + kJoystickUsageNumber),
|
| + DeviceMatching(kGenericDesktopUsagePage,
|
| + kGameUsageNumber),
|
| + DeviceMatching(kGenericDesktopUsagePage,
|
| + kMultiAxisUsageNumber),
|
| + nil]);
|
| + IOHIDManagerSetDeviceMatchingMultiple(hid_manager_ref_,
|
| + base::mac::NSToCFCast(criteria));
|
|
|
| RegisterForNotifications();
|
| }
|
|
|
| void GamepadPlatformDataFetcherMac::RegisterForNotifications() {
|
| // Register for plug/unplug notifications.
|
| - IOHIDManagerRegisterDeviceMatchingCallback(
|
| - hid_manager_ref_,
|
| - DeviceAddCallback,
|
| - this);
|
| - IOHIDManagerRegisterDeviceRemovalCallback(
|
| - hid_manager_ref_,
|
| - DeviceRemoveCallback,
|
| - this);
|
| + IOHIDManagerRegisterDeviceMatchingCallback(hid_manager_ref_,
|
| + DeviceAddCallback, this);
|
| + IOHIDManagerRegisterDeviceRemovalCallback(hid_manager_ref_,
|
| + DeviceRemoveCallback, this);
|
|
|
| // Register for value change notifications.
|
| - IOHIDManagerRegisterInputValueCallback(
|
| - hid_manager_ref_,
|
| - ValueChangedCallback,
|
| - this);
|
| + IOHIDManagerRegisterInputValueCallback(hid_manager_ref_, ValueChangedCallback,
|
| + this);
|
|
|
| - IOHIDManagerScheduleWithRunLoop(
|
| - hid_manager_ref_,
|
| - CFRunLoopGetMain(),
|
| - kCFRunLoopDefaultMode);
|
| + IOHIDManagerScheduleWithRunLoop(hid_manager_ref_, CFRunLoopGetMain(),
|
| + kCFRunLoopDefaultMode);
|
|
|
| - enabled_ = IOHIDManagerOpen(hid_manager_ref_,
|
| - kIOHIDOptionsTypeNone) == kIOReturnSuccess;
|
| + enabled_ = IOHIDManagerOpen(hid_manager_ref_, kIOHIDOptionsTypeNone) ==
|
| + kIOReturnSuccess;
|
|
|
| if (xbox_fetcher_)
|
| xbox_fetcher_->RegisterForNotifications();
|
| }
|
|
|
| void GamepadPlatformDataFetcherMac::UnregisterFromNotifications() {
|
| - IOHIDManagerUnscheduleFromRunLoop(
|
| - hid_manager_ref_,
|
| - CFRunLoopGetCurrent(),
|
| - kCFRunLoopDefaultMode);
|
| + IOHIDManagerUnscheduleFromRunLoop(hid_manager_ref_, CFRunLoopGetCurrent(),
|
| + kCFRunLoopDefaultMode);
|
| IOHIDManagerClose(hid_manager_ref_, kIOHIDOptionsTypeNone);
|
| if (xbox_fetcher_)
|
| xbox_fetcher_->UnregisterFromNotifications();
|
| @@ -175,8 +170,7 @@ bool GamepadPlatformDataFetcherMac::CheckCollection(IOHIDElementRef element) {
|
| uint32_t usage_page = IOHIDElementGetUsagePage(element);
|
| uint32_t usage = IOHIDElementGetUsage(element);
|
| if (usage_page == kGenericDesktopUsagePage) {
|
| - if (usage == kJoystickUsageNumber ||
|
| - usage == kGameUsageNumber ||
|
| + if (usage == kJoystickUsageNumber || usage == kGameUsageNumber ||
|
| usage == kMultiAxisUsageNumber) {
|
| return true;
|
| }
|
| @@ -213,8 +207,7 @@ bool GamepadPlatformDataFetcherMac::AddButtonsAndAxes(NSArray* elements,
|
| associated.hid.button_elements[button_index] = element;
|
| pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1);
|
| }
|
| - }
|
| - else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) {
|
| + } else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) {
|
| uint32_t axis_index = usage - kAxisMinimumUsageNumber;
|
| if (axis_index < WebGamepad::axesLengthCap) {
|
| associated.hid.axis_elements[axis_index] = element;
|
| @@ -289,8 +282,7 @@ size_t GamepadPlatformDataFetcherMac::GetSlotForDevice(IOHIDDeviceRef device) {
|
| for (size_t slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
|
| // If we already have this device, and it's already connected, don't do
|
| // anything now.
|
| - if (pad_state_[slot].data.connected &&
|
| - !associated_[slot].is_xbox &&
|
| + if (pad_state_[slot].data.connected && !associated_[slot].is_xbox &&
|
| associated_[slot].hid.device_ref == device)
|
| return WebGamepads::itemsLengthCap;
|
| }
|
| @@ -349,22 +341,16 @@ void GamepadPlatformDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) {
|
| pad_state_[slot].mapper =
|
| GetGamepadStandardMappingFunction(vendor_as_str, product_as_str);
|
|
|
| - NSString* ident = [NSString stringWithFormat:
|
| - @"%@ (%sVendor: %04x Product: %04x)",
|
| - product,
|
| - pad_state_[slot].mapper ? "STANDARD GAMEPAD " : "",
|
| - vendor_int,
|
| - product_int];
|
| - CopyNSStringAsUTF16LittleEndian(
|
| - ident,
|
| - pad_state_[slot].data.id,
|
| - sizeof(pad_state_[slot].data.id));
|
| + NSString* ident = [NSString
|
| + stringWithFormat:@"%@ (%sVendor: %04x Product: %04x)", product,
|
| + pad_state_[slot].mapper ? "STANDARD GAMEPAD " : "",
|
| + vendor_int, product_int];
|
| + CopyNSStringAsUTF16LittleEndian(ident, pad_state_[slot].data.id,
|
| + sizeof(pad_state_[slot].data.id));
|
|
|
| if (pad_state_[slot].mapper) {
|
| - CopyNSStringAsUTF16LittleEndian(
|
| - @"standard",
|
| - pad_state_[slot].data.mapping,
|
| - sizeof(pad_state_[slot].data.mapping));
|
| + CopyNSStringAsUTF16LittleEndian(@"standard", pad_state_[slot].data.mapping,
|
| + sizeof(pad_state_[slot].data.mapping));
|
| } else {
|
| pad_state_[slot].data.mapping[0] = 0;
|
| }
|
| @@ -388,8 +374,7 @@ void GamepadPlatformDataFetcherMac::DeviceRemove(IOHIDDeviceRef device) {
|
| // Find the index for this device.
|
| size_t slot;
|
| for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
|
| - if (pad_state_[slot].data.connected &&
|
| - !associated_[slot].is_xbox &&
|
| + if (pad_state_[slot].data.connected && !associated_[slot].is_xbox &&
|
| associated_[slot].hid.device_ref == device)
|
| break;
|
| }
|
| @@ -409,8 +394,7 @@ void GamepadPlatformDataFetcherMac::ValueChanged(IOHIDValueRef value) {
|
| // Find device slot.
|
| size_t slot;
|
| for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
|
| - if (pad_state_[slot].data.connected &&
|
| - !associated_[slot].is_xbox &&
|
| + if (pad_state_[slot].data.connected && !associated_[slot].is_xbox &&
|
| associated_[slot].hid.device_ref == device)
|
| break;
|
| }
|
| @@ -480,22 +464,18 @@ void GamepadPlatformDataFetcherMac::XboxDeviceAdd(XboxController* device) {
|
| device->SetLEDPattern(
|
| (XboxController::LEDPattern)(XboxController::LED_FLASH_TOP_LEFT + slot));
|
|
|
| - NSString* ident =
|
| - [NSString stringWithFormat:
|
| - @"%@ (STANDARD GAMEPAD Vendor: %04x Product: %04x)",
|
| - device->GetControllerType() == XboxController::XBOX_360_CONTROLLER
|
| - ? @"Xbox 360 Controller"
|
| - : @"Xbox One Controller",
|
| - device->GetProductId(), device->GetVendorId()];
|
| - CopyNSStringAsUTF16LittleEndian(
|
| - ident,
|
| - pad_state_[slot].data.id,
|
| - sizeof(pad_state_[slot].data.id));
|
| -
|
| - CopyNSStringAsUTF16LittleEndian(
|
| - @"standard",
|
| - pad_state_[slot].data.mapping,
|
| - sizeof(pad_state_[slot].data.mapping));
|
| + NSString* ident = [NSString
|
| + stringWithFormat:@"%@ (STANDARD GAMEPAD Vendor: %04x Product: %04x)",
|
| + device->GetControllerType() ==
|
| + XboxController::XBOX_360_CONTROLLER
|
| + ? @"Xbox 360 Controller"
|
| + : @"Xbox One Controller",
|
| + device->GetProductId(), device->GetVendorId()];
|
| + CopyNSStringAsUTF16LittleEndian(ident, pad_state_[slot].data.id,
|
| + sizeof(pad_state_[slot].data.id));
|
| +
|
| + CopyNSStringAsUTF16LittleEndian(@"standard", pad_state_[slot].data.mapping,
|
| + sizeof(pad_state_[slot].data.mapping));
|
|
|
| associated_[slot].is_xbox = true;
|
| associated_[slot].xbox.device = device;
|
| @@ -516,8 +496,7 @@ void GamepadPlatformDataFetcherMac::XboxDeviceRemove(XboxController* device) {
|
| // Find the index for this device.
|
| size_t slot;
|
| for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
|
| - if (pad_state_[slot].data.connected &&
|
| - associated_[slot].is_xbox &&
|
| + if (pad_state_[slot].data.connected && associated_[slot].is_xbox &&
|
| associated_[slot].xbox.device == device)
|
| break;
|
| }
|
| @@ -528,12 +507,12 @@ void GamepadPlatformDataFetcherMac::XboxDeviceRemove(XboxController* device) {
|
| }
|
|
|
| void GamepadPlatformDataFetcherMac::XboxValueChanged(
|
| - XboxController* device, const XboxController::Data& data) {
|
| + XboxController* device,
|
| + const XboxController::Data& data) {
|
| // Find device slot.
|
| size_t slot;
|
| for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) {
|
| - if (pad_state_[slot].data.connected &&
|
| - associated_[slot].is_xbox &&
|
| + if (pad_state_[slot].data.connected && associated_[slot].is_xbox &&
|
| associated_[slot].xbox.device == device)
|
| break;
|
| }
|
| @@ -572,4 +551,4 @@ void GamepadPlatformDataFetcherMac::GetGamepadData(WebGamepads* pads, bool) {
|
| MapAndSanitizeGamepadData(&pad_state_[i], &pads->items[i]);
|
| }
|
|
|
| -} // namespace content
|
| +} // namespace device
|
|
|