From 53afd7c25f4423020c805c3587875cdce99ba3f4 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Sun, 10 Mar 2013 19:50:06 +0100 Subject: [PATCH] Fix various bad names in input controller. Make devices appear as a group header. --- Classes/NJDeviceController.h | 2 - Classes/NJDeviceController.m | 71 ++++++++++++++++++++---------------- Classes/NJInput.h | 2 +- Classes/NJInput.m | 4 +- Classes/NJInputPathElement.h | 1 + Info.plist | 2 +- 6 files changed, 45 insertions(+), 37 deletions(-) diff --git a/Classes/NJDeviceController.h b/Classes/NJDeviceController.h index 07695f7..ce05cee 100644 --- a/Classes/NJDeviceController.h +++ b/Classes/NJDeviceController.h @@ -25,8 +25,6 @@ @property (nonatomic, assign) BOOL translatingEvents; - (void)setup; -- (NJDevice *)findDeviceByRef:(IOHIDDeviceRef)device; - - (IBAction)translatingEventsChanged:(id)sender; @end diff --git a/Classes/NJDeviceController.m b/Classes/NJDeviceController.m index db821bf..8417927 100644 --- a/Classes/NJDeviceController.m +++ b/Classes/NJDeviceController.m @@ -16,24 +16,24 @@ #import "NJEvents.h" @implementation NJDeviceController { - IOHIDManagerRef hidManager; - NSTimer *continuousTimer; - NSMutableArray *runningOutputs; + IOHIDManagerRef _hidManager; + NSTimer *_continuousOutputsTick; + NSMutableArray *_continousOutputs; NSMutableArray *_devices; } - (id)init { if ((self = [super init])) { _devices = [[NSMutableArray alloc] initWithCapacity:16]; - runningOutputs = [[NSMutableArray alloc] initWithCapacity:32]; + _continousOutputs = [[NSMutableArray alloc] initWithCapacity:32]; } return self; } - (void)dealloc { - [continuousTimer invalidate]; - IOHIDManagerClose(hidManager, kIOHIDOptionsTypeNone); - CFRelease(hidManager); + [_continuousOutputsTick invalidate]; + IOHIDManagerClose(_hidManager, kIOHIDOptionsTypeNone); + CFRelease(_hidManager); } - (void)expandRecursive:(id )pathElement { @@ -44,13 +44,11 @@ } - (void)addRunningOutput:(NJOutput *)output { - if (![runningOutputs containsObject:output]) { - [runningOutputs addObject:output]; - } - if (!continuousTimer) { - continuousTimer = [NSTimer scheduledTimerWithTimeInterval:1.f/60.f + [_continousOutputs addObject:output]; + if (!_continuousOutputsTick) { + _continuousOutputsTick = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self - selector:@selector(updateContinuousInputs:) + selector:@selector(updateContinuousOutputs:) userInfo:nil repeats:YES]; } @@ -77,7 +75,8 @@ return; [self expandRecursive:handler]; - [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[outlineView rowForItem:handler]] byExtendingSelection: NO]; + [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[outlineView rowForItem:handler]] + byExtendingSelection: NO]; [outputController focusKey]; } @@ -107,12 +106,12 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) { } - (void)addDeviceForDevice:(IOHIDDeviceRef)device { - IOHIDDeviceRegisterInputValueCallback(device, input_callback, (__bridge void*)self); + IOHIDDeviceRegisterInputValueCallback(device, input_callback, (__bridge void *)self); NJDevice *dev = [[NJDevice alloc] initWithDevice:device]; dev.index = findAvailableIndex(_devices, dev); [_devices addObject:dev]; [outlineView reloadData]; - [connectDevicePrompt setHidden:!!_devices.count]; + connectDevicePrompt.hidden = !!_devices.count; } static void add_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) { @@ -138,28 +137,29 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD if (match) { [_devices removeObject:match]; [outlineView reloadData]; - [connectDevicePrompt setHidden:!!_devices.count]; + connectDevicePrompt.hidden = !!_devices.count; } - + if (_devices.count == 1) + [outlineView expandItem:_devices[0]]; } -- (void)updateContinuousInputs:(NSTimer *)timer { +- (void)updateContinuousOutputs:(NSTimer *)timer { self.mouseLoc = [NSEvent mouseLocation]; - for (NJOutput *output in [runningOutputs copy]) { + for (NJOutput *output in [_continousOutputs copy]) { if (![output update:self]) { - [runningOutputs removeObject:output]; + [_continousOutputs removeObject:output]; } } - if (!runningOutputs.count) { - [continuousTimer invalidate]; - continuousTimer = nil; + if (!_continousOutputs.count) { + [_continuousOutputsTick invalidate]; + _continuousOutputsTick = nil; } } #define NSSTR(e) ((NSString *)CFSTR(e)) - (void)setup { - hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); + _hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); NSArray *criteria = @[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_Joystick) }, @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), @@ -167,10 +167,10 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_MultiAxisController) } ]; - IOHIDManagerSetDeviceMatchingMultiple(hidManager, (__bridge CFArrayRef)criteria); + IOHIDManagerSetDeviceMatchingMultiple(_hidManager, (__bridge CFArrayRef)criteria); - IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - IOReturn ret = IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone); + IOHIDManagerScheduleWithRunLoop(_hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + IOReturn ret = IOHIDManagerOpen(_hidManager, kIOHIDOptionsTypeNone); if (ret != kIOReturnSuccess) { [[NSAlert alertWithMessageText:@"Input devices are unavailable" defaultButton:nil @@ -185,8 +185,8 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD contextInfo:nil]; } - IOHIDManagerRegisterDeviceMatchingCallback(hidManager, add_callback, (__bridge void *)self); - IOHIDManagerRegisterDeviceRemovalCallback(hidManager, remove_callback, (__bridge void *)self); + IOHIDManagerRegisterDeviceMatchingCallback(_hidManager, add_callback, (__bridge void *)self); + IOHIDManagerRegisterDeviceRemovalCallback(_hidManager, remove_callback, (__bridge void *)self); } - (NJInput *)selectedInput { @@ -217,10 +217,19 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn } - (void)outlineViewSelectionDidChange:(NSNotification *)notification { - [outputController loadCurrent]; } +- (BOOL)outlineView:(NSOutlineView *)outlineView + isGroupItem:(id )item { + return [item isKindOfClass:NJDevice.class]; +} + +- (BOOL)outlineView:(NSOutlineView *)outlineView_ + shouldSelectItem:(id )item { + return ![self outlineView:outlineView_ isGroupItem:item]; +} + - (void)setTranslatingEvents:(BOOL)translatingEvents { if (translatingEvents != _translatingEvents) { _translatingEvents = translatingEvents; diff --git a/Classes/NJInput.h b/Classes/NJInput.h index 380e858..d63be1b 100644 --- a/Classes/NJInput.h +++ b/Classes/NJInput.h @@ -12,7 +12,7 @@ @property (nonatomic, assign) IOHIDElementCookie cookie; @property (nonatomic, copy) NSArray *children; -@property (nonatomic, weak) id base; +@property (nonatomic, weak) id base; @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) BOOL active; @property (nonatomic, assign) float magnitude; diff --git a/Classes/NJInput.m b/Classes/NJInput.m index 077dac8..a279322 100644 --- a/Classes/NJInput.m +++ b/Classes/NJInput.m @@ -18,11 +18,11 @@ } - (id)findSubInputForValue:(IOHIDValueRef)value { - return NULL; + return nil; } - (NSString *)uid { - return [NSString stringWithFormat:@"%@~%@", [_base uid], _name]; + return [NSString stringWithFormat:@"%@~%@", _base.uid, _name]; } - (void)notifyEvent:(IOHIDValueRef)value { diff --git a/Classes/NJInputPathElement.h b/Classes/NJInputPathElement.h index 8fe5c65..8750038 100644 --- a/Classes/NJInputPathElement.h +++ b/Classes/NJInputPathElement.h @@ -5,5 +5,6 @@ - (NSArray *)children; - (id ) base; - (NSString *)name; +- (NSString *)uid; @end diff --git a/Info.plist b/Info.plist index 2b4ff49..e875f02 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 90 + 97 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.30.2