From 561fa1774ce33b5a4a8112ba5802cc33a2ca819d Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 14 Mar 2013 14:02:06 +0100 Subject: [PATCH] Write proper constructors for NJInput and subclasses. Rename base to parent to match children, d(evice internal)id to e(lement)id to avoid confusion with actual device IDs/UID. --- Classes/NJDevice.m | 40 ++++++++++++++++++------------------ Classes/NJDeviceController.m | 4 ++-- Classes/NJInput.h | 9 +++++--- Classes/NJInput.m | 14 ++++++++----- Classes/NJInputAnalog.h | 4 +++- Classes/NJInputAnalog.m | 34 ++++++++++++++++-------------- Classes/NJInputButton.h | 4 +++- Classes/NJInputButton.m | 18 ++++++++-------- Classes/NJInputHat.h | 4 +++- Classes/NJInputHat.m | 37 +++++++++++++++++++-------------- Classes/NJInputPathElement.h | 6 +++--- Classes/NJInputPathElement.m | 12 +++++------ Classes/NJOutputController.m | 2 +- Info.plist | 2 +- 14 files changed, 108 insertions(+), 82 deletions(-) diff --git a/Classes/NJDevice.m b/Classes/NJDevice.m index 3b0cc6a..e4c6b3d 100644 --- a/Classes/NJDevice.m +++ b/Classes/NJDevice.m @@ -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]; diff --git a/Classes/NJDeviceController.m b/Classes/NJDeviceController.m index 6fc4d13..8cc5f2a 100644 --- a/Classes/NJDeviceController.m +++ b/Classes/NJDeviceController.m @@ -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 diff --git a/Classes/NJInput.h b/Classes/NJInput.h index b9d2dca..db47121 100644 --- a/Classes/NJInput.h +++ b/Classes/NJInput.h @@ -10,10 +10,13 @@ @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; diff --git a/Classes/NJInput.m b/Classes/NJInput.m index 78376e6..82d5993 100644 --- a/Classes/NJInput.m +++ b/Classes/NJInput.m @@ -10,11 +10,15 @@ @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; } diff --git a/Classes/NJInputAnalog.h b/Classes/NJInputAnalog.h index e79ed89..de35e3d 100644 --- a/Classes/NJInputAnalog.h +++ b/Classes/NJInputAnalog.h @@ -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 diff --git a/Classes/NJInputAnalog.m b/Classes/NJInputAnalog.m index af64954..3746c11 100644 --- a/Classes/NJInputAnalog.m +++ b/Classes/NJInputAnalog.m @@ -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]; diff --git a/Classes/NJInputButton.h b/Classes/NJInputButton.h index 31cba73..e9abdd3 100644 --- a/Classes/NJInputButton.h +++ b/Classes/NJInputButton.h @@ -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 diff --git a/Classes/NJInputButton.m b/Classes/NJInputButton.m index caa759b..354220a 100644 --- a/Classes/NJInputButton.m +++ b/Classes/NJInputButton.m @@ -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; } diff --git a/Classes/NJInputHat.h b/Classes/NJInputHat.h index 5290d71..67bb21d 100644 --- a/Classes/NJInputHat.h +++ b/Classes/NJInputHat.h @@ -10,6 +10,8 @@ @interface NJInputHat : NJInput -- (id)initWithIndex:(int)index; +- (id)initWithElement:(IOHIDElementRef)element + index:(int)index + parent:(NJInputPathElement *)parent; @end diff --git a/Classes/NJInputHat.m b/Classes/NJInputHat.m index 46ea94f..0a5afa2 100644 --- a/Classes/NJInputHat.m +++ b/Classes/NJInputHat.m @@ -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++; diff --git a/Classes/NJInputPathElement.h b/Classes/NJInputPathElement.h index 3ad50c9..98c96fd 100644 --- a/Classes/NJInputPathElement.h +++ b/Classes/NJInputPathElement.h @@ -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; diff --git a/Classes/NJInputPathElement.m b/Classes/NJInputPathElement.m index eb5bfa7..ca9265e 100644 --- a/Classes/NJInputPathElement.m +++ b/Classes/NJInputPathElement.m @@ -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 { diff --git a/Classes/NJOutputController.m b/Classes/NJOutputController.m index 4c8ce64..95d28c4 100644 --- a/Classes/NJOutputController.m +++ b/Classes/NJOutputController.m @@ -229,7 +229,7 @@ } 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; diff --git a/Info.plist b/Info.plist index 285b858..bc56ad4 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 294 + 296 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.20.1