[v2 1/1] dinput: Sort virtual joysticks by name on the Mac

David Lawrie david.dljunk at gmail.com
Mon Jul 18 01:57:00 CDT 2016


Tested on OS X 10.10.5.

Signed-off-by: David Lawrie <david.dljunk at gmail.com>
---
 dlls/dinput/joystick_osx.c | 42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git a/dlls/dinput/joystick_osx.c b/dlls/dinput/joystick_osx.c
index 637692b..b008cdf 100644
--- a/dlls/dinput/joystick_osx.c
+++ b/dlls/dinput/joystick_osx.c
@@ -193,6 +193,30 @@ static long get_device_property_long(IOHIDDeviceRef device, CFStringRef key)
     return result;
 }
 
+static CFStringRef copy_device_name(IOHIDDeviceRef device)
+{
+    CFTypeRef ref;
+    CFStringRef name = NULL;
+
+    if(device)
+    {
+        assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));
+
+        ref = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
+
+        if (ref && CFStringGetTypeID() == CFGetTypeID(ref))
+            name = CFStringCreateCopy(kCFAllocatorDefault,ref);
+        else if (ref)
+            name = CFCopyDescription(ref);
+        else
+            name = CFStringCreateCopy(kCFAllocatorDefault,CFSTR("(null)"));
+    }
+    else
+        ERR("Invalid Device requested %p\n",device);
+
+    return name;
+}
+
 static long get_device_location_ID(IOHIDDeviceRef device)
 {
     return get_device_property_long(device, CFSTR(kIOHIDLocationIDKey));
@@ -203,7 +227,18 @@ static void copy_set_to_array(const void *value, void *context)
     CFArrayAppendValue(context, value);
 }
 
-static CFComparisonResult device_location_comparator(const void *val1, const void *val2, void *context)
+static CFComparisonResult device_name_comparator(IOHIDDeviceRef device1, IOHIDDeviceRef device2)
+{
+    CFStringRef name1 = copy_device_name(device1), name2 = copy_device_name(device2);
+    CFComparisonResult result = CFStringCompare(name1, name2, (kCFCompareForcedOrdering | kCFCompareNumerically));
+    if(name1)
+        CFRelease(name1);
+    if(name2)
+        CFRelease(name2);
+    return  result;
+}
+
+static CFComparisonResult device_location_name_comparator(const void *val1, const void *val2, void *context)
 {
     IOHIDDeviceRef device1 = (IOHIDDeviceRef)val1, device2 = (IOHIDDeviceRef)val2;
     long loc1 = get_device_location_ID(device1), loc2 = get_device_location_ID(device2);
@@ -212,7 +247,8 @@ static CFComparisonResult device_location_comparator(const void *val1, const voi
         return kCFCompareLessThan;
     else if (loc1 > loc2)
         return kCFCompareGreaterThan;
-    return kCFCompareEqualTo;
+    /* virtual joysticks may not have a kIOHIDLocationIDKey and will default to location ID of 0, this orders virtual joysticks by their name */
+    return device_name_comparator(device1, device2);
 }
 
 static const char* debugstr_cf(CFTypeRef t)
@@ -491,7 +527,7 @@ static int find_osx_devices(void)
         devices = CFArrayCreateMutable(kCFAllocatorDefault, num_devices, &kCFTypeArrayCallBacks);
         CFSetApplyFunction(devset, copy_set_to_array, devices);
         CFRelease(devset);
-        CFArraySortValues(devices, CFRangeMake(0, num_devices), device_location_comparator, NULL);
+        CFArraySortValues(devices, CFRangeMake(0, num_devices), device_location_name_comparator, NULL);
 
         device_main_elements = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
         if (!device_main_elements)
-- 
1.7.12.4 (Apple Git-37)




More information about the wine-patches mailing list