Improve website.
[enjoyable.git] / Classes / NJInputHat.m
1 //
2 // NJInputHat.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #import "NJInputHat.h"
9
10 static BOOL active_eightway[36] = {
11 NO, NO, NO, NO , // center
12 YES, NO, NO, NO , // N
13 YES, NO, NO, YES, // NE
14 NO, NO, NO, YES, // E
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
20 };
21
22 static BOOL active_fourway[20] = {
23 NO, NO, NO, NO , // center
24 YES, NO, NO, NO , // N
25 NO, NO, NO, YES, // E
26 NO, YES, NO, NO , // S
27 NO, NO, YES, NO , // W
28 };
29
30 @implementation NJInputHat
31
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];
39 }
40 return self;
41 }
42
43 - (id)findSubInputForValue:(IOHIDValueRef)value {
44 long parsed = IOHIDValueGetIntegerValue(value);
45 switch (IOHIDElementGetLogicalMax(IOHIDValueGetElement(value))) {
46 case 7: // 8-way switch, 0-7.
47 switch (parsed) {
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];
52 default: return nil;
53 }
54 case 8: // 8-way switch, 1-8 (neutral 0).
55 switch (parsed) {
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];
60 default: return nil;
61 }
62 case 3: // 4-way switch, 0-3.
63 switch (parsed) {
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];
68 default: return nil;
69 }
70 case 4: // 4-way switch, 1-4 (neutral 0).
71 switch (parsed) {
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];
76 default: return nil;
77 }
78 default:
79 return nil;
80 }
81 }
82
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.
87 if (size & 1) {
88 parsed++;
89 size++;
90 }
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];
96 }
97 }
98
99 @end