Add missing makefile.
[enjoyable.git] / NJOutputMouseMove.m
1 //
2 // NJOutputMouseMove.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/26/12.
6 //
7
8 #import "NJOutputMouseMove.h"
9
10 #import "NJDeviceController.h"
11
12 @implementation NJOutputMouseMove
13
14 -(BOOL) isContinuous {
15 return YES;
16 }
17
18 + (NSString *)serializationCode {
19 return @"mouse move";
20 }
21
22 - (NSDictionary *)serialize {
23 return @{ @"type": self.class.serializationCode,
24 @"axis": @(_axis),
25 @"speed": @(_speed),
26 };
27 }
28
29 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
30 withMappings:(NSArray *)mappings {
31 NJOutputMouseMove *output = [[NJOutputMouseMove alloc] init];
32 output.axis = [serialization[@"axis"] intValue];
33 output.speed = [serialization[@"speed"] floatValue];
34 if (!output.speed)
35 output.speed = 4;
36 return output;
37 }
38
39 - (BOOL)update:(NJDeviceController *)jc {
40 if (self.magnitude < 0.05)
41 return NO; // dead zone
42
43 CGFloat height = NSScreen.mainScreen.frame.size.height;
44
45 float dx = 0.f, dy = 0.f;
46 switch (_axis) {
47 case 0:
48 dx = -self.magnitude * _speed;
49 break;
50 case 1:
51 dx = self.magnitude * _speed;
52 break;
53 case 2:
54 dy = -self.magnitude * _speed;
55 break;
56 case 3:
57 dy = self.magnitude * _speed;
58 break;
59 }
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 CGEventPost(kCGHIDEventTap, move);
72 CFRelease(move);
73 return YES;
74 }
75
76 @end