Post active mapping changes through notification center rather than to the applicatio...
[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 #import "NJEvents.h"
15
16 @implementation ApplicationController {
17 BOOL active;
18 }
19
20 - (void)didSwitchApplication:(NSNotification *)notification {
21 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
22 [self.configsController activateConfigForProcess:currentApp.localizedName];
23 }
24
25 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
26 [drawer open];
27 self.targetController.enabled = NO;
28 [self.jsController setup];
29 [self.configsController load];
30 [NSNotificationCenter.defaultCenter
31 addObserver:self
32 selector:@selector(mappingDidChange:)
33 name:NJEventMappingChanged
34 object:nil];
35 }
36
37 - (void)applicationWillTerminate:(NSNotification *)aNotification {
38 [NSUserDefaults.standardUserDefaults synchronize];
39 }
40
41 - (IBAction)toggleActivity:(id)sender {
42 BOOL sendRealEvents = !self.jsController.sendingRealEvents;
43 self.jsController.sendingRealEvents = sendRealEvents;
44 activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
45 activeMenuItem.state = sendRealEvents;
46
47 if (sendRealEvents) {
48 [NSWorkspace.sharedWorkspace.notificationCenter
49 addObserver:self
50 selector:@selector(didSwitchApplication:)
51 name:NSWorkspaceDidActivateApplicationNotification
52 object:nil];
53 NSLog(@"Listening for application changes.");
54 } else {
55 [NSWorkspace.sharedWorkspace.notificationCenter
56 removeObserver:self
57 name:NSWorkspaceDidActivateApplicationNotification
58 object:nil];
59 NSLog(@"Ignoring application changes.");
60 }
61
62 }
63
64 - (NSInteger)firstConfigMenuIndex {
65 for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
66 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
67 return i + 1;
68 return dockMenuBase.numberOfItems;
69 }
70
71 - (void)configsChanged {
72 NSInteger removeFrom = self.firstConfigMenuIndex;
73 while (dockMenuBase.numberOfItems > removeFrom)
74 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
75 int added = 0;
76 for (Config *config in self.configsController.configs) {
77 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
78 [dockMenuBase addItemWithTitle:config.name
79 action:@selector(chooseConfig:)
80 keyEquivalent:keyEquiv];
81
82 }
83 [_targetController refreshConfigs];
84 }
85
86 - (void)mappingDidChange:(NSNotification *)note {
87 NSInteger firstConfig = self.firstConfigMenuIndex;
88 Config *current = note.object;
89 NSArray *configs = self.configsController.configs;
90 for (NSUInteger i = 0; i < configs.count; ++i)
91 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
92 }
93
94 - (void)chooseConfig:(id)sender {
95 NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstConfigMenuIndex;
96 Config *chosen = self.configsController.configs[idx];
97 [_configsController activateConfig:chosen];
98 }
99 @end