5 // Created by Sam McCall on 5/05/09.
10 static BOOL active_eightway[36] = {
11 NO, NO, NO, NO , // center
12 YES, NO, NO, NO , // N
13 YES, NO, NO, YES, // NE
15 NO, YES, NO, YES, // SE
16 NO, YES, NO, NO , // S
17 NO, YES, YES, NO , // SW
18 NO, NO, YES, NO , // W
19 YES, NO, YES, NO , // NW
22 static BOOL active_fourway[20] = {
23 NO, NO, NO, NO , // center
24 YES, NO, NO, NO , // N
26 NO, YES, NO, NO , // S
27 NO, NO, YES, NO , // W
30 @implementation NJInputHat
32 - (id)initWithIndex:(int)index {
33 if ((self = [super init])) {
34 self.children = @[[[NJInput alloc] initWithName:@"Up" base:self],
35 [[NJInput alloc] initWithName:@"Down" base:self],
36 [[NJInput alloc] initWithName:@"Left" base:self],
37 [[NJInput alloc] initWithName:@"Right" base:self]];
38 self.name = [NSString stringWithFormat:@"Hat Switch %d", index];
43 - (id)findSubInputForValue:(IOHIDValueRef)value {
44 long parsed = IOHIDValueGetIntegerValue(value);
45 switch (IOHIDElementGetLogicalMax(IOHIDValueGetElement(value))) {
46 case 7: // 8-way switch, 0-7.
48 case 0: return self.children[0];
49 case 4: return self.children[1];
50 case 6: return self.children[2];
51 case 2: return self.children[3];
54 case 8: // 8-way switch, 1-8 (neutral 0).
56 case 1: return self.children[0];
57 case 5: return self.children[1];
58 case 7: return self.children[2];
59 case 3: return self.children[3];
62 case 3: // 4-way switch, 0-3.
64 case 0: return self.children[0];
65 case 2: return self.children[1];
66 case 3: return self.children[2];
67 case 1: return self.children[3];
70 case 4: // 4-way switch, 1-4 (neutral 0).
72 case 1: return self.children[0];
73 case 3: return self.children[1];
74 case 4: return self.children[2];
75 case 2: return self.children[3];
83 - (void)notifyEvent:(IOHIDValueRef)value {
84 long parsed = IOHIDValueGetIntegerValue(value);
85 long size = IOHIDElementGetLogicalMax(IOHIDValueGetElement(value));
86 // Skip first row in table if 0 is not neutral.
91 BOOL *activechildren = (size == 8) ? active_eightway : active_fourway;
92 for (unsigned i = 0; i < 4; i++) {
93 BOOL active = activechildren[parsed * 4 + i];
94 [self.children[i] setActive:active];
95 [self.children[i] setMagnitude:active];