From 94254e4c790113e63e760fedd5d9e9940279a7c0 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 30 Mar 2018 11:49:25 +0200 Subject: [PATCH] (IOHIDManager) Cleanups --- input/drivers_hid/iohidmanager_hid.c | 242 ++++++++++++++------------- 1 file changed, 126 insertions(+), 116 deletions(-) diff --git a/input/drivers_hid/iohidmanager_hid.c b/input/drivers_hid/iohidmanager_hid.c index 3d545ccc82..b85a3c1117 100644 --- a/input/drivers_hid/iohidmanager_hid.c +++ b/input/drivers_hid/iohidmanager_hid.c @@ -70,7 +70,7 @@ CFComparisonResult iohidmanager_sort_elements(const void *val1, const void *val2 if (page1 != page2) return (CFComparisonResult)(page1 > page2); - if(use1 != use2) + if (use1 != use2) return (CFComparisonResult)(use1 > use2); return (CFComparisonResult)(cookie1 > cookie2); @@ -78,9 +78,9 @@ CFComparisonResult iohidmanager_sort_elements(const void *val1, const void *val2 static bool iohidmanager_check_for_id(apple_input_rec_t *rec, uint32_t id) { - while(rec) + while (rec) { - if(rec->id == id) + if (rec->id == id) return true; rec = rec->next; } @@ -90,7 +90,7 @@ static bool iohidmanager_check_for_id(apple_input_rec_t *rec, uint32_t id) static void iohidmanager_append_record(apple_input_rec_t *rec, apple_input_rec_t *b) { apple_input_rec_t *tmp = rec; - while(tmp->next) + while (tmp->next) tmp = tmp->next; tmp->next = b; } @@ -110,12 +110,13 @@ static void iohidmanager_append_record(apple_input_rec_t *rec, apple_input_rec_t static void iohidmanager_append_record_ordered(apple_input_rec_t **p_rec, apple_input_rec_t *b) { apple_input_rec_t *tmp = *p_rec; - while(tmp && (tmp->id <= b->id)) { - p_rec = &tmp->next; - tmp = tmp->next; + while (tmp && (tmp->id <= b->id)) + { + p_rec = &tmp->next; + tmp = tmp->next; } - b->next = tmp; - *p_rec = b; + b->next = tmp; + *p_rec = b; } static bool iohidmanager_hid_joypad_query(void *data, unsigned pad) @@ -154,7 +155,7 @@ static bool iohidmanager_hid_joypad_button(void *data, if (hat_dir) { unsigned h = GET_HAT(joykey); - if(h >= 1) + if (h >= 1) return false; switch(hat_dir) @@ -207,7 +208,7 @@ static int16_t iohidmanager_hid_joypad_axis(void *data, if (val >= 0) val = 0; } - else if(AXIS_POS_GET(joyaxis) < 6) + else if (AXIS_POS_GET(joyaxis) < 6) { val += hid->axes[port][AXIS_POS_GET(joyaxis)]; val += pad_connection_get_axis(&hid->slots[port], @@ -282,15 +283,15 @@ static void iohidmanager_hid_device_input_callback(void *data, IOReturn result, { tmp = adapter->hats; - while(tmp && tmp->cookie != (IOHIDElementCookie)cookie) + while (tmp && tmp->cookie != (IOHIDElementCookie)cookie) tmp = tmp->next; - if(tmp->cookie == (IOHIDElementCookie)cookie) + if (tmp->cookie == (IOHIDElementCookie)cookie) { CFIndex range = IOHIDElementGetLogicalMax(element) - IOHIDElementGetLogicalMin(element); CFIndex val = IOHIDValueGetIntegerValue(value); - if(range == 3) + if (range == 3) val *= 2; switch(val) @@ -347,12 +348,12 @@ static void iohidmanager_hid_device_input_callback(void *data, IOReturn result, default: tmp = adapter->axes; - while(tmp && tmp->cookie != (IOHIDElementCookie)cookie) + while (tmp && tmp->cookie != (IOHIDElementCookie)cookie) tmp = tmp->next; if (tmp) { - if(tmp->cookie == (IOHIDElementCookie)cookie) + if (tmp->cookie == (IOHIDElementCookie)cookie) { CFIndex min = IOHIDElementGetPhysicalMin(element); CFIndex state = IOHIDValueGetIntegerValue(value) - min; @@ -384,16 +385,17 @@ static void iohidmanager_hid_device_input_callback(void *data, IOReturn result, if (pushed_button) { - tmp = adapter->buttons; - uint8_t bit = 0; - while(tmp && tmp->cookie != (IOHIDElementCookie)cookie) + + tmp = adapter->buttons; + + while (tmp && tmp->cookie != (IOHIDElementCookie)cookie) { bit++; tmp = tmp->next; } - if(tmp && tmp->cookie == (IOHIDElementCookie)cookie) + if (tmp && tmp->cookie == (IOHIDElementCookie)cookie) { CFIndex state = IOHIDValueGetIntegerValue(value); if (state) @@ -425,19 +427,21 @@ static void iohidmanager_hid_device_remove(void *data, if (adapter) { apple_input_rec_t* tmp = NULL; - while(adapter->hats != NULL) + while (adapter->hats != NULL) { tmp = adapter->hats; adapter->hats = adapter->hats->next; free(tmp); } - while(adapter->axes != NULL) + + while (adapter->axes != NULL) { tmp = adapter->axes; adapter->axes = adapter->axes->next; free(tmp); } - while(adapter->buttons != NULL) + + while (adapter->buttons != NULL) { tmp = adapter->buttons; adapter->buttons = adapter->buttons->next; @@ -450,11 +454,11 @@ static void iohidmanager_hid_device_remove(void *data, static int32_t iohidmanager_hid_device_get_int_property( IOHIDDeviceRef device, CFStringRef key) { - int32_t value; CFNumberRef ref = (CFNumberRef)IOHIDDeviceGetProperty(device, key); if (ref && (CFGetTypeID(ref) == CFNumberGetTypeID())) { + int32_t value = 0; CFNumberGetValue((CFNumberRef)ref, kCFNumberIntType, &value); return value; } @@ -581,17 +585,18 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ for (i = 0; i < count; i++) { + IOHIDElementType type; + uint32_t page, use, cookie; + int detected_button = 0; IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i); if (!element) continue; - IOHIDElementType type = IOHIDElementGetType(element); - uint32_t page = (uint32_t)IOHIDElementGetUsagePage(element); - uint32_t use = (uint32_t)IOHIDElementGetUsage(element); - uint32_t cookie = (uint32_t)IOHIDElementGetCookie(element); - - int detected_button = 0; + type = IOHIDElementGetType(element); + page = (uint32_t)IOHIDElementGetUsagePage(element); + use = (uint32_t)IOHIDElementGetUsage(element); + cookie = (uint32_t)IOHIDElementGetCookie(element); switch (page) { @@ -636,21 +641,21 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ axis->cookie = (IOHIDElementCookie)cookie; axis->next = NULL; - if(iohidmanager_check_for_id(adapter->axes,i)) + if (iohidmanager_check_for_id(adapter->axes,i)) { /* axis ID already exists, save to tmp for appending later */ - if(tmpAxes) + if (tmpAxes) iohidmanager_append_record(tmpAxes, axis); else - tmpAxes = axis; + tmpAxes = axis; } else { found_axis[axis->id] = true; - if(adapter->axes) + if (adapter->axes) iohidmanager_append_record(adapter->axes, axis); else - adapter->axes = axis; + adapter->axes = axis; } } else @@ -687,16 +692,16 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ btn->cookie = (IOHIDElementCookie)cookie; btn->next = NULL; - if(iohidmanager_check_for_id(adapter->buttons,btn->id)) + if (iohidmanager_check_for_id(adapter->buttons,btn->id)) { - if(tmpButtons) + if (tmpButtons) iohidmanager_append_record_ordered(&tmpButtons, btn); else tmpButtons = btn; } else { - if(adapter->buttons) + if (adapter->buttons) iohidmanager_append_record_ordered(&adapter->buttons, btn); else adapter->buttons = btn; @@ -707,13 +712,13 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ /* take care of buttons/axes with duplicate 'use' values */ for (i = 0; i < 6; i++) { - if(found_axis[i] == false && tmpAxes) + if (found_axis[i] == false && tmpAxes) { apple_input_rec_t *next = tmpAxes->next; tmpAxes->id = i; tmpAxes->next = NULL; iohidmanager_append_record(adapter->axes, tmpAxes); - tmpAxes = next; + tmpAxes = next; } } @@ -721,11 +726,11 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ if (tmp) { - while(tmp->next) + while (tmp->next) tmp = tmp->next; } - while(tmpButtons) + while (tmpButtons) { apple_input_rec_t *next = tmpButtons->next; @@ -746,34 +751,34 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_ error: { apple_input_rec_t *tmp = NULL; - while(adapter->hats != NULL) + while (adapter->hats != NULL) { - tmp = adapter->hats; - adapter->hats = adapter->hats->next; + tmp = adapter->hats; + adapter->hats = adapter->hats->next; free(tmp); } - while(adapter->axes != NULL) + while (adapter->axes != NULL) { - tmp = adapter->axes; - adapter->axes = adapter->axes->next; + tmp = adapter->axes; + adapter->axes = adapter->axes->next; free(tmp); } - while(adapter->buttons != NULL) + while (adapter->buttons != NULL) { tmp = adapter->buttons; adapter->buttons = adapter->buttons->next; free(tmp); } - while(tmpAxes != NULL) + while (tmpAxes != NULL) { - tmp = tmpAxes; - tmpAxes = tmpAxes->next; + tmp = tmpAxes; + tmpAxes = tmpAxes->next; free(tmp); } - while(tmpButtons != NULL) + while (tmpButtons != NULL) { - tmp = tmpButtons; - tmpButtons = tmpButtons->next; + tmp = tmpButtons; + tmpButtons = tmpButtons->next; free(tmp); } free(adapter); @@ -833,14 +838,6 @@ static int iohidmanager_hid_manager_free(iohidmanager_hid_t *hid) return 0; } -static int iohidmanager_hid_manager_set_device_matching( - iohidmanager_hid_t *hid) -{ - CFSetRef set = IOHIDManagerCopyDevices(hid->ptr); - CFIndex num_devices = CFSetGetCount(set); - IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef)); - CFSetGetValues(set, (const void **) device_array); - /* re order device by location id */ typedef struct hid_list { @@ -848,61 +845,73 @@ static int iohidmanager_hid_manager_set_device_matching( uint32_t lid; struct hid_list *next; } hid_list_t; + +static int iohidmanager_hid_manager_set_device_matching( + iohidmanager_hid_t *hid) +{ + unsigned i; + hid_list_t* devList = NULL; + CFSetRef set = IOHIDManagerCopyDevices(hid->ptr); + CFIndex num_devices = CFSetGetCount(set); + IOHIDDeviceRef *device_array = (IOHIDDeviceRef*)calloc(num_devices, sizeof(IOHIDDeviceRef)); + + CFSetGetValues(set, (const void **) device_array); - hid_list_t* devList = NULL; - for (long i=0; idevice = dev; - devList->lid = iohidmanager_hid_device_get_location_id(dev); - //printf("%d\n",devList->lid); - devList->next = NULL; - } - else - { - hid_list_t * new = (hid_list_t *)malloc(sizeof(hid_list_t)); - new->device = dev; - new->lid = iohidmanager_hid_device_get_location_id(dev); - //printf("%d\n",new->lid); - new->next = NULL; + for (i = 0; i < num_devices; i++) + { + IOHIDDeviceRef dev = device_array[i]; - hid_list_t * ptr = devList; - if ( new->lid < ptr->lid ) - { - new->next = ptr; - devList = new; - } - else - { - while ( ( ptr->lid < new->lid ) && (ptr->next != NULL) ) - { - ptr = ptr->next; - } - new->next = ptr->next; - ptr->next = new; - } - } - } - } + /* filter gamepad/joystick devices */ + if ( IOHIDDeviceConformsTo(dev, kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick) + || IOHIDDeviceConformsTo(dev, kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad) + ) + { + if (!devList) + { + devList = (hid_list_t *)malloc(sizeof(hid_list_t)); + devList->device = dev; + devList->lid = iohidmanager_hid_device_get_location_id(dev); + devList->next = NULL; + } + else + { + hid_list_t *ptr = NULL; + hid_list_t *new = (hid_list_t *)malloc(sizeof(hid_list_t)); + new->device = dev; + new->lid = iohidmanager_hid_device_get_location_id(dev); + new->next = NULL; + + ptr = devList; + + if ( new->lid < ptr->lid ) + { + new->next = ptr; + devList = new; + } + else + { + while ((ptr->lid < new->lid ) && ptr->next) + ptr = ptr->next; + + new->next = ptr->next; + ptr->next = new; + } + } + } + } + + { + /* register devices */ + hid_list_t * ptr = devList; + while (ptr) + { + iohidmanager_hid_device_add(ptr->device, hid); + ptr = ptr->next; + free(devList); + devList = ptr; + } + } - /* register devices */ - hid_list_t * ptr = devList; - while (ptr != NULL) - { - iohidmanager_hid_device_add(ptr->device, hid); - //printf("%d\n",ptr->lid); - ptr = ptr->next; - free(devList); - devList = ptr; - } free(device_array); return 0; @@ -914,7 +923,8 @@ static void *iohidmanager_hid_init(void) calloc(1, sizeof(*hid_apple)); if (!hid_apple) - goto error; + return NULL; + hid_apple->slots = pad_connection_init(MAX_USERS); if (!hid_apple->slots)