Forked Enjoy, mouse movement
[enjoyable.git] / JSActionHat.m
1 //
2 // JSActionHat.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 static BOOL active_eightway[36] = {
9 NO, NO, NO, NO , // center
10 YES, NO, NO, NO , // N
11 YES, NO, NO, YES, // NE
12 NO, NO, NO, YES, // E
13 NO, YES, NO, YES, // SE
14 NO, YES, NO, NO , // S
15 NO, YES, YES, NO , // SW
16 NO, NO, YES, NO , // W
17 YES, NO, YES, NO , // NW
18 };
19 static BOOL active_fourway[20] = {
20 NO, NO, NO, NO , // center
21 YES, NO, NO, NO , // N
22 NO, NO, NO, YES, // E
23 NO, YES, NO, NO , // S
24 NO, NO, YES, NO , // W
25 };
26
27 @implementation JSActionHat
28
29 - (id) init {
30 if(self = [super init]) {
31 subActions = [NSArray arrayWithObjects:
32 [[SubAction alloc] initWithIndex: 0 name: @"Up" base: self],
33 [[SubAction alloc] initWithIndex: 1 name: @"Down" base: self],
34 [[SubAction alloc] initWithIndex: 2 name: @"Left" base: self],
35 [[SubAction alloc] initWithIndex: 3 name: @"Right" base: self],
36 nil
37 ];
38 [subActions retain];
39 name = @"Hat switch";
40 }
41 return self;
42 }
43
44 -(id) findSubActionForValue: (IOHIDValueRef) value {
45 int parsed = IOHIDValueGetIntegerValue(value);
46 if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 7) {
47 // 8-way
48 switch(parsed) {
49 case 0: return [subActions objectAtIndex: 0];
50 case 4: return [subActions objectAtIndex: 1];
51 case 6: return [subActions objectAtIndex: 2];
52 case 2: return [subActions objectAtIndex: 3];
53 }
54 } else if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 8) {
55 // 8-way
56 switch(parsed) {
57 case 1: return [subActions objectAtIndex: 0];
58 case 5: return [subActions objectAtIndex: 1];
59 case 7: return [subActions objectAtIndex: 2];
60 case 3: return [subActions objectAtIndex: 3];
61 }
62 } else if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 3) {
63 // 4-way
64 switch(parsed) {
65 case 0: return [subActions objectAtIndex: 0];
66 case 2: return [subActions objectAtIndex: 1];
67 case 3: return [subActions objectAtIndex: 2];
68 case 1: return [subActions objectAtIndex: 3];
69 }
70 } else if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 4) {
71 // 4-way
72 switch(parsed) {
73 case 1: return [subActions objectAtIndex: 0];
74 case 3: return [subActions objectAtIndex: 1];
75 case 4: return [subActions objectAtIndex: 2];
76 case 2: return [subActions objectAtIndex: 3];
77 }
78 }
79 return NULL;
80 }
81
82 -(void) notifyEvent: (IOHIDValueRef) value {
83 int parsed = IOHIDValueGetIntegerValue(value);
84 int size = IOHIDElementGetLogicalMax(IOHIDValueGetElement(value));
85 if(size == 7 || size == 3) {
86 parsed++;
87 size++;
88 }
89 BOOL* activeSubactions = (size == 8) ? active_eightway : active_fourway;
90 for(int i=0; i<4; i++)
91 [[subActions objectAtIndex: i] setActive: activeSubactions[parsed * 4 + i]];
92 }
93
94 @end