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)handler {
40 [self expandRecursive:[handler base]];
41 [outlineView expandItem:handler];
44 - (void)addRunningTarget:(Target *)target {
45 if (![runningTargets containsObject:target]) {
46 [runningTargets addObject:target];
48 if (!continuousTimer) {
49 continuousTimer = [NSTimer scheduledTimerWithTimeInterval:1.f/60.f
51 selector:@selector(updateContinuousActions:)
54 NSLog(@"Scheduled continuous target timer.");
58 - (void)runTargetForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
59 Joystick *js = [self findJoystickByRef:device];
60 JSAction *mainAction = [js actionForEvent:value];
61 [mainAction notifyEvent:value];
62 NSArray *children = mainAction.children ? mainAction.children : mainAction ? @[mainAction] : @[];
63 for (JSAction *subaction in children) {
64 Target *target = configsController.currentConfig[subaction];
65 target.magnitude = mainAction.magnitude;
66 target.running = subaction.active;
67 if (target.running && target.isContinuous)
68 [self addRunningTarget:target];
72 - (void)showTargetForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
73 Joystick *js = [self findJoystickByRef:device];
74 JSAction *handler = [js handlerForEvent:value];
78 [self expandRecursive:handler];
79 [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[outlineView rowForItem:handler]] byExtendingSelection: NO];
80 [targetController focusKey];
83 static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDValueRef value) {
84 JoystickController *controller = (__bridge JoystickController *)ctx;
85 IOHIDDeviceRef device = IOHIDQueueGetDevice(inSender);
87 if (controller.sendingRealEvents) {
88 [controller runTargetForDevice:device value:value];
89 } else if ([NSApplication sharedApplication].mainWindow.isVisible) {
90 [controller showTargetForDevice:device value:value];
94 static int findAvailableIndex(NSArray *list, Joystick *js) {
95 for (int index = 1; ; index++) {
97 for (Joystick *used in list) {
98 if ([used.productName isEqualToString:js.productName] && used.index == index) {
108 - (void)addJoystickForDevice:(IOHIDDeviceRef)device {
109 IOHIDDeviceRegisterInputValueCallback(device, input_callback, (__bridge void*)self);
110 Joystick *js = [[Joystick alloc] initWithDevice:device];
111 js.index = findAvailableIndex(_joysticks, js);
112 [_joysticks addObject:js];
113 [outlineView reloadData];
116 static void add_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
117 JoystickController *controller = (__bridge JoystickController *)ctx;
118 [controller addJoystickForDevice:device];
121 - (Joystick *)findJoystickByRef:(IOHIDDeviceRef)device {
122 for (Joystick *js in _joysticks)
123 if (js.device == device)
128 static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
129 JoystickController *controller = (__bridge JoystickController *)ctx;
130 [controller removeJoystickForDevice:device];
133 - (void)removeJoystickForDevice:(IOHIDDeviceRef)device {
134 Joystick *match = [self findJoystickByRef:device];
135 IOHIDDeviceRegisterInputValueCallback(device, NULL, NULL);
137 [_joysticks removeObject:match];
138 [outlineView reloadData];
143 - (void)updateContinuousActions:(NSTimer *)timer {
144 self.mouseLoc = [NSEvent mouseLocation];
145 for (Target *target in [runningTargets copy]) {
146 if (![target update:self]) {
147 [runningTargets removeObject:target];
150 if (!runningTargets.count) {
151 [continuousTimer invalidate];
152 continuousTimer = nil;
153 NSLog(@"Unscheduled continuous target timer.");
157 #define NSSTR(e) ((NSString *)CFSTR(e))
160 hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
161 NSArray *criteria = @[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
162 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_Joystick) },
163 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
164 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_GamePad) },
165 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
166 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_MultiAxisController) }
168 IOHIDManagerSetDeviceMatchingMultiple(hidManager, (__bridge CFArrayRef)criteria);
170 IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
171 IOReturn ret = IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone);
172 if (ret != kIOReturnSuccess) {
173 [[NSAlert alertWithMessageText:@"Input devices are unavailable"
177 informativeTextWithFormat:@"Error 0x%08x occured trying to access your devices. "
178 @"Input may not be correctly detected or mapped.",
180 beginSheetModalForWindow:outlineView.window
186 IOHIDManagerRegisterDeviceMatchingCallback(hidManager, add_callback, (__bridge void *)self);
187 IOHIDManagerRegisterDeviceRemovalCallback(hidManager, remove_callback, (__bridge void *)self);
190 - (JSAction *)selectedAction {
191 id item = [outlineView itemAtRow:outlineView.selectedRow];
192 return [item children] ? nil : item;
195 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
196 return item ? [[item children] count] : _joysticks.count;
199 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
200 return item ? [[item children] count] > 0: YES;
203 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
204 return item ? [item children][index] : _joysticks[index];
207 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
213 - (void)outlineViewSelectionDidChange:(NSNotification *)notification {
214 [targetController loadCurrent];