X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJInputController.m;h=84c8b69cd093b1a7962b1702c31c330b849823b5;hp=cd665de54b1f78718e669bbb77e4f24b96661eeb;hb=HEAD;hpb=fb780593799014faf609388b479c405ae125b5f4 diff --git a/Classes/NJInputController.m b/Classes/NJInputController.m index cd665de..84c8b69 100644 --- a/Classes/NJInputController.m +++ b/Classes/NJInputController.m @@ -11,14 +11,34 @@ #import "NJOutput.h" #import "NJEvents.h" +#import + +@interface NJInputController () + +- (void)updateContinuousOutputs; + +@end + +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 + waitUntilDone:NO]; + return kCVReturnSuccess; +} + @implementation NJInputController { NJHIDManager *_HIDManager; - NSTimer *_continuousOutputsTick; NSMutableArray *_continousOutputs; NSMutableArray *_devices; NSMutableArray *_mappings; NJMapping *_manualMapping; - + CVDisplayLinkRef _displayLink; } #define NSSTR(e) ((NSString *)CFSTR(e)) @@ -27,6 +47,17 @@ if ((self = [super init])) { _devices = [[NSMutableArray alloc] initWithCapacity:16]; _continousOutputs = [[NSMutableArray alloc] initWithCapacity:32]; + + 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, _updateDL, (__bridge void *)self); _HIDManager = [[NJHIDManager alloc] initWithCriteria:@[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), @@ -69,7 +100,10 @@ - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; - [_continuousOutputsTick invalidate]; + if (_displayLink) { + CVDisplayLinkStop(_displayLink); + CVDisplayLinkRelease(_displayLink); + } } - (void)addRunningOutput:(NJOutput *)output { @@ -77,16 +111,13 @@ // re-adding them or they trigger multiple times each time. if (![_continousOutputs containsObject:output]) [_continousOutputs addObject:output]; - if (!_continuousOutputsTick) { - _continuousOutputsTick = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 - target:self - selector:@selector(updateContinuousOutputs:) - userInfo:nil - repeats:YES]; - } + 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]; @@ -100,7 +131,9 @@ } } -- (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) @@ -109,14 +142,11 @@ [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]; } } @@ -157,22 +187,23 @@ } } -- (void)updateContinuousOutputs:(NSTimer *)timer { +- (void)updateContinuousOutputs { self.mouseLoc = [NSEvent mouseLocation]; for (NJOutput *output in [_continousOutputs copy]) { if (![output update:self]) { [_continousOutputs removeObject:output]; } } - if (!_continousOutputs.count) { - [_continuousOutputsTick invalidate]; - _continuousOutputsTick = nil; + 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); } - (void)HIDManagerDidStart:(NJHIDManager *)manager { @@ -181,6 +212,8 @@ - (void)HIDManagerDidStop:(NJHIDManager *)manager { [_devices removeAllObjects]; + if (_displayLink) + CVDisplayLinkStop(_displayLink); [self.delegate inputControllerDidStopHID:self]; } @@ -232,10 +265,10 @@ - (void)mappingsSet { [self postLoadProcess]; [NSNotificationCenter.defaultCenter - postNotificationName:NJEventMappingListChanged - object:self - userInfo:@{ NJMappingListKey: _mappings, - NJMappingKey: _currentMapping }]; + postNotificationName:NJEventMappingListChanged + object:self + userInfo:@{ NJMappingListKey: _mappings, + NJMappingKey: _currentMapping }]; } - (void)mappingsChanged { @@ -261,8 +294,7 @@ 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; @@ -273,10 +305,10 @@ _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 { @@ -349,7 +381,8 @@ - (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]; }