#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)];
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;
|| 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];
}
}
- (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];
- (void)expandRecursive:(NJInputPathElement *)pathElement {
if (pathElement) {
- [self expandRecursive:pathElement.base];
+ [self expandRecursive:pathElement.parent];
[outlineView expandItem:pathElement];
}
}
- (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
@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;
@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;
}
@interface NJInputAnalog : NJInput
-- (id)initWithIndex:(int)index rawMin:(long)rawMin rawMax:(long)rawMax;
+- (id)initWithElement:(IOHIDElementRef)element
+ index:(int)index
+ parent:(NJInputPathElement *)parent;
@end
#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)
}
- (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];
@interface NJInputButton : NJInput
-- (id)initWithName:(NSString *)name idx:(int)idx max:(long)max;
+- (id)initWithElement:(IOHIDElementRef)element
+ index:(int)index
+ parent:(NJInputPathElement *)parent;
@end
#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;
}
@interface NJInputHat : NJInput
-- (id)initWithIndex:(int)index;
+- (id)initWithElement:(IOHIDElementRef)element
+ index:(int)index
+ parent:(NJInputPathElement *)parent;
@end
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];
- (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++;
@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;
#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;
}
}
- (NSString *)uid {
- return [NSString stringWithFormat:@"%@~%@", _base.uid, _did];
+ return [NSString stringWithFormat:@"%@~%@", _parent.uid, _eid];
}
- (NJInputPathElement *)elementForUID:(NSString *)uid {
} 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;
<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>