forked from BaconWrappedShrimpWithEyes/enjoy2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplicationController.m
104 lines (86 loc) · 3.39 KB
/
ApplicationController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// ApplicationController.m
// Enjoy
//
// Created by Sam McCall on 4/05/09.
//
#include <Carbon/Carbon.h>
@implementation ApplicationController
@synthesize jsController, targetController, configsController;
static BOOL active;
pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData);
void onUncaughtException(NSException *exception) {
NSLog(@"Uncaught exception: %@", exception.description);
}
-(void) applicationDidFinishLaunching: (NSNotification*) notification {
// Debug: print exceptions
NSSetUncaughtExceptionHandler(&onUncaughtException);
[jsController setup];
[drawer open];
[targetController setEnabled: false];
[self setActive: NO];
[configsController load];
EventTypeSpec et;
et.eventClass = kEventClassApplication;
et.eventKind = kEventAppFrontSwitched;
EventHandlerUPP handler = NewEventHandlerUPP(appSwitch);
InstallApplicationEventHandler(handler, 1, &et, self, NULL);
}
-(void) awakeFromNib {
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"Let joystick commands fire in the background"];
}
}
-(void) applicationWillTerminate: (NSNotification *)aNotification {
[configsController save];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
hasVisibleWindows:(BOOL)flag
{
[mainWindow makeKeyAndOrderFront:self];
return YES;
}
pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) {
ApplicationController* self = (ApplicationController*)userData;
NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication];
ProcessSerialNumber psn;
psn.lowLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
psn.highLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
[self->configsController applicationSwitchedTo: [currentApp objectForKey:@"NSApplicationName"] withPsn: psn];
return noErr;
}
-(BOOL) active {
return active;
}
-(void) setActive: (BOOL) newActive {
[activeButton setLabel: (newActive ? @"Stop" : @"Start")];
[activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]];
[activeMenuItem setState: (newActive ? 1 : 0)];
active = newActive;
}
-(IBAction) toggleActivity: (id)sender {
[self setActive: ![self active]];
}
-(void) configsListChanged {
// Update configs list in File menu
while([dockMenuBase numberOfItems] > 2)
[dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
for(Config* config in [configsController configs]) {
[dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""];
}
[self configChanged];
}
-(void) configChanged {
Config* current = [configsController currentConfig];
NSArray* configs = [configsController configs];
if ([dockMenuBase numberOfItems] - 2 != [configs count]) {
NSLog(@"dockMenuBase has wrong number of items!");
}
for(int i=0; i<[configs count]; i++) {
[[dockMenuBase itemAtIndex: (2+i)] setState: (([configs objectAtIndex:i] == current) ? YES : NO)];
}
}
-(void) chooseConfig: (id) sender {
[configsController activateConfig: [[configsController configs] objectAtIndex: ([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];
}
@end