swift - 在 Swift 中获取 ForceFeedback 设备

标签 swift macos hid iokit

我可以得到这样的隐藏设备

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)

然而,这些都不会返回 io_service_tUnsafeMutablePointer<FFDeviceObjectReference>需要使用 FFCreateDevice()

如何获得力反馈设备并使用 FFCreateEffect在上面吗?

最佳答案

很多 Objective-C 或 C 风格的框架背后的想法是,你为你希望由函数创建的对象传入指针,函数返回代码,以便你知道操作是否成功.

您可以使用 IOHIDDeviceGetService(device) 获取您的 io_service_t。然后,您可以创建类型为 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 回调,以便代码在添加设备时运行。

关于swift - 在 Swift 中获取 ForceFeedback 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929448/

相关文章:

MySQL 服务器启动权限被拒绝

c# - 打开带有 .cs 扩展文件的项目?

linux - USB端口在Gentoo Linux中不起作用

windows - 获取 HID 设备的功能报告描述符的 native 方法?

ios - coreData 中的关系如何运作

objective-c - 在 Objective-C 中使用 Swift 协议(protocol)

swift - Google Places Autocomplete API 未在我的 Tableview 中填充确切的地址

ios - 如何对 Codable 的所有属性使用 decodeIfPresent?

java - USB HID 设备与 usb4java 之间的通信

c++ - 我应该使用 CFPlugin 还是 dlopen() 作为 OSX 上的 C++ 插件