X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;h=50f9c295393fdf64e03269820b910c064155a431;hp=c87a6ee0ebff6f13e6a09f49ee21a5ef6032e979;hb=e2a4d830dd9817f6a515a3b1b6aa152d3bb98c2b;hpb=530009447c5bbd360ac5023979cffc6d32a28df3 diff --git a/ApplicationController.m b/ApplicationController.m index c87a6ee..50f9c29 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -4,82 +4,108 @@ // // Created by Sam McCall on 4/05/09. // -#include -@implementation ApplicationController +#import "ApplicationController.h" -@synthesize jsController, targetController, configsController; +#import "NJMapping.h" +#import "NJMappingsController.h" +#import "NJInputController.h" +#import "TargetController.h" +#import "NJEvents.h" -static BOOL active; - -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData); - --(void) applicationDidFinishLaunching: (NSNotification*) notification { - [jsController setup]; - [drawer open]; - [targetController setEnabled: false]; - [self setActive: NO]; - [configsController load]; - EventTypeSpec et; - et.eventClass = kEventClassApplication; - et.eventKind = kEventAppFrontSwitched; - EventHandlerUPP handler = NewEventHandlerUPP(appSwitch); - InstallApplicationEventHandler(handler, 1, &et, self, NULL); +@implementation ApplicationController { + BOOL active; } --(void) applicationWillTerminate: (NSNotification *)aNotification { - [configsController save]; +- (void)didSwitchApplication:(NSNotification *)notification { + NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; + [self.mappingsController activateMappingForProcess:currentApp.localizedName]; } -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication - hasVisibleWindows:(BOOL)flag -{ - [mainWindow makeKeyAndOrderFront:self]; - return YES; +- (void)applicationDidFinishLaunching:(NSNotification *)notification { + [drawer open]; + self.targetController.enabled = NO; + [self.inputController setup]; + [self.mappingsController load]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(mappingDidChange:) + name:NJEventMappingChanged + object:nil]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(eventTranslationActivated:) + name:NJEventTranslationActivated + object:nil]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(eventTranslationDeactivated:) + name:NJEventTranslationDeactivated + object:nil]; } -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) { - ApplicationController* self = (ApplicationController*)userData; - NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication]; - ProcessSerialNumber psn; - psn.lowLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue]; - psn.highLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue]; - [self->configsController applicationSwitchedTo: [currentApp objectForKey:@"NSApplicationName"] withPsn: psn]; - return noErr; +- (void)applicationWillTerminate:(NSNotification *)aNotification { + [NSUserDefaults.standardUserDefaults synchronize]; } --(BOOL) active { - return active; +- (void)eventTranslationActivated:(NSNotification *)note { + activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"]; + activeMenuItem.state = [note.object translatingEvents]; + [NSWorkspace.sharedWorkspace.notificationCenter + addObserver:self + selector:@selector(didSwitchApplication:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + NSLog(@"Listening for application changes."); } --(void) setActive: (BOOL) newActive { - [activeButton setLabel: (newActive ? @"Stop" : @"Start")]; - [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]]; - [activeMenuItem setState: (newActive ? 1 : 0)]; - active = newActive; +- (void)eventTranslationDeactivated:(NSNotification *)note { + activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"]; + activeMenuItem.state = [note.object translatingEvents]; + [NSWorkspace.sharedWorkspace.notificationCenter + removeObserver:self + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + NSLog(@"Ignoring application changes."); } --(IBAction) toggleActivity: (id)sender { - [self setActive: ![self active]]; +- (IBAction)toggleActivity:(id)sender { + self.inputController.translatingEvents = !self.inputController.translatingEvents; } --(void) configsChanged { - while([dockMenuBase numberOfItems] > 2) - [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)]; +- (NSInteger)firstMappingMenuIndex { + for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i) + if ([dockMenuBase itemAtIndex:i].isSeparatorItem) + return i + 1; + return dockMenuBase.numberOfItems; +} - for(Config* config in [configsController configs]) { - [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""]; - } - [self configChanged]; +- (void)mappingsChanged { + NSInteger removeFrom = self.firstMappingMenuIndex; + while (dockMenuBase.numberOfItems > removeFrom) + [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1]; + int added = 0; + for (NJMapping *mapping in self.mappingsController.mappings) { + NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @""; + [dockMenuBase addItemWithTitle:mapping.name + action:@selector(chooseMapping:) + keyEquivalent:keyEquiv]; + + } + [_targetController refreshMappings]; } --(void) configChanged { - Config* current = [configsController currentConfig]; - NSArray* configs = [configsController configs]; - for(int i=0; i<[configs count]; i++) - [[dockMenuBase itemAtIndex: (2+i)] setState: (([configs objectAtIndex:i] == current) ? YES : NO)]; + +- (void)mappingDidChange:(NSNotification *)note { + NSInteger firstMapping = self.firstMappingMenuIndex; + NJMapping *current = note.object; + NSArray *mappings = self.mappingsController.mappings; + for (NSUInteger i = 0; i < mappings.count; ++i) + [dockMenuBase itemAtIndex:i + firstMapping].state = mappings[i] == current; } --(void) chooseConfig: (id) sender { - [configsController activateConfig: [[configsController configs] objectAtIndex: ([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL]; +- (void)chooseMapping:(id)sender { + NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstMappingMenuIndex; + NJMapping *chosen = self.mappingsController.mappings[idx]; + [_mappingsController activateMapping:chosen]; } @end