Big rename part 2: 'config' etc. to 'mapping.
[enjoyable.git] / ApplicationController.m
index 8fed5d6..50f9c29 100644 (file)
 
 #import "ApplicationController.h"
 
-#import "Config.h"
-#import "ConfigsController.h"
-#import "JoystickController.h"
+#import "NJMapping.h"
+#import "NJMappingsController.h"
+#import "NJInputController.h"
 #import "TargetController.h"
+#import "NJEvents.h"
 
 @implementation ApplicationController {
     BOOL active;
 }
 
-@synthesize jsController;
-@synthesize targetController;
-@synthesize configsController;
-
 - (void)didSwitchApplication:(NSNotification *)notification {
     NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
-    [self.configsController activateConfigForProcess:currentApp.localizedName];
+    [self.mappingsController activateMappingForProcess:currentApp.localizedName];
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
     [drawer open];
     self.targetController.enabled = NO;
-    self.active = NO;
-    [self.jsController setup];
-    [self.configsController load];
-    [[NSWorkspace sharedWorkspace].notificationCenter
+    [self.inputController setup];
+    [self.mappingsController load];
+    [NSNotificationCenter.defaultCenter
      addObserver:self
-     selector:@selector(didSwitchApplication:)
-     name:NSWorkspaceDidActivateApplicationNotification
+     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 {
-    // TODO: Save immediately / shortly after changing and then enable
-    // sudden termination support.
-    [configsController save];
-    [[NSWorkspace sharedWorkspace].notificationCenter
-     removeObserver:self
-     name:NSWorkspaceDidActivateApplicationNotification
-     object:nil];
+       [NSUserDefaults.standardUserDefaults synchronize];
 }
 
-// TODO: Active state should probably be in the ConfigsController or
-// JoystickController, not here.
-
-- (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.label = newActive ? @"Stop" : @"Start";
-    activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
-    activeMenuItem.state = newActive;
-    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.active = !self.active;
+    self.inputController.translatingEvents = !self.inputController.translatingEvents;
 }
 
-- (NSUInteger)firstConfigMenuIndex {
-    NSUInteger count = dockMenuBase.numberOfItems;
-    for (int i = 0; i < count; ++i)
+- (NSInteger)firstMappingMenuIndex {
+    for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
         if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
             return i + 1;
-    return count;
+    return dockMenuBase.numberOfItems;
 }
 
-- (void)configsChanged {
-    NSUInteger removeFrom = [self firstConfigMenuIndex];
+- (void)mappingsChanged {
+    NSInteger removeFrom = self.firstMappingMenuIndex;
     while (dockMenuBase.numberOfItems > removeFrom)
         [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
-    for (Config *config in self.configsController.configs)
-        [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
-    [self configChanged];
+    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 {
-    NSUInteger firstConfig = [self firstConfigMenuIndex];
-    Config *current = self.configsController.currentConfig;
-    NSArray *configs = self.configsController.configs;
-    for (int i = 0; i < configs.count; ++i)
-        [dockMenuBase itemAtIndex:i + firstConfig].state = 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 {
-    int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
-    Config *chosen = self.configsController.configs[idx];
-    [configsController activateConfig:chosen];
+- (void)chooseMapping:(id)sender {
+    NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstMappingMenuIndex;
+    NJMapping *chosen = self.mappingsController.mappings[idx];
+    [_mappingsController activateMapping:chosen];
 }
 @end