[v1 1/1] winejoystick.drv: Sort virtual joysticks by name on the Mac

David Lawrie david.dljunk at gmail.com
Sat Jul 16 03:15:43 CDT 2016


Tested on OS X 10.10.5.

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

diff --git a/dlls/winejoystick.drv/joystick_osx.c b/dlls/winejoystick.drv/joystick_osx.c
index 7e2e97f..5d24dd9 100644
--- a/dlls/winejoystick.drv/joystick_osx.c
+++ b/dlls/winejoystick.drv/joystick_osx.c
@@ -139,6 +139,26 @@ static long get_device_property_long(IOHIDDeviceRef device, CFStringRef key)
     return result;
 }
 
+static CFStringRef get_device_name(IOHIDDeviceRef device)
+{
+    CFTypeRef ref;
+    CFStringRef name = CFSTR("Default Name");
+
+    if(device)
+    {
+        assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));
+
+        ref = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
+
+        if (ref && CFStringGetTypeID() == CFGetTypeID(ref))
+            name = ref;
+        else if (ref)
+            name = CFCopyDescription(ref);
+    }
+
+    return name;
+}
+
 static long get_device_location_ID(IOHIDDeviceRef device)
 {
     return get_device_property_long(device, CFSTR(kIOHIDLocationIDKey));
@@ -312,13 +332,25 @@ static CFIndex find_top_level(IOHIDDeviceRef hid_device, CFMutableArrayRef main_
     TRACE("-> total %d\n", (int)total);
     return total;
 }
+/**************************************************************************
+ *                              device_name_comparator
+ *
+ * Virtual joysticks may not have a kIOHIDLocationIDKey and will default to location ID of 0, this orders virtual joysticks by their name
+ */
+static CFComparisonResult device_name_comparator(IOHIDDeviceRef device1, IOHIDDeviceRef device2)
+{
+    CFStringRef name1 = get_device_name(device1), name2 = get_device_name(device2);
+    CFStringCompareFlags compareOptions = kCFCompareForcedOrdering | kCFCompareNumerically;
+
+    return CFStringCompare(name1, name2, compareOptions);
+}
 
 /**************************************************************************
- *                              device_location_comparator
+ *                              device_location_name_comparator
  *
- * Helper to sort device array by location ID since location IDs are consistent across boots & launches
+ * Helper to sort device array first by location ID, since location IDs are consistent across boots & launches, then by product name
  */
-static CFComparisonResult device_location_comparator(const void *val1, const void *val2, void *context)
+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);
@@ -327,7 +359,7 @@ static CFComparisonResult device_location_comparator(const void *val1, const voi
         return kCFCompareLessThan;
     else if (loc1 > loc2)
         return kCFCompareGreaterThan;
-    return kCFCompareEqualTo;
+    return device_name_comparator(device1, device2);
 }
 
 /**************************************************************************
@@ -390,7 +422,7 @@ static int find_osx_devices(void)
         num_devices = CFSetGetCount(devset);
         devices = CFArrayCreateMutable(kCFAllocatorDefault, num_devices, &kCFTypeArrayCallBacks);
         CFSetApplyFunction(devset, copy_set_to_array, (void *)devices);
-        CFArraySortValues(devices, CFRangeMake(0, num_devices), device_location_comparator, NULL);
+        CFArraySortValues(devices, CFRangeMake(0, num_devices), device_location_name_comparator, NULL);
 
         CFRelease(devset);
         if (!devices)
-- 
1.7.12.4 (Apple Git-37)




More information about the wine-patches mailing list