From: Joe Wreschnig Date: Tue, 26 Feb 2013 18:57:43 +0000 (+0100) Subject: Switch from kEventAppFrontSwitched (Carbon) to NSWorkspaceDidActivateApplicationNotif... X-Git-Tag: version-1.0~116 X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=commitdiff_plain;h=9ba68ad5a4378bd897fc611929f201681bc71c16 Switch from kEventAppFrontSwitched (Carbon) to NSWorkspaceDidActivateApplicationNotification (Cocoa) to detect application switching. --- diff --git a/ApplicationController.m b/ApplicationController.m index 2edac94..55abf05 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -4,31 +4,44 @@ // // Created by Sam McCall on 4/05/09. // -#include -@implementation ApplicationController +@implementation ApplicationController { + BOOL active; +} @synthesize jsController, targetController, configsController; -static BOOL active; - -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData); +- (void)didSwitchApplication:(NSNotification *)notification { + NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; + ProcessSerialNumber psn; + OSStatus err; + if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) { + [self->configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn]; + } else { + NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil]; + NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error); + } +} --(void) applicationDidFinishLaunching: (NSNotification*) notification { +-(void) applicationDidFinishLaunching:(NSNotification*) notification { [jsController setup]; [drawer open]; - [targetController setEnabled: false]; - [self setActive: NO]; + [targetController setEnabled: NO]; + self.active = NO; [configsController load]; - EventTypeSpec et; - et.eventClass = kEventClassApplication; - et.eventKind = kEventAppFrontSwitched; - EventHandlerUPP handler = NewEventHandlerUPP(appSwitch); - InstallApplicationEventHandler(handler, 1, &et, (void *)CFBridgingRetain(self), NULL); + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(didSwitchApplication:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; } -(void) applicationWillTerminate: (NSNotification *)aNotification { [configsController save]; + [[[NSWorkspace sharedWorkspace] notificationCenter] + removeObserver:self + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; } - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication @@ -38,29 +51,20 @@ pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void return YES; } -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) { - ApplicationController* self = (__bridge ApplicationController*)userData; - NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication]; - ProcessSerialNumber psn; - psn.lowLongOfPSN = [currentApp[@"NSApplicationProcessSerialNumberLow"] longValue]; - psn.highLongOfPSN = [currentApp[@"NSApplicationProcessSerialNumberHigh"] longValue]; - [self->configsController applicationSwitchedTo: currentApp[@"NSApplicationName"] withPsn: psn]; - return noErr; -} - --(BOOL) active { - return active; +- (BOOL)active { + return active; } --(void) setActive: (BOOL) newActive { - [activeButton setLabel: (newActive ? @"Stop" : @"Start")]; - [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]]; - [activeMenuItem setState: (newActive ? 1 : 0)]; +- (void)setActive:(BOOL)newActive { + [activeButton setLabel:newActive ? @"Stop" : @"Start"]; + NSImage *buttonImage = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; + [activeButton setImage:buttonImage]; + [activeMenuItem setState:newActive]; active = newActive; } --(IBAction) toggleActivity: (id)sender { - [self setActive: ![self active]]; +- (IBAction)toggleActivity:(id)sender { + self.active = !self.active; } -(void) configsChanged { @@ -76,7 +80,7 @@ pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void Config* current = [configsController currentConfig]; NSArray* configs = [configsController configs]; for(int i=0; i<[configs count]; i++) - [[dockMenuBase itemAtIndex: (2+i)] setState: ((configs[i] == current) ? YES : NO)]; + [[dockMenuBase itemAtIndex: (2+i)] setState: (configs[i] == current)]; } -(void) chooseConfig: (id) sender {