-void input_callback(void* inContext, IOReturn inResult, void* inSender, IOHIDValueRef value) {
- JoystickController* self = (JoystickController*)inContext;
- IOHIDDeviceRef device = IOHIDQueueGetDevice((IOHIDQueueRef) inSender);
-
- Joystick* js = [self findJoystickByRef: device];
- if([[[NSApplication sharedApplication] delegate] active]) {
- // for reals
- JSAction* mainAction = [js actionForEvent: value];
- if(!mainAction)
- return;
-
- [mainAction notifyEvent: value];
- NSArray* subactions = [mainAction subActions];
- if(!subactions)
- subactions = [NSArray arrayWithObject:mainAction];
- for(id subaction in subactions) {
- Target* target = [[self->configsController currentConfig] getTargetForAction:subaction];
- if(!target)
- continue;
- /* target application? doesn't seem to be any need since we are only active when it's in front */
- /* might be required for some strange actions */
- if ([target running] != [subaction active]) {
- if ([subaction active]) {
- [target trigger: self];
- }
- else {
- [target untrigger: self];
- }
- [target setRunning: [subaction active]];
- }
-
- if ([mainAction isKindOfClass: [JSActionAnalog class]]) {
- double realValue = [(JSActionAnalog*)mainAction getRealValue: IOHIDValueGetIntegerValue(value)];
- [target setInputValue: realValue];
-
- // Add to list of running targets
- if ([target isContinuous] && [target running]) {
- if (!objInArray([self runningTargets], target)) {
- [[self runningTargets] addObject: target];
- }
- }
- }
- }
- } else if([[NSApplication sharedApplication] isActive] && [[[NSApplication sharedApplication]mainWindow]isVisible]) {
- // joysticks not active, use it to select stuff
- id handler = [js handlerForEvent: value];
- if(!handler)
- return;
-
- [self expandRecursive: handler];
- self->programmaticallySelecting = YES;
- [self->outlineView selectRowIndexes: [NSIndexSet indexSetWithIndex: [self->outlineView rowForItem: handler]] byExtendingSelection: NO];
- }
+- (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];