Fix case where quick axis movements could create duplicate running targets.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 28 Feb 2013 16:28:29 +0000 (17:28 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 28 Feb 2013 16:28:29 +0000 (17:28 +0100)
JoystickController.h
JoystickController.m
TargetMouseMove.m

index 498ea39..c67a7c1 100644 (file)
@@ -24,7 +24,6 @@
 @property (readonly) Config *currentConfig;
 @property (readonly) JSAction *selectedAction;
 @property (readonly) NSMutableArray *joysticks;
 @property (readonly) Config *currentConfig;
 @property (readonly) JSAction *selectedAction;
 @property (readonly) NSMutableArray *joysticks;
-@property (readonly) NSMutableArray *runningTargets;
 @property (assign) NSPoint mouseLoc;
 @property (assign) BOOL frontWindowOnly;
 
 @property (assign) NSPoint mouseLoc;
 @property (assign) BOOL frontWindowOnly;
 
index 0675075..6acc545 100644 (file)
 @implementation JoystickController {
     IOHIDManagerRef hidManager;
     NSTimer *continuousTimer;
 @implementation JoystickController {
     IOHIDManagerRef hidManager;
     NSTimer *continuousTimer;
+    NSMutableArray *runningTargets;
 }
 
 @synthesize joysticks;
 }
 
 @synthesize joysticks;
-@synthesize runningTargets;
 @synthesize selectedAction;
 @synthesize frontWindowOnly;
 @synthesize mouseLoc;
 @synthesize selectedAction;
 @synthesize frontWindowOnly;
 @synthesize mouseLoc;
@@ -47,8 +47,9 @@
 }
 
 - (void)addRunningTarget:(Target *)target {
 }
 
 - (void)addRunningTarget:(Target *)target {
-    if (![runningTargets containsObject:target])
+    if (![runningTargets containsObject:target]) {
         [runningTargets addObject:target];
         [runningTargets addObject:target];
+    }
     if (!continuousTimer) {
         continuousTimer = [NSTimer scheduledTimerWithTimeInterval:1.f/60.f
                                                            target:self
     if (!continuousTimer) {
         continuousTimer = [NSTimer scheduledTimerWithTimeInterval:1.f/60.f
                                                            target:self
@@ -130,11 +131,13 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD
 
 - (void)updateContinuousActions:(NSTimer *)timer {
     self.mouseLoc = [NSEvent mouseLocation];
 
 - (void)updateContinuousActions:(NSTimer *)timer {
     self.mouseLoc = [NSEvent mouseLocation];
-    for (Target *target in [self.runningTargets copy]) {
-        if (![target update:self])
-            [self.runningTargets removeObject:target];
+    for (Target *target in [runningTargets copy]) {
+        if (![target update:self]) {
+            [runningTargets removeObject:target];
+            NSLog(@"Removing action, now running %lu.", runningTargets.count);
+        }
     }
     }
-    if (!self.runningTargets.count) {
+    if (!runningTargets.count) {
         [continuousTimer invalidate];
         continuousTimer = nil;
         NSLog(@"Unscheduled continuous target timer.");
         [continuousTimer invalidate];
         continuousTimer = nil;
         NSLog(@"Unscheduled continuous target timer.");
index 8eeae88..d2bc943 100644 (file)
@@ -10,7 +10,9 @@
 
 #import "JoystickController.h"
 
 
 #import "JoystickController.h"
 
-@implementation TargetMouseMove
+@implementation TargetMouseMove {
+    int sign;
+}
 
 -(BOOL) isContinuous {
     return true;
 
 -(BOOL) isContinuous {
     return true;
 }
 
 - (BOOL)update:(JoystickController *)jc {
 }
 
 - (BOOL)update:(JoystickController *)jc {
-    //printf("Dir %d inputValue %f\n", [self dir], [self inputValue]);
-    if (fabs(self.magnitude) < 0.01)
+    if (fabsf(self.magnitude) < 0.01) {
+        sign = 0;
         return NO; // dead zone
         return NO; // dead zone
+    }
+
+    // If the action crossed over High/Low, this target is done.
+    if (!sign)
+        sign = self.magnitude < 0 ? -1 : 1;
+    else if (sign / self.magnitude < 0) {
+        sign = 0;
+        return NO;
+    }
     
     NSRect screenRect = [[NSScreen mainScreen] frame];
     NSInteger height = screenRect.size.height;
     
     // TODO
     
     NSRect screenRect = [[NSScreen mainScreen] frame];
     NSInteger height = screenRect.size.height;
     
     // TODO
-    float speed = 4.0;
+    float speed = 4.f;
     if ([jc frontWindowOnly])
     if ([jc frontWindowOnly])
-        speed = 12.0;
-    float dx = 0.0, dy = 0.0;
-    if ([self dir] == 0)
+        speed = 12.f;
+    float dx = 0.f, dy = 0.f;
+    if (self.dir == 0)
         dx = self.magnitude * speed;
     else
         dy = self.magnitude * speed;
         dx = self.magnitude * speed;
     else
         dy = self.magnitude * speed;
@@ -72,7 +83,7 @@
     }
     
     CFRelease(move);
     }
     
     CFRelease(move);
-    return dx || dy;
+    return YES;
 }
 
 @end
 }
 
 @end