Clean up JoystickController. Modernize more Objective-C syntax. Remove direct public...
[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 -(void) trigger: (JoystickController *)jc {
31 return;
32 }
33
34 -(void) untrigger: (JoystickController *)jc {
35 return;
36 }
37
38 - (BOOL)update:(JoystickController *)jc {
39 //printf("Dir %d inputValue %f\n", [self dir], [self inputValue]);
40 if (fabs([self inputValue]) < 0.01)
41 return NO; // dead zone
42
43 NSRect screenRect = [[NSScreen mainScreen] frame];
44 NSInteger height = screenRect.size.height;
45
46 // TODO
47 double speed = 4.0;
48 if ([jc frontWindowOnly])
49 speed = 12.0;
50 double dx = 0.0, dy = 0.0;
51 if ([self dir] == 0)
52 dx = [self inputValue] * speed;
53 else
54 dy = [self inputValue] * speed;
55 NSPoint mouseLoc = jc.mouseLoc;
56 mouseLoc.x += dx;
57 mouseLoc.y -= dy;
58 jc.mouseLoc = mouseLoc;
59
60 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
61 CGPointMake(mouseLoc.x, height - mouseLoc.y),
62 0);
63 CGEventSetType(move, kCGEventMouseMoved);
64 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, dx);
65 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, dy);
66
67 if ([jc frontWindowOnly]) {
68 ProcessSerialNumber psn;
69 GetFrontProcess(&psn);
70 CGEventPostToPSN(&psn, move);
71 }
72 else {
73 CGEventPost(kCGHIDEventTap, move);
74 }
75
76 CFRelease(move);
77 return dx || dy;
78 }
79
80 @end