在Swift中安装一个力反馈装置
我可以得到一个像这样的隐藏设备在Swift中安装一个力反馈装置,swift,macos,hid,iokit,Swift,Macos,Hid,Iokit,我可以得到一个像这样的隐藏设备 import ForceFeedback import IOKit.hid private func createDeviceMatchingDictionary( usagePage: Int, usage: Int) -> CFMutableDictionary { let dict = [ kIOHIDDeviceUsageKey: usage, kIOHIDDeviceUsagePageKey: usageP
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)
但是,这些都不会返回使用FFCreateDevice()
如何获得forcefeedback设备并在其上使用
FFCreateEffect
?许多Objective-C或C风格框架背后的想法是,您为预期由函数创建的对象传入指针,函数返回代码,以便您知道操作是否成功
您可以使用IOHIDDeviceGetService(设备)
获取您的io\u服务。然后可以创建类型为FFDeviceObjectReference
的可选对象,并将其引用传递给FFCreateDevice
。下面是一个(未经测试)的示例,它似乎应该可以工作:
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()
}
}
请注意,我注册了HID回调,以便在添加设备时运行代码。它无法成功创建forcefeedback设备,结果等于-2147483644
,除了FFERR_INTERNAL
之外,我无法将其与任何返回值进行比较,因为整数文本“…”在存储到带符号的“Builtin.Int32”中时溢出。我试图将结果与FFERR\u INVALIDPARAM
FFERR\u NOINTERFACE
FFERR\u OUTOFMEMORY
和FFERR\u INTERNAL
进行比较,但我无法打印或与前3mmm进行比较。我在尝试打印这些FFERR值时也会出现溢出错误。似乎这个框架还没有得到苹果公司的青睐。请看ForceFeedback.swift in。这家伙已经声明了自定义枚举中的所有错误代码-2147483644
对应于那里的noInterface
,但我不知道从那里去哪里。