Automatically trigger/untrigger targets based on setting the 'running' property;...
[enjoyable.git] / TargetMouseMove.m
1 //
2 // TargetMouseMove.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/26/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "TargetMouseMove.h"
10
11 @implementation TargetMouseMove
12
13 -(BOOL) isContinuous {
14 return true;
15 }
16
17 @synthesize dir;
18
19 -(NSString*) stringify {
20 return [[NSString alloc] initWithFormat: @"mmove~%d", dir];
21 }
22
23 +(TargetMouseMove*) unstringifyImpl: (NSArray*) comps {
24 NSParameterAssert([comps count] == 2);
25 TargetMouseMove* target = [[TargetMouseMove alloc] init];
26 [target setDir: [comps[1] integerValue]];
27 return target;
28 }
29
30 - (BOOL)update:(JoystickController *)jc {
31 //printf("Dir %d inputValue %f\n", [self dir], [self inputValue]);
32 if (fabs(self.magnitude) < 0.01)
33 return NO; // dead zone
34
35 NSRect screenRect = [[NSScreen mainScreen] frame];
36 NSInteger height = screenRect.size.height;
37
38 // TODO
39 float speed = 4.0;
40 if ([jc frontWindowOnly])
41 speed = 12.0;
42 float dx = 0.0, dy = 0.0;
43 if ([self dir] == 0)
44 dx = self.magnitude * speed;
45 else
46 dy = self.magnitude * speed;
47 NSPoint mouseLoc = jc.mouseLoc;
48 mouseLoc.x += dx;
49 mouseLoc.y -= dy;
50 jc.mouseLoc = mouseLoc;
51
52 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
53 CGPointMake(mouseLoc.x, height - mouseLoc.y),
54 0);
55 CGEventSetType(move, kCGEventMouseMoved);
56 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, dx);
57 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, dy);
58
59 if ([jc frontWindowOnly]) {
60 ProcessSerialNumber psn;
61 GetFrontProcess(&psn);
62 CGEventPostToPSN(&psn, move);
63 }
64 else {
65 CGEventPost(kCGHIDEventTap, move);
66 }
67
68 CFRelease(move);
69 return dx || dy;
70 }
71
72 @end