fix(input): preserve control visibility and routing

This commit is contained in:
Kurt Himebauch committed 2026-11-20 07:48:12 -05:00
1 parent 7c2955b058
commit 97e0251e6a
9 files changed
+103 -26

No files matched your search

@@ -25,7 +25,10 @@ data class PhysicalInputKey(val device: String, val name: String, val keyCode: I
data class PhysicalInputBinding(val key: PhysicalInputKey, val action: InputAction)
class PhysicalInputBindings(private val router: InputRouter) {
class PhysicalInputBindings(
private val router: InputRouter,
private val onPress: (KeyEvent) -> Unit = {},
) {
private class Entry(val binding: PhysicalInputBinding, val channel: InputRouter.Channel) {
var heldDevice = -1
var suppressed = false
@@ -66,7 +69,10 @@ class PhysicalInputBindings(private val router: InputRouter) {
if (identity != key.device) continue
entry.heldDevice = event.deviceId
entry.suppressed = event.repeatCount != 0
if (!entry.suppressed) entry.channel.update(1f)
if (!entry.suppressed) {
entry.channel.update(1f)
onPress(event)
}
return true
}
return false
@@ -20,7 +20,11 @@ import java.util.ArrayDeque
import java.util.UUID
@SuppressLint("MissingPermission")
class SteamControllerBle(private val context: Context, router: InputRouter? = null) {
class SteamControllerBle(
private val context: Context,
router: InputRouter? = null,
private val onPhysicalInput: (() -> Unit)? = null,
) {
private val inputLock = router ?: Any()
private var routedInput = router?.let(::TritonInputRouter)
@Volatile private var stopped = false
@@ -39,6 +43,7 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
private var outputSequence = 0L
private var outputWritePending = false
private var featureRead = 0L
private var lastPressedControls = 0
fun start(): Unit = synchronized(inputLock) {
if (stopped || started) return@synchronized
@@ -266,12 +271,18 @@ class SteamControllerBle(private val context: Context, router: InputRouter? = nu
val timing = InputTiming.begin()
FakeInputWriter.writeTritonState(SLOT, reportId, routedInput?.route(data) ?: data)
InputTiming.end(timing)
if (connected && onPhysicalInput != null) {
val pressed = TritonInputRouter.pressedControls(data)
if (pressed and lastPressedControls.inv() != 0) onPhysicalInput()
lastPressedControls = pressed
}
}
private fun release() = synchronized(inputLock) {
if (stopped) return@synchronized
routedInput?.release()
withSlot {
lastPressedControls = 0
FakeInputWriter.writeTritonState(SLOT, if (reportId == 0x47) 0x47 else 0x45, ByteArray(STATE_BYTES))
}
}
@@ -63,16 +63,25 @@ class TritonInputRouter(private val router: InputRouter) {
fun release() = synchronized(router) { channels.forEach { it.release() }; last = null }
fun close() = synchronized(router) { release(); channels.forEach { it.close() }; router.removeSnapshot(snapshot) }
private fun bits(data: ByteArray): Int = (data[1].toInt() and 255) or ((data[2].toInt() and 255) shl 8) or
((data[3].toInt() and 255) shl 16) or ((data[4].toInt() and 255) shl 24)
private fun putBits(data: ByteArray, value: Int) { for (i in 0..3) data[i + 1] = (value ushr (i * 8)).toByte() }
private fun short(data: ByteArray, offset: Int) = ((data[offset].toInt() and 255) or (data[offset + 1].toInt() shl 8)).toShort().toInt()
private fun axis(data: ByteArray, offset: Int): Float = short(data, offset).let { it / if (it < 0) 32768f else 32767f }
private fun shortValue(value: Float) = (value * if (value < 0f) 32768f else 32767f).roundToInt().coerceIn(-32768, 32767)
private fun putShort(data: ByteArray, offset: Int, value: Int) { data[offset] = value.toByte(); data[offset + 1] = (value shr 8).toByte() }
companion object {
private const val CONTROL_BITS = 0x0fffffff
internal fun pressedControls(data: ByteArray): Int {
var active = bits(data) and CONTROL_BITS
if ((5..15 step 2).any { kotlin.math.abs(short(data, it)) > 16384 }) active = active or (1 shl 28)
return active
}
private fun bits(data: ByteArray): Int = (data[1].toInt() and 255) or ((data[2].toInt() and 255) shl 8) or
((data[3].toInt() and 255) shl 16) or ((data[4].toInt() and 255) shl 24)
private fun short(data: ByteArray, offset: Int) =
((data[offset].toInt() and 255) or (data[offset + 1].toInt() shl 8)).toShort().toInt()
private fun bit(c: InputControl): Int = when (c) {
InputControl.A -> 0x1; InputControl.B -> 0x2; InputControl.X -> 0x4; InputControl.Y -> 0x8
InputControl.QAM -> 0x10; InputControl.R3 -> 0x20; InputControl.START -> 0x40