X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJDeviceController.m;h=bdd1bbf9e0c21f382f861c97bf9ec95c0877b3e7;hp=610c07b5a4f29c925572a850209c3b4e3d6bfc9d;hb=e7c0b2d96e9e3209b5ba80cc1fdc8e7213cb5302;hpb=402a1b679ced5422e46c7a5caeecc45e5ed878db diff --git a/Classes/NJDeviceController.m b/Classes/NJDeviceController.m index 610c07b..bdd1bbf 100644 --- a/Classes/NJDeviceController.m +++ b/Classes/NJDeviceController.m @@ -14,16 +14,15 @@ #import "NJOutput.h" #import "NJOutputController.h" #import "NJEvents.h" +#import "NJDeviceViewController.h" @implementation NJDeviceController { NJHIDManager *_hidManager; NSTimer *_continuousOutputsTick; NSMutableArray *_continousOutputs; NSMutableArray *_devices; - NSMutableArray *_expanded; } -#define EXPANDED_MEMORY_MAX_SIZE 100 #define NSSTR(e) ((NSString *)CFSTR(e)) - (id)init { @@ -31,12 +30,6 @@ _devices = [[NSMutableArray alloc] initWithCapacity:16]; _continousOutputs = [[NSMutableArray alloc] initWithCapacity:32]; - NSArray *expanded = [NSUserDefaults.standardUserDefaults objectForKey:@"expanded rows"]; - if (![expanded isKindOfClass:NSArray.class]) - expanded = @[]; - _expanded = [[NSMutableArray alloc] initWithCapacity:MAX(16, _expanded.count)]; - [_expanded addObjectsFromArray:expanded]; - _hidManager = [[NJHIDManager alloc] initWithCriteria:@[ @{ NSSTR(kIOHIDDeviceUsagePageKey) : @(kHIDPage_GenericDesktop), NSSTR(kIOHIDDeviceUsageKey) : @(kHIDUsage_GD_Joystick) }, @@ -47,17 +40,11 @@ ] delegate:self]; - [NSNotificationCenter.defaultCenter - addObserver:self - selector:@selector(startHid) - name:NSApplicationDidFinishLaunchingNotification - object:nil]; - // The HID manager uses 5-10ms per second doing basically // nothing if a noisy device is plugged in (the % of that // spent in input_callback is negligible, so it's not // something we can make faster). I don't really think that's - // acceptable, CPU/power wise. So if translation is disabled + // acceptable, CPU/power wise. So if simulation is disabled // and the window is closed, just switch off the HID manager // entirely. This probably also has some marginal benefits for // compatibility with other applications that want exclusive @@ -81,26 +68,6 @@ [_continuousOutputsTick invalidate]; } -- (void)expandRecursive:(id )pathElement { - if (pathElement) { - [self expandRecursive:pathElement.base]; - [outlineView expandItem:pathElement]; - } -} - -- (id)elementForUID:(NSString *)uid { - for (NJDevice *dev in _devices) { - id item = [dev elementForUID:uid]; - if (item) - return item; - } - return nil; -} - -- (void)expandRecursiveByUID:(NSString *)uid { - [self expandRecursive:[self elementForUID:uid]]; -} - - (void)addRunningOutput:(NJOutput *)output { // Axis events will trigger every small movement, don't keep // re-adding them or they trigger multiple times each time. @@ -135,45 +102,42 @@ if (!handler) return; - [self expandRecursive:handler]; - [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:[outlineView rowForItem:handler]] - byExtendingSelection: NO]; + [devicesViewController expandAndSelectItem:handler]; [outputController focusKey]; } - (void)hidManager:(NJHIDManager *)manager valueChanged:(IOHIDValueRef)value fromDevice:(IOHIDDeviceRef)device { - if (self.translatingEvents) { + if (self.simulatingEvents + && !NSApplication.sharedApplication.isActive) { [self runOutputForDevice:device value:value]; } else { [self showOutputForDevice:device value:value]; } } -static int findAvailableIndex(NSArray *list, NJDevice *dev) { - for (int index = 1; ; index++) { - BOOL available = YES; - for (NJDevice *used in list) { - if ([used.productName isEqualToString:dev.productName] - && used.index == index) { +- (void)addDevice:(NJDevice *)device { + BOOL available; + do { + available = YES; + for (NJDevice *used in _devices) { + if ([used isEqual:device]) { + device.index += 1; available = NO; - break; } } - if (available) - return index; - } + } while (!available); + + [_devices addObject:device]; } - (void)hidManager:(NJHIDManager *)manager deviceAdded:(IOHIDDeviceRef)device { NJDevice *match = [[NJDevice alloc] initWithDevice:device]; - match.index = findAvailableIndex(_devices, match); - [_devices addObject:match]; - [outlineView reloadData]; - [self reexpandAll]; - hidSleepingPrompt.hidden = YES; - connectDevicePrompt.hidden = !!_devices.count; + [devicesViewController beginUpdates]; + [self addDevice:match]; + [devicesViewController addedDevice:match atIndex:_devices.count - 1]; + [devicesViewController endUpdates]; } - (NJDevice *)findDeviceByRef:(IOHIDDeviceRef)device { @@ -187,13 +151,12 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) { NJDevice *match = [self findDeviceByRef:device]; IOHIDDeviceRegisterInputValueCallback(device, NULL, NULL); if (match) { - [_devices removeObject:match]; - [outlineView reloadData]; - connectDevicePrompt.hidden = !!_devices.count; - hidSleepingPrompt.hidden = YES; + NSInteger idx = [_devices indexOfObjectIdenticalTo:match]; + [devicesViewController beginUpdates]; + [_devices removeObjectAtIndex:idx]; + [devicesViewController removedDevice:match atIndex:idx]; + [devicesViewController endUpdates]; } - if (_devices.count == 1) - [outlineView expandItem:_devices[0]]; } - (void)updateContinuousOutputs:(NSTimer *)timer { @@ -210,23 +173,28 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) { } - (void)hidManager:(NJHIDManager *)manager didError:(NSError *)error { - [outlineView.window presentError:error - modalForWindow:outlineView.window - delegate:nil - didPresentSelector:nil - contextInfo:nil]; + // Since the error shows the window, it can trigger another attempt + // to re-open the HID manager, which will also probably fail and error, + // so don't bother repeating ourselves. + if (!simulatingEventsButton.window.attachedSheet) { + [NSApplication.sharedApplication activateIgnoringOtherApps:YES]; + [simulatingEventsButton.window makeKeyAndOrderFront:nil]; + [simulatingEventsButton.window presentError:error + modalForWindow:simulatingEventsButton.window + delegate:nil + didPresentSelector:nil + contextInfo:nil]; + } + self.simulatingEvents = NO; } - (void)hidManagerDidStart:(NJHIDManager *)manager { - hidSleepingPrompt.hidden = YES; - connectDevicePrompt.hidden = !!_devices.count; + [devicesViewController hidStarted]; } - (void)hidManagerDidStop:(NJHIDManager *)manager { [_devices removeAllObjects]; - [outlineView reloadData]; - hidSleepingPrompt.hidden = NO; - connectDevicePrompt.hidden = YES; + [devicesViewController hidStopped]; } - (void)startHid { @@ -238,105 +206,72 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) { } - (NJInput *)selectedInput { - id item = [outlineView itemAtRow:outlineView.selectedRow]; - return (!item.children && item.base) ? item : nil; + return (NJInput *)devicesViewController.selectedHandler; } -- (NSInteger)outlineView:(NSOutlineView *)outlineView - numberOfChildrenOfItem:(id )item { - return item ? item.children.count : _devices.count; -} - -- (BOOL)outlineView:(NSOutlineView *)outlineView - isItemExpandable:(id )item { - return item ? [[item children] count] > 0: YES; -} - -- (id)outlineView:(NSOutlineView *)outlineView - child:(NSInteger)index - ofItem:(id )item { - return item ? item.children[index] : _devices[index]; -} +- (void)setSimulatingEvents:(BOOL)simulatingEvents { + if (simulatingEvents != _simulatingEvents) { + _simulatingEvents = simulatingEvents; + NSInteger state = simulatingEvents ? NSOnState : NSOffState; + simulatingEventsButton.state = state; + NSString *name = simulatingEvents + ? NJEventSimulationStarted + : NJEventSimulationStopped; + [NSNotificationCenter.defaultCenter postNotificationName:name + object:self]; -- (id)outlineView:(NSOutlineView *)outlineView -objectValueForTableColumn:(NSTableColumn *)tableColumn - byItem:(id )item { - return item ? item.name : @"root"; + if (!simulatingEvents && !NSApplication.sharedApplication.isActive) + [self stopHid]; + else + [self startHid]; + } } -- (void)outlineViewSelectionDidChange:(NSNotification *)notification { - id item = [outlineView itemAtRow:outlineView.selectedRow]; - if (item) - [NSUserDefaults.standardUserDefaults setObject:item.uid - forKey:@"selected input"]; - [outputController loadCurrent]; +- (void)stopHidIfDisabled:(NSNotification *)application { + if (!self.simulatingEvents && !NSProcessInfo.processInfo.isBeingDebugged) + [self stopHid]; } -- (BOOL)outlineView:(NSOutlineView *)outlineView - isGroupItem:(id )item { - return [item isKindOfClass:NJDevice.class]; +- (IBAction)simulatingEventsChanged:(NSButton *)sender { + self.simulatingEvents = sender.state == NSOnState; } -- (BOOL)outlineView:(NSOutlineView *)outlineView_ - shouldSelectItem:(id )item { - return ![self outlineView:outlineView_ isGroupItem:item]; +- (NSInteger)numberOfDevicesInDeviceList:(NJDeviceViewController *)dvc { + return _devices.count; } -- (void)outlineViewItemDidExpand:(NSNotification *)notification { - id item = notification.userInfo[@"NSObject"]; - NSString *uid = item.uid; - if (![_expanded containsObject:uid]) - [_expanded addObject:uid]; - while (_expanded.count > EXPANDED_MEMORY_MAX_SIZE) - [_expanded removeObjectAtIndex:0]; - [NSUserDefaults.standardUserDefaults setObject:_expanded - forKey:@"expanded rows"]; +- (NJDevice *)deviceViewController:(NJDeviceViewController *)dvc + deviceForIndex:(NSUInteger)idx { + return _devices[idx]; } -- (void)outlineViewItemDidCollapse:(NSNotification *)notification { - id item = notification.userInfo[@"NSObject"]; - [_expanded removeObject:item.uid]; - [NSUserDefaults.standardUserDefaults setObject:_expanded - forKey:@"expanded rows"]; +- (id)deviceViewController:(NJDeviceViewController *)dvc + elementForUID:(NSString *)uid { + for (NJDevice *dev in _devices) { + id item = [dev elementForUID:uid]; + if (item) + return item; + } + return nil; } -- (void)setTranslatingEvents:(BOOL)translatingEvents { - if (translatingEvents != _translatingEvents) { - _translatingEvents = translatingEvents; - NSInteger state = translatingEvents ? NSOnState : NSOffState; - translatingEventsButton.state = state; - NSString *name = translatingEvents - ? NJEventTranslationActivated - : NJEventTranslationDeactivated; - [NSNotificationCenter.defaultCenter postNotificationName:name - object:self]; - - if (!translatingEvents && !NSApplication.sharedApplication.isActive) - [self stopHid]; - else if (translatingEvents || NSApplication.sharedApplication.isActive) - [self startHid]; - } +- (void)deviceViewControllerDidSelectNothing:(NJDeviceViewController *)dvc { + [outputController loadCurrent]; } -- (void)reexpandAll { - for (NSString *uid in [_expanded copy]) - [self expandRecursiveByUID:uid]; - if (outlineView.selectedRow == -1) { - NSString *selectedUid = [NSUserDefaults.standardUserDefaults objectForKey:@"selected input"]; - id item = [self elementForUID:selectedUid]; - NSInteger row = [outlineView rowForItem:item]; - if (row >= 0) - [outlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; - } +- (void)deviceViewController:(NJDeviceViewController *)dvc + didSelectBranch:(NJInputPathElement *)handler { + [outputController loadCurrent]; } -- (void)stopHidIfDisabled:(NSNotification *)application { - if (!self.translatingEvents) - [self stopHid]; +- (void)deviceViewController:(NJDeviceViewController *)dvc + didSelectHandler:(NJInputPathElement *)handler { + [outputController loadCurrent]; } -- (IBAction)translatingEventsChanged:(NSButton *)sender { - self.translatingEvents = sender.state == NSOnState; +- (void)deviceViewController:(NJDeviceViewController *)dvc + didSelectDevice:(NJInputPathElement *)device { + [outputController loadCurrent]; } @end