Optimize _glfwInitJoysticks on OS X.

This function used to enumerate all devices and capture all properties
of every device into a dictionary. This takes 180 ms on a MacBook Pro
without external devices and 280 ms with an external keyboard/mouse
attached.

Since we're only interested in a few properties, we can just get them
one by one - this reduces the time to <1 ms.

Note that we still use the dictionary to get the joystick elements.
For unknown reason this is required to get all axes/buttons - without
doing this we don't get any joystick elements in addJoystickElement.

Closes #332.
This commit is contained in:
Arseny Kapoulkine 2014-05-01 22:39:21 -07:00 committed by Camilla Berglund
parent 3b7d34a1d5
commit e7de2bbc9e

View File

@ -324,9 +324,41 @@ void _glfwInitJoysticks(void)
HRESULT plugInResult = S_OK; HRESULT plugInResult = S_OK;
SInt32 score = 0; SInt32 score = 0;
long usagePage, usage; long usagePage = 0;
long usage = 0;
valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject,
CFSTR(kIOHIDPrimaryUsagePageKey),
kCFAllocatorDefault, kNilOptions);
if (valueRef)
{
CFNumberGetValue(valueRef, kCFNumberLongType, &usagePage);
CFRelease(valueRef);
}
valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject,
CFSTR(kIOHIDPrimaryUsageKey),
kCFAllocatorDefault, kNilOptions);
if (valueRef)
{
CFNumberGetValue(valueRef, kCFNumberLongType, &usage);
CFRelease(valueRef);
}
if (usagePage != kHIDPage_GenericDesktop)
{
// This device is not relevant to GLFW
continue;
}
if ((usage != kHIDUsage_GD_Joystick &&
usage != kHIDUsage_GD_GamePad &&
usage != kHIDUsage_GD_MultiAxisController))
{
// This device is not relevant to GLFW
continue;
}
// Check device type
result = IORegistryEntryCreateCFProperties(ioHIDDeviceObject, result = IORegistryEntryCreateCFProperties(ioHIDDeviceObject,
&propsRef, &propsRef,
kCFAllocatorDefault, kCFAllocatorDefault,
@ -335,33 +367,6 @@ void _glfwInitJoysticks(void)
if (result != kIOReturnSuccess) if (result != kIOReturnSuccess)
continue; continue;
valueRef = CFDictionaryGetValue(propsRef, CFSTR(kIOHIDPrimaryUsagePageKey));
if (valueRef)
{
CFNumberGetValue(valueRef, kCFNumberLongType, &usagePage);
if (usagePage != kHIDPage_GenericDesktop)
{
// This device is not relevant to GLFW
CFRelease(propsRef);
continue;
}
}
valueRef = CFDictionaryGetValue(propsRef, CFSTR(kIOHIDPrimaryUsageKey));
if (valueRef)
{
CFNumberGetValue(valueRef, kCFNumberLongType, &usage);
if ((usage != kHIDUsage_GD_Joystick &&
usage != kHIDUsage_GD_GamePad &&
usage != kHIDUsage_GD_MultiAxisController))
{
// This device is not relevant to GLFW
CFRelease(propsRef);
continue;
}
}
_GLFWjoystickIOKit* joystick = _glfw.iokit_js + joy; _GLFWjoystickIOKit* joystick = _glfw.iokit_js + joy;
joystick->present = GL_TRUE; joystick->present = GL_TRUE;