Publish activated/deactivated as notifications.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 3 Mar 2013 19:56:04 +0000 (20:56 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sun, 3 Mar 2013 19:56:04 +0000 (20:56 +0100)
ApplicationController.m
JoystickController.h
JoystickController.m
NJEvents.h

index 531d1f469a54aa57c31869ffc23587a10330610f..1acc692151d78f548dda0c36cfbb872952338145 100644 (file)
     [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 {
index cc0419fa833f48151970753029ccae3dc8b9c319..d56f932f6168ea463856879e88d11527ccbfafc1 100644 (file)
@@ -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
index ab59c82ad435a34ab5ad0ae748862fa7e859cde6..2efd68f34586384d64133025c9bd923ef5af3c1d 100644 (file)
@@ -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
index 153a69a11e21e21c20ade512f72127ce95dbf139..84bdd8273a5f055048b5893bcc6c8d5e7996f8fe 100644 (file)
@@ -7,3 +7,5 @@
 //
 
 #define NJEventMappingChanged @"NJEventMappingChanged"
+#define NJEventTranslationActivated @"NJEventTranslationActivated"
+#define NJEventTranslationDeactivated @"NJEventTranslationDeactivated"