I write a small utility in swift which registers a callback using IOHIDManagerRegisterInputValueCallback
. I am trying to be a good citizen and cleanup after myself.
The [documentation] (https://developer.apple.com/library/content/documentation/DeviceDrivers/Conceptual/HID/new_api_10_5/tn2187.html#//apple_ref/doc/uid/TP40000970-CH214-SW61) says that to unregister one should call the registration function with Null
Note: To unregister pass NULL for the callback.
unfortunately
IOHIDManagerRegisterInputValueCallback( hidManager, nil , nil);
doesn't compile saying :
Nil is not compatible with expected argument type 'IOHIDValueCallback' (aka '@convention(c) (Optional, Int32, Optional, IOHIDValue) -> ()')
How can I cast nil to the correct type ?
Here is a sample code which should compile :
import Foundation
import Carbon
import IOKit
import IOKit.usb
import IOKit.hid
class IOEventManager{
func start()->Void{
let hidManager = IOHIDManagerCreate( kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone) );
let context = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque());
IOHIDManagerRegisterInputValueCallback( hidManager, nil, context);
}
}
if you want a full project, checkout https://github.com/jeantil/autokbisw and look at IOKeyEventMonitor#deinit
After a long while I opened that project again and while perusing the method signature again, I noticed that the signature of the swift binding actually expects an IOHIDValueCallback?
which means an optional IOHIDValueCallback
@available(OSX 10.5, *)
public func IOHIDManagerRegisterInputValueCallback(_ manager: IOHIDManager, _ callback: IOKit.IOHIDValueCallback?, _ context: UnsafeMutableRawPointer?)
This was not obviously apparent to me because you can directly pass the IOHIDValueCallback
value without explicit wrapping when calling the method.
Noticing this made the solution obvious :
IOHIDManagerRegisterInputValueCallback( hidManager, Optional.none , context);
The program now compiles and seems to behave properly.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments