Clean up save/restore. Write preferences immediately but continue relying on automati...
[enjoyable.git] / ApplicationController.m
index e87421c..2b122b6 100644 (file)
@@ -5,6 +5,13 @@
 //  Created by Sam McCall on 4/05/09.
 //
 
+#import "ApplicationController.h"
+
+#import "Config.h"
+#import "ConfigsController.h"
+#import "JoystickController.h"
+#import "TargetController.h"
+
 @implementation ApplicationController {
     BOOL active;
 }
 
 - (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.configsController activateConfigForProcess:currentApp.localizedName];
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
@@ -39,9 +39,7 @@
 }
 
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
-    // TODO: Save immediately / shortly after changing and then enable
-    // sudden termination support.
-    [configsController save];
+       [[NSUserDefaults standardUserDefaults] synchronize];
     [[NSWorkspace sharedWorkspace].notificationCenter
      removeObserver:self
      name:NSWorkspaceDidActivateApplicationNotification
@@ -94,6 +92,6 @@
 - (void)chooseConfig:(id)sender {
     int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
     Config *chosen = self.configsController.configs[idx];
-    [configsController activateConfig:chosen forApplication:NULL];
+    [configsController activateConfig:chosen];
 }
 @end