Forked Enjoy, mouse movement
[enjoyable.git] / ApplicationController.m
1 //
2 // ApplicationController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7 #include <Carbon/Carbon.h>
8
9 @implementation ApplicationController
10
11 @synthesize jsController, targetController, configsController;
12
13 static BOOL active;
14
15 pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData);
16
17 -(void) applicationDidFinishLaunching: (NSNotification*) notification {
18 [jsController setup];
19 [drawer open];
20 [targetController setEnabled: false];
21 [self setActive: NO];
22 [configsController load];
23 EventTypeSpec et;
24 et.eventClass = kEventClassApplication;
25 et.eventKind = kEventAppFrontSwitched;
26 EventHandlerUPP handler = NewEventHandlerUPP(appSwitch);
27 InstallApplicationEventHandler(handler, 1, &et, self, NULL);
28 }
29
30 -(void) applicationWillTerminate: (NSNotification *)aNotification {
31 [configsController save];
32 }
33
34 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
35 hasVisibleWindows:(BOOL)flag
36 {
37 [mainWindow makeKeyAndOrderFront:self];
38 return YES;
39 }
40
41 pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) {
42 ApplicationController* self = (ApplicationController*)userData;
43 NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication];
44 ProcessSerialNumber psn;
45 psn.lowLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
46 psn.highLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
47 [self->configsController applicationSwitchedTo: [currentApp objectForKey:@"NSApplicationName"] withPsn: psn];
48 return noErr;
49 }
50
51 -(BOOL) active {
52 return active;
53 }
54
55 -(void) setActive: (BOOL) newActive {
56 [activeButton setLabel: (newActive ? @"Stop" : @"Start")];
57 [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]];
58 [activeMenuItem setState: (newActive ? 1 : 0)];
59 active = newActive;
60 }
61
62 -(IBAction) toggleActivity: (id)sender {
63 [self setActive: ![self active]];
64 }
65
66 -(void) configsChanged {
67 while([dockMenuBase numberOfItems] > 2)
68 [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
69
70 for(Config* config in [configsController configs]) {
71 [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""];
72 }
73 [self configChanged];
74 }
75 -(void) configChanged {
76 Config* current = [configsController currentConfig];
77 NSArray* configs = [configsController configs];
78 for(int i=0; i<[configs count]; i++)
79 [[dockMenuBase itemAtIndex: (2+i)] setState: (([configs objectAtIndex:i] == current) ? YES : NO)];
80 }
81
82 -(void) chooseConfig: (id) sender {
83 [configsController activateConfig: [[configsController configs] objectAtIndex: ([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];
84 }
85 @end