This commit is contained in:
Camilla Löwy 2017-02-26 22:18:40 +01:00
parent 4928e2bf0a
commit 9950d8a319

View File

@ -233,46 +233,6 @@ static void removeCallback(void* context,
} }
} }
// Creates a dictionary to match against devices with the specified usage page
// and usage
//
static CFMutableDictionaryRef createMatchingDictionary(long usagePage,
long usage)
{
CFMutableDictionaryRef result =
CFDictionaryCreateMutable(kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (result)
{
CFNumberRef pageRef = CFNumberCreate(kCFAllocatorDefault,
kCFNumberLongType,
&usagePage);
if (pageRef)
{
CFDictionarySetValue(result,
CFSTR(kIOHIDDeviceUsagePageKey),
pageRef);
CFRelease(pageRef);
CFNumberRef usageRef = CFNumberCreate(kCFAllocatorDefault,
kCFNumberLongType,
&usage);
if (usageRef)
{
CFDictionarySetValue(result,
CFSTR(kIOHIDDeviceUsageKey),
usageRef);
CFRelease(usageRef);
}
}
}
return result;
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW internal API ////// ////// GLFW internal API //////
@ -282,56 +242,73 @@ static CFMutableDictionaryRef createMatchingDictionary(long usagePage,
// //
void _glfwInitJoysticksNS(void) void _glfwInitJoysticksNS(void)
{ {
CFMutableArrayRef matchingCFArrayRef; CFMutableArrayRef matching;
const long usages[] =
{
kHIDUsage_GD_Joystick,
kHIDUsage_GD_GamePad,
kHIDUsage_GD_MultiAxisController
};
_glfw.ns.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, _glfw.ns.hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
kIOHIDOptionsTypeNone); kIOHIDOptionsTypeNone);
matchingCFArrayRef = CFArrayCreateMutable(kCFAllocatorDefault, matching = CFArrayCreateMutable(kCFAllocatorDefault,
0, 0,
&kCFTypeArrayCallBacks); &kCFTypeArrayCallBacks);
if (matchingCFArrayRef) if (!matching)
{ {
CFDictionaryRef matchingCFDictRef = _glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create array");
createMatchingDictionary(kHIDPage_GenericDesktop, return;
kHIDUsage_GD_Joystick);
if (matchingCFDictRef)
{
CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef);
CFRelease(matchingCFDictRef);
} }
matchingCFDictRef = createMatchingDictionary(kHIDPage_GenericDesktop, for (int i = 0; i < sizeof(usages) / sizeof(long); i++)
kHIDUsage_GD_GamePad);
if (matchingCFDictRef)
{ {
CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef); const long page = kHIDPage_GenericDesktop;
CFRelease(matchingCFDictRef);
CFMutableDictionaryRef dict =
CFDictionaryCreateMutable(kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
if (!dict)
continue;
CFNumberRef pageRef = CFNumberCreate(kCFAllocatorDefault,
kCFNumberLongType,
&page);
CFNumberRef usageRef = CFNumberCreate(kCFAllocatorDefault,
kCFNumberLongType,
&usages[i]);
if (pageRef && usageRef)
{
CFDictionarySetValue(dict,
CFSTR(kIOHIDDeviceUsagePageKey),
pageRef);
CFDictionarySetValue(dict,
CFSTR(kIOHIDDeviceUsageKey),
usageRef);
CFArrayAppendValue(matching, dict);
} }
matchingCFDictRef = if (pageRef)
createMatchingDictionary(kHIDPage_GenericDesktop, CFRelease(pageRef);
kHIDUsage_GD_MultiAxisController); if (usageRef)
if (matchingCFDictRef) CFRelease(usageRef);
{
CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef); CFRelease(dict);
CFRelease(matchingCFDictRef);
} }
IOHIDManagerSetDeviceMatchingMultiple(_glfw.ns.hidManager, IOHIDManagerSetDeviceMatchingMultiple(_glfw.ns.hidManager, matching);
matchingCFArrayRef); CFRelease(matching);
CFRelease(matchingCFArrayRef);
}
IOHIDManagerRegisterDeviceMatchingCallback(_glfw.ns.hidManager, IOHIDManagerRegisterDeviceMatchingCallback(_glfw.ns.hidManager,
&matchCallback, NULL); &matchCallback, NULL);
IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns.hidManager, IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns.hidManager,
&removeCallback, NULL); &removeCallback, NULL);
IOHIDManagerScheduleWithRunLoop(_glfw.ns.hidManager, IOHIDManagerScheduleWithRunLoop(_glfw.ns.hidManager,
CFRunLoopGetMain(), CFRunLoopGetMain(),
kCFRunLoopDefaultMode); kCFRunLoopDefaultMode);
IOHIDManagerOpen(_glfw.ns.hidManager, kIOHIDOptionsTypeNone); IOHIDManagerOpen(_glfw.ns.hidManager, kIOHIDOptionsTypeNone);
// Execute the run loop once in order to register any initially-attached // Execute the run loop once in order to register any initially-attached