static INT find_joystick_devices(void) { static INT joystick_devices_count = -1; if (joystick_devices_count != -1) return joystick_devices_count; joystick_devices_count = find_osx_devices(); return joystick_devices_count; }
/************************************************************************** * open_joystick */ static BOOL open_joystick(joystick_t* joystick) { CFIndex index; CFRange range; if (joystick->element) return TRUE; if (!device_main_elements) { find_osx_devices(); if (!device_main_elements) return FALSE; } index = joystick - joysticks; if (index >= CFArrayGetCount(device_main_elements)) return FALSE; joystick->element = (IOHIDElementRef)CFArrayGetValueAtIndex(device_main_elements, index); joystick->buttons = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); collect_joystick_elements(joystick, joystick->element); /* Sort buttons into correct order */ range.location = 0; range.length = CFArrayGetCount(joystick->buttons); CFArraySortValues(joystick->buttons, range, button_usage_comparator, NULL); if (range.length > 32) { /* Delete any buttons beyond the first 32 */ range.location = 32; range.length -= 32; CFArrayReplaceValues(joystick->buttons, range, NULL, 0); } return TRUE; }