Reputation: 3577
I can get a hid device like this
import ForceFeedback
import IOKit.hid
private func createDeviceMatchingDictionary( usagePage: Int, usage: Int) -> CFMutableDictionary {
let dict = [
kIOHIDDeviceUsageKey: usage,
kIOHIDDeviceUsagePageKey: usagePage
] as NSDictionary
return dict.mutableCopy() as! NSMutableDictionary;
}
let manager = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone));
let trackpad = createDeviceMatchingDictionary(usagePage: kHIDPage_GenericDesktop, usage: kHIDUsage_GD_Mouse)
IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone) )
IOHIDManagerSetDeviceMatching(manager, trackpad)
let devices = IOHIDManagerCopyDevices(manager)
However none of this returns either a io_service_t
or a UnsafeMutablePointer<FFDeviceObjectReference>
required to use FFCreateDevice()
How do I get a forcefeedback device and use FFCreateEffect
on it?
Upvotes: 0
Views: 307
Reputation: 1746
The idea behind a lot of Objective-C or C style frameworks is that you pass in pointers for the objects you expect to be created by a function, and the functions return codes so that you know if the operation was successful or not.
You can use IOHIDDeviceGetService(device)
to get your io_service_t
. You can then create an optional object of type FFDeviceObjectReference
and pass in its reference to FFCreateDevice
. Here's an (untested) example that seems like it should work:
import IOKit.hid
import ForceFeedback
class ForceFeedback {
public func listen() {
let hidManager = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone))
let hidDevices = [
kIOHIDDeviceUsagePageKey: kHIDPage_GenericDesktop,
kIOHIDDeviceUsageKey: kHIDUsage_GD_Mouse
]
IOHIDManagerSetDeviceMatching(hidManager, hidDevices as CFDictionary)
let deviceMatchingCallback: IOHIDDeviceCallback = {context, result, sender, device in
var ffDevice: FFDeviceObjectReference? = nil
let result = FFCreateDevice(IOHIDDeviceGetService(device), &ffDevice)
if result != FF_OK {
print("could not create force feedback device")
}
var ffEffect: FFEffectObjectReference? = nil
var effectDefinition = FFEFFECT() // You'd create your effect here
// Pick your UUID and build it here. For example, this is the constant force effect type
// E559C460-C5CD-11D6-8A1C-00039353BD00
let constantForce: CFUUID = CFUUIDCreateWithBytes(kCFAllocatorDefault,
0xE5, 0x59, 0xC4, 0x60, 0xC5, 0xCD, 0x11, 0xD6,
0x8A, 0x1C, 0x00, 0x03, 0x93, 0x53, 0xBD, 0x00)
let effectResult = FFDeviceCreateEffect(ffDevice, constantForce, &effectDefinition, &ffEffect)
if effectResult != FF_OK {
print("could not create effect")
}
// Start and stop your effect
// Don't forget to clean up your ffDevice and ffEffect when done.
}
// bridge your context and send it as a parameter instead of nil if needed
IOHIDManagerRegisterDeviceMatchingCallback(hidManager, deviceMatchingCallback, nil)
IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), CFRunLoopMode.defaultMode.rawValue)
CFRunLoopRun()
}
}
Note that I register the HID callback so that the code runs when a device is added.
Upvotes: 1