From 79859cb3ac62e6bc3866187605fcd1da19f8177f Mon Sep 17 00:00:00 2001 From: Aric Stewart Date: Thu, 12 Mar 2009 08:03:04 -0500 Subject: [PATCH] dinput: OS/X joystick: Sort buttons based on usage as some controllers report the buttons out of order. Makes the xbox 360 controller button mapping more logical. --- dlls/dinput/joystick_osx.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/dlls/dinput/joystick_osx.c b/dlls/dinput/joystick_osx.c index 21f8ca3d22a..d5a5f1795e1 100644 --- a/dlls/dinput/joystick_osx.c +++ b/dlls/dinput/joystick_osx.c @@ -284,6 +284,27 @@ static int get_osx_device_name(int id, char *name, int length) return 0; } +static void insert_sort_button(int header, IOHIDElementRef tIOHIDElementRef, + CFMutableArrayRef elementCFArrayRef, int index, + int target) +{ + IOHIDElementRef targetElement; + int usage; + + CFArraySetValueAtIndex(elementCFArrayRef, header+index, NULL); + targetElement = ( IOHIDElementRef ) CFArrayGetValueAtIndex( elementCFArrayRef, header+target); + if (targetElement == NULL) + { + CFArraySetValueAtIndex(elementCFArrayRef, header+target,tIOHIDElementRef); + return; + } + usage = IOHIDElementGetUsage( targetElement ); + usage --; /* usage 1 based index */ + + insert_sort_button(header, targetElement, elementCFArrayRef, target, usage); + CFArraySetValueAtIndex(elementCFArrayRef, header+target,tIOHIDElementRef); +} + static void get_osx_device_elements(JoystickImpl *device, int axis_map[8]) { IOHIDDeviceRef tIOHIDDeviceRef; @@ -382,6 +403,18 @@ static void get_osx_device_elements(JoystickImpl *device, int axis_map[8]) device->generic.devcaps.dwAxes = axes; device->generic.devcaps.dwButtons = buttons; device->generic.devcaps.dwPOVs = povs; + + /* Sort buttons into correct order */ + for (buttons = 0; buttons < device->generic.devcaps.dwButtons; buttons++) + { + IOHIDElementRef tIOHIDElementRef = ( IOHIDElementRef ) CFArrayGetValueAtIndex( device->elementCFArrayRef, axes+povs+buttons); + uint32_t usage = IOHIDElementGetUsage( tIOHIDElementRef ); + usage --; /* usage is 1 indexed we need 0 indexed */ + if (usage == buttons) + continue; + + insert_sort_button(axes+povs, tIOHIDElementRef, device->elementCFArrayRef,buttons,usage); + } } static void get_osx_device_elements_props(JoystickImpl *device)