From bda2aa3a8897f574b97c0bd125e84374087017a9 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Wed, 13 Mar 2013 12:56:47 +0100 Subject: [PATCH] Restore application if an error occurs. --- Classes/EnjoyableApplicationDelegate.m | 4 ++++ Classes/NJDeviceController.m | 24 ++++++++++++++++++------ Classes/NJHIDManager.m | 1 + Info.plist | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Classes/EnjoyableApplicationDelegate.m b/Classes/EnjoyableApplicationDelegate.m index 6ccfc0f..f151399 100644 --- a/Classes/EnjoyableApplicationDelegate.m +++ b/Classes/EnjoyableApplicationDelegate.m @@ -67,6 +67,10 @@ object:self]; } +- (void)applicationWillBecomeActive:(NSNotification *)notification { + [self restoreToForeground:notification]; +} + - (void)transformIntoElement:(id)sender { ProcessSerialNumber psn = { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToUIElementApplication); diff --git a/Classes/NJDeviceController.m b/Classes/NJDeviceController.m index 610c07b..2f1736b 100644 --- a/Classes/NJDeviceController.m +++ b/Classes/NJDeviceController.m @@ -210,11 +210,23 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) { } - (void)hidManager:(NJHIDManager *)manager didError:(NSError *)error { - [outlineView.window presentError:error - modalForWindow:outlineView.window - delegate:nil - didPresentSelector:nil - contextInfo:nil]; + // Since the error shows the window, it can trigger another attempt + // to re-open the HID manager, which will also probably fail and error, + // so don't bother repeating ourselves. + if (!outlineView.window.attachedSheet) { + [NSApplication.sharedApplication activateIgnoringOtherApps:YES]; + [outlineView.window makeKeyAndOrderFront:nil]; + [outlineView.window presentError:error + modalForWindow:outlineView.window + delegate:nil + didPresentSelector:nil + contextInfo:nil]; + } + self.translatingEvents = NO; + if (manager.running) + [self hidManagerDidStart:manager]; + else + [self hidManagerDidStop:manager]; } - (void)hidManagerDidStart:(NJHIDManager *)manager { @@ -313,7 +325,7 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn if (!translatingEvents && !NSApplication.sharedApplication.isActive) [self stopHid]; - else if (translatingEvents || NSApplication.sharedApplication.isActive) + else [self startHid]; } } diff --git a/Classes/NJHIDManager.m b/Classes/NJHIDManager.m index 2a139df..5245bf5 100644 --- a/Classes/NJHIDManager.m +++ b/Classes/NJHIDManager.m @@ -55,6 +55,7 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD IOHIDManagerClose(manager, kIOHIDOptionsTypeNone); CFRelease(manager); [self.delegate hidManager:self didError:error]; + NSLog(@"Error starting HID manager: %@.", error); } else { _manager = manager; IOHIDManagerScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); diff --git a/Info.plist b/Info.plist index 04d4b4b..be1810b 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 228 + 244 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.20.1