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