mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-24 11:01:20 -08:00
Redo HID device handling. No more shit abstraction on top of HidApi
This commit is contained in:
1 parent
c09bddf148
commit
e97c9a16bd
8 files changed
+629
-278
No files matched your search
@@ -1,169 +1,113 @@
|
||||
package dev.slimevr.desktop.hid
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.VRServerActions
|
||||
import dev.slimevr.hid.HIDReceiver
|
||||
import dev.slimevr.hid.HIDReceiverActions
|
||||
import dev.slimevr.hid.HidConnection
|
||||
import dev.slimevr.hid.HidDeviceDescriptor
|
||||
import dev.slimevr.hid.HidTransport
|
||||
import dev.slimevr.hid.hidVendorIds
|
||||
import dev.slimevr.hid.isCompatibleHidReceiver
|
||||
import dev.slimevr.hid.isCompatibleHidTracker
|
||||
import dev.slimevr.hid.parseHIDPackets
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import dev.slimevr.hid.runHidManager
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.hid4java.HidDevice
|
||||
import org.hid4java.HidManager
|
||||
import org.hid4java.HidServicesSpecification
|
||||
import org.hid4java.jna.HidApi
|
||||
import org.hid4java.jna.HidDeviceInfoStructure
|
||||
import solarxr_protocol.data_feed.dongle_data.DongleStatus
|
||||
import org.hid4java.jna.HidDeviceStructure
|
||||
|
||||
private const val HID_POLL_INTERVAL_MS = 3000L
|
||||
private val hidLibLock = Any()
|
||||
private var hidLibInitialized = false
|
||||
|
||||
// A HID report is 64 bytes, which parseHIDPackets splits into four 16-byte packets
|
||||
private const val HID_READ_BUFFER_SIZE = 64
|
||||
|
||||
// Bounds how long cancellation waits on the uninterruptible native read when a device is idle
|
||||
private const val HID_READ_TIMEOUT_MS = 100
|
||||
|
||||
// We manage device open/close/read ourselves; hid4java's own shutdown hook would call
|
||||
// HidApi.exit() on a separate thread with no coordination, racing our in-flight native reads.
|
||||
// hidapi's own contract forbids this (hid_exit must not run concurrently with hid_read on
|
||||
// another thread), and the same call shape has hung indefinitely on Windows before:
|
||||
// https://github.com/libusb/hidapi/issues/133
|
||||
private val hidSpec = HidServicesSpecification().apply {
|
||||
isAutoStart = false
|
||||
isAutoShutdown = false
|
||||
private fun ensureHidApi() {
|
||||
synchronized(hidLibLock) {
|
||||
if (hidLibInitialized) return
|
||||
HidApi.init()
|
||||
hidLibInitialized = true
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the native HID library. Must be called before enumerateDevices.
|
||||
private val hidServices by lazy { HidManager.getHidServices(hidSpec) }
|
||||
private fun shutdownHidApi() {
|
||||
synchronized(hidLibLock) {
|
||||
if (!hidLibInitialized) return
|
||||
HidApi.exit()
|
||||
hidLibInitialized = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun enumerateCompatibleDevices(directTrackersEnabled: Boolean): Map<String, HidDevice> {
|
||||
hidServices // ensure native lib is loaded
|
||||
val root = HidApi.enumerateDevices(0, 0) ?: return emptyMap()
|
||||
val result = mutableMapOf<String, HidDevice>()
|
||||
var info: HidDeviceInfoStructure? = root
|
||||
while (info != null) {
|
||||
val vid = info.vendor_id.toInt()
|
||||
val pid = info.product_id.toInt()
|
||||
if (isCompatibleHidReceiver(vid, pid) || (directTrackersEnabled && isCompatibleHidTracker(vid, pid))) {
|
||||
val device = HidDevice(info, null, hidSpec)
|
||||
// Use path as key, unique per physical device, available without opening
|
||||
result[info.path] = device
|
||||
private fun enumerateHidDevices(directTrackersEnabled: Boolean): Map<String, HidDeviceDescriptor> {
|
||||
ensureHidApi()
|
||||
|
||||
val result = mutableMapOf<String, HidDeviceDescriptor>()
|
||||
for (vendorId in hidVendorIds(directTrackersEnabled)) {
|
||||
val root = HidApi.enumerateDevices(vendorId, 0) ?: continue
|
||||
try {
|
||||
var info: HidDeviceInfoStructure? = root
|
||||
while (info != null) {
|
||||
val vid = info.vendor_id.toInt() and 0xFFFF
|
||||
val pid = info.product_id.toInt() and 0xFFFF
|
||||
if (isCompatibleHidReceiver(vid, pid) || (directTrackersEnabled && isCompatibleHidTracker(vid, pid))) {
|
||||
// Needs to be copied over as freeEnumeration would free the memory. JNA shenanigans
|
||||
result[info.path] = HidDeviceDescriptor(
|
||||
key = info.path,
|
||||
vendorId = vid,
|
||||
productId = pid,
|
||||
serialNumber = info.serial_number?.toString(),
|
||||
)
|
||||
}
|
||||
info = info.next()
|
||||
}
|
||||
} finally {
|
||||
HidApi.freeEnumeration(root)
|
||||
}
|
||||
info = info.next()
|
||||
}
|
||||
HidApi.freeEnumeration(root)
|
||||
return result
|
||||
}
|
||||
|
||||
private data class ActiveReceiver(val job: Job, val receiver: HIDReceiver)
|
||||
private class DesktopHidConnection(private val struct: HidDeviceStructure) : HidConnection {
|
||||
private val writeLock = Mutex()
|
||||
|
||||
fun createDesktopHIDManager(appContext: AppContextProvider, scope: CoroutineScope): Job {
|
||||
val active = mutableMapOf<String, ActiveReceiver>()
|
||||
@Volatile
|
||||
private var closed = false
|
||||
|
||||
return scope.launch {
|
||||
try {
|
||||
while (isActive) {
|
||||
val directTrackersEnabled = appContext.config.settings.context.state.value.data.hidConfig.trackersOverHid
|
||||
val found = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
enumerateCompatibleDevices(directTrackersEnabled)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.hid.error(e, "HID enumeration failed")
|
||||
emptyMap()
|
||||
}
|
||||
}
|
||||
override suspend fun read(buffer: ByteArray, timeoutMs: Int): Int = withContext(Dispatchers.IO) {
|
||||
if (closed) return@withContext -1
|
||||
HidApi.read(struct, buffer, timeoutMs)
|
||||
}
|
||||
|
||||
// Devices no longer present + jobs that exited on their own (read error)
|
||||
val toRemove = (active.keys - found.keys) +
|
||||
active.entries.filter { !it.value.job.isActive }.map { it.key }
|
||||
for (path in toRemove) {
|
||||
val entry = active.remove(path) ?: continue
|
||||
entry.job.cancel()
|
||||
entry.job.join()
|
||||
AppLogger.hid.info("HID device removed: $path")
|
||||
}
|
||||
override suspend fun write(data: ByteArray): Int = writeLock.withLock {
|
||||
if (closed) return@withLock -1
|
||||
withContext(Dispatchers.IO) {
|
||||
val written = HidApi.write(struct, data, data.size, 0)
|
||||
if (written > 0) written - 1 else written
|
||||
}
|
||||
}
|
||||
|
||||
// Open newly detected devices
|
||||
for ((path, hidDevice) in found) {
|
||||
if (path in active) continue
|
||||
|
||||
if (!hidDevice.open()) {
|
||||
AppLogger.hid.warn("Failed to open HID device: $path")
|
||||
continue
|
||||
}
|
||||
|
||||
val serial = hidDevice.serialNumber ?: path
|
||||
AppLogger.hid.info("HID device detected: $serial")
|
||||
|
||||
val deviceJob = SupervisorJob(scope.coroutineContext[Job])
|
||||
val deviceScope = CoroutineScope(scope.coroutineContext + deviceJob)
|
||||
|
||||
val receiver = appContext.server.context.state.value.dongles.values
|
||||
.find { it.context.state.value.serialNumber == serial }
|
||||
?: run {
|
||||
val id = appContext.server.nextHandle()
|
||||
val created = HIDReceiver.create(
|
||||
id = id,
|
||||
serialNumber = serial,
|
||||
isDirect = isCompatibleHidTracker(hidDevice.vendorId, hidDevice.productId),
|
||||
appContext = appContext,
|
||||
scope = scope,
|
||||
)
|
||||
appContext.server.context.dispatch(VRServerActions.NewDongle(id, created))
|
||||
created
|
||||
}
|
||||
receiver.context.dispatch(HIDReceiverActions.SetStatus(DongleStatus.CONNECTED))
|
||||
|
||||
deviceScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
// Reused across reads: parseHIDPackets is told how much of it is live
|
||||
val buffer = ByteArray(HID_READ_BUFFER_SIZE)
|
||||
while (isActive) {
|
||||
val read = try {
|
||||
hidDevice.read(buffer, HID_READ_TIMEOUT_MS)
|
||||
} catch (_: Exception) {
|
||||
-1
|
||||
}
|
||||
when {
|
||||
// read error, device gone
|
||||
read < 0 -> return@launch
|
||||
|
||||
read > 0 -> parseHIDPackets(buffer, read).forEach { receiver.packetEvents.emit(it) }
|
||||
|
||||
// 0 is a timeout with no data: the read already blocked, so just go again
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
withContext(NonCancellable + Dispatchers.IO) { hidDevice.close() }
|
||||
withContext(NonCancellable) { receiver.onDisconnected() }
|
||||
}
|
||||
}
|
||||
deviceJob.complete()
|
||||
|
||||
active[path] = ActiveReceiver(deviceJob, receiver)
|
||||
}
|
||||
|
||||
delay(HID_POLL_INTERVAL_MS)
|
||||
}
|
||||
} finally {
|
||||
// Stop every in-flight native read before HidApi.exit() runs, so we never
|
||||
// call hid_exit() while another thread is still inside hid_read()/hid_close().
|
||||
withContext(NonCancellable) {
|
||||
for (entry in active.values) {
|
||||
entry.job.cancel()
|
||||
entry.job.join()
|
||||
}
|
||||
withContext(Dispatchers.IO) { HidApi.exit() }
|
||||
}
|
||||
override suspend fun close() {
|
||||
writeLock.withLock {
|
||||
if (closed) return@withLock
|
||||
closed = true
|
||||
withContext(Dispatchers.IO) { HidApi.close(struct) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DesktopHidTransport : HidTransport {
|
||||
override val wakeSignal: ReceiveChannel<Unit>? = null
|
||||
|
||||
override suspend fun enumerate(directTrackersEnabled: Boolean): Map<String, HidDeviceDescriptor> = withContext(Dispatchers.IO) {
|
||||
enumerateHidDevices(directTrackersEnabled)
|
||||
}
|
||||
|
||||
override suspend fun open(descriptor: HidDeviceDescriptor): HidConnection? = withContext(Dispatchers.IO) {
|
||||
ensureHidApi()
|
||||
HidApi.open(descriptor.key)?.let { struct -> DesktopHidConnection(struct) }
|
||||
}
|
||||
|
||||
override suspend fun shutdown() = withContext(Dispatchers.IO) { shutdownHidApi() }
|
||||
}
|
||||
|
||||
fun createDesktopHIDManager(appContext: AppContextProvider, scope: CoroutineScope): Job = runHidManager(appContext, DesktopHidTransport(), scope)
|
||||
Reference in new issue
Block a user