From 0200b85f24cc59ecac11e80c6f846c7ad2bf2258 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 27 Oct 2013 12:20:55 +0000 Subject: [PATCH 1/2] convert osx joystick code to use HID interface --- src/cocoa_joystick.m | 439 ++++++++++++++++++++----------------------- src/cocoa_platform.h | 3 +- 2 files changed, 205 insertions(+), 237 deletions(-) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 6824726f..352bbdf2 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -42,7 +42,7 @@ //------------------------------------------------------------------------ typedef struct { - IOHIDElementCookie cookie; + IOHIDElementRef elementRef; long min; long max; @@ -52,128 +52,34 @@ typedef struct } _GLFWjoyelement; - -static void getElementsCFArrayHandler(const void* value, void* parameter); - - -// Adds an element to the specified joystick -// -static void addJoystickElement(_GLFWjoy* joystick, CFTypeRef elementRef) +static void CFSetApplierFunctionCopyToCFArray(const void *value, void *context) { - long elementType, usagePage, usage; - CFMutableArrayRef elementsArray = NULL; - - CFNumberGetValue(CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementTypeKey)), - kCFNumberLongType, &elementType); - CFNumberGetValue(CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementUsagePageKey)), - kCFNumberLongType, &usagePage); - CFNumberGetValue(CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementUsageKey)), - kCFNumberLongType, &usage); - - if ((elementType == kIOHIDElementTypeInput_Axis) || - (elementType == kIOHIDElementTypeInput_Button) || - (elementType == kIOHIDElementTypeInput_Misc)) - { - switch (usagePage) - { - case kHIDPage_GenericDesktop: - { - switch (usage) - { - case kHIDUsage_GD_X: - case kHIDUsage_GD_Y: - case kHIDUsage_GD_Z: - case kHIDUsage_GD_Rx: - case kHIDUsage_GD_Ry: - case kHIDUsage_GD_Rz: - case kHIDUsage_GD_Slider: - case kHIDUsage_GD_Dial: - case kHIDUsage_GD_Wheel: - elementsArray = joystick->axisElements; - break; - case kHIDUsage_GD_Hatswitch: - elementsArray = joystick->hatElements; - break; - } - - break; - } - - case kHIDPage_Button: - elementsArray = joystick->buttonElements; - break; - default: - break; - } - - if (elementsArray) - { - long number; - CFTypeRef numberRef; - _GLFWjoyelement* element = calloc(1, sizeof(_GLFWjoyelement)); - - CFArrayAppendValue(elementsArray, element); - - numberRef = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementCookieKey)); - if (numberRef && CFNumberGetValue(numberRef, kCFNumberLongType, &number)) - element->cookie = (IOHIDElementCookie) number; - - numberRef = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementMinKey)); - if (numberRef && CFNumberGetValue(numberRef, kCFNumberLongType, &number)) - element->minReport = element->min = number; - - numberRef = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementMaxKey)); - if (numberRef && CFNumberGetValue(numberRef, kCFNumberLongType, &number)) - element->maxReport = element->max = number; - } - } - else - { - CFTypeRef array = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementKey)); - if (array) - { - if (CFGetTypeID(array) == CFArrayGetTypeID()) - { - CFRange range = { 0, CFArrayGetCount(array) }; - CFArrayApplyFunction(array, range, getElementsCFArrayHandler, joystick); - } - } - } + CFArrayAppendValue( ( CFMutableArrayRef ) context, value ); } -// Adds an element to the specified joystick -// -static void getElementsCFArrayHandler(const void* value, void* parameter) -{ - if (CFGetTypeID(value) == CFDictionaryGetTypeID()) - addJoystickElement((_GLFWjoy*) parameter, (CFTypeRef) value); -} + // Returns the value of the specified element of the specified joystick // static long getElementValue(_GLFWjoy* joystick, _GLFWjoyelement* element) { - IOReturn result = kIOReturnSuccess; - IOHIDEventStruct hidEvent; - hidEvent.value = 0; - - if (joystick && element && joystick->interface) + if (joystick && element && joystick->device) { - result = (*(joystick->interface))->getElementValue(joystick->interface, - element->cookie, - &hidEvent); + IOHIDValueRef valRef; + IOReturn result = IOHIDDeviceGetValue(joystick->device,element->elementRef,&valRef); + if (kIOReturnSuccess == result) { + CFIndex value = IOHIDValueGetIntegerValue(valRef); // Record min and max for auto calibration - if (hidEvent.value < element->minReport) - element->minReport = hidEvent.value; - if (hidEvent.value > element->maxReport) - element->maxReport = hidEvent.value; + if (value < element->minReport) + element->minReport = value; + if (value > element->maxReport) + element->maxReport = value; + return (long) value; } } - - // Auto user scale - return (long) hidEvent.value; + return 0; } // Removes the specified joystick @@ -186,31 +92,54 @@ static void removeJoystick(_GLFWjoy* joystick) return; for (i = 0; i < CFArrayGetCount(joystick->axisElements); i++) - free((void*) CFArrayGetValueAtIndex(joystick->axisElements, i)); + { + _GLFWjoyelement* control = (_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->axisElements, i); + CFRelease(control->elementRef); + free(control); + } CFArrayRemoveAllValues(joystick->axisElements); + //CFRelease(joystick->axisElements); for (i = 0; i < CFArrayGetCount(joystick->buttonElements); i++) - free((void*) CFArrayGetValueAtIndex(joystick->buttonElements, i)); + { + _GLFWjoyelement* control = (_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->buttonElements, i); + CFRelease(control->elementRef); + free(control); + } CFArrayRemoveAllValues(joystick->buttonElements); + //CFRelease(joystick->buttonElements); for (i = 0; i < CFArrayGetCount(joystick->hatElements); i++) - free((void*) CFArrayGetValueAtIndex(joystick->hatElements, i)); + { + _GLFWjoyelement* control = (_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->hatElements, i); + CFRelease(control->elementRef); + free(control); + } CFArrayRemoveAllValues(joystick->hatElements); + //CFRelease(joystick->hatElements); free(joystick->axes); free(joystick->buttons); - (*(joystick->interface))->close(joystick->interface); - (*(joystick->interface))->Release(joystick->interface); memset(joystick, 0, sizeof(_GLFWjoy)); } // Callback for user-initiated joystick removal // -static void removalCallback(void* target, IOReturn result, void* refcon, void* sender) +static void removalCallback(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef) { - removeJoystick((_GLFWjoy*) refcon); + int joy; + + for (joy = 0; joy <= GLFW_JOYSTICK_LAST; joy++) + { + _GLFWjoy* joystick = _glfw.ns.joysticks + joy; + if(joystick->device == inIOHIDDeviceRef) + { + removeJoystick(joystick); + return; + } + } } // Polls for joystick events and updates GLFW state @@ -233,10 +162,11 @@ static void pollJoystickEvents(void) _GLFWjoyelement* button = (_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->buttonElements, i); - if (getElementValue(joystick, button)) - joystick->buttons[buttonIndex++] = GLFW_PRESS; + long value = getElementValue(joystick, button); + if(button->maxReport>1) + joystick->buttons[buttonIndex++] = value;//analogue button else - joystick->buttons[buttonIndex++] = GLFW_RELEASE; + joystick->buttons[buttonIndex++] = value?GLFW_PRESS:GLFW_RELEASE; } for (i = 0; i < CFArrayGetCount(joystick->axisElements); i++) @@ -251,9 +181,6 @@ static void pollJoystickEvents(void) joystick->axes[i] = value; else joystick->axes[i] = (2.f * (value - axis->minReport) / readScale) - 1.f; - - if (i & 1) - joystick->axes[i] = -joystick->axes[i]; } for (i = 0; i < CFArrayGetCount(joystick->hatElements); i++) @@ -279,6 +206,32 @@ static void pollJoystickEvents(void) } } +static CFMutableDictionaryRef creates_osx_device_match(int usage) +{ + CFMutableDictionaryRef result = CFDictionaryCreateMutable( NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); + + if ( !result ) + return NULL; + + int page = kHIDPage_GenericDesktop; + CFNumberRef pageCFNumberRef = CFNumberCreate( NULL, kCFNumberIntType, &page); + + if ( !pageCFNumberRef ) + return NULL; + + CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsagePageKey ), pageCFNumberRef ); + CFRelease( pageCFNumberRef ); + + CFNumberRef usageCFNumberRef = CFNumberCreate( NULL, kCFNumberIntType, &usage); + if ( !usageCFNumberRef ) + return NULL; + + CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsageKey ), usageCFNumberRef ); + CFRelease( usageCFNumberRef ); + + return result; +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW internal API ////// @@ -289,140 +242,144 @@ static void pollJoystickEvents(void) void _glfwInitJoysticks(void) { int joy = 0; - IOReturn result = kIOReturnSuccess; - mach_port_t masterPort = 0; - io_iterator_t objectIterator = 0; - CFMutableDictionaryRef hidMatchDictionary = NULL; - io_object_t ioHIDDeviceObject = 0; - result = IOMasterPort(bootstrap_port, &masterPort); - hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey); - if (kIOReturnSuccess != result || !hidMatchDictionary) - { - if (hidMatchDictionary) - CFRelease(hidMatchDictionary); - - return; - } - - result = IOServiceGetMatchingServices(masterPort, - hidMatchDictionary, - &objectIterator); - if (result != kIOReturnSuccess) + _glfw.ns.ioHIDManagerRef = IOHIDManagerCreate( kCFAllocatorDefault, 0L ); + IOReturn tIOReturn = IOHIDManagerOpen( _glfw.ns.ioHIDManagerRef, 0L); + if ( kIOReturnSuccess != tIOReturn ) return; - if (!objectIterator) + CFMutableArrayRef matching = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); { - // There are no joysticks - return; - } - - while ((ioHIDDeviceObject = IOIteratorNext(objectIterator))) - { - kern_return_t result; - CFTypeRef valueRef = 0; - - IOCFPlugInInterface** ppPlugInInterface = NULL; - HRESULT plugInResult = S_OK; - SInt32 score = 0; - - long usagePage, usage; - - // Check device type - valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject, - CFSTR(kIOHIDPrimaryUsagePageKey), - kCFAllocatorDefault, - kNilOptions); - if (valueRef) + CFMutableDictionaryRef result = creates_osx_device_match(kHIDUsage_GD_Joystick); + if (result) { - CFNumberGetValue(valueRef, kCFNumberLongType, &usagePage); - if (usagePage != kHIDPage_GenericDesktop) - { - // This device is not relevant to GLFW - continue; - } - - CFRelease(valueRef); + CFArrayAppendValue( matching, result ); + CFRelease(result); + result = creates_osx_device_match(kHIDUsage_GD_GamePad); } - - valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject, - CFSTR(kIOHIDPrimaryUsageKey), - kCFAllocatorDefault, - kNilOptions); - if (valueRef) + if (result) { - CFNumberGetValue(valueRef, kCFNumberLongType, &usage); + CFArrayAppendValue( matching, result ); + CFRelease(result); + result = creates_osx_device_match(kHIDUsage_GD_MultiAxisController); + } + if (result) + { + CFArrayAppendValue( matching, result ); + CFRelease(result); + } + } + IOHIDManagerSetDeviceMatchingMultiple( _glfw.ns.ioHIDManagerRef, matching); + CFRelease(matching); + CFSetRef devset = IOHIDManagerCopyDevices( _glfw.ns.ioHIDManagerRef ); + if(!devset) + return; - if ((usage != kHIDUsage_GD_Joystick && - usage != kHIDUsage_GD_GamePad && - usage != kHIDUsage_GD_MultiAxisController)) - { - // This device is not relevant to GLFW - continue; - } + CFMutableArrayRef devarray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); + CFSetApplyFunction(devset, CFSetApplierFunctionCopyToCFArray, devarray); + CFRelease( devset); + CFIndex idx,countDevices = CFArrayGetCount(devarray); - CFRelease(valueRef); + for (idx = 0; idx < countDevices; idx++) + { + IOHIDDeviceRef tDevice = (IOHIDDeviceRef) CFArrayGetValueAtIndex(devarray, idx); + + CFArrayRef elementCFArrayRef = IOHIDDeviceCopyMatchingElements(tDevice, NULL, 0); + + if (!elementCFArrayRef) + continue; + + CFTypeRef valueRef = IOHIDDeviceGetProperty(tDevice, CFSTR( kIOHIDPrimaryUsageKey )); + if (!valueRef) + { + CFRelease(elementCFArrayRef); + continue; + } + long primaryUsage; + CFNumberGetValue(valueRef, kCFNumberLongType, &primaryUsage); + CFRelease(valueRef); + + if ((primaryUsage != kHIDUsage_GD_Joystick && + primaryUsage != kHIDUsage_GD_GamePad && + primaryUsage != kHIDUsage_GD_MultiAxisController)) + { + // This device is not relevant to GLFW + CFRelease(elementCFArrayRef); + continue; } _GLFWjoy* joystick = _glfw.ns.joysticks + joy; joystick->present = GL_TRUE; - result = IOCreatePlugInInterfaceForService(ioHIDDeviceObject, - kIOHIDDeviceUserClientTypeID, - kIOCFPlugInInterfaceID, - &ppPlugInInterface, - &score); - - if (kIOReturnSuccess != result) - return; - - plugInResult = (*ppPlugInInterface)->QueryInterface( - ppPlugInInterface, - CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), - (void *) &(joystick->interface)); - - if (plugInResult != S_OK) - return; - - (*ppPlugInInterface)->Release(ppPlugInInterface); - - (*(joystick->interface))->open(joystick->interface, 0); - (*(joystick->interface))->setRemovalCallback(joystick->interface, - removalCallback, - joystick, - joystick); - - // Get product string - valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject, - CFSTR(kIOHIDProductKey), - kCFAllocatorDefault, - kNilOptions); - if (valueRef) - { - CFStringGetCString(valueRef, - joystick->name, - sizeof(joystick->name), - kCFStringEncodingUTF8); - CFRelease(valueRef); - } + CFStringRef strRef = IOHIDDeviceGetProperty(tDevice, CFSTR( kIOHIDProductKey )); + CFStringGetCString(strRef, joystick->name, sizeof(joystick->name), kCFStringEncodingUTF8); + CFRelease(strRef); + joystick->device = tDevice; joystick->axisElements = CFArrayCreateMutable(NULL, 0, NULL); joystick->buttonElements = CFArrayCreateMutable(NULL, 0, NULL); joystick->hatElements = CFArrayCreateMutable(NULL, 0, NULL); - valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject, - CFSTR(kIOHIDElementKey), - kCFAllocatorDefault, - kNilOptions); - if (CFGetTypeID(valueRef) == CFArrayGetTypeID()) + CFIndex eleidx, elecnt = CFArrayGetCount(elementCFArrayRef); + for (eleidx=0; eleidxbuttonElements; + switch (usagePage) + { + case kHIDPage_GenericDesktop: + { + switch (usage) + { + case kHIDUsage_GD_X: + case kHIDUsage_GD_Y: + case kHIDUsage_GD_Z: + case kHIDUsage_GD_Rx: + case kHIDUsage_GD_Ry: + case kHIDUsage_GD_Rz: + case kHIDUsage_GD_Slider: + case kHIDUsage_GD_Dial: + case kHIDUsage_GD_Wheel: + elementsArray = joystick->axisElements; + break; + case kHIDUsage_GD_Hatswitch: + elementsArray = joystick->hatElements; + break; + } + + break; + } + + case kHIDPage_Button: + elementsArray = joystick->buttonElements; + break; + } + + if(!elementsArray) + continue; + + _GLFWjoyelement* control = calloc(1, sizeof(_GLFWjoyelement)); + + CFArrayAppendValue(elementsArray, control); + + control->elementRef = tIOHIDElementRef; + CFRetain(control->elementRef); + + control->max = control->maxReport = IOHIDElementGetLogicalMax(tIOHIDElementRef); + control->min = control->minReport = IOHIDElementGetLogicalMin(tIOHIDElementRef); + } + CFRelease(elementCFArrayRef); joystick->axes = calloc(CFArrayGetCount(joystick->axisElements), sizeof(float)); @@ -432,9 +389,18 @@ void _glfwInitJoysticks(void) joy++; if (joy > GLFW_JOYSTICK_LAST) break; + } + CFRelease(devarray); + + IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns.ioHIDManagerRef, &removalCallback, NULL); + + IOHIDManagerScheduleWithRunLoop(_glfw.ns.ioHIDManagerRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + + } + // Close all opened joystick handles // void _glfwTerminateJoysticks(void) @@ -446,6 +412,8 @@ void _glfwTerminateJoysticks(void) _GLFWjoy* joystick = &_glfw.ns.joysticks[i]; removeJoystick(joystick); } + IOHIDManagerClose(_glfw.ns.ioHIDManagerRef, kIOHIDOptionsTypeNone); + CFRelease(_glfw.ns.ioHIDManagerRef); } @@ -493,4 +461,3 @@ const char* _glfwPlatformGetJoystickName(int joy) return _glfw.ns.joysticks[joy].name; } - diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index 0e92c4a2..76f94272 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -78,7 +78,7 @@ typedef struct int present; char name[256]; - IOHIDDeviceInterface** interface; + IOHIDDeviceRef device; CFMutableArrayRef axisElements; CFMutableArrayRef buttonElements; @@ -107,6 +107,7 @@ typedef struct _GLFWlibraryNS char* clipboardString; + IOHIDManagerRef ioHIDManagerRef; _GLFWjoy joysticks[GLFW_JOYSTICK_LAST + 1]; } _GLFWlibraryNS; From ffdbdcee58491d3ce0b54a95aa3945caa7c33107 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 28 Oct 2013 20:09:41 +0000 Subject: [PATCH 2/2] clean up --- src/cocoa_joystick.m | 134 ++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 84 deletions(-) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 352bbdf2..92c32d32 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -52,7 +52,7 @@ typedef struct } _GLFWjoyelement; -static void CFSetApplierFunctionCopyToCFArray(const void *value, void *context) +static void copyToCFArray(const void *value, void *context) { CFArrayAppendValue( ( CFMutableArrayRef ) context, value ); } @@ -206,30 +206,23 @@ static void pollJoystickEvents(void) } } -static CFMutableDictionaryRef creates_osx_device_match(int usage) +static CFMutableDictionaryRef createDevMatchDictionary(int page, int usage) { - CFMutableDictionaryRef result = CFDictionaryCreateMutable( NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); + CFMutableDictionaryRef resultRef = CFDictionaryCreateMutable( NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); + CFNumberRef pageRef = CFNumberCreate( NULL, kCFNumberIntType, &page); + CFNumberRef usageRef = CFNumberCreate( NULL, kCFNumberIntType, &usage); - if ( !result ) + if ( resultRef && pageRef && usageRef) + { + CFDictionarySetValue( resultRef, CFSTR( kIOHIDDeviceUsagePageKey ), pageRef ); + CFDictionarySetValue( resultRef, CFSTR( kIOHIDPrimaryUsageKey ), usageRef ); + CFRelease( pageRef ); + CFRelease( usageRef ); + + return resultRef; + } + else return NULL; - - int page = kHIDPage_GenericDesktop; - CFNumberRef pageCFNumberRef = CFNumberCreate( NULL, kCFNumberIntType, &page); - - if ( !pageCFNumberRef ) - return NULL; - - CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsagePageKey ), pageCFNumberRef ); - CFRelease( pageCFNumberRef ); - - CFNumberRef usageCFNumberRef = CFNumberCreate( NULL, kCFNumberIntType, &usage); - if ( !usageCFNumberRef ) - return NULL; - - CFDictionarySetValue( result, CFSTR( kIOHIDDeviceUsageKey ), usageCFNumberRef ); - CFRelease( usageCFNumberRef ); - - return result; } @@ -248,85 +241,58 @@ void _glfwInitJoysticks(void) if ( kIOReturnSuccess != tIOReturn ) return; - CFMutableArrayRef matching = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); + CFMutableDictionaryRef joystickMatchRef = createDevMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick); + CFMutableDictionaryRef gamepadMatchRef = createDevMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad); + CFMutableDictionaryRef multiAxisMatchRef = createDevMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController); + CFArrayRef matchArrayRef = NULL; + if(multiAxisMatchRef && gamepadMatchRef && multiAxisMatchRef) { - CFMutableDictionaryRef result = creates_osx_device_match(kHIDUsage_GD_Joystick); - if (result) - { - CFArrayAppendValue( matching, result ); - CFRelease(result); - result = creates_osx_device_match(kHIDUsage_GD_GamePad); - } - if (result) - { - CFArrayAppendValue( matching, result ); - CFRelease(result); - result = creates_osx_device_match(kHIDUsage_GD_MultiAxisController); - } - if (result) - { - CFArrayAppendValue( matching, result ); - CFRelease(result); - } + CFMutableDictionaryRef matchArray[] = { joystickMatchRef, gamepadMatchRef, multiAxisMatchRef }; + matchArrayRef = CFArrayCreate(kCFAllocatorDefault,(const void **)matchArray, 3, &kCFTypeArrayCallBacks); + CFRelease(joystickMatchRef);CFRelease(gamepadMatchRef);CFRelease(multiAxisMatchRef); } - IOHIDManagerSetDeviceMatchingMultiple( _glfw.ns.ioHIDManagerRef, matching); - CFRelease(matching); - CFSetRef devset = IOHIDManagerCopyDevices( _glfw.ns.ioHIDManagerRef ); - if(!devset) + if(!matchArrayRef) return; - CFMutableArrayRef devarray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); - CFSetApplyFunction(devset, CFSetApplierFunctionCopyToCFArray, devarray); - CFRelease( devset); - CFIndex idx,countDevices = CFArrayGetCount(devarray); + IOHIDManagerSetDeviceMatchingMultiple( _glfw.ns.ioHIDManagerRef, matchArrayRef); + CFRelease(matchArrayRef); + CFSetRef deviceSet = IOHIDManagerCopyDevices( _glfw.ns.ioHIDManagerRef ); + if(!deviceSet) + return; - for (idx = 0; idx < countDevices; idx++) + CFMutableArrayRef deviceArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); + CFSetApplyFunction(deviceSet, copyToCFArray, deviceArray); + CFRelease( deviceSet); + + CFIndex idx; + for (idx = 0; idx < CFArrayGetCount(deviceArray); idx++) { - IOHIDDeviceRef tDevice = (IOHIDDeviceRef) CFArrayGetValueAtIndex(devarray, idx); + IOHIDDeviceRef dev = (IOHIDDeviceRef) CFArrayGetValueAtIndex(deviceArray, idx); - CFArrayRef elementCFArrayRef = IOHIDDeviceCopyMatchingElements(tDevice, NULL, 0); + CFArrayRef elementArray = IOHIDDeviceCopyMatchingElements(dev, NULL, 0); - if (!elementCFArrayRef) + if (!elementArray) continue; - CFTypeRef valueRef = IOHIDDeviceGetProperty(tDevice, CFSTR( kIOHIDPrimaryUsageKey )); - if (!valueRef) - { - CFRelease(elementCFArrayRef); - continue; - } - long primaryUsage; - CFNumberGetValue(valueRef, kCFNumberLongType, &primaryUsage); - CFRelease(valueRef); - - if ((primaryUsage != kHIDUsage_GD_Joystick && - primaryUsage != kHIDUsage_GD_GamePad && - primaryUsage != kHIDUsage_GD_MultiAxisController)) - { - // This device is not relevant to GLFW - CFRelease(elementCFArrayRef); - continue; - } - _GLFWjoy* joystick = _glfw.ns.joysticks + joy; joystick->present = GL_TRUE; - CFStringRef strRef = IOHIDDeviceGetProperty(tDevice, CFSTR( kIOHIDProductKey )); + CFStringRef strRef = IOHIDDeviceGetProperty(dev, CFSTR( kIOHIDProductKey )); CFStringGetCString(strRef, joystick->name, sizeof(joystick->name), kCFStringEncodingUTF8); CFRelease(strRef); - joystick->device = tDevice; + joystick->device = dev; joystick->axisElements = CFArrayCreateMutable(NULL, 0, NULL); joystick->buttonElements = CFArrayCreateMutable(NULL, 0, NULL); joystick->hatElements = CFArrayCreateMutable(NULL, 0, NULL); - CFIndex eleidx, elecnt = CFArrayGetCount(elementCFArrayRef); + CFIndex eleidx, elecnt = CFArrayGetCount(elementArray); for (eleidx=0; eleidxelementRef = tIOHIDElementRef; + control->elementRef = elementRef; CFRetain(control->elementRef); - control->max = control->maxReport = IOHIDElementGetLogicalMax(tIOHIDElementRef); - control->min = control->minReport = IOHIDElementGetLogicalMin(tIOHIDElementRef); + control->max = control->maxReport = IOHIDElementGetLogicalMax(elementRef); + control->min = control->minReport = IOHIDElementGetLogicalMin(elementRef); } - CFRelease(elementCFArrayRef); + CFRelease(elementArray); joystick->axes = calloc(CFArrayGetCount(joystick->axisElements), sizeof(float)); @@ -391,7 +357,7 @@ void _glfwInitJoysticks(void) break; } - CFRelease(devarray); + CFRelease(deviceArray); IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns.ioHIDManagerRef, &removalCallback, NULL);