Clean-up of Joystick class. Refactor constructor to avoid mandatory 'post-constructor...
[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 = @[[[SubAction alloc] initWithIndex: 0 name: @"Up" base: self],
32 [[SubAction alloc] initWithIndex: 1 name: @"Down" base: self],
33 [[SubAction alloc] initWithIndex: 2 name: @"Left" base: self],
34 [[SubAction alloc] initWithIndex: 3 name: @"Right" base: self]];
35 name = @"Hat switch";
36 }
37 return self;
38 }
39
40 -(id) findSubActionForValue: (IOHIDValueRef) value {
41 int parsed = IOHIDValueGetIntegerValue(value);
42 if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 7) {
43 // 8-way
44 switch(parsed) {
45 case 0: return subActions[0];
46 case 4: return subActions[1];
47 case 6: return subActions[2];
48 case 2: return subActions[3];
49 }
50 } else if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 8) {
51 // 8-way
52 switch(parsed) {
53 case 1: return subActions[0];
54 case 5: return subActions[1];
55 case 7: return subActions[2];
56 case 3: return subActions[3];
57 }
58 } else if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 3) {
59 // 4-way
60 switch(parsed) {
61 case 0: return subActions[0];
62 case 2: return subActions[1];
63 case 3: return subActions[2];
64 case 1: return subActions[3];
65 }
66 } else if(IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)) == 4) {
67 // 4-way
68 switch(parsed) {
69 case 1: return subActions[0];
70 case 3: return subActions[1];
71 case 4: return subActions[2];
72 case 2: return subActions[3];
73 }
74 }
75 return NULL;
76 }
77
78 -(void) notifyEvent: (IOHIDValueRef) value {
79 int parsed = IOHIDValueGetIntegerValue(value);
80 int size = IOHIDElementGetLogicalMax(IOHIDValueGetElement(value));
81 if(size == 7 || size == 3) {
82 parsed++;
83 size++;
84 }
85 BOOL* activeSubactions = (size == 8) ? active_eightway : active_fourway;
86 for(int i=0; i<4; i++)
87 [subActions[i] setActive: activeSubactions[parsed * 4 + i]];
88 }
89
90 @end