X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJInputController.m;h=84c8b69cd093b1a7962b1702c31c330b849823b5;hp=5ba9828e29b77cc8b0f16da682e3dfecc4692f94;hb=HEAD;hpb=7fa330d1595971c7ddf79aba72cc85ab1b6c95b2 diff --git a/Classes/NJInputController.m b/Classes/NJInputController.m index 5ba9828..84c8b69 100644 --- a/Classes/NJInputController.m +++ b/Classes/NJInputController.m @@ -19,12 +19,12 @@ @end -static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, - const CVTimeStamp *inNow, - const CVTimeStamp *inOutputTime, - CVOptionFlags flagsIn, - CVOptionFlags *flagsOut, - void *ctxManager) { +static CVReturn _updateDL(CVDisplayLinkRef displayLink, + const CVTimeStamp *inNow, + const CVTimeStamp *inOutputTime, + CVOptionFlags flagsIn, + CVOptionFlags *flagsOut, + void *ctxManager) { NJInputController *manager = (__bridge NJInputController *)ctxManager; [manager performSelectorOnMainThread:@selector(updateContinuousOutputs) withObject:nil @@ -38,8 +38,7 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, NSMutableArray *_devices; NSMutableArray *_mappings; NJMapping *_manualMapping; - CVDisplayLinkRef displayLink; - + CVDisplayLinkRef _displayLink; } #define NSSTR(e) ((NSString *)CFSTR(e)) @@ -49,16 +48,16 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, _devices = [[NSMutableArray alloc] initWithCapacity:16]; _continousOutputs = [[NSMutableArray alloc] initWithCapacity:32]; - CVReturn error = CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); - if (error) { - [self.delegate inputController:self - didError:[NSError errorWithDomain:NSCocoaErrorDomain - code:error - userInfo:nil]]; - NSLog(@"DisplayLink failed creation with error: %d.", error); - displayLink = NULL; + CVReturn cvErr = CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); + if (cvErr) { + NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain + code:cvErr + userInfo:nil]; + [self.delegate inputController:self didError:error]; + NSLog(@"DisplayLink failed creation with error: %@", error); + _displayLink = NULL; } - CVDisplayLinkSetOutputCallback(displayLink, displayLink_update_cb, (__bridge void *)self); + CVDisplayLinkSetOutputCallback(_displayLink, _updateDL, (__bridge void *)self); _HIDManager = [[NJHIDManager alloc] initWithCriteria:@[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), @@ -101,9 +100,9 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; - if (displayLink) { - CVDisplayLinkStop(displayLink); - CVDisplayLinkRelease(displayLink); + if (_displayLink) { + CVDisplayLinkStop(_displayLink); + CVDisplayLinkRelease(_displayLink); } } @@ -112,12 +111,13 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, // re-adding them or they trigger multiple times each time. if (![_continousOutputs containsObject:output]) [_continousOutputs addObject:output]; - if (displayLink && !CVDisplayLinkIsRunning(displayLink)) { - CVDisplayLinkStart(displayLink); - } + if (_displayLink && !CVDisplayLinkIsRunning(_displayLink)) + CVDisplayLinkStart(_displayLink); } -- (void)runOutputForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value { +- (void)runOutputForValue:(IOHIDValueRef)value { + IOHIDElementRef elt = value ? IOHIDValueGetElement(value) : NULL; + IOHIDDeviceRef device = elt ? IOHIDElementGetDevice(elt) : NULL; NJDevice *dev = [self findDeviceByRef:device]; NJInput *mainInput = [dev inputForEvent:value]; [mainInput notifyEvent:value]; @@ -131,7 +131,9 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, } } -- (void)showOutputForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value { +- (void)showOutputForValue:(IOHIDValueRef)value { + IOHIDElementRef elt = value ? IOHIDValueGetElement(value) : NULL; + IOHIDDeviceRef device = elt ? IOHIDElementGetDevice(elt) : NULL; NJDevice *dev = [self findDeviceByRef:device]; NJInput *handler = [dev handlerForEvent:value]; if (!handler) @@ -140,14 +142,11 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, [self.delegate inputController:self didInput:handler]; } -- (void)HIDManager:(NJHIDManager *)manager - valueChanged:(IOHIDValueRef)value - fromDevice:(IOHIDDeviceRef)device { - if (self.simulatingEvents - && !NSApplication.sharedApplication.isActive) { - [self runOutputForDevice:device value:value]; +- (void)HIDManager:(NJHIDManager *)manager valueChanged:(IOHIDValueRef)value { + if (self.simulatingEvents && !NSApplication.sharedApplication.isActive) { + [self runOutputForValue:value]; } else { - [self showOutputForDevice:device value:value]; + [self showOutputForValue:value]; } } @@ -195,16 +194,16 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, [_continousOutputs removeObject:output]; } } - if (!_continousOutputs.count && displayLink) { - CVDisplayLinkStop(displayLink); + if (!_continousOutputs.count && _displayLink) { + CVDisplayLinkStop(_displayLink); } } - (void)HIDManager:(NJHIDManager *)manager didError:(NSError *)error { [self.delegate inputController:self didError:error]; self.simulatingEvents = NO; - if (displayLink) - CVDisplayLinkStop(displayLink); + if (_displayLink) + CVDisplayLinkStop(_displayLink); } - (void)HIDManagerDidStart:(NJHIDManager *)manager { @@ -213,8 +212,8 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, - (void)HIDManagerDidStop:(NJHIDManager *)manager { [_devices removeAllObjects]; - if (displayLink) - CVDisplayLinkStop(displayLink); + if (_displayLink) + CVDisplayLinkStop(_displayLink); [self.delegate inputControllerDidStopHID:self]; } @@ -295,8 +294,7 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, if ([oldMapping.name.lowercaseString isEqualToString:@"@application"] || [oldMapping.name.lowercaseString isEqualToString: NSLocalizedString(@"@Application", nil).lowercaseString]) { - oldMapping.name = app.bestMappingName; - [self mappingsChanged]; + [self renameMapping:oldMapping to:app.bestMappingName]; } } _manualMapping = oldMapping; @@ -307,10 +305,10 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, _currentMapping = mapping; NSUInteger idx = [self indexOfMapping:_currentMapping]; [NSNotificationCenter.defaultCenter - postNotificationName:NJEventMappingChanged - object:self - userInfo:@{ NJMappingKey : _currentMapping, - NJMappingIndexKey: @(idx) }]; + postNotificationName:NJEventMappingChanged + object:self + userInfo:@{ NJMappingKey : _currentMapping, + NJMappingIndexKey: @(idx) }]; } - (void)activateMapping:(NJMapping *)mapping { @@ -383,7 +381,8 @@ static CVReturn displayLink_update_cb(CVDisplayLinkRef displayLink, - (void)removeMappingAtIndex:(NSInteger)idx { NSInteger currentIdx = [self indexOfMapping:_currentMapping]; [_mappings removeObjectAtIndex:idx]; - [self activateMapping:self.mappings[MIN(currentIdx, _mappings.count - 1)]]; + NSInteger activeIdx = MIN(currentIdx, (NSInteger)_mappings.count - 1); + [self activateMapping:self.mappings[activeIdx]]; [self mappingsChanged]; }