Show an error message if opening input devices fail. Move real vs. configuration...
[enjoyable.git] / ApplicationController.m
1 //
2 // ApplicationController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "ApplicationController.h"
9
10 #import "Config.h"
11 #import "ConfigsController.h"
12 #import "JoystickController.h"
13 #import "TargetController.h"
14
15 @implementation ApplicationController {
16 BOOL active;
17 }
18
19 @synthesize jsController;
20 @synthesize targetController;
21 @synthesize configsController;
22
23 - (void)didSwitchApplication:(NSNotification *)notification {
24 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
25 [self.configsController activateConfigForProcess:currentApp.localizedName];
26 }
27
28 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
29 [drawer open];
30 self.targetController.enabled = NO;
31 [self.jsController setup];
32 [self.configsController load];
33 [[NSWorkspace sharedWorkspace].notificationCenter
34 addObserver:self
35 selector:@selector(didSwitchApplication:)
36 name:NSWorkspaceDidActivateApplicationNotification
37 object:nil];
38 }
39
40 - (void)applicationWillTerminate:(NSNotification *)aNotification {
41 [[NSUserDefaults standardUserDefaults] synchronize];
42 [[NSWorkspace sharedWorkspace].notificationCenter
43 removeObserver:self
44 name:NSWorkspaceDidActivateApplicationNotification
45 object:nil];
46 }
47
48 - (IBAction)toggleActivity:(id)sender {
49 BOOL sendRealEvents = !self.jsController.sendingRealEvents;
50 self.jsController.sendingRealEvents = sendRealEvents;
51 activeButton.label = sendRealEvents ? @"Stop" : @"Start";
52 activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
53 activeMenuItem.state = sendRealEvents;
54 }
55
56 - (NSUInteger)firstConfigMenuIndex {
57 NSUInteger count = dockMenuBase.numberOfItems;
58 for (int i = 0; i < count; ++i)
59 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
60 return i + 1;
61 return count;
62 }
63
64 - (void)configsChanged {
65 NSUInteger removeFrom = [self firstConfigMenuIndex];
66 while (dockMenuBase.numberOfItems > removeFrom)
67 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
68 for (Config *config in self.configsController.configs)
69 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
70 [self configChanged];
71 }
72
73 - (void)configChanged {
74 NSUInteger firstConfig = [self firstConfigMenuIndex];
75 Config *current = self.configsController.currentConfig;
76 NSArray *configs = self.configsController.configs;
77 for (int i = 0; i < configs.count; ++i)
78 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
79 }
80
81 - (void)chooseConfig:(id)sender {
82 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
83 Config *chosen = self.configsController.configs[idx];
84 [configsController activateConfig:chosen];
85 }
86 @end