-//
-// NJHIDManager.h
-// Enjoyable
-//
-// Created by Joe Wreschnig on 3/13/13.
-//
-//
-
#import <Foundation/Foundation.h>
+#import <IOKit/hid/IOHIDLib.h>
@protocol NJHIDManagerDelegate;
@interface NJHIDManager : NSObject
+ // Light OO wrapper around IOKit callbacks.
-@property (nonatomic, copy) NSArray *criteria;
- // Changing the criteria may trigger a stop and restart. If this happens,
- // messages will be sent to the delegate as usual.
+- (id)initWithCriteria:(NSArray *)criteria
+ delegate:(id <NJHIDManagerDelegate>)delegate;
-@property (nonatomic, assign) BOOL running;
@property (nonatomic, weak) id <NJHIDManagerDelegate> delegate;
-- (id)initWithCriteria:(NSArray *)criteria
- delegate:(id <NJHIDManagerDelegate>)delegate;
+@property (nonatomic, copy) NSArray *criteria;
+ // Changing the criteria may trigger a stop and restart. If this
+ // happens, messages will be sent to the delegate as usual.
+
+@property (nonatomic, assign) BOOL running;
+ // Assigning YES is like sending start; NO like stop.
- (void)start;
- (void)stop;
@protocol NJHIDManagerDelegate
-- (void)hidManagerDidStart:(NJHIDManager *)manager;
-- (void)hidManagerDidStop:(NJHIDManager *)manager;
- // Stopping the device will not trigger any removal events, so any
+- (void)HIDManagerDidStart:(NJHIDManager *)manager;
+- (void)HIDManagerDidStop:(NJHIDManager *)manager;
+ // Stopping the device will not trigger any removal messages, so any
// cleanup in the delegate must be done here.
-- (void)hidManager:(NJHIDManager *)manager didError:(NSError *)error;
+- (void)HIDManager:(NJHIDManager *)manager deviceAdded:(IOHIDDeviceRef)device;
+- (void)HIDManager:(NJHIDManager *)manager deviceRemoved:(IOHIDDeviceRef)device;
-- (void)hidManager:(NJHIDManager *)manager deviceAdded:(IOHIDDeviceRef)device;
-- (void)hidManager:(NJHIDManager *)manager deviceRemoved:(IOHIDDeviceRef)device;
-
-- (void)hidManager:(NJHIDManager *)manager
+- (void)HIDManager:(NJHIDManager *)manager
valueChanged:(IOHIDValueRef)value
fromDevice:(IOHIDDeviceRef)device;
+
+- (void)HIDManager:(NJHIDManager *)manager didError:(NSError *)error;
+
@end
-//
-// NJHIDManager.m
-// Enjoyable
-//
-// Created by Joe Wreschnig on 3/13/13.
-//
-//
-
#import "NJHIDManager.h"
@implementation NJHIDManager {
[self stop];
}
-static void input_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDValueRef value) {
+static void _input(void *ctx, IOReturn inResult, void *inSender, IOHIDValueRef value) {
NJHIDManager *self = (__bridge NJHIDManager *)ctx;
IOHIDDeviceRef device = IOHIDQueueGetDevice(inSender);
- [self.delegate hidManager:self valueChanged:value fromDevice:device];
+ [self.delegate HIDManager:self valueChanged:value fromDevice:device];
}
-static void add_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
+static void _add(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
NJHIDManager *self = (__bridge NJHIDManager *)ctx;
- [self.delegate hidManager:self deviceAdded:device];
- IOHIDDeviceRegisterInputValueCallback(device, input_callback, ctx);
+ [self.delegate HIDManager:self deviceAdded:device];
+ IOHIDDeviceRegisterInputValueCallback(device, _input, ctx);
}
-static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
+static void _remove(void *ctx, IOReturn inResult, void *inSender, IOHIDDeviceRef device) {
NJHIDManager *self = (__bridge NJHIDManager *)ctx;
- [self.delegate hidManager:self deviceRemoved:device];
+ [self.delegate HIDManager:self deviceRemoved:device];
}
- (void)start {
NSError *error = [NSError errorWithDomain:NSMachErrorDomain code:ret userInfo:nil];
IOHIDManagerClose(manager, kIOHIDOptionsTypeNone);
CFRelease(manager);
- [self.delegate hidManager:self didError:error];
+ [self.delegate HIDManager:self didError:error];
NSLog(@"Error starting HID manager: %@.", error);
} else {
_manager = manager;
IOHIDManagerScheduleWithRunLoop(_manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
- IOHIDManagerRegisterDeviceMatchingCallback(_manager, add_callback, (__bridge void *)self);
- IOHIDManagerRegisterDeviceRemovalCallback(_manager, remove_callback, (__bridge void *)self);
- [self.delegate hidManagerDidStart:self];
+ IOHIDManagerRegisterDeviceMatchingCallback(_manager, _add, (__bridge void *)self);
+ IOHIDManagerRegisterDeviceRemovalCallback(_manager, _remove, (__bridge void *)self);
+ [self.delegate HIDManagerDidStart:self];
NSLog(@"Started HID manager.");
}
}
IOHIDManagerClose(_manager, kIOHIDOptionsTypeNone);
CFRelease(_manager);
_manager = NULL;
- [self.delegate hidManagerDidStop:self];
+ [self.delegate HIDManagerDidStop:self];
NSLog(@"Stopped HID manager.");
}
return !!_manager;
}
+- (void)setRunning:(BOOL)running {
+ if (running)
+ [self start];
+ else
+ [self stop];
+}
+
- (NSArray *)criteria {
return _criteria;
}
#import "NJEvents.h"
@implementation NJInputController {
- NJHIDManager *_hidManager;
+ NJHIDManager *_HIDManager;
NSTimer *_continuousOutputsTick;
NSMutableArray *_continousOutputs;
NSMutableArray *_devices;
_devices = [[NSMutableArray alloc] initWithCapacity:16];
_continousOutputs = [[NSMutableArray alloc] initWithCapacity:32];
- _hidManager = [[NJHIDManager alloc] initWithCriteria:@[
+ _HIDManager = [[NJHIDManager alloc] initWithCriteria:@[
@{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_Joystick) },
@{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
[self.delegate inputController:self didInput:handler];
}
-- (void)hidManager:(NJHIDManager *)manager
+- (void)HIDManager:(NJHIDManager *)manager
valueChanged:(IOHIDValueRef)value
fromDevice:(IOHIDDeviceRef)device {
if (self.simulatingEvents
[_devices addObject:device];
}
-- (void)hidManager:(NJHIDManager *)manager deviceAdded:(IOHIDDeviceRef)device {
+- (void)HIDManager:(NJHIDManager *)manager deviceAdded:(IOHIDDeviceRef)device {
NJDevice *match = [[NJDevice alloc] initWithDevice:device];
[self addDevice:match];
[self.delegate inputController:self didAddDevice:match];
return nil;
}
-- (void)hidManager:(NJHIDManager *)manager deviceRemoved:(IOHIDDeviceRef)device {
+- (void)HIDManager:(NJHIDManager *)manager deviceRemoved:(IOHIDDeviceRef)device {
NJDevice *match = [self findDeviceByRef:device];
if (match) {
NSInteger idx = [_devices indexOfObjectIdenticalTo:match];
}
}
-- (void)hidManager:(NJHIDManager *)manager didError:(NSError *)error {
+- (void)HIDManager:(NJHIDManager *)manager didError:(NSError *)error {
[self.delegate inputController:self didError:error];
self.simulatingEvents = NO;
}
-- (void)hidManagerDidStart:(NJHIDManager *)manager {
+- (void)HIDManagerDidStart:(NJHIDManager *)manager {
[self.delegate inputControllerDidStartHID:self];
}
-- (void)hidManagerDidStop:(NJHIDManager *)manager {
+- (void)HIDManagerDidStop:(NJHIDManager *)manager {
[_devices removeAllObjects];
[self.delegate inputControllerDidStopHID:self];
}
- (void)startHid {
- [_hidManager start];
+ [_HIDManager start];
}
- (void)stopHid {
- [_hidManager stop];
+ [_HIDManager stop];
}
- (void)setSimulatingEvents:(BOOL)simulatingEvents {
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
- <string>575</string>
+ <string>578</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>NSHumanReadableCopyright</key>
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
-#endif
-
-#import <IOKit/hid/IOHIDLib.h>
+ #import <IOKit/hid/IOHIDLib.h>
-#import "NSError+Description.h"
-#import "NSMenu+RepresentedObjectAccessors.h"
-#import "NSView+FirstResponder.h"
-#import "NSMutableArray+MoveObject.h"
-#import "NSFileManager+UniqueNames.h"
-#import "NSString+FixFilename.h"
-#import "NSRunningApplication+NJPossibleNames.h"
-#import "NSRunningApplication+LoginItem.h"
-#import "NSOutlineView+ItemAccessors.h"
-#import "NSProcessInfo+Debugging.h"
+ #import "NSError+Description.h"
+ #import "NSMenu+RepresentedObjectAccessors.h"
+ #import "NSView+FirstResponder.h"
+ #import "NSMutableArray+MoveObject.h"
+ #import "NSFileManager+UniqueNames.h"
+ #import "NSString+FixFilename.h"
+ #import "NSRunningApplication+NJPossibleNames.h"
+ #import "NSRunningApplication+LoginItem.h"
+ #import "NSOutlineView+ItemAccessors.h"
+ #import "NSProcessInfo+Debugging.h"
+#endif