From f563321aec9e13b8479ab3b890a9179f095a8b17 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Sun, 3 Mar 2013 20:56:04 +0100 Subject: [PATCH] Publish activated/deactivated as notifications. --- ApplicationController.m | 60 ++++++++++++++++++++++++----------------- JoystickController.h | 2 +- JoystickController.m | 15 ++++++++++- NJEvents.h | 2 ++ 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/ApplicationController.m b/ApplicationController.m index 531d1f4..1acc692 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -28,37 +28,49 @@ [self.jsController setup]; [self.configsController load]; [NSNotificationCenter.defaultCenter - addObserver:self - selector:@selector(mappingDidChange:) - name:NJEventMappingChanged - object:nil]; + addObserver:self + selector:@selector(mappingDidChange:) + name:NJEventMappingChanged + object:nil]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(eventTranslationActivated:) + name:NJEventTranslationActivated + object:nil]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(eventTranslationDeactivated:) + name:NJEventTranslationDeactivated + object:nil]; } - (void)applicationWillTerminate:(NSNotification *)aNotification { [NSUserDefaults.standardUserDefaults synchronize]; } -- (IBAction)toggleActivity:(id)sender { - BOOL sendRealEvents = !self.jsController.sendingRealEvents; - self.jsController.sendingRealEvents = sendRealEvents; - activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; - activeMenuItem.state = sendRealEvents; - - if (sendRealEvents) { - [NSWorkspace.sharedWorkspace.notificationCenter - addObserver:self - selector:@selector(didSwitchApplication:) - name:NSWorkspaceDidActivateApplicationNotification - object:nil]; - NSLog(@"Listening for application changes."); - } else { - [NSWorkspace.sharedWorkspace.notificationCenter - removeObserver:self - name:NSWorkspaceDidActivateApplicationNotification - object:nil]; - NSLog(@"Ignoring application changes."); - } +- (void)eventTranslationActivated:(NSNotification *)note { + activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"]; + activeMenuItem.state = [note.object translatingEvents]; + [NSWorkspace.sharedWorkspace.notificationCenter + addObserver:self + selector:@selector(didSwitchApplication:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + NSLog(@"Listening for application changes."); +} + +- (void)eventTranslationDeactivated:(NSNotification *)note { + activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"]; + activeMenuItem.state = [note.object translatingEvents]; + [NSWorkspace.sharedWorkspace.notificationCenter + removeObserver:self + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + NSLog(@"Ignoring application changes."); +} +- (IBAction)toggleActivity:(id)sender { + self.jsController.translatingEvents = !self.jsController.translatingEvents; } - (NSInteger)firstConfigMenuIndex { diff --git a/JoystickController.h b/JoystickController.h index cc0419f..d56f932 100644 --- a/JoystickController.h +++ b/JoystickController.h @@ -23,6 +23,6 @@ @property (nonatomic, readonly) JSAction *selectedAction; @property (nonatomic, assign) NSPoint mouseLoc; @property (nonatomic, assign) BOOL frontWindowOnly; -@property (nonatomic, assign) BOOL sendingRealEvents; +@property (nonatomic, assign) BOOL translatingEvents; @end diff --git a/JoystickController.m b/JoystickController.m index ab59c82..2efd68f 100644 --- a/JoystickController.m +++ b/JoystickController.m @@ -13,6 +13,7 @@ #import "JSAction.h" #import "Target.h" #import "TargetController.h" +#import "NJEvents.h" @implementation JoystickController { IOHIDManagerRef hidManager; @@ -85,7 +86,7 @@ static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDVa JoystickController *controller = (__bridge JoystickController *)ctx; IOHIDDeviceRef device = IOHIDQueueGetDevice(inSender); - if (controller.sendingRealEvents) { + if (controller.translatingEvents) { [controller runTargetForDevice:device value:value]; } else if ([NSApplication sharedApplication].mainWindow.isVisible) { [controller showTargetForDevice:device value:value]; @@ -216,7 +217,19 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn } - (void)outlineViewSelectionDidChange:(NSNotification *)notification { + [targetController loadCurrent]; } +- (void)setTranslatingEvents:(BOOL)translatingEvents { + if (translatingEvents != _translatingEvents) { + _translatingEvents = translatingEvents; + NSString *name = translatingEvents + ? NJEventTranslationActivated + : NJEventTranslationDeactivated; + [NSNotificationCenter.defaultCenter postNotificationName:name + object:self]; + } +} + @end diff --git a/NJEvents.h b/NJEvents.h index 153a69a..84bdd82 100644 --- a/NJEvents.h +++ b/NJEvents.h @@ -7,3 +7,5 @@ // #define NJEventMappingChanged @"NJEventMappingChanged" +#define NJEventTranslationActivated @"NJEventTranslationActivated" +#define NJEventTranslationDeactivated @"NJEventTranslationDeactivated" -- 2.30.2