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

@@ -854,7 +854,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
bridge.setStickDeadZone(ControllerPrefs.deadZone(this))
bridge.setBuiltinPad(SessionState.mode != SessionService.MODE_STEAM || SessionPrefs.builtinPad(this))
padBridge = bridge
inputRouter = InputRouter(inputOutput).also { bridge.setInputRouter(it); DeckControls.setRouter(it); physicalInputBindings = com.droiddeck.launcher.input.PhysicalInputBindings(it) }
inputRouter = InputRouter(inputOutput).also { bridge.setInputRouter(it); DeckControls.setRouter(it); physicalInputBindings = com.droiddeck.launcher.input.PhysicalInputBindings(it, ::noteRemappedControllerUse) }
refreshInputProfile()
bridge.setOnChordInput(backQamInput::onChordInput)
SessionState.padBridge = bridge
@@ -868,8 +868,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
bridge.setExcluded(vr::owns)
joyConVr = vr
}
SteamControllerBle.listener = { uiHandler.post { updatePadMotion(); refreshControls() } }
steamController = SteamControllerBle(this, inputRouter).also { it.start() }
SteamControllerBle.listener = {
uiHandler.post {
if (isDestroyed) return@post
if (SteamControllerBle.connected) controllerInUse = true
updatePadMotion()
refreshControls()
}
}
steamController = SteamControllerBle(this, inputRouter, ::noteSteamControllerUse).also { it.start() }
if (Build.VERSION.SDK_INT >= 31 && checkSelfPermission(BLUETOOTH_CONNECT) != android.content.pm.PackageManager.PERMISSION_GRANTED &&
!SessionPrefs.bluetoothAsked(this)) {
SessionPrefs.setBluetoothAsked(this)
@@ -1271,7 +1278,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
onBack = { inputSettingsOpen = false; secondScreenPresentation?.refreshControls() },
onChanged = {
controllerSettings = ControllerPrefs.read(this@SessionActivity)
oscMode = SessionPrefs.oscMode(this@SessionActivity)
oscMode = sessionOscMode()
padBridge?.setStickDeadZone(ControllerPrefs.deadZone(this@SessionActivity))
joyConVr?.refresh()
onScreenControls?.reload()
@@ -1460,7 +1467,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
micAvailable = SessionState.micAvailable && !SessionState.suspended,
micEnabled = SessionState.micEnabled,
onMicEnabled = { SessionService.setMicEnabled(this@SessionActivity, it) },
touchAuto = getString(if (usingTouchpad()) R.string.mode_touch_touchpad else R.string.mode_touch_mouse),
touchAuto = getString(if (usingTouchpad(SessionPrefs.TOUCH_AUTO)) R.string.mode_touch_touchpad else R.string.mode_touch_mouse),
pen = penSettings.takeIf { penSeen },
onPen = { key, value -> SessionPrefs.setPenSetting(this@SessionActivity, key, value); readPrefs() },
fexPreset = fexPreset,
@@ -2548,7 +2555,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
fexPreset = SessionPrefs.fexPreset(this)
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
steamDownloadsInBackground = SessionPrefs.steamDownloadsInBackground(this)
oscMode = SessionPrefs.oscMode(this)
oscMode = sessionOscMode()
backHold = SessionPrefs.backHold(this)
backActionsInverted = SessionPrefs.backActionsInverted(this)
physicalShortcuts.bindings = PhysicalShortcuts.read(this)
@@ -4471,7 +4478,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
onScreenControls?.updateLayout(primaryInputLayout)
bottomInputControls?.updateLayout(primaryInputLayout)
unfoldedPanel?.updateLayout(primaryInputLayout)
if (unfoldedPanel != null) applyUnfoldedControls()
if (onScreenControls != null) applyUnfoldedControls()
updateOnScreenPad()
secondScreenPresentation?.refreshControls()
}
@@ -4506,7 +4513,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun controlsBelowWanted(): Boolean {
if (!::sessionRoot.isInitialized || glassesScreen != null || bottomGame != null || cardboard.enabled) return false
if (SessionPrefs.oscMode(this) == SessionPrefs.OSC_NEVER) return false
if (sessionOscMode() == SessionPrefs.OSC_NEVER || usesSteamTouchLayout()) return false
return SessionPrefs.controlsBelowWanted(this, SessionState.mode, sessionRoot.width, sessionRoot.height,
pipUi || isInPictureInPictureMode)
}
@@ -4772,7 +4779,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun deliversTouches(mode: String) =
mode == SessionPrefs.TOUCH_DIRECT || mode == SessionPrefs.TOUCH_AUTO
private fun usingTouchpad(): Boolean = when (routedTouchMode) {
private fun usingTouchpad(mode: String = routedTouchMode): Boolean = when (mode) {
SessionPrefs.TOUCH_PAD -> true
SessionPrefs.TOUCH_DIRECT -> false
else -> SessionState.mode != SessionService.MODE_STEAM || SessionState.steamUi == "desktop"
@@ -5376,17 +5383,22 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
}
private fun sessionOscMode() = SessionPrefs.oscMode(this, steamSession = SessionState.mode == SessionService.MODE_STEAM)
private fun usesSteamTouchLayout() = SessionState.mode == SessionService.MODE_STEAM &&
com.droiddeck.launcher.input.GameControls.steamLayout(this, controlsGameId())
private fun controlsPlan(): com.droiddeck.launcher.input.ControlsPresenter.Plan {
val steam = SessionState.mode == SessionService.MODE_STEAM
if (!PadBridge.anyControllerConnected() && !SteamControllerBle.connected) controllerInUse = false
return com.droiddeck.launcher.input.ControlsPresenter.plan(com.droiddeck.launcher.input.ControlsPresenter.State(
mode = SessionPrefs.oscMode(this),
mode = sessionOscMode(),
desktop = SessionState.mode == SessionService.MODE_DESKTOP,
virtualPad = !steam || SessionPrefs.builtinPad(this),
covered = controlsCovered(),
controllerInUse = controllerInUse,
hiddenByTap = controlsHiddenByTap,
steamLayout = steam && com.droiddeck.launcher.input.GameControls.steamLayout(this, controlsGameId()),
steamLayout = usesSteamTouchLayout(),
bottomScreen = bottomGame?.surfaceView?.parent is android.view.ViewGroup,
))
}
@@ -5398,8 +5410,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
fun hideBottomControls() { bottomInputControls?.takeIf { it.visibility == View.VISIBLE }?.let { it.releaseAll(); it.visibility = View.GONE } }
fun hideSteam() {
steamTouchControls?.let { it.cancelEditing(); it.releaseAll(); it.visibility = View.GONE }
val keyboardOver = SessionState.mode == SessionService.MODE_STEAM && controlsCovered() &&
com.droiddeck.launcher.input.GameControls.steamLayout(this, controlsGameId())
val keyboardOver = controlsCovered() && usesSteamTouchLayout()
if (!keyboardOver) com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false
}
val plan = controlsPlan()
@@ -5407,8 +5418,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
val wantsSteam = plan.place == com.droiddeck.launcher.input.ControlsPresenter.Place.STEAM
val steam = if (wantsSteam) steamTouch() else null
steamTouchInUse = steam != null
uiHandler.removeCallbacks(steamTouchRetry)
if (wantsSteam && steam == null && !isFinishing && !isDestroyed) {
uiHandler.removeCallbacks(steamTouchRetry)
uiHandler.postDelayed(steamTouchRetry, 1000)
}
if (steam != null) {
@@ -5442,7 +5453,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
bottom.setHidden(plan.hidden)
bottom.visibility = View.VISIBLE
}
com.droiddeck.launcher.input.ControlsPresenter.Place.MAIN -> {
com.droiddeck.launcher.input.ControlsPresenter.Place.MAIN,
com.droiddeck.launcher.input.ControlsPresenter.Place.STEAM -> {
hideBottomControls()
controls.setHidden(plan.hidden)
controls.visibility = View.VISIBLE
@@ -5466,6 +5478,19 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
drawerOpen = true
}
private fun noteRemappedControllerUse(event: KeyEvent) {
if (PadBridge.isControllerKeyEvent(event) ||
(event.isFromSource(InputDevice.SOURCE_GAMEPAD) && PadBridge.isFromController(event.device))) {
noteControllerUse()
}
}
private fun noteSteamControllerUse() {
uiHandler.post {
if (!isDestroyed && SteamControllerBle.connected) noteControllerUse()
}
}
private fun noteControllerUse() {
if (controllerInUse) return
controllerInUse = true
@@ -6706,7 +6731,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode != REQUEST_BLUETOOTH || grantResults.firstOrNull() != android.content.pm.PackageManager.PERMISSION_GRANTED) return
steamController?.stop()
steamController = SteamControllerBle(this, inputRouter).also { it.start() }
steamController = SteamControllerBle(this, inputRouter, ::noteSteamControllerUse).also { it.start() }
}
companion object {
@@ -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
@@ -665,9 +665,10 @@ object SessionPrefs {
?: if (context.packageManager.hasSystemFeature("android.hardware.sensor.hinge_angle")) SHAPE_WIDE else SHAPE_AUTO
/** Auto, Always or Never. Older builds also stored the Steam layout and a buttons-only mode here. */
fun oscMode(context: Context): String = when (val mode = prefs(context).getString("osc", OSC_AUTO)) {
fun oscMode(context: Context, steamSession: Boolean = false): String = when (val mode = prefs(context).getString("osc", OSC_AUTO)) {
OSC_ALWAYS, OSC_NEVER -> mode
OSC_STEAM_QAM -> OSC_ALWAYS
OSC_STEAM_TOUCH -> if (steamSession) OSC_ALWAYS else OSC_AUTO
else -> OSC_AUTO
}
@@ -1,7 +1,7 @@
package com.droiddeck.launcher.ui
internal enum class SessionDrawerPage(val firstControl: String) {
CONTROLLER("input-profile"),
CONTROLLER("touch"),
GLASSES("glasses-game"),
DISPLAY("fg"),
HUD("hud"),
@@ -30,6 +30,11 @@ class OnScreenControlPrefsTest {
session.edit().putString("osc", saved).commit()
assertEquals(saved, mode, SessionPrefs.oscMode(context))
}
session.edit().putString("osc", SessionPrefs.OSC_STEAM_TOUCH).commit()
assertEquals(SessionPrefs.OSC_ALWAYS, SessionPrefs.oscMode(context, steamSession = true))
SessionPrefs.setOscMode(context, SessionPrefs.OSC_AUTO)
assertEquals(SessionPrefs.OSC_AUTO, SessionPrefs.oscMode(context, steamSession = true))
assertTrue(SessionPrefs.steamTouchLayout(context))
}
@Test fun theOldSteamTouchModeBecomesTheSteamLayoutUntilChanged() {
@@ -24,7 +24,8 @@ class PhysicalInputBindingsTest {
override fun command(command: InputCommand) { events += command.name }
override fun requestMotion() {}
})
private val bindings = PhysicalInputBindings(router)
private val presses = ArrayList<Int>()
private val bindings = PhysicalInputBindings(router) { presses += it.deviceId }
private fun device(id: Int): InputDevice {
val device = InputDeviceBuilder.newBuilder().setId(id).setName("Thor extra keys").setDescriptor("keys-$id")
@@ -55,6 +56,7 @@ class PhysicalInputBindingsTest {
assertEquals(listOf("29 true", "30 true", "30 false", "29 false"), events)
bindings.onKey(event(device.id, true), true)
assertEquals("28 true", events.last())
assertEquals(listOf(device.id, device.id), presses)
}
@Test fun releasesRemainConsumedAfterOpeningMenuAndVirtualKeysAreUntouched() {
@@ -25,6 +25,7 @@ import java.util.UUID
@Config(sdk = [34], manifest = Config.NONE, shadows = [SteamControllerBleTest.Writer::class])
class SteamControllerBleTest {
private val keys = ArrayList<Pair<Int, Boolean>>()
private var presses = 0
private val router = InputRouter(object : InputOutput {
override fun key(code: Int, down: Boolean) { keys += code to down }
override fun button(code: Int, down: Boolean) {}
@@ -42,7 +43,7 @@ class SteamControllerBleTest {
Writer.features = 0
router.setProfile(InputProfile.defaults(InputProfile.GAME)
.withBinding(InputRole.PHYSICAL, InputControl.A, InputAction.Keys(listOf(57))))
controller = SteamControllerBle(RuntimeEnvironment.getApplication(), router)
controller = SteamControllerBle(RuntimeEnvironment.getApplication(), router) { presses++ }
handler = ReflectionHelpers.getField(controller, "handler")
shadowOf(handler.looper).pause()
controller.start()
@@ -60,6 +61,23 @@ class SteamControllerBleTest {
drain(handler.looper)
}
@Test fun newInputNotifiesOncePerPressOrStickDeflection() {
subscribe(controller)
val held = ByteArray(45).apply { this[1] = 1 }
val stick = held.copyOf().apply { this[10] = 80 }
state(ByteArray(45))
assertEquals(0, presses)
state(held)
state(held)
assertEquals(1, presses)
state(stick)
state(stick)
assertEquals(2, presses)
state(ByteArray(45))
state(held)
assertEquals(3, presses)
}
@Test fun stopReleasesAndDetachesInputBeforeQueuedCallbacksCanPublish() {
state(ByteArray(45).apply { this[1] = 1 })
assertEquals(listOf(57 to true), keys)