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 3b0cc6a4c2c53e181ca73e79e863c15e35c30caa..e4c6b3d27c0f735376eeb60f8cb87c332ca86c89 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 6fc4d13267a2ee060745f1a6dd600e9d67cd3d84..8cc5f2aadf7ff34528c0c39a028d009d5ced68a3 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 b9d2dcab28226b4f1270a83de87d9a6499720f02..db471217a65fa8ce2e384d68fb81a6ac40489aed 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 78376e6dfe18472d6a170929f5a6215c9aaddb01..82d59935bcaefef716bf9893f1cd240cec9f9409 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 e79ed896d71964b6d0b05096031ea089cdb82ab5..de35e3d812832a9a9b329fa65f3d5ca734df0b4f 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 af6495430cbe424c1f7dc2be2fd09acd3e29d119..3746c1182c2b4b304938a48b4854a34ada3869a9 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 31cba7363475c06e3bb170490c9ed927700230f6..e9abdd3ef730dee53a216847bcd7eca0588c552e 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 caa759b6844888358eda0a95c718fe58f0073d13..354220a739b784612e61d347b13e20103cc60f1e 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 5290d71e9b80996f8d2ec482e3e602059b869eea..67bb21ded1fd7c297dd9a89988b08bc9a683a9c7 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 46ea94f2a61eab7715ad342261d11913b4745899..0a5afa2901149f2362e616512756baf8ed702898 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 3ad50c9bc5e183c1cd31d62800ba2333faf42d71..98c96fdbab4670b11aa2ebdea7224c0621bcef6f 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 eb5bfa7b5bcb92f1516032787e28d1e375b9eda7..ca9265e76f3a8775341fd7a99485dc16e3b946f4 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 4c8ce640afd36b75e238810d213c365d66425734..95d28c44e8344084c39775da6e0de0f587b21494 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 285b858ad8ecd57a3ea1825dd5e19e66cafff26a..bc56ad47ccc1a6e81e8edf4ef3706bb61896527c 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>