Fix various bad names in input controller. Make devices appear as a group header.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 10 Mar 2013 18:50:06 +0000 (19:50 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 10 Mar 2013 18:50:06 +0000 (19:50 +0100)
Classes/NJDeviceController.h
Classes/NJDeviceController.m
Classes/NJInput.h
Classes/NJInput.m
Classes/NJInputPathElement.h
Info.plist

index 07695f7581693daabdc8d6094853acfc5f9f84c2..ce05cee96f12b2565efdcc376965726b45ace260 100644 (file)
@@ -25,8 +25,6 @@
 @property (nonatomic, assign) BOOL translatingEvents;
 
 - (void)setup;
-- (NJDevice *)findDeviceByRef:(IOHIDDeviceRef)device;
-
 - (IBAction)translatingEventsChanged:(id)sender;
 
 @end
index db821bf73b6a720825c3be814127f36421a67d74..8417927dbb5ddb029c7a530150d842bef13ebd2c 100644 (file)
 #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 <NJInputPathElement>)pathElement {
 }
 
 - (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 <NJInputPathElement>)item {
+    return [item isKindOfClass:NJDevice.class];
+}
+
+- (BOOL)outlineView:(NSOutlineView *)outlineView_
+   shouldSelectItem:(id <NJInputPathElement>)item {
+    return ![self outlineView:outlineView_ isGroupItem:item];
+}
+
 - (void)setTranslatingEvents:(BOOL)translatingEvents {
     if (translatingEvents != _translatingEvents) {
         _translatingEvents = translatingEvents;
index 380e85858ad4a67cb182d75afecb119c9e2e4a25..d63be1bd6e2028d94402f843b0d16336316f22df 100644 (file)
@@ -12,7 +12,7 @@
 
 @property (nonatomic, assign) IOHIDElementCookie cookie;
 @property (nonatomic, copy) NSArray *children;
-@property (nonatomic, weak) id base;
+@property (nonatomic, weak) id <NJInputPathElement> base;
 @property (nonatomic, copy) NSString *name;
 @property (nonatomic, assign) BOOL active;
 @property (nonatomic, assign) float magnitude;
index 077dac86158748c377fa8181e8b0886661ebc301..a279322583d4a19e9fedb83513b2b30da138e0cc 100644 (file)
 }
 
 - (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 {
index 8fe5c65aa3be5a3a35d2fee91308ae73124a5274..87500382f9bdf8836398beac4175f6795dcfbbd4 100644 (file)
@@ -5,5 +5,6 @@
 - (NSArray *)children;
 - (id <NJInputPathElement>) base;
 - (NSString *)name;
+- (NSString *)uid;
 
 @end
index 2b4ff4948598fdeb76ca93837048c11fa44a96c9..e875f02020d5a1ea772f0e3c2457a7dc9fd1800d 100644 (file)
@@ -46,7 +46,7 @@
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
-       <string>90</string>
+       <string>97</string>
        <key>LSApplicationCategoryType</key>
        <string>public.app-category.utilities</string>
        <key>NSHumanReadableCopyright</key>