X-Git-Url: https://git.yukkurigames.com/?a=blobdiff_plain;f=Classes%2FNJInputController.m;h=3ee088455f2d282b6a227662a03b75d2bc9872b3;hb=71c958acabb545ef231ad100ef6410834cac3c23;hp=cd665de54b1f78718e669bbb77e4f24b96661eeb;hpb=fb780593799014faf609388b479c405ae125b5f4;p=enjoyable.git diff --git a/Classes/NJInputController.m b/Classes/NJInputController.m index cd665de..3ee0884 100644 --- a/Classes/NJInputController.m +++ b/Classes/NJInputController.m @@ -11,13 +11,34 @@ #import "NJOutput.h" #import "NJEvents.h" +#import + +@interface NJInputController () + +- (void)updateContinuousOutputs; + +@end + +static CVReturn displayLink_update_cb(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; } @@ -27,6 +48,17 @@ if ((self = [super init])) { _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; + } + CVDisplayLinkSetOutputCallback(displayLink, displayLink_update_cb, (__bridge void *)self); _HIDManager = [[NJHIDManager alloc] initWithCriteria:@[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), @@ -69,7 +101,10 @@ - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; - [_continuousOutputsTick invalidate]; + if (displayLink) { + CVDisplayLinkStop(displayLink); + CVDisplayLinkRelease(displayLink); + } } - (void)addRunningOutput:(NJOutput *)output { @@ -77,12 +112,8 @@ // 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); } } @@ -157,22 +188,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 +213,8 @@ - (void)HIDManagerDidStop:(NJHIDManager *)manager { [_devices removeAllObjects]; + if (displayLink) + CVDisplayLinkStop(displayLink); [self.delegate inputControllerDidStopHID:self]; } @@ -232,10 +266,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 +295,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 +306,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 {