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