From 03b4a589de71a49ad00450701630673631e88647 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Fri, 1 Mar 2013 02:27:59 +0100 Subject: [PATCH] Move logic into messages rather than directly in the callback functions. --- JoystickController.h | 3 -- JoystickController.m | 78 ++++++++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/JoystickController.h b/JoystickController.h index 5e46256..4da83cd 100644 --- a/JoystickController.h +++ b/JoystickController.h @@ -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; diff --git a/JoystickController.m b/JoystickController.m index 97ca74b..cd513de 100644 --- a/JoystickController.m +++ b/JoystickController.m @@ -18,6 +18,7 @@ IOHIDManagerRef hidManager; NSTimer *continuousTimer; NSMutableArray *runningTargets; + NSMutableArray *_joysticks; } - (id)init { @@ -54,31 +55,39 @@ } } +- (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; -- 2.30.2