2 // JoystickController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "JoystickController.h"
11 #import "ConfigsController.h"
15 #import "TargetController.h"
17 @implementation JoystickController {
18 IOHIDManagerRef hidManager;
19 NSTimer *continuousTimer;
20 NSMutableArray *runningTargets;
21 NSMutableArray *_joysticks;
25 if ((self = [super init])) {
26 _joysticks = [[NSMutableArray alloc] initWithCapacity:16];
27 runningTargets = [[NSMutableArray alloc] initWithCapacity:32];
33 [continuousTimer invalidate];
34 IOHIDManagerClose(hidManager, kIOHIDOptionsTypeNone);
35 CFRelease(hidManager);
38 - (void)expandRecursive:(id <NJActionPathElement>)pathElement {
40 [self expandRecursive:pathElement.base];
41 [outlineView expandItem:pathElement];
45 - (void)addRunningTarget:(Target *)target {
46 if (![runningTargets containsObject:target]) {
47 [runningTargets addObject:target];
49 if (!continuousTimer) {
50 continuousTimer = [NSTimer scheduledTimerWithTimeInterval:1.f/60.f
52 selector:@selector(updateContinuousActions:)
55 NSLog(@"Scheduled continuous target timer.");
59 - (void)runTargetForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
60 Joystick *js = [self findJoystickByRef:device];
61 JSAction *mainAction = [js actionForEvent:value];
62 [mainAction notifyEvent:value];
63 NSArray *children = mainAction.children ? mainAction.children : mainAction ? @[mainAction] : @[];
64 for (JSAction *subaction in children) {
65 Target *target = configsController.currentConfig[subaction];
66 target.magnitude = mainAction.magnitude;
67 target.running = subaction.active;
68 if (target.running && target.isContinuous)
69 [self addRunningTarget:target];
73 - (void)showTargetForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
74 Joystick *js = [self findJoystickByRef:device];
75 JSAction *handler = [js handlerForEvent:value];
79 [self expandRecursive:handler];
80 [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[outlineView rowForItem:handler]] byExtendingSelection: NO];
81 [targetController focusKey];
84 static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDValueRef value) {
85 JoystickController *controller = (__bridge JoystickController *)ctx;
86 IOHIDDeviceRef device = IOHIDQueueGetDevice(inSender);
88 if (controller.sendingRealEvents) {
89 [controller runTargetForDevice:device value:value];
90 } else if ([NSApplication sharedApplication].mainWindow.isVisible) {
91 [controller showTargetForDevice:device value:value];
95 static int findAvailableIndex(NSArray *list, Joystick *js) {
96 for (int index = 1; ; index++) {
98 for (Joystick *used in list) {
99 if ([used.productName isEqualToString:js.productName] && used.index == index) {
109 - (void)addJoystickForDevice:(IOHIDDeviceRef)device {
110 IOHIDDeviceRegisterInputValueCallback(device, input_callback, (__bridge void*)self);
111 Joystick *js = [[Joystick alloc] initWithDevice:device];
112 js.index = findAvailableIndex(_joysticks, js);
113 [_joysticks addObject:js];
114 [outlineView reloadData];
117 static void add_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
118 JoystickController *controller = (__bridge JoystickController *)ctx;
119 [controller addJoystickForDevice:device];
122 - (Joystick *)findJoystickByRef:(IOHIDDeviceRef)device {
123 for (Joystick *js in _joysticks)
124 if (js.device == device)
129 static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
130 JoystickController *controller = (__bridge JoystickController *)ctx;
131 [controller removeJoystickForDevice:device];
134 - (void)removeJoystickForDevice:(IOHIDDeviceRef)device {
135 Joystick *match = [self findJoystickByRef:device];
136 IOHIDDeviceRegisterInputValueCallback(device, NULL, NULL);
138 [_joysticks removeObject:match];
139 [outlineView reloadData];
144 - (void)updateContinuousActions:(NSTimer *)timer {
145 self.mouseLoc = [NSEvent mouseLocation];
146 for (Target *target in [runningTargets copy]) {
147 if (![target update:self]) {
148 [runningTargets removeObject:target];
151 if (!runningTargets.count) {
152 [continuousTimer invalidate];
153 continuousTimer = nil;
154 NSLog(@"Unscheduled continuous target timer.");
158 #define NSSTR(e) ((NSString *)CFSTR(e))
161 hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
162 NSArray *criteria = @[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
163 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_Joystick) },
164 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
165 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_GamePad) },
166 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
167 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_MultiAxisController) }
169 IOHIDManagerSetDeviceMatchingMultiple(hidManager, (__bridge CFArrayRef)criteria);
171 IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
172 IOReturn ret = IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone);
173 if (ret != kIOReturnSuccess) {
174 [[NSAlert alertWithMessageText:@"Input devices are unavailable"
178 informativeTextWithFormat:@"Error 0x%08x occured trying to access your devices. "
179 @"Input may not be correctly detected or mapped.",
181 beginSheetModalForWindow:outlineView.window
187 IOHIDManagerRegisterDeviceMatchingCallback(hidManager, add_callback, (__bridge void *)self);
188 IOHIDManagerRegisterDeviceRemovalCallback(hidManager, remove_callback, (__bridge void *)self);
191 - (JSAction *)selectedAction {
192 id <NJActionPathElement> item = [outlineView itemAtRow:outlineView.selectedRow];
193 return (!item.children && item.base) ? item : nil;
196 - (NSInteger)outlineView:(NSOutlineView *)outlineView
197 numberOfChildrenOfItem:(id <NJActionPathElement>)item {
198 return item ? item.children.count : _joysticks.count;
201 - (BOOL)outlineView:(NSOutlineView *)outlineView
202 isItemExpandable:(id <NJActionPathElement>)item {
203 return item ? [[item children] count] > 0: YES;
206 - (id)outlineView:(NSOutlineView *)outlineView
207 child:(NSInteger)index
208 ofItem:(id <NJActionPathElement>)item {
209 return item ? item.children[index] : _joysticks[index];
212 - (id)outlineView:(NSOutlineView *)outlineView
213 objectValueForTableColumn:(NSTableColumn *)tableColumn
214 byItem:(id <NJActionPathElement>)item {
215 return item ? item.name : @"root";
218 - (void)outlineViewSelectionDidChange:(NSNotification *)notification {
219 [targetController loadCurrent];