ae278b30b3094527adaee68d0efa9ba818df55be
[enjoyable.git] / Joystick.m
1 //
2 // Joystick.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "Joystick.h"
9
10 #import "JSAction.h"
11 #import "JSActionAnalog.h"
12 #import "JSActionButton.h"
13 #import "JSActionHat.h"
14
15 static NSArray *ActionsForElement(IOHIDDeviceRef device, id base) {
16 CFArrayRef elements = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
17 NSMutableArray *children = [NSMutableArray arrayWithCapacity:CFArrayGetCount(elements)];
18
19 int buttons = 0;
20 int axes = 0;
21 int hats = 0;
22
23 for (int i = 0; i < CFArrayGetCount(elements); i++) {
24 IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
25 int type = IOHIDElementGetType(element);
26 int usage = IOHIDElementGetUsage(element);
27 int usagePage = IOHIDElementGetUsagePage(element);
28 int max = IOHIDElementGetPhysicalMax(element);
29 int min = IOHIDElementGetPhysicalMin(element);
30 CFStringRef elName = IOHIDElementGetName(element);
31
32 JSAction *action = nil;
33
34 if (!(type == kIOHIDElementTypeInput_Misc
35 || type == kIOHIDElementTypeInput_Axis
36 || type == kIOHIDElementTypeInput_Button))
37 continue;
38
39 if (max - min == 1 || usagePage == kHIDPage_Button || type == kIOHIDElementTypeInput_Button) {
40 action = [[JSActionButton alloc] initWithName:(__bridge NSString *)elName
41 idx:++buttons
42 max:max];
43 } else if (usage == kHIDUsage_GD_Hatswitch) {
44 action = [[JSActionHat alloc] initWithIndex:++hats];
45 } else if (usage >= kHIDUsage_GD_X && usage <= kHIDUsage_GD_Rz) {
46 action = [[JSActionAnalog alloc] initWithIndex:++axes
47 rawMin:min
48 rawMax:max];
49 } else {
50 continue;
51 }
52
53 // TODO(jfw): Should be moved into better constructors.
54 action.base = base;
55 action.cookie = IOHIDElementGetCookie(element);
56 [children addObject:action];
57 }
58
59 CFRelease(elements);
60 return children;
61 }
62
63 @implementation Joystick
64
65 @synthesize vendorId;
66 @synthesize productId;
67 @synthesize productName;
68 @synthesize index;
69 @synthesize device;
70 @synthesize children;
71
72 - (id)initWithDevice:(IOHIDDeviceRef)dev {
73 if ((self = [super init])) {
74 self.device = dev;
75 self.productName = (__bridge NSString *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDProductKey));
76 self.vendorId = [(__bridge NSNumber *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDVendorIDKey)) intValue];
77 self.productId = [(__bridge NSNumber *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDProductIDKey)) intValue];
78 self.children = ActionsForElement(dev, self);
79 }
80 return self;
81 }
82
83 - (NSString *)name {
84 return [NSString stringWithFormat:@"%@ #%d", productName, index];
85 }
86
87 - (id)base {
88 return nil;
89 }
90
91 - (NSString *)uid {
92 return [NSString stringWithFormat: @"%d:%d:%d", vendorId, productId, index];
93 }
94
95 - (JSAction *)findActionByCookie:(void *)cookie {
96 for (JSAction *child in children)
97 if (child.cookie == cookie)
98 return child;
99 return nil;
100 }
101
102 - (id)handlerForEvent:(IOHIDValueRef) value {
103 JSAction *mainAction = [self actionForEvent:value];
104 return [mainAction findSubActionForValue:value];
105 }
106
107 - (JSAction *)actionForEvent:(IOHIDValueRef)value {
108 IOHIDElementRef elt = IOHIDValueGetElement(value);
109 void *cookie = IOHIDElementGetCookie(elt);
110 return [self findActionByCookie:cookie];
111 }
112
113 @end