From bb37f8d2f0a7dbfa5814e2d9faa897b52fdfd230 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 2 May 2020 23:04:12 +0300 Subject: [PATCH] Optimize Joypad initialization --- JoyKit/JOYElement.m | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/JoyKit/JOYElement.m b/JoyKit/JOYElement.m index 4050312..2432002 100644 --- a/JoyKit/JOYElement.m +++ b/JoyKit/JOYElement.m @@ -1,5 +1,6 @@ #import "JOYElement.h" #include +#include @implementation JOYElement { @@ -28,6 +29,24 @@ _max = max; } +/* Ugly hack because IOHIDDeviceCopyMatchingElements is slow */ ++ (NSArray *) cookiesToSkipForDevice:(IOHIDDeviceRef)device +{ + id _device = (__bridge id)device; + NSMutableArray *ret = objc_getAssociatedObject(_device, _cmd); + if (ret) return ret; + + ret = [NSMutableArray array]; + NSArray *nones = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(device, + (__bridge CFDictionaryRef)@{@(kIOHIDElementTypeKey): @(kIOHIDElementTypeInput_NULL)}, + 0)); + for (id none in nones) { + [ret addObject:@(IOHIDElementGetCookie((__bridge IOHIDElementRef)none))]; + } + objc_setAssociatedObject(_device, _cmd, ret, OBJC_ASSOCIATION_RETAIN); + return ret; +} + - (instancetype)initWithElement:(IOHIDElementRef)element { if ((self = [super init])) { @@ -45,14 +64,12 @@ /* Catalina added a new input type in a way that breaks cookie consistency across macOS versions, we shall adjust our cookies to to compensate */ unsigned cookieShift = 0, parentCookieShift = 0; - NSArray *nones = CFBridgingRelease(IOHIDDeviceCopyMatchingElements(IOHIDElementGetDevice(element), - (__bridge CFDictionaryRef)@{@(kIOHIDElementTypeKey): @(kIOHIDElementTypeInput_NULL)}, - 0)); - for (id none in nones) { - if (IOHIDElementGetCookie((__bridge IOHIDElementRef) none) < _uniqueID) { + + for (NSNumber *none in [JOYElement cookiesToSkipForDevice:_device]) { + if (none.unsignedIntValue < _uniqueID) { cookieShift++; } - if (IOHIDElementGetCookie((__bridge IOHIDElementRef) none) < (int32_t)_parentID) { + if (none.unsignedIntValue < (int32_t)_parentID) { parentCookieShift++; } }