From dd1f684886c2809133356bb9b335a35293e8849e Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 28 Feb 2013 19:02:21 +0100 Subject: [PATCH] Show an error message if opening input devices fail. Move real vs. configuration state onto JoystickController. --- ApplicationController.h | 1 - ApplicationController.m | 21 +++++---------------- JoystickController.h | 1 + JoystickController.m | 17 +++++++++++++---- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/ApplicationController.h b/ApplicationController.h index 6bb37b9..f1a3649 100644 --- a/ApplicationController.h +++ b/ApplicationController.h @@ -21,7 +21,6 @@ @property (strong) IBOutlet JoystickController *jsController; @property (strong) IBOutlet TargetController *targetController; @property (strong) IBOutlet ConfigsController *configsController; -@property (assign) BOOL active; - (IBAction)toggleActivity:(id)sender; - (void)configsChanged; diff --git a/ApplicationController.m b/ApplicationController.m index 2b122b6..aa51eeb 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -28,7 +28,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification { [drawer open]; self.targetController.enabled = NO; - self.active = NO; [self.jsController setup]; [self.configsController load]; [[NSWorkspace sharedWorkspace].notificationCenter @@ -46,22 +45,12 @@ object:nil]; } -// TODO: Active state should probably be in the ConfigsController or -// JoystickController, not here. - -- (BOOL)active { - return active; -} - -- (void)setActive:(BOOL)newActive { - activeButton.label = newActive ? @"Stop" : @"Start"; - activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; - activeMenuItem.state = newActive; - active = newActive; -} - - (IBAction)toggleActivity:(id)sender { - self.active = !self.active; + BOOL sendRealEvents = !self.jsController.sendingRealEvents; + self.jsController.sendingRealEvents = sendRealEvents; + activeButton.label = sendRealEvents ? @"Stop" : @"Start"; + activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; + activeMenuItem.state = sendRealEvents; } - (NSUInteger)firstConfigMenuIndex { diff --git a/JoystickController.h b/JoystickController.h index c67a7c1..5e46256 100644 --- a/JoystickController.h +++ b/JoystickController.h @@ -26,5 +26,6 @@ @property (readonly) NSMutableArray *joysticks; @property (assign) NSPoint mouseLoc; @property (assign) BOOL frontWindowOnly; +@property (assign) BOOL sendingRealEvents; @end diff --git a/JoystickController.m b/JoystickController.m index 6acc545..ed941ed 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" @@ -25,6 +24,7 @@ @synthesize selectedAction; @synthesize frontWindowOnly; @synthesize mouseLoc; +@synthesize sendingRealEvents; - (id)init { if ((self = [super init])) { @@ -65,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] : @[]; @@ -78,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; @@ -158,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); -- 2.30.2