2 // NJDeviceController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "NJDeviceController.h"
11 #import "NJMappingsController.h"
16 #import "NJDeviceViewController.h"
18 @implementation NJDeviceController {
19 NJHIDManager *_hidManager;
20 NSTimer *_continuousOutputsTick;
21 NSMutableArray *_continousOutputs;
22 NSMutableArray *_devices;
25 #define NSSTR(e) ((NSString *)CFSTR(e))
28 if ((self = [super init])) {
29 _devices = [[NSMutableArray alloc] initWithCapacity:16];
30 _continousOutputs = [[NSMutableArray alloc] initWithCapacity:32];
32 _hidManager = [[NJHIDManager alloc] initWithCriteria:@[
33 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
34 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_Joystick) },
35 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
36 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_GamePad) },
37 @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop),
38 NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_MultiAxisController) }
42 // The HID manager uses 5-10ms per second doing basically
43 // nothing if a noisy device is plugged in (the % of that
44 // spent in input_callback is negligible, so it's not
45 // something we can make faster). I don't really think that's
46 // acceptable, CPU/power wise. So if simulation is disabled
47 // and the window is closed, just switch off the HID manager
48 // entirely. This probably also has some marginal benefits for
49 // compatibility with other applications that want exclusive
51 [NSNotificationCenter.defaultCenter
53 selector:@selector(stopHidIfDisabled:)
54 name:NSApplicationDidResignActiveNotification
56 [NSNotificationCenter.defaultCenter
58 selector:@selector(startHid)
59 name:NSApplicationDidBecomeActiveNotification
66 [NSNotificationCenter.defaultCenter removeObserver:self];
67 [_continuousOutputsTick invalidate];
70 - (NJDevice *)objectAtIndexedSubscript:(NSUInteger)idx {
71 return idx < _devices.count ? _devices[idx] : nil;
75 return _devices.count;
78 - (void)addRunningOutput:(NJOutput *)output {
79 // Axis events will trigger every small movement, don't keep
80 // re-adding them or they trigger multiple times each time.
81 if (![_continousOutputs containsObject:output])
82 [_continousOutputs addObject:output];
83 if (!_continuousOutputsTick) {
84 _continuousOutputsTick = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0
86 selector:@selector(updateContinuousOutputs:)
92 - (void)runOutputForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
93 NJDevice *dev = [self findDeviceByRef:device];
94 NJInput *mainInput = [dev inputForEvent:value];
95 [mainInput notifyEvent:value];
96 NSArray *children = mainInput.children ? mainInput.children : mainInput ? @[mainInput] : @[];
97 for (NJInput *subInput in children) {
98 NJOutput *output = mappingsController.currentMapping[subInput];
99 output.magnitude = subInput.magnitude;
100 output.running = subInput.active;
101 if ((output.running || output.magnitude) && output.isContinuous)
102 [self addRunningOutput:output];
106 - (void)showOutputForDevice:(IOHIDDeviceRef)device value:(IOHIDValueRef)value {
107 NJDevice *dev = [self findDeviceByRef:device];
108 NJInput *handler = [dev handlerForEvent:value];
112 [self.delegate deviceController:self didInput:handler];
115 - (void)hidManager:(NJHIDManager *)manager
116 valueChanged:(IOHIDValueRef)value
117 fromDevice:(IOHIDDeviceRef)device {
118 if (self.simulatingEvents
119 && !NSApplication.sharedApplication.isActive) {
120 [self runOutputForDevice:device value:value];
122 [self showOutputForDevice:device value:value];
126 - (void)addDevice:(NJDevice *)device {
130 for (NJDevice *used in _devices) {
131 if ([used isEqual:device]) {
136 } while (!available);
138 [_devices addObject:device];
141 - (void)hidManager:(NJHIDManager *)manager deviceAdded:(IOHIDDeviceRef)device {
142 NJDevice *match = [[NJDevice alloc] initWithDevice:device];
143 [self addDevice:match];
144 [self.delegate deviceController:self didAddDevice:match];
147 - (NJDevice *)findDeviceByRef:(IOHIDDeviceRef)device {
148 for (NJDevice *dev in _devices)
149 if (dev.device == device)
154 - (void)hidManager:(NJHIDManager *)manager deviceRemoved:(IOHIDDeviceRef)device {
155 NJDevice *match = [self findDeviceByRef:device];
157 NSInteger idx = [_devices indexOfObjectIdenticalTo:match];
158 [_devices removeObjectAtIndex:idx];
159 [self.delegate deviceController:self didRemoveDeviceAtIndex:idx];
163 - (void)updateContinuousOutputs:(NSTimer *)timer {
164 self.mouseLoc = [NSEvent mouseLocation];
165 for (NJOutput *output in [_continousOutputs copy]) {
166 if (![output update:self]) {
167 [_continousOutputs removeObject:output];
170 if (!_continousOutputs.count) {
171 [_continuousOutputsTick invalidate];
172 _continuousOutputsTick = nil;
176 - (void)hidManager:(NJHIDManager *)manager didError:(NSError *)error {
177 [self.delegate deviceController:self didError:error];
178 self.simulatingEvents = NO;
181 - (void)hidManagerDidStart:(NJHIDManager *)manager {
182 [self.delegate deviceControllerDidStartHID:self];
185 - (void)hidManagerDidStop:(NJHIDManager *)manager {
186 [_devices removeAllObjects];
187 [self.delegate deviceControllerDidStopHID:self];
198 - (void)setSimulatingEvents:(BOOL)simulatingEvents {
199 if (simulatingEvents != _simulatingEvents) {
200 _simulatingEvents = simulatingEvents;
201 NSInteger state = simulatingEvents ? NSOnState : NSOffState;
202 simulatingEventsButton.state = state;
203 NSString *name = simulatingEvents
204 ? NJEventSimulationStarted
205 : NJEventSimulationStopped;
206 [NSNotificationCenter.defaultCenter postNotificationName:name
209 if (!simulatingEvents && !NSApplication.sharedApplication.isActive)
216 - (void)stopHidIfDisabled:(NSNotification *)application {
217 if (!self.simulatingEvents && !NSProcessInfo.processInfo.isBeingDebugged)
221 - (IBAction)simulatingEventsChanged:(NSButton *)sender {
222 self.simulatingEvents = sender.state == NSOnState;
225 - (NSInteger)numberOfDevicesInDeviceList:(NJDeviceViewController *)dvc {
226 return _devices.count;
229 - (NJDevice *)deviceViewController:(NJDeviceViewController *)dvc
230 deviceForIndex:(NSUInteger)idx {
231 return _devices[idx];
234 - (NJInputPathElement *)objectForKeyedSubscript:(NSString *)uid {
235 for (NJDevice *dev in _devices) {
236 id item = [dev elementForUID:uid];