mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-23 18:41:24 -08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2d9c96b70 |
No files matched your search
@@ -12,6 +12,7 @@ class BaseBehaviour : VRServerBehaviour {
|
||||
is VRServerActions.DriverDisconnected -> state.copy(drivers = state.drivers - action.bridgeId)
|
||||
is VRServerActions.SolarXRConnected -> state.copy(solarxr = state.solarxr + (action.connection.id to action.connection))
|
||||
is VRServerActions.SolarXRDisconnected -> state.copy(solarxr = state.solarxr - action.connectionId)
|
||||
is VRServerActions.NewDongle -> state.copy(receiverDongles = state.receiverDongles + (action.receiverDongleId to action.context))
|
||||
}
|
||||
|
||||
override fun observe(receiver: VRServer) {
|
||||
|
||||
@@ -10,7 +10,6 @@ import dev.slimevr.tracker.TrackerActions
|
||||
import dev.slimevr.util.safeLaunch
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
|
||||
class HIDRegistrationBehaviour : HIDReceiverBehaviour {
|
||||
@@ -31,7 +30,7 @@ class HIDRegistrationBehaviour : HIDReceiverBehaviour {
|
||||
}
|
||||
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDDeviceRegister> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDDeviceRegister> { packet ->
|
||||
val state = receiver.context.state.value
|
||||
val existing = state.trackers[packet.hidId]
|
||||
if (existing != null) return@onPacket
|
||||
@@ -73,7 +72,7 @@ class HIDDeviceInfoBehaviour : HIDReceiverBehaviour {
|
||||
}
|
||||
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDDeviceInfo> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDDeviceInfo> { packet ->
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@onPacket
|
||||
val deviceState = device.context.state.value
|
||||
|
||||
@@ -117,22 +116,22 @@ class HIDDeviceInfoBehaviour : HIDReceiverBehaviour {
|
||||
|
||||
class HIDRotationBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDRotation> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotation> { packet ->
|
||||
val tracker = receiver.getTracker(packet.hidId) ?: return@onPacket
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = packet.rotation, acceleration = packet.acceleration))
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationBattery> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotationBattery> { packet ->
|
||||
val tracker = receiver.getTracker(packet.hidId) ?: return@onPacket
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = packet.rotation, acceleration = packet.acceleration))
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationMag> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotationMag> { packet ->
|
||||
val tracker = receiver.getTracker(packet.hidId) ?: return@onPacket
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = packet.rotation, magnetometer = packet.magnetometer))
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationButton> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotationButton> { packet ->
|
||||
val tracker = receiver.getTracker(packet.hidId) ?: return@onPacket
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = packet.rotation, acceleration = packet.acceleration))
|
||||
}
|
||||
@@ -141,7 +140,7 @@ class HIDRotationBehaviour : HIDReceiverBehaviour {
|
||||
|
||||
class HIDBatteryBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDRotationBattery> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotationBattery> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update {
|
||||
copy(batteryLevel = packet.batteryLevel, batteryVoltage = packet.batteryVoltage, signalStrength = packet.rssi)
|
||||
@@ -149,19 +148,19 @@ class HIDBatteryBehaviour : HIDReceiverBehaviour {
|
||||
)
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationButton> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotationButton> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update { copy(signalStrength = packet.rssi) },
|
||||
)
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDData> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDData> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update { copy(signalStrength = packet.rssi) },
|
||||
)
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRuntime> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRuntime> { packet ->
|
||||
// -1: not yet known (keep existing value); 0: N/A (e.g. charging)
|
||||
if (packet.runtime >= 0) {
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
@@ -210,27 +209,27 @@ class HIDSleepBehaviour : HIDReceiverBehaviour {
|
||||
armIdleTimeout(hidId)
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationButton> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDRotationButton> { packet ->
|
||||
onPacket(packet.hidId)
|
||||
scheduleSleep(packet.hidId, packet.timeout)
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDData> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDData> { packet ->
|
||||
onPacket(packet.hidId)
|
||||
scheduleSleep(packet.hidId, packet.timeout)
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotation> { packet -> onPacket(packet.hidId) }
|
||||
receiver.packetEvents.onPacket<HIDRotationBattery> { packet -> onPacket(packet.hidId) }
|
||||
receiver.packetEvents.onPacket<HIDRotationMag> { packet -> onPacket(packet.hidId) }
|
||||
receiver.packetEvents.onPacket<HIDStatus> { packet -> onPacket(packet.hidId) }
|
||||
receiver.packetEvents.onPacket<HIDRuntime> { packet -> onPacket(packet.hidId) }
|
||||
receiver.inboundPackets.onPacket<HIDRotation> { packet -> onPacket(packet.hidId) }
|
||||
receiver.inboundPackets.onPacket<HIDRotationBattery> { packet -> onPacket(packet.hidId) }
|
||||
receiver.inboundPackets.onPacket<HIDRotationMag> { packet -> onPacket(packet.hidId) }
|
||||
receiver.inboundPackets.onPacket<HIDStatus> { packet -> onPacket(packet.hidId) }
|
||||
receiver.inboundPackets.onPacket<HIDRuntime> { packet -> onPacket(packet.hidId) }
|
||||
}
|
||||
}
|
||||
|
||||
class HIDStatusBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDStatus> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDStatus> { packet ->
|
||||
if (receiver.getTracker(packet.hidId) == null) return@onPacket
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update { copy(status = packet.status, signalStrength = packet.rssi) },
|
||||
@@ -241,7 +240,7 @@ class HIDStatusBehaviour : HIDReceiverBehaviour {
|
||||
|
||||
class HIDPacketLossBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDStatus> { packet ->
|
||||
receiver.inboundPackets.onPacket<HIDStatus> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.PacketStats(packetsReceived = packet.packetsReceived.toLong(), packetsLost = packet.packetsLost.toLong()),
|
||||
)
|
||||
|
||||
+13
-12
@@ -12,12 +12,12 @@ import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
import kotlin.math.sqrt
|
||||
|
||||
sealed interface HIDPacket {
|
||||
sealed interface HIDInboundPacket {
|
||||
val hidId: Int
|
||||
}
|
||||
|
||||
/** Receiver associates a wireless tracker ID with its 6-byte address (type 255). */
|
||||
data class HIDDeviceRegister(override val hidId: Int, val address: String) : HIDPacket
|
||||
data class HIDDeviceRegister(override val hidId: Int, val address: String) : HIDInboundPacket
|
||||
|
||||
/** Board/MCU/firmware/battery + IMU type for tracker registration (type 0). */
|
||||
data class HIDDeviceInfo(
|
||||
@@ -30,14 +30,14 @@ data class HIDDeviceInfo(
|
||||
val batteryVoltage: Float,
|
||||
val rssi: Int,
|
||||
val magStatus: MagnetometerStatus,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
/** Full-precision Q15 quaternion + Q7 acceleration (type 1). */
|
||||
data class HIDRotation(
|
||||
override val hidId: Int,
|
||||
val rotation: Quaternion,
|
||||
val acceleration: Vector3,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
/** Compact exp-map quaternion + Q7 acceleration + battery level + rssi (type 2). */
|
||||
data class HIDRotationBattery(
|
||||
@@ -47,7 +47,7 @@ data class HIDRotationBattery(
|
||||
val batteryLevel: Float?,
|
||||
val batteryVoltage: Float,
|
||||
val rssi: Int,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
/** Tracker status report + rssi (type 3). */
|
||||
data class HIDStatus(
|
||||
@@ -56,14 +56,14 @@ data class HIDStatus(
|
||||
val rssi: Int,
|
||||
val packetsReceived: Int,
|
||||
val packetsLost: Int,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
/** Full-precision Q15 quaternion + Q10 magnetometer (type 4). */
|
||||
data class HIDRotationMag(
|
||||
override val hidId: Int,
|
||||
val rotation: Quaternion,
|
||||
val magnetometer: Vector3,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
/** Button state + timeout hint + compact exp-map quaternion + Q7 acceleration + rssi (type 7). */
|
||||
data class HIDRotationButton(
|
||||
@@ -73,10 +73,10 @@ data class HIDRotationButton(
|
||||
val rotation: Quaternion,
|
||||
val acceleration: Vector3,
|
||||
val rssi: Int,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
/** Battery remaining runtime in milliseconds (type 5). -1 = unknown, 0 = N/A (e.g. charging). */
|
||||
data class HIDRuntime(override val hidId: Int, val runtime: Long) : HIDPacket
|
||||
data class HIDRuntime(override val hidId: Int, val runtime: Long) : HIDInboundPacket
|
||||
|
||||
/** Button state + timeout hint + rssi, no rotation (type 6). */
|
||||
data class HIDData(
|
||||
@@ -84,7 +84,8 @@ data class HIDData(
|
||||
val button: Int,
|
||||
val timeout: Int,
|
||||
val rssi: Int,
|
||||
) : HIDPacket
|
||||
) : HIDInboundPacket
|
||||
|
||||
|
||||
private const val HID_PACKET_SIZE = 16
|
||||
|
||||
@@ -148,7 +149,7 @@ private fun decodeBattery(raw: Int): Float? = if (raw == 128) null else (raw and
|
||||
|
||||
private fun decodeBatteryVoltage(raw: Int): Float = (raw.toFloat() + 245f) / 100f
|
||||
|
||||
private fun parseSingleHIDPacket(data: ByteArray, i: Int): HIDPacket? {
|
||||
private fun parseSingleHIDPacket(data: ByteArray, i: Int): HIDInboundPacket? {
|
||||
val packetType = data[i].toUByte().toInt()
|
||||
val hidId = data[i + 1].toUByte().toInt()
|
||||
|
||||
@@ -247,7 +248,7 @@ private fun parseSingleHIDPacket(data: ByteArray, i: Int): HIDPacket? {
|
||||
}
|
||||
}
|
||||
|
||||
fun parseHIDPackets(data: ByteArray): List<HIDPacket> {
|
||||
fun parseHIDPackets(data: ByteArray): List<HIDInboundPacket> {
|
||||
if (data.size % HID_PACKET_SIZE != 0) return emptyList()
|
||||
return (0 until data.size / HID_PACKET_SIZE).mapNotNull { parseSingleHIDPacket(data, it * HID_PACKET_SIZE) }
|
||||
}
|
||||
@@ -27,18 +27,21 @@ sealed interface HIDReceiverActions {
|
||||
|
||||
typealias HIDReceiverContext = Context<HIDReceiverState, HIDReceiverActions>
|
||||
typealias HIDReceiverBehaviour = Behaviour<HIDReceiverState, HIDReceiverActions, HIDReceiver>
|
||||
typealias HIDPacketDispatcher = EventDispatcher<HIDPacket>
|
||||
typealias HIDInboundPacketDispatcher = EventDispatcher<HIDInboundPacket>
|
||||
typealias HIDOutboundPacketDispatcher = EventDispatcher<HIDOutboundPacket>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T : HIDPacket> HIDPacketDispatcher.onPacket(crossinline callback: suspend (T) -> Unit) {
|
||||
inline fun <reified T : HIDInboundPacket> HIDInboundPacketDispatcher.onPacket(crossinline callback: suspend (T) -> Unit) {
|
||||
register(T::class) { callback(it as T) }
|
||||
}
|
||||
|
||||
class HIDReceiver(
|
||||
val context: HIDReceiverContext,
|
||||
val appContext: AppContextProvider,
|
||||
val packetEvents: HIDPacketDispatcher,
|
||||
) {
|
||||
val outboundPackets: HIDOutboundPacketDispatcher = HIDOutboundPacketDispatcher()
|
||||
val inboundPackets: HIDInboundPacketDispatcher = HIDInboundPacketDispatcher()
|
||||
|
||||
fun startObserving() = context.observeAll(this)
|
||||
|
||||
fun getDevice(hidId: Int): Device? {
|
||||
@@ -52,6 +55,10 @@ class HIDReceiver(
|
||||
return appContext.server.getTracker(trackerId)
|
||||
}
|
||||
|
||||
suspend fun send(hidOutboundPacket: HIDOutboundPacket) {
|
||||
outboundPackets.emit(hidOutboundPacket)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create(
|
||||
serialNumber: String,
|
||||
@@ -75,12 +82,9 @@ class HIDReceiver(
|
||||
name = "HIDReceiver[$serialNumber]",
|
||||
)
|
||||
|
||||
val dispatcher = HIDPacketDispatcher()
|
||||
|
||||
val receiver = HIDReceiver(
|
||||
context = context,
|
||||
appContext = appContext,
|
||||
packetEvents = dispatcher,
|
||||
appContext = appContext
|
||||
)
|
||||
receiver.startObserving()
|
||||
return receiver
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package dev.slimevr.hid
|
||||
|
||||
import kotlinx.io.Sink
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
sealed interface HIDOutboundPacket {
|
||||
fun write(sink: Sink)
|
||||
}
|
||||
|
||||
data class HIDCommand(
|
||||
val trackerId : Int,
|
||||
val command: Command,
|
||||
) : HIDOutboundPacket {
|
||||
override fun write(sink: Sink) {
|
||||
sink.writeByte(0)
|
||||
sink.writeInt(trackerId)
|
||||
sink.writeInt(command.id)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum class Command(
|
||||
var id: Int
|
||||
) {
|
||||
SHUTDOWN (0),
|
||||
UNPAIR (1)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.slimevr.solarxr
|
||||
|
||||
/*
|
||||
class DongleBehaviour : SolarXRBridgeBehaviour {
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
receiver.rpcDispatcher.on<>
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -3,6 +3,9 @@ package dev.slimevr.solarxr
|
||||
import dev.slimevr.config.ResetsConfig
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.config.SettingsActions
|
||||
import dev.slimevr.hid.Command
|
||||
import dev.slimevr.hid.HIDCommand
|
||||
import dev.slimevr.hid.HIDOutboundPacket
|
||||
import solarxr_protocol.rpc.ArmsResetMode
|
||||
import solarxr_protocol.rpc.ChangeResetsSettingsRequest
|
||||
import solarxr_protocol.rpc.ResetsSettingsRequest
|
||||
@@ -14,6 +17,7 @@ class ResetsBehaviour(
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
// Send config
|
||||
receiver.rpcDispatcher.on<ResetsSettingsRequest> {
|
||||
/*
|
||||
val config = settings.context.state.value.data.resetsConfig
|
||||
receiver.sendRpc(
|
||||
ResetsSettingsResponse(
|
||||
@@ -24,6 +28,10 @@ class ResetsBehaviour(
|
||||
resetHmdPitch = config.resetHmdPitch,
|
||||
),
|
||||
)
|
||||
*/
|
||||
val dongle = receiver.appContext.server.context.state.value.receiverDongles.values.first()
|
||||
|
||||
dongle.send(HIDCommand(1, Command.SHUTDOWN))
|
||||
}
|
||||
|
||||
// Receive config
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.slimevr.context.Behaviour
|
||||
import dev.slimevr.context.Context
|
||||
import dev.slimevr.device.Device
|
||||
import dev.slimevr.driver.DriverBridge
|
||||
import dev.slimevr.hid.HIDReceiver
|
||||
import dev.slimevr.solarxr.SolarXRBridge
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -17,6 +18,7 @@ data class VRServerState(
|
||||
val devices: Map<Int, Device>,
|
||||
val drivers: Map<Int, DriverBridge>,
|
||||
val solarxr: Map<Int, SolarXRBridge>,
|
||||
val receiverDongles: Map<Int, HIDReceiver>
|
||||
)
|
||||
|
||||
sealed interface VRServerActions {
|
||||
@@ -26,6 +28,7 @@ sealed interface VRServerActions {
|
||||
data class DriverDisconnected(val bridgeId: Int) : VRServerActions
|
||||
data class SolarXRConnected(val connection: SolarXRBridge) : VRServerActions
|
||||
data class SolarXRDisconnected(val connectionId: Int) : VRServerActions
|
||||
data class NewDongle(val receiverDongleId: Int, val context: HIDReceiver) : VRServerActions
|
||||
}
|
||||
|
||||
typealias VRServerContext = Context<VRServerState, VRServerActions>
|
||||
@@ -46,6 +49,7 @@ class VRServer(
|
||||
val deviceId = getTracker(id)?.context?.state?.value?.deviceId ?: return null
|
||||
return getDevice(deviceId)
|
||||
}
|
||||
fun getDongle(id: Int) = context.state.value.receiverDongles[id]
|
||||
|
||||
suspend fun sendSolarxrRpc(message: RpcMessage) = context.state.value.solarxr.values.forEach { it.sendRpc(message) }
|
||||
|
||||
@@ -58,6 +62,7 @@ class VRServer(
|
||||
devices = emptyMap(),
|
||||
drivers = emptyMap(),
|
||||
solarxr = emptyMap(),
|
||||
receiverDongles = emptyMap(),
|
||||
),
|
||||
scope = scope,
|
||||
behaviours = behaviours,
|
||||
|
||||
@@ -2,19 +2,15 @@ package dev.slimevr.desktop.hid
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.AppLogger
|
||||
import dev.slimevr.VRServerActions
|
||||
import dev.slimevr.device.DeviceActions
|
||||
import dev.slimevr.hid.HIDReceiver
|
||||
import dev.slimevr.hid.isCompatibleHidDevice
|
||||
import dev.slimevr.hid.parseHIDPackets
|
||||
import dev.slimevr.util.safeLaunch
|
||||
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.withContext
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.io.Buffer
|
||||
import kotlinx.io.readByteArray
|
||||
import org.hid4java.HidDevice
|
||||
import org.hid4java.HidManager
|
||||
import org.hid4java.HidServicesSpecification
|
||||
@@ -31,18 +27,15 @@ private val hidServices by lazy { HidManager.getHidServices(hidSpec) }
|
||||
|
||||
private fun enumerateCompatibleDevices(): Map<String, HidDevice> {
|
||||
hidServices // ensure native lib is loaded
|
||||
val root = HidApi.enumerateDevices(0, 0) ?: return emptyMap()
|
||||
hidServices.start()
|
||||
val result = mutableMapOf<String, HidDevice>()
|
||||
var info: HidDeviceInfoStructure? = root
|
||||
while (info != null) {
|
||||
if (isCompatibleHidDevice(info.vendor_id.toInt(), info.product_id.toInt())) {
|
||||
val device = HidDevice(info, null, hidSpec)
|
||||
// Use path as key, unique per physical device, available without opening
|
||||
result[info.path] = device
|
||||
|
||||
for (device in hidServices.attachedHidDevices) {
|
||||
if (isCompatibleHidDevice(device.vendorId, device.productId)) {
|
||||
result[device.path] = device
|
||||
}
|
||||
info = info.next()
|
||||
}
|
||||
HidApi.freeEnumeration(root)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -93,6 +86,20 @@ fun createDesktopHIDManager(appContext: AppContextProvider, scope: CoroutineScop
|
||||
scope = deviceScope,
|
||||
)
|
||||
|
||||
val dongleId = appContext.server.nextHandle()
|
||||
appContext.server.context.dispatch(VRServerActions.NewDongle(dongleId, receiver))
|
||||
|
||||
deviceScope.safeLaunch {
|
||||
receiver.outboundPackets.onAny{ packet ->
|
||||
deviceScope.safeLaunch {
|
||||
val buf = Buffer()
|
||||
AppLogger.hid.info("Received packet: $packet")
|
||||
packet.write(buf)
|
||||
hidDevice.write(buf.readByteArray(), buf.size.toInt(), 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
deviceScope.safeLaunch {
|
||||
try {
|
||||
while (isActive) {
|
||||
@@ -107,7 +114,7 @@ fun createDesktopHIDManager(appContext: AppContextProvider, scope: CoroutineScop
|
||||
data == null -> return@safeLaunch
|
||||
|
||||
// read error, device gone
|
||||
data.isNotEmpty() -> parseHIDPackets(data).forEach { receiver.packetEvents.emit(it) }
|
||||
data.isNotEmpty() -> parseHIDPackets(data).forEach { receiver.inboundPackets.emit(it) }
|
||||
|
||||
else -> delay(1) // no data yet, yield without busy-spinning
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user