X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=JoystickController.m;h=13db938c8d33febd60669a0dd10104111314230d;hp=0675075f158545ed1fba58d7e7cd7b2e95cc964e;hb=320e3065d251e11370e571df5705675937b76521;hpb=08c920c47d68423c16a6deba2a59488772066f6b diff --git a/JoystickController.m b/JoystickController.m index 0675075..13db938 100644 --- a/JoystickController.m +++ b/JoystickController.m @@ -7,7 +7,6 @@ #import "JoystickController.h" -#import "ApplicationController.h" #import "Config.h" #import "ConfigsController.h" #import "Joystick.h" @@ -18,13 +17,14 @@ @implementation JoystickController { IOHIDManagerRef hidManager; NSTimer *continuousTimer; + NSMutableArray *runningTargets; } @synthesize joysticks; -@synthesize runningTargets; @synthesize selectedAction; @synthesize frontWindowOnly; @synthesize mouseLoc; +@synthesize sendingRealEvents; - (id)init { if ((self = [super init])) { @@ -47,8 +47,9 @@ } - (void)addRunningTarget:(Target *)target { - if (![runningTargets containsObject:target]) + if (![runningTargets containsObject:target]) { [runningTargets addObject:target]; + } if (!continuousTimer) { continuousTimer = [NSTimer scheduledTimerWithTimeInterval:1.f/60.f target:self @@ -64,7 +65,7 @@ static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDVa IOHIDDeviceRef device = IOHIDQueueGetDevice(inSender); Joystick *js = [controller findJoystickByRef:device]; - if (((ApplicationController *)[NSApplication sharedApplication].delegate).active) { + if (controller.sendingRealEvents) { JSAction *mainAction = [js actionForEvent:value]; [mainAction notifyEvent:value]; NSArray *children = mainAction.children ? mainAction.children : mainAction ? @[mainAction] : @[]; @@ -77,7 +78,6 @@ static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDVa } } else if ([NSApplication sharedApplication].isActive && [NSApplication sharedApplication].mainWindow.isVisible) { - // joysticks not active, use it to select stuff JSAction *handler = [js handlerForEvent:value]; if (!handler) return; @@ -130,11 +130,13 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD - (void)updateContinuousActions:(NSTimer *)timer { self.mouseLoc = [NSEvent mouseLocation]; - for (Target *target in [self.runningTargets copy]) { - if (![target update:self]) - [self.runningTargets removeObject:target]; + for (Target *target in [runningTargets copy]) { + if (![target update:self]) { + [runningTargets removeObject:target]; + NSLog(@"Removing action, now running %lu.", runningTargets.count); + } } - if (!self.runningTargets.count) { + if (!runningTargets.count) { [continuousTimer invalidate]; continuousTimer = nil; NSLog(@"Unscheduled continuous target timer."); @@ -155,7 +157,17 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD IOHIDManagerSetDeviceMatchingMultiple(hidManager, (__bridge CFArrayRef)criteria); IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone); // FIXME: If an error happens, report it! + IOReturn ret = IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone); + if (ret != kIOReturnSuccess) { + [[NSAlert alertWithMessageText:@"Input devices are unavailable" + defaultButton:nil + alternateButton:nil + otherButton:nil + informativeTextWithFormat:@"Error 0x%08x occured trying to access your devices. " + @"Input may not be correctly detected or mapped.", + ret] + runModal]; + } IOHIDManagerRegisterDeviceMatchingCallback(hidManager, add_callback, (__bridge void *)self); IOHIDManagerRegisterDeviceRemovalCallback(hidManager, remove_callback, (__bridge void *)self);