Write proper constructors for NJInput and subclasses. Rename base to parent to match...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 14 Mar 2013 13:02:06 +0000 (14:02 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Thu, 14 Mar 2013 13:02:06 +0000 (14:02 +0100)
14 files changed:
Classes/NJDevice.m
Classes/NJDeviceController.m
Classes/NJInput.h
Classes/NJInput.m
Classes/NJInputAnalog.h
Classes/NJInputAnalog.m
Classes/NJInputButton.h
Classes/NJInputButton.m
Classes/NJInputHat.h
Classes/NJInputHat.m
Classes/NJInputPathElement.h
Classes/NJInputPathElement.m
Classes/NJOutputController.m
Info.plist

index 3b0cc6a..e4c6b3d 100644 (file)
@@ -12,7 +12,7 @@
 #import "NJInputHat.h"
 #import "NJInputButton.h"
 
-static NSArray *InputsForElement(IOHIDDeviceRef device, id base) {
+static NSArray *InputsForElement(IOHIDDeviceRef device, id parent) {
     CFArrayRef elements = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
     NSMutableArray *children = [NSMutableArray arrayWithCapacity:CFArrayGetCount(elements)];
     
@@ -20,14 +20,13 @@ static NSArray *InputsForElement(IOHIDDeviceRef device, id base) {
     int axes = 0;
     int hats = 0;
     
-    for (int i = 0; i < CFArrayGetCount(elements); i++) {
+    for (CFIndex i = 0; i < CFArrayGetCount(elements); i++) {
         IOHIDElementRef element = (IOHIDElementRef)CFArrayGetValueAtIndex(elements, i);
-        int type = IOHIDElementGetType(element);
-        unsigned usage = IOHIDElementGetUsage(element);
-        unsigned usagePage = IOHIDElementGetUsagePage(element);
-        long max = IOHIDElementGetPhysicalMax(element);
-        long min = IOHIDElementGetPhysicalMin(element);
-        CFStringRef elName = IOHIDElementGetName(element);
+        IOHIDElementType type = IOHIDElementGetType(element);
+        uint32_t usage = IOHIDElementGetUsage(element);
+        uint32_t usagePage = IOHIDElementGetUsagePage(element);
+        CFIndex max = IOHIDElementGetPhysicalMax(element);
+        CFIndex min = IOHIDElementGetPhysicalMin(element);
         
         NJInput *input = nil;
         
@@ -36,23 +35,24 @@ static NSArray *InputsForElement(IOHIDDeviceRef device, id base) {
               || type == kIOHIDElementTypeInput_Button))
              continue;
         
-        if (max - min == 1 || usagePage == kHIDPage_Button || type == kIOHIDElementTypeInput_Button) {
-            input = [[NJInputButton alloc] initWithName:(__bridge NSString *)elName
-                                                    idx:++buttons
-                                                    max:max];
+        if (max - min == 1
+            || usagePage == kHIDPage_Button
+            || type == kIOHIDElementTypeInput_Button) {
+            input = [[NJInputButton alloc] initWithElement:element
+                                                     index:++buttons
+                                                    parent:parent];
         } else if (usage == kHIDUsage_GD_Hatswitch) {
-            input = [[NJInputHat alloc] initWithIndex:++hats];
+            input = [[NJInputHat alloc] initWithElement:element
+                                                  index:++hats
+                                                 parent:parent];
         } else if (usage >= kHIDUsage_GD_X && usage <= kHIDUsage_GD_Rz) {
-            input = [[NJInputAnalog alloc] initWithIndex:++axes
-                                                  rawMin:min
-                                                  rawMax:max];
+            input = [[NJInputAnalog alloc] initWithElement:element
+                                                     index:++axes
+                                                    parent:parent];
         } else {
             continue;
         }
         
-        // TODO(jfw): Should be moved into better constructors.
-        input.base = base;
-        input.cookie = IOHIDElementGetCookie(element);
         [children addObject:input];
     }
 
@@ -66,7 +66,7 @@ static NSArray *InputsForElement(IOHIDDeviceRef device, id base) {
 }
 
 - (id)initWithDevice:(IOHIDDeviceRef)dev {
-    if ((self = [super initWithName:nil did:nil base:nil])) {
+    if ((self = [super initWithName:nil eid:nil parent:nil])) {
         self.device = dev;
         self.productName = (__bridge NSString *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDProductKey));
         _vendorId = [(__bridge NSNumber *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDVendorIDKey)) intValue];
index 6fc4d13..8cc5f2a 100644 (file)
@@ -83,7 +83,7 @@
 
 - (void)expandRecursive:(NJInputPathElement *)pathElement {
     if (pathElement) {
-        [self expandRecursive:pathElement.base];
+        [self expandRecursive:pathElement.parent];
         [outlineView expandItem:pathElement];
     }
 }
@@ -251,7 +251,7 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) {
 
 - (NJInput *)selectedInput {
     NJInputPathElement *item = [outlineView itemAtRow:outlineView.selectedRow];
-    return (NJInput *)((!item.children && item.base) ? item : nil);
+    return (NJInput *)((!item.children && item.parent) ? item : nil);
 }
 
 - (NSInteger)outlineView:(NSOutlineView *)outlineView
index b9d2dca..db47121 100644 (file)
 
 @interface NJInput : NJInputPathElement
 
+#define NJINPUT_DID(name, index) [[NSString alloc] initWithFormat:@"%s %d", name, index]
+#define NJINPUT_NAME(name, index) [[NSString alloc] initWithFormat:name, index]
+
 - (id)initWithName:(NSString *)name
-               did:(NSString *)did
-            cookie:(IOHIDElementCookie)cookie
-              base:(NJInputPathElement *)base;
+               eid:(NSString *)eid
+           element:(IOHIDElementRef)element
+            parent:(NJInputPathElement *)parent;
 
 @property (nonatomic, assign) IOHIDElementCookie cookie;
 @property (nonatomic, assign) BOOL active;
index 78376e6..82d5993 100644 (file)
 @implementation NJInput
 
 - (id)initWithName:(NSString *)name
-               did:(NSString *)did
-            cookie:(IOHIDElementCookie)cookie
-              base:(NJInputPathElement *)base {
-    if ((self = [super initWithName:name did:did base:base])) {
-        self.cookie = cookie;
+               eid:(NSString *)eid
+           element:(IOHIDElementRef)element
+            parent:(NJInputPathElement *)parent
+{
+    NSString *elementName = (__bridge NSString *)(IOHIDElementGetName(element));
+    if (elementName.length)
+        name = [name stringByAppendingFormat:@"- %@", elementName];
+    if ((self = [super initWithName:name eid:eid parent:parent])) {
+        self.cookie = IOHIDElementGetCookie(element);
     }
     return self;
 }
index e79ed89..de35e3d 100644 (file)
@@ -12,6 +12,8 @@
 
 @interface NJInputAnalog : NJInput
 
-- (id)initWithIndex:(int)index rawMin:(long)rawMin rawMax:(long)rawMax;
+- (id)initWithElement:(IOHIDElementRef)element
+                index:(int)index
+               parent:(NJInputPathElement *)parent;
 
 @end
index af64954..3746c11 100644 (file)
@@ -9,33 +9,37 @@
 
 #import "NJInputAnalog.h"
 
-static float normalize(long p, long min, long max) {
+static float normalize(CFIndex p, CFIndex min, CFIndex max) {
     return 2 * (p - min) / (float)(max - min) - 1;
 }
 
 @implementation NJInputAnalog {
-    long rawMin;
-    long rawMax;
+    CFIndex _rawMin;
+    CFIndex _rawMax;
 }
 
-- (id)initWithIndex:(int)index rawMin:(long)rawMin_ rawMax:(long)rawMax_ {
-    NSString *name = [[NSString alloc] initWithFormat:NSLocalizedString(@"axis %d", @"axis name"), index];
-    NSString *did = [[NSString alloc] initWithFormat:@"Axis %d", index];
-    if ((self = [super initWithName:name did:did base:nil])) {
+- (id)initWithElement:(IOHIDElementRef)element
+                index:(int)index
+               parent:(NJInputPathElement *)parent
+{
+    if ((self = [super initWithName:NJINPUT_NAME(NSLocalizedString(@"axis %d", @"axis name"), index)
+                                eid:NJINPUT_DID("Axis", index)
+                            element:element
+                             parent:nil])) {
         self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"axis low", @"axis low trigger")
-                                                    did:@"Low"
-                                                   base:self],
+                                                    eid:@"Low"
+                                                   parent:self],
                           [[NJInput alloc] initWithName:NSLocalizedString(@"axis high", @"axis high trigger")
-                                                    did:@"High"
-                                                   base:self]];
-        rawMax = rawMax_;
-        rawMin = rawMin_;
+                                                    eid:@"High"
+                                                   parent:self]];
+        _rawMax = IOHIDElementGetPhysicalMax(element);
+        _rawMin = IOHIDElementGetPhysicalMin(element);
     }
     return self;
 }
 
 - (id)findSubInputForValue:(IOHIDValueRef)value {
-    float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
+    float mag = normalize(IOHIDValueGetIntegerValue(value), _rawMin, _rawMax);
     if (mag < -DEAD_ZONE)
         return self.children[0];
     else if (mag > DEAD_ZONE)
@@ -45,7 +49,7 @@ static float normalize(long p, long min, long max) {
 }
 
 - (void)notifyEvent:(IOHIDValueRef)value {
-    float magnitude = self.magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
+    float magnitude = self.magnitude = normalize(IOHIDValueGetIntegerValue(value), _rawMin, _rawMax);
     [self.children[0] setMagnitude:fabsf(MIN(magnitude, 0))];
     [self.children[1] setMagnitude:fabsf(MAX(magnitude, 0))];
     [self.children[0] setActive:magnitude < -DEAD_ZONE];
index 31cba73..e9abdd3 100644 (file)
@@ -10,6 +10,8 @@
 
 @interface NJInputButton : NJInput
 
-- (id)initWithName:(NSString *)name idx:(int)idx max:(long)max;
+- (id)initWithElement:(IOHIDElementRef)element
+                index:(int)index
+               parent:(NJInputPathElement *)parent;
 
 @end
index caa759b..354220a 100644 (file)
@@ -8,16 +8,18 @@
 #import "NJInputButton.h"
 
 @implementation NJInputButton {
-    long _max;
+    CFIndex _max;
 }
 
-- (id)initWithName:(NSString *)name idx:(int)idx max:(long)max {
-    NSString *fullname = [NSString stringWithFormat:NSLocalizedString(@"button %d", @"button name"), idx];
-    if (name.length)
-        fullname = [fullname stringByAppendingFormat:@"- %@", name];
-    NSString *did = [[NSString alloc] initWithFormat:@"Button %d", idx];
-    if ((self = [super initWithName:fullname did:did base:nil])) {
-        _max = max;
+- (id)initWithElement:(IOHIDElementRef)element
+                index:(int)index
+               parent:(NJInputPathElement *)parent
+{
+    if ((self = [super initWithName:NJINPUT_NAME(NSLocalizedString(@"button %d", @"button name"), index)
+                                eid:NJINPUT_DID("Button", index)
+                            element:element
+                             parent:parent])) {
+        _max = IOHIDElementGetLogicalMax(element);
     }
     return self;
 }
index 5290d71..67bb21d 100644 (file)
@@ -10,6 +10,8 @@
 
 @interface NJInputHat : NJInput
 
-- (id)initWithIndex:(int)index;
+- (id)initWithElement:(IOHIDElementRef)element
+                index:(int)index
+               parent:(NJInputPathElement *)parent;
 
 @end
index 46ea94f..0a5afa2 100644 (file)
@@ -27,31 +27,38 @@ static BOOL active_fourway[20] = {
     NO,  NO,  YES, NO , // W
 };
 
-@implementation NJInputHat
+@implementation NJInputHat {
+    CFIndex _max;
+}
 
-- (id)initWithIndex:(int)index {
-    NSString *name = [NSString stringWithFormat:NSLocalizedString(@"hat switch %d", @"hat switch name"), index];
-    NSString *did = [NSString stringWithFormat:@"Hat Switch %d", index];
-    if ((self = [super initWithName:name did:did base:nil])) {
+- (id)initWithElement:(IOHIDElementRef)element
+                index:(int)index
+               parent:(NJInputPathElement *)parent
+{
+    if ((self = [super initWithName:NJINPUT_NAME(NSLocalizedString(@"hat switch %d", @"hat switch name"), index)
+                                eid:NJINPUT_DID("Hat Switch", index)
+                            element:element
+                               parent:parent])) {
         self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"hat up", @"hat switch up state")
-                                                    did:@"Up"
-                                                   base:self],
+                                                    eid:@"Up"
+                                                   parent:self],
                           [[NJInput alloc] initWithName:NSLocalizedString(@"hat down", @"hat switch down state")
-                                                    did:@"Down"
-                                                   base:self],
+                                                    eid:@"Down"
+                                                   parent:self],
                           [[NJInput alloc] initWithName:NSLocalizedString(@"hat left", @"hat switch left state")
-                                                    did:@"Left"
-                                                   base:self],
+                                                    eid:@"Left"
+                                                   parent:self],
                           [[NJInput alloc] initWithName:NSLocalizedString(@"hat right", @"hat switch right state")
-                                                    did:@"Right"
-                                                   base:self]];
+                                                    eid:@"Right"
+                                                   parent:self]];
+        _max = IOHIDElementGetLogicalMax(element);
     }
     return self;
 }
 
 - (id)findSubInputForValue:(IOHIDValueRef)value {
     long parsed = IOHIDValueGetIntegerValue(value);
-    switch (IOHIDElementGetLogicalMax(IOHIDValueGetElement(value))) {
+    switch (_max) {
         case 7: // 8-way switch, 0-7.
             switch (parsed) {
                 case 0: return self.children[0];
@@ -91,7 +98,7 @@ static BOOL active_fourway[20] = {
 
 - (void)notifyEvent:(IOHIDValueRef)value {
     long parsed = IOHIDValueGetIntegerValue(value);
-    long size = IOHIDElementGetLogicalMax(IOHIDValueGetElement(value));
+    long size = _max;
     // Skip first row in table if 0 is not neutral.
     if (size & 1) {
         parsed++;
index 3ad50c9..98c96fd 100644 (file)
@@ -8,10 +8,10 @@
 @interface NJInputPathElement : NSObject
 
 - (id)initWithName:(NSString *)name
-               did:(NSString *)did
-              base:(NJInputPathElement *)base;
+               eid:(NSString *)eid
+            parent:(NJInputPathElement *)parent;
 
-@property (nonatomic, weak) NJInputPathElement *base;
+@property (nonatomic, weak) NJInputPathElement *parent;
 @property (nonatomic, copy) NSString *name;
 @property (nonatomic, readonly) NSString *uid;
 @property (nonatomic, strong) NSArray *children;
index eb5bfa7..ca9265e 100644 (file)
@@ -9,16 +9,16 @@
 #include "NJInputPathElement.h"
 
 @implementation NJInputPathElement {
-    NSString *_did;
+    NSString *_eid;
 }
 
 - (id)initWithName:(NSString *)name
-               did:(NSString *)did
-              base:(NJInputPathElement *)base {
+               eid:(NSString *)eid
+            parent:(NJInputPathElement *)parent {
     if ((self = [super init])) {
         self.name = name;
-        self.base = base;
-        _did = did;
+        self.parent = parent;
+        _eid = eid;
     }
     return self;
 }
@@ -33,7 +33,7 @@
 }
 
 - (NSString *)uid {
-    return [NSString stringWithFormat:@"%@~%@", _base.uid, _did];
+    return [NSString stringWithFormat:@"%@~%@", _parent.uid, _eid];
 }
 
 - (NJInputPathElement *)elementForUID:(NSString *)uid {
index 4c8ce64..95d28c4 100644 (file)
     } else {
         self.enabled = YES;
         NSString *inpFullName = input.name;
-        for (NJInputPathElement *cur = input.base; cur; cur = cur.base) {
+        for (NJInputPathElement *cur = input.parent; cur; cur = cur.parent) {
             inpFullName = [[NSString alloc] initWithFormat:@"%@ ▸ %@", cur.name, inpFullName];
         }
         title.stringValue = inpFullName;
index 285b858..bc56ad4 100644 (file)
@@ -46,7 +46,7 @@
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
-       <string>294</string>
+       <string>296</string>
        <key>LSApplicationCategoryType</key>
        <string>public.app-category.utilities</string>
        <key>NSHumanReadableCopyright</key>