Move logic into messages rather than directly in the callback functions.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Fri, 1 Mar 2013 01:27:59 +0000 (02:27 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Fri, 1 Mar 2013 01:27:59 +0000 (02:27 +0100)
JoystickController.h
JoystickController.m

index 5e46256b2898725bbfd57e0c6e724ccecbf6b55b..4da83cd4f91d81b4eeba079154f9cdfbb18bc74e 100644 (file)
@@ -10,7 +10,6 @@
 @class JSAction;
 @class ConfigsController;
 @class TargetController;
-@class Config;
 
 @interface JoystickController : NSObject {
        IBOutlet NSOutlineView *outlineView;
@@ -21,9 +20,7 @@
 - (void)setup;
 - (Joystick *)findJoystickByRef:(IOHIDDeviceRef)device;
 
-@property (readonly) Config *currentConfig;
 @property (readonly) JSAction *selectedAction;
-@property (readonly) NSMutableArray *joysticks;
 @property (assign) NSPoint mouseLoc;
 @property (assign) BOOL frontWindowOnly;
 @property (assign) BOOL sendingRealEvents;
index 97ca74b6e10e7e42d86264d70ae50f66e2408c5a..cd513de0dde7024c2cc5c9144db16ef1f99818b6 100644 (file)
@@ -18,6 +18,7 @@
     IOHIDManagerRef hidManager;
     NSTimer *continuousTimer;
     NSMutableArray *runningTargets;
+    NSMutableArray *_joysticks;
 }
 
 - (id)init {
     }
 }
 
+- (void)runTargetForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
+    Joystick *js = [self findJoystickByRef:device];
+    JSAction *mainAction = [js actionForEvent:value];
+    [mainAction notifyEvent:value];
+    NSArray *children = mainAction.children ? mainAction.children : mainAction ? @[mainAction] : @[];
+    for (JSAction *subaction in children) {
+        Target *target = configsController.currentConfig[subaction];
+        target.magnitude = mainAction.magnitude;
+        target.running = subaction.active;
+        if (target.running && target.isContinuous)
+            [self addRunningTarget:target];
+    }
+}
+
+- (void)showTargetForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
+    Joystick *js = [self findJoystickByRef:device];
+    JSAction *handler = [js handlerForEvent:value];
+    if (!handler)
+        return;
+    
+    [self expandRecursive:handler];
+    [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[outlineView rowForItem:handler]] byExtendingSelection: NO];
+    [targetController focusKey];
+}
+
 static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDValueRef value) {
     JoystickController *controller = (__bridge JoystickController *)ctx;
     IOHIDDeviceRef device = IOHIDQueueGetDevice(inSender);
     
-    Joystick *js = [controller findJoystickByRef:device];
     if (controller.sendingRealEvents) {
-        JSAction *mainAction = [js actionForEvent:value];
-        [mainAction notifyEvent:value];
-        NSArray *children = mainAction.children ? mainAction.children : mainAction ? @[mainAction] : @[];
-        for (JSAction *subaction in children) {
-            Target *target = controller.currentConfig[subaction];
-            target.magnitude = mainAction.magnitude;
-            target.running = subaction.active;
-            if (target.running && target.isContinuous)
-                [controller addRunningTarget:target];
-        }
-    } else if ([NSApplication sharedApplication].isActive
-               && [NSApplication sharedApplication].mainWindow.isVisible) {
-        JSAction *handler = [js handlerForEvent:value];
-        if (!handler)
-            return;
-        
-        [controller expandRecursive:handler];
-        [controller->outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[controller->outlineView rowForItem:handler]] byExtendingSelection: NO];
-        [controller->targetController focusKey];
+        [controller runTargetForDevice:device value:value];
+    } else if ([NSApplication sharedApplication].mainWindow.isVisible) {
+        [controller showTargetForDevice:device value:value];
     }
 }
 
@@ -96,13 +105,17 @@ static int findAvailableIndex(NSArray *list, Joystick *js) {
     }
 }
 
+- (void)addJoystickForDevice:(IOHIDDeviceRef)device {
+    IOHIDDeviceRegisterInputValueCallback(device, input_callback, (__bridge void*)self);
+    Joystick *js = [[Joystick alloc] initWithDevice:device];
+    js.index = findAvailableIndex(_joysticks, js);
+    [_joysticks addObject:js];
+    [outlineView reloadData];
+}
+
 static void add_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
     JoystickController *controller = (__bridge JoystickController *)ctx;
-    IOHIDDeviceRegisterInputValueCallback(device, input_callback, (__bridge void*)controller);
-    Joystick *js = [[Joystick alloc] initWithDevice:device];
-    js.index = findAvailableIndex(controller.joysticks, js);
-    [[controller joysticks] addObject:js];
-    [controller->outlineView reloadData];
+    [controller addJoystickForDevice:device];
 }
 
 - (Joystick *)findJoystickByRef:(IOHIDDeviceRef)device {
@@ -114,12 +127,17 @@ static void add_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDevi
 
 static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
     JoystickController *controller = (__bridge JoystickController *)ctx;
-    Joystick *match = [controller findJoystickByRef:device];
+    [controller removeJoystickForDevice:device];
+}
+
+- (void)removeJoystickForDevice:(IOHIDDeviceRef)device {
+    Joystick *match = [self findJoystickByRef:device];
     IOHIDDeviceRegisterInputValueCallback(device, NULL, NULL);
     if (match) {
-        [controller.joysticks removeObject:match];
-        [controller->outlineView reloadData];
+        [_joysticks removeObject:match];
+        [outlineView reloadData];
     }
+    
 }
 
 - (void)updateContinuousActions:(NSTimer *)timer {
@@ -166,10 +184,6 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD
     IOHIDManagerRegisterDeviceRemovalCallback(hidManager, remove_callback, (__bridge void *)self);
 }
 
-- (Config *)currentConfig {
-    return configsController.currentConfig;
-}
-
 - (JSAction *)selectedAction {
     id item = [outlineView itemAtRow:outlineView.selectedRow];
     return [item children] ? nil : item;