Skip to content

Commit

Permalink
Switch hs.streamdeck from using implied memory management to using AR…
Browse files Browse the repository at this point in the history
…C for storing IOHIDManagerRef objects
  • Loading branch information
cmsj committed Apr 4, 2018
1 parent c2b79fe commit 3b8f170
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion extensions/streamdeck/HSStreamDeckManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#import "streamdeck.h"

@interface HSStreamDeckManager : NSObject
@property (nonatomic) IOHIDManagerRef ioHIDManager;
@property (nonatomic, strong) id ioHIDManager;
@property (nonatomic, strong) NSMutableArray *devices;
@property (nonatomic) int discoveryCallbackRef;

Expand Down
22 changes: 11 additions & 11 deletions extensions/streamdeck/HSStreamDeckManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,45 @@ - (id)init {
self.discoveryCallbackRef = LUA_NOREF;

// Create a HID device manager
self.ioHIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone);
self.ioHIDManager = CFBridgingRelease(IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone));
//NSLog(@"Created HID Manager: %p", (void *)self.ioHIDManager);

// Configure the HID manager to match against Stream Deck devices
NSDictionary *match = @{
@(kIOHIDVendorIDKey): @0x0fd9,
@(kIOHIDProductIDKey): @0x0060,
};
IOHIDManagerSetDeviceMatching (self.ioHIDManager, (__bridge CFDictionaryRef)match);
IOHIDManagerSetDeviceMatching ((__bridge IOHIDManagerRef)self.ioHIDManager, (__bridge CFDictionaryRef)match);

// Add our callbacks for relevant events
IOHIDManagerRegisterDeviceMatchingCallback(self.ioHIDManager, HIDconnect, (__bridge void*)self);
IOHIDManagerRegisterDeviceRemovalCallback(self.ioHIDManager, HIDdisconnect, (__bridge void*)self);
IOHIDManagerRegisterDeviceMatchingCallback((__bridge IOHIDManagerRef)self.ioHIDManager, HIDconnect, (__bridge void*)self);
IOHIDManagerRegisterDeviceRemovalCallback((__bridge IOHIDManagerRef)self.ioHIDManager, HIDdisconnect, (__bridge void*)self);

// Start our HID manager
IOHIDManagerScheduleWithRunLoop(self.ioHIDManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDManagerScheduleWithRunLoop((__bridge IOHIDManagerRef)self.ioHIDManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
}
return self;
}

- (void)doGC {
// Remove our callbacks
IOHIDManagerRegisterDeviceMatchingCallback(self.ioHIDManager, NULL, (__bridge void*)self);
IOHIDManagerRegisterDeviceRemovalCallback(self.ioHIDManager, NULL, (__bridge void*)self);
IOHIDManagerRegisterDeviceMatchingCallback((__bridge IOHIDManagerRef)self.ioHIDManager, NULL, (__bridge void*)self);
IOHIDManagerRegisterDeviceRemovalCallback((__bridge IOHIDManagerRef)self.ioHIDManager, NULL, (__bridge void*)self);

// Remove our HID manager from the runloop
IOHIDManagerUnscheduleFromRunLoop(self.ioHIDManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDManagerUnscheduleFromRunLoop((__bridge IOHIDManagerRef)self.ioHIDManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

// Deallocate the HID manager
CFRelease(self.ioHIDManager);
self.ioHIDManager = nil;
}

- (BOOL)startHIDManager {
IOReturn tIOReturn = IOHIDManagerOpen(self.ioHIDManager, kIOHIDOptionsTypeNone);
IOReturn tIOReturn = IOHIDManagerOpen((__bridge IOHIDManagerRef)self.ioHIDManager, kIOHIDOptionsTypeNone);
return tIOReturn == kIOReturnSuccess;
}

- (BOOL)stopHIDManager {
IOReturn tIOReturn = IOHIDManagerClose(self.ioHIDManager, kIOHIDOptionsTypeNone);
IOReturn tIOReturn = IOHIDManagerClose((__bridge IOHIDManagerRef)self.ioHIDManager, kIOHIDOptionsTypeNone);
return tIOReturn == kIOReturnSuccess;
}

Expand Down

0 comments on commit 3b8f170

Please sign in to comment.