//
// Created by Sam McCall on 4/05/09.
//
-#include <Carbon/Carbon.h>
-@implementation ApplicationController
+#import "ApplicationController.h"
-@synthesize jsController, targetController, configsController;
+#import "Config.h"
+#import "ConfigsController.h"
+#import "JoystickController.h"
+#import "TargetController.h"
-static BOOL active;
-
-pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData);
-
--(void) applicationDidFinishLaunching: (NSNotification*) notification {
- [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, (void *)CFBridgingRetain(self), NULL);
+@implementation ApplicationController {
+ BOOL active;
}
--(void) applicationWillTerminate: (NSNotification *)aNotification {
- [configsController save];
+- (void)didSwitchApplication:(NSNotification *)notification {
+ NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
+ [self.configsController activateConfigForProcess:currentApp.localizedName];
}
-- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
- hasVisibleWindows:(BOOL)flag
-{
- [mainWindow makeKeyAndOrderFront:self];
- return YES;
+- (void)applicationDidFinishLaunching:(NSNotification *)notification {
+ [drawer open];
+ self.targetController.enabled = NO;
+ [self.jsController setup];
+ [self.configsController load];
+ [[NSWorkspace sharedWorkspace].notificationCenter
+ addObserver:self
+ selector:@selector(didSwitchApplication:)
+ name:NSWorkspaceDidActivateApplicationNotification
+ object:nil];
}
-pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) {
- ApplicationController* self = (__bridge ApplicationController*)userData;
- NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication];
- ProcessSerialNumber psn;
- psn.lowLongOfPSN = [currentApp[@"NSApplicationProcessSerialNumberLow"] longValue];
- psn.highLongOfPSN = [currentApp[@"NSApplicationProcessSerialNumberHigh"] longValue];
- [self->configsController applicationSwitchedTo: currentApp[@"NSApplicationName"] withPsn: psn];
- return noErr;
+- (void)applicationWillTerminate:(NSNotification *)aNotification {
+ [[NSUserDefaults standardUserDefaults] synchronize];
+ [[NSWorkspace sharedWorkspace].notificationCenter
+ removeObserver:self
+ name:NSWorkspaceDidActivateApplicationNotification
+ object:nil];
}
--(BOOL) active {
- return active;
+- (IBAction)toggleActivity:(id)sender {
+ BOOL sendRealEvents = !self.jsController.sendingRealEvents;
+ self.jsController.sendingRealEvents = sendRealEvents;
+ activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
+ activeMenuItem.state = sendRealEvents;
}
--(void) setActive: (BOOL) newActive {
- [activeButton setLabel: (newActive ? @"Stop" : @"Start")];
- [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]];
- [activeMenuItem setState: (newActive ? 1 : 0)];
- active = newActive;
+- (NSInteger)firstConfigMenuIndex {
+ for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
+ if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
+ return i + 1;
+ return dockMenuBase.numberOfItems;
}
--(IBAction) toggleActivity: (id)sender {
- [self setActive: ![self active]];
+- (void)configsChanged {
+ NSInteger removeFrom = [self firstConfigMenuIndex];
+ while (dockMenuBase.numberOfItems > removeFrom)
+ [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
+ int added = 0;
+ for (Config *config in self.configsController.configs) {
+ NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
+ [dockMenuBase addItemWithTitle:config.name
+ action:@selector(chooseConfig:)
+ keyEquivalent:keyEquiv];
+
+ }
+ [_targetController refreshConfigs];
+ [self configChanged];
}
--(void) configsChanged {
- 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];
- for(int i=0; i<[configs count]; i++)
- [[dockMenuBase itemAtIndex: (2+i)] setState: ((configs[i] == current) ? YES : NO)];
+- (void)configChanged {
+ NSInteger firstConfig = [self firstConfigMenuIndex];
+ Config *current = self.configsController.currentConfig;
+ NSArray *configs = self.configsController.configs;
+ for (int i = 0; i < configs.count; ++i)
+ [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
}
--(void) chooseConfig: (id) sender {
- [configsController activateConfig: [configsController configs][([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];
+- (void)chooseConfig:(id)sender {
+ NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
+ Config *chosen = self.configsController.configs[idx];
+ [_configsController activateConfig:chosen];
}
@end