X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;h=50f9c295393fdf64e03269820b910c064155a431;hp=55abf057e250e9475ded859c096f231d83eede1a;hb=e2a4d830dd9817f6a515a3b1b6aa152d3bb98c2b;hpb=9ba68ad5a4378bd897fc611929f201681bc71c16 diff --git a/ApplicationController.m b/ApplicationController.m index 55abf05..50f9c29 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -5,85 +5,107 @@ // Created by Sam McCall on 4/05/09. // +#import "ApplicationController.h" + +#import "NJMapping.h" +#import "NJMappingsController.h" +#import "NJInputController.h" +#import "TargetController.h" +#import "NJEvents.h" + @implementation ApplicationController { BOOL active; } -@synthesize jsController, targetController, configsController; - - (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); - } + [self.mappingsController activateMappingForProcess:currentApp.localizedName]; } --(void) applicationDidFinishLaunching:(NSNotification*) notification { - [jsController setup]; - [drawer open]; - [targetController setEnabled: NO]; - self.active = NO; - [configsController load]; - [[[NSWorkspace sharedWorkspace] notificationCenter] +- (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]; +} + +- (void)applicationWillTerminate:(NSNotification *)aNotification { + [NSUserDefaults.standardUserDefaults synchronize]; +} + +- (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) applicationWillTerminate: (NSNotification *)aNotification { - [configsController save]; - [[[NSWorkspace sharedWorkspace] notificationCenter] +- (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."); } -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication - hasVisibleWindows:(BOOL)flag -{ - [mainWindow makeKeyAndOrderFront:self]; - return YES; -} - -- (BOOL)active { - return active; +- (IBAction)toggleActivity:(id)sender { + self.inputController.translatingEvents = !self.inputController.translatingEvents; } -- (void)setActive:(BOOL)newActive { - [activeButton setLabel:newActive ? @"Stop" : @"Start"]; - NSImage *buttonImage = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; - [activeButton setImage:buttonImage]; - [activeMenuItem setState:newActive]; - active = newActive; +- (NSInteger)firstMappingMenuIndex { + for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i) + if ([dockMenuBase itemAtIndex:i].isSeparatorItem) + return i + 1; + return dockMenuBase.numberOfItems; } -- (IBAction)toggleActivity:(id)sender { - self.active = !self.active; +- (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) configsChanged { - while([dockMenuBase numberOfItems] > 2) - [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)]; - - for(Config* config in [configsController configs]) { - [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""]; - } - [self configChanged]; -} --(void) configChanged { - Config* current = [configsController currentConfig]; - NSArray* configs = [configsController configs]; - for(int i=0; i<[configs count]; i++) - [[dockMenuBase itemAtIndex: (2+i)] setState: (configs[i] == current)]; +- (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][([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