Post active mapping changes through notification center rather than to the applicatio...
[enjoyable.git] / TargetMouseMove.m
1 //
2 // TargetMouseMove.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/26/12.
6 //
7
8 #import "TargetMouseMove.h"
9
10 #import "JoystickController.h"
11
12 @implementation TargetMouseMove {
13 int sign;
14 }
15
16 -(BOOL) isContinuous {
17 return YES;
18 }
19
20 + (NSString *)serializationCode {
21 return @"mmove";
22 }
23
24 - (NSDictionary *)serialize {
25 return @{ @"type": @"mmove", @"axis": @(_axis) };
26 }
27
28 + (Target *)targetDeserialize:(NSDictionary *)serialization
29 withConfigs:(NSArray *)configs {
30 TargetMouseMove *target = [[TargetMouseMove alloc] init];
31 target.axis = [serialization[@"axis"] intValue];
32 return target;
33 }
34
35 - (BOOL)update:(JoystickController *)jc {
36 if (fabsf(self.magnitude) < 0.01) {
37 sign = 0;
38 return NO; // dead zone
39 }
40
41 // If the action crossed over High/Low, this target is done.
42 if (!sign)
43 sign = self.magnitude < 0 ? -1 : 1;
44 else if (sign / self.magnitude < 0) {
45 sign = 0;
46 return NO;
47 }
48
49 CGFloat height = NSScreen.mainScreen.frame.size.height;
50
51 // TODO
52 float speed = 4.f;
53 if (jc.frontWindowOnly)
54 speed = 12.f;
55 float dx = 0.f, dy = 0.f;
56 if (_axis == 0)
57 dx = self.magnitude * speed;
58 else
59 dy = self.magnitude * speed;
60 NSPoint mouseLoc = jc.mouseLoc;
61 mouseLoc.x += dx;
62 mouseLoc.y -= dy;
63 jc.mouseLoc = mouseLoc;
64
65 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
66 CGPointMake(mouseLoc.x, height - mouseLoc.y),
67 0);
68 CGEventSetType(move, kCGEventMouseMoved);
69 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, (int)dx);
70 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, (int)dy);
71
72 if (jc.frontWindowOnly) {
73 ProcessSerialNumber psn;
74 GetFrontProcess(&psn);
75 CGEventPostToPSN(&psn, move);
76 }
77 else {
78 CGEventPost(kCGHIDEventTap, move);
79 }
80
81 CFRelease(move);
82 return YES;
83 }
84
85 @end