Remove printf.
[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 - (void)didSwitchApplication:(NSNotification *)notification {
20 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
21 [self.configsController activateConfigForProcess:currentApp.localizedName];
22 }
23
24 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
25 [drawer open];
26 self.targetController.enabled = NO;
27 [self.jsController setup];
28 [self.configsController load];
29 [[NSWorkspace sharedWorkspace].notificationCenter
30 addObserver:self
31 selector:@selector(didSwitchApplication:)
32 name:NSWorkspaceDidActivateApplicationNotification
33 object:nil];
34 }
35
36 - (void)applicationWillTerminate:(NSNotification *)aNotification {
37 [[NSUserDefaults standardUserDefaults] synchronize];
38 [[NSWorkspace sharedWorkspace].notificationCenter
39 removeObserver:self
40 name:NSWorkspaceDidActivateApplicationNotification
41 object:nil];
42 }
43
44 - (IBAction)toggleActivity:(id)sender {
45 BOOL sendRealEvents = !self.jsController.sendingRealEvents;
46 self.jsController.sendingRealEvents = sendRealEvents;
47 activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
48 activeMenuItem.state = sendRealEvents;
49 }
50
51 - (NSInteger)firstConfigMenuIndex {
52 for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
53 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
54 return i + 1;
55 return dockMenuBase.numberOfItems;
56 }
57
58 - (void)configsChanged {
59 NSInteger removeFrom = [self firstConfigMenuIndex];
60 while (dockMenuBase.numberOfItems > removeFrom)
61 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
62 int added = 0;
63 for (Config *config in self.configsController.configs) {
64 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
65 [dockMenuBase addItemWithTitle:config.name
66 action:@selector(chooseConfig:)
67 keyEquivalent:keyEquiv];
68
69 }
70 [_targetController refreshConfigs];
71 [self configChanged];
72 }
73
74 - (void)configChanged {
75 NSInteger firstConfig = [self firstConfigMenuIndex];
76 Config *current = self.configsController.currentConfig;
77 NSArray *configs = self.configsController.configs;
78 for (NSUInteger i = 0; i < configs.count; ++i)
79 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
80 }
81
82 - (void)chooseConfig:(id)sender {
83 NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
84 Config *chosen = self.configsController.configs[idx];
85 [_configsController activateConfig:chosen];
86 }
87 @end