Commit 41656747 authored by David Lawrie's avatar David Lawrie Committed by Alexandre Julliard

winejoystick.drv: Sort virtual joysticks by name on the Mac.

parent 49bf4f7e
...@@ -139,6 +139,38 @@ static long get_device_property_long(IOHIDDeviceRef device, CFStringRef key) ...@@ -139,6 +139,38 @@ static long get_device_property_long(IOHIDDeviceRef device, CFStringRef key)
return result; return result;
} }
static CFStringRef copy_device_name(IOHIDDeviceRef device)
{
CFStringRef name;
if (device)
{
CFTypeRef ref_name;
assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));
ref_name = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
if (ref_name && CFStringGetTypeID() == CFGetTypeID(ref_name))
name = CFStringCreateCopy(kCFAllocatorDefault, ref_name);
else
{
long vendID, prodID;
vendID = get_device_property_long(device, CFSTR(kIOHIDVendorIDKey));
prodID = get_device_property_long(device, CFSTR(kIOHIDProductIDKey));
name = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("0x%04lx 0x%04lx"), vendID, prodID);
}
}
else
{
ERR("NULL device\n");
name = CFStringCreateCopy(kCFAllocatorDefault, CFSTR(""));
}
return name;
}
static long get_device_location_ID(IOHIDDeviceRef device) static long get_device_location_ID(IOHIDDeviceRef device)
{ {
return get_device_property_long(device, CFSTR(kIOHIDLocationIDKey)); return get_device_property_long(device, CFSTR(kIOHIDLocationIDKey));
...@@ -312,13 +344,26 @@ static CFIndex find_top_level(IOHIDDeviceRef hid_device, CFMutableArrayRef main_ ...@@ -312,13 +344,26 @@ static CFIndex find_top_level(IOHIDDeviceRef hid_device, CFMutableArrayRef main_
TRACE("-> total %d\n", (int)total); TRACE("-> total %d\n", (int)total);
return 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 = copy_device_name(device1), name2 = copy_device_name(device2);
CFComparisonResult result = CFStringCompare(name1, name2, (kCFCompareForcedOrdering | kCFCompareNumerically));
CFRelease(name1);
CFRelease(name2);
return result;
}
/************************************************************************** /**************************************************************************
* 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; IOHIDDeviceRef device1 = (IOHIDDeviceRef)val1, device2 = (IOHIDDeviceRef)val2;
long loc1 = get_device_location_ID(device1), loc2 = get_device_location_ID(device2); long loc1 = get_device_location_ID(device1), loc2 = get_device_location_ID(device2);
...@@ -327,7 +372,7 @@ static CFComparisonResult device_location_comparator(const void *val1, const voi ...@@ -327,7 +372,7 @@ static CFComparisonResult device_location_comparator(const void *val1, const voi
return kCFCompareLessThan; return kCFCompareLessThan;
else if (loc1 > loc2) else if (loc1 > loc2)
return kCFCompareGreaterThan; return kCFCompareGreaterThan;
return kCFCompareEqualTo; return device_name_comparator(device1, device2);
} }
/************************************************************************** /**************************************************************************
...@@ -390,7 +435,7 @@ static int find_osx_devices(void) ...@@ -390,7 +435,7 @@ static int find_osx_devices(void)
num_devices = CFSetGetCount(devset); num_devices = CFSetGetCount(devset);
devices = CFArrayCreateMutable(kCFAllocatorDefault, num_devices, &kCFTypeArrayCallBacks); devices = CFArrayCreateMutable(kCFAllocatorDefault, num_devices, &kCFTypeArrayCallBacks);
CFSetApplyFunction(devset, copy_set_to_array, (void *)devices); 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); CFRelease(devset);
if (!devices) if (!devices)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment