diff --git a/apple/OSX/hid_pad.c b/apple/OSX/hid_pad.c index 28f326ea4d..6979c7d84b 100644 --- a/apple/OSX/hid_pad.c +++ b/apple/OSX/hid_pad.c @@ -18,76 +18,88 @@ static IOHIDManagerRef g_hid_manager; -static CFMutableDictionaryRef hu_CreateDeviceMatchingDictionary( UInt32 inUsagePage, UInt32 inUsage ) -{ - // create a dictionary to add usage page/usages to - CFMutableDictionaryRef result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); - if (!result) return 0; - - CFNumberRef pageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsagePage); - CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsagePageKey), pageCFNumberRef); - CFRelease(pageCFNumberRef); - - CFNumberRef usageCFNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &inUsage); - CFDictionarySetValue(result, CFSTR(kIOHIDDeviceUsageKey), usageCFNumberRef); - CFRelease(usageCFNumberRef); - - return result; -} - -static void hid_device_attached(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef inDevice) -{ -} - -static void hid_device_removed(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef inDevice) -{ -} - static void hid_input_callback(void* inContext, IOReturn inResult, void* inSender, IOHIDValueRef inIOHIDValueRef) { - IOHIDElementRef ref = IOHIDValueGetElement(inIOHIDValueRef); + IOHIDElementRef element = IOHIDValueGetElement(inIOHIDValueRef); - uint32_t type = IOHIDElementGetType(ref); - uint32_t page = IOHIDElementGetUsagePage(ref); - uint32_t use = IOHIDElementGetUsage(ref); + uint32_t slot = (uint32_t)inContext; + if (slot >= 4) + return; + + uint32_t type = IOHIDElementGetType(element); + uint32_t page = IOHIDElementGetUsagePage(element); + uint32_t use = IOHIDElementGetUsage(element); if (type == 2 && page == 9) { int state = (int)IOHIDValueGetIntegerValue(inIOHIDValueRef); - if (state) g_current_input_data.pad_buttons[0] |= (1 << use); - else g_current_input_data.pad_buttons[0] &= ~(1 << use); + if (state) g_current_input_data.pad_buttons[slot] |= (1 << (use - 1)); + else g_current_input_data.pad_buttons[slot] &= ~(1 << (use - 1)); } else if (page == 1) { -/* static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 }; + static const uint32_t axis_use_ids[4] = { 48, 49, 50, 53 }; for (int i = 0; i < 4; i ++) { if (use == axis_use_ids[i]) { - int state = (int)IOHIDValueGetIntegerValue(inIOHIDValueRef); - printf("axis %d %d\n", i, state); + CFIndex min = IOHIDElementGetPhysicalMin(element); + CFIndex max = IOHIDElementGetPhysicalMax(element); + CFIndex state = IOHIDValueGetIntegerValue(inIOHIDValueRef); + + if (min != 0) + { + max -= min; + state -= min; + min = 0; + } + + float val = (float)state / (float)max; + g_current_input_data.pad_axis[slot][i] = ((val * 2.0f) - 1.0f) * 32767.0f; } - }*/ + } } } +static void hid_device_attached(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef inDevice) +{ + IOHIDDeviceOpen(inDevice, kIOHIDOptionsTypeNone); + IOHIDDeviceScheduleWithRunLoop(inDevice, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + IOHIDDeviceRegisterInputValueCallback(inDevice, hid_input_callback, 0); +} + +static void hid_device_removed(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef inDevice) +{ + IOHIDDeviceClose(inDevice, kIOHIDOptionsTypeNone); +} + void osx_pad_init() { if (!g_hid_manager) { g_hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); - CFDictionaryRef matchingCFDictRef = hu_CreateDeviceMatchingDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick); - IOHIDManagerSetDeviceMatching(g_hid_manager, matchingCFDictRef); - CFRelease(matchingCFDictRef); + CFMutableDictionaryRef matcher = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); + uint32_t page = kHIDPage_GenericDesktop; + uint32_t use = kHIDUsage_GD_Joystick; + + CFNumberRef pagen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page); + CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsagePageKey), pagen); + CFRelease(pagen); + + CFNumberRef usen = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &use); + CFDictionarySetValue(matcher, CFSTR(kIOHIDDeviceUsageKey), usen); + CFRelease(usen); + + IOHIDManagerSetDeviceMatching(g_hid_manager, matcher); + CFRelease(matcher); IOHIDManagerRegisterDeviceMatchingCallback(g_hid_manager, hid_device_attached, 0); IOHIDManagerRegisterDeviceRemovalCallback(g_hid_manager, hid_device_removed, 0); IOHIDManagerScheduleWithRunLoop(g_hid_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); IOHIDManagerOpen(g_hid_manager, kIOHIDOptionsTypeNone); - IOHIDManagerRegisterInputValueCallback(g_hid_manager, hid_input_callback, 0); } }