feat(steam): sync QAM controls and pass held Back shortcuts (#1621)

Steam's native QAM sliders now control Android brightness and media
volume, with device changes reflected back in Steam. Holding Android
Back for 700 ms sends a real held QAM button for Steam shortcuts. Single
and double taps keep the existing menu/QAM order and swap setting; the
double-tap window remains 500 ms.

- Discover native brightness through a guest backlight descriptor and
use Android's Modify system settings permission. Retain the latest
pending request until permission is granted or the session stops, with a
notification action for hidden sessions.
- Give DirectAudio and fallback AAudio sinks Android media volume/mute
callbacks, keeping software gain at unity. Recover when initial volume
state is absent or invalid, brightness acknowledgements differ or never
arrive, or the Display bridge is not ready yet.
- Forward other buttons while QAM is held in Steam Deck and Steam
Controller modes. Consume release after a hold and clear pending input
on overlays, focus loss, pause, disconnect, and teardown. The session
menu retains its compact Back choice without an explanatory paragraph.

Validation:
- Local ARM64 release build passed. The latest timing change passed 20
targeted input tests, including release at 600 ms remaining a tap. The
preceding full feature check passed 61 Android input/device-control
tests, with permission cases on API 28 and 33.
- 66 Linux Python/Node/C tests passed for device controls, audio
recovery, Steam compatibility, and file dialogs. Both audio sink modules
rebuilt with Android NDK 27.2.
- Release-signed integration build 14d12cf0 is installed on the unrooted
AYN Thor, carrying the Android/input changes through 749ade4a plus the
existing portrait changes. Device checks confirmed the configured
QAM/menu tap order, Back dismissal, removal of the inline menu
description, and held QAM overlapping X with clean release. Hands-on
testing confirmed this installed build works.
- Earlier Thor testing covered brightness permission and pending
requests, brightness synchronization in both directions, native volume
changes, hardware volume key readback, and both sink backends. Failure
recovery is covered by the automated tests above.

The subsequent queued-notification fix in 4d7ceb23 is covered by the
Linux regression suite and has not replaced the Thor install.

Bluetooth/wired output switching and other device models remain
untested. CI and CodeRabbit monitoring continue against the pushed head.
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-26 13:46:14 -04:00
commit a0a68f7280
19 files changed
+1243 -8

No files matched your search

@@ -0,0 +1,175 @@
package com.droiddeck.launcher.input
import android.os.Looper
import android.view.KeyEvent
import java.time.Duration
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE)
class BackQamInputTest {
private val changes = mutableListOf<Boolean>()
private val taps = mutableListOf<Boolean>()
private val input = BackQamInput(changes::add, taps::add)
@Test fun singleTapWaitsForTheDoubleTapWindowWithoutPressingQam() {
send(KeyEvent.ACTION_DOWN)
advance(100)
send(KeyEvent.ACTION_UP)
advance(499)
assertTrue(taps.isEmpty())
advance(1)
assertEquals(listOf(false), taps)
assertTrue(changes.isEmpty())
}
@Test fun doubleTapProducesOnlyTheDoubleAction() {
repeat(2) {
send(KeyEvent.ACTION_DOWN)
advance(100)
send(KeyEvent.ACTION_UP)
advance(100)
}
advance(1000)
assertEquals(listOf(true), taps)
assertTrue(changes.isEmpty())
}
@Test fun aHoldStartsAt700msAndNeverProducesATap() {
assertTrue(send(KeyEvent.ACTION_DOWN))
advance(699)
assertTrue(changes.isEmpty())
advance(1)
for (repeat in 1..10) assertTrue(send(KeyEvent.ACTION_DOWN, repeat = repeat))
advance(4000)
assertEquals(listOf(true), changes)
assertTrue(send(KeyEvent.ACTION_UP))
advance(1000)
assertEquals(listOf(true, false), changes)
assertTrue(taps.isEmpty())
}
@Test fun releasingBetween500And700msStillProducesASingleTap() {
send(KeyEvent.ACTION_DOWN)
advance(600)
send(KeyEvent.ACTION_UP)
advance(500)
assertEquals(listOf(false), taps)
assertTrue(changes.isEmpty())
}
@Test fun holdingTheSecondPressCancelsTheFirstTap() {
send(KeyEvent.ACTION_DOWN)
send(KeyEvent.ACTION_UP)
advance(400)
send(KeyEvent.ACTION_DOWN)
advance(699)
assertTrue(taps.isEmpty())
advance(1)
assertEquals(listOf(true), changes)
send(KeyEvent.ACTION_UP)
advance(1000)
assertTrue(taps.isEmpty())
}
@Test fun otherButtonsAndSelectPassThroughWhileBackIsHeld() {
send(KeyEvent.ACTION_DOWN)
advance(700)
for (code in listOf(KeyEvent.KEYCODE_BUTTON_X, KeyEvent.KEYCODE_BUTTON_SELECT)) {
for (action in listOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) {
assertFalse(input.onKeyEvent(KeyEvent(action, code), true))
}
}
assertEquals(listOf(true), changes)
send(KeyEvent.ACTION_UP)
assertEquals(listOf(true, false), changes)
}
@Test fun disabledRoutingPreservesBackNavigation() {
assertFalse(send(KeyEvent.ACTION_DOWN, enabled = false))
assertFalse(send(KeyEvent.ACTION_UP, enabled = false))
assertTrue(changes.isEmpty())
assertTrue(taps.isEmpty())
}
@Test fun losingFocusReleasesQamAndConsumesTheLateRelease() {
send(KeyEvent.ACTION_DOWN)
advance(700)
input.cancel()
input.cancel()
assertTrue(send(KeyEvent.ACTION_DOWN, repeat = 1, enabled = false))
assertTrue(send(KeyEvent.ACTION_UP, enabled = false))
advance(1000)
assertEquals(listOf(true, false), changes)
assertTrue(taps.isEmpty())
}
@Test fun cancellationBeforeTheThresholdPreventsBothHoldAndTap() {
send(KeyEvent.ACTION_DOWN)
advance(100)
input.cancel()
send(KeyEvent.ACTION_UP)
advance(1000)
assertTrue(changes.isEmpty())
assertTrue(taps.isEmpty())
}
@Test fun cancellationAlsoDiscardsAPendingSingleTap() {
send(KeyEvent.ACTION_DOWN)
send(KeyEvent.ACTION_UP)
input.cancel()
advance(1000)
assertTrue(taps.isEmpty())
}
@Test fun openingAnOverlayReleasesQamBeforeRoutingItsNextKey() {
send(KeyEvent.ACTION_DOWN)
advance(700)
assertFalse(input.onKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BUTTON_B), false))
assertTrue(send(KeyEvent.ACTION_UP, enabled = false))
assertEquals(listOf(true, false), changes)
}
@Test fun repeatsAfterCancellationCannotReassertQam() {
send(KeyEvent.ACTION_DOWN)
advance(700)
input.cancel()
assertTrue(send(KeyEvent.ACTION_DOWN, repeat = 1))
advance(1000)
assertEquals(listOf(true, false), changes)
assertTrue(send(KeyEvent.ACTION_DOWN))
advance(700)
assertTrue(send(KeyEvent.ACTION_UP))
assertEquals(listOf(true, false, true, false), changes)
}
@Test fun cancelledReleaseNeverProducesATap() {
send(KeyEvent.ACTION_DOWN)
assertTrue(input.onKeyEvent(KeyEvent(0, 100, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK,
0, 0, 4, 0, KeyEvent.FLAG_CANCELED), true))
advance(1000)
assertTrue(changes.isEmpty())
assertTrue(taps.isEmpty())
}
@Test fun removingOneDeviceDoesNotReleaseAnotherDevicesHold() {
send(KeyEvent.ACTION_DOWN, device = 4)
send(KeyEvent.ACTION_DOWN, device = 5)
advance(700)
input.onDeviceRemoved(4)
assertEquals(listOf(true), changes)
send(KeyEvent.ACTION_UP, device = 5)
assertEquals(listOf(true, false), changes)
assertTrue(taps.isEmpty())
}
private fun advance(millis: Long) = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(millis))
private fun send(action: Int, repeat: Int = 0, device: Int = 4, enabled: Boolean = true) =
input.onKeyEvent(KeyEvent(0, 100, action, KeyEvent.KEYCODE_BACK, repeat, 0, device, 0), enabled)
}
@@ -0,0 +1,115 @@
package com.droiddeck.launcher.input
import android.os.Handler
import com.droiddeck.launcher.session.SessionState
import java.nio.file.Files
import java.time.Duration
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.util.ReflectionHelpers
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE, instrumentedPackages = ["com.droiddeck.launcher.input.FakeInputWriter"],
shadows = [JoyConPadTest.RecordingWriter::class])
class PadBridgeQamTest {
private val directory = Files.createTempDirectory("qam-pad").toFile()
private lateinit var bridge: PadBridge
@Before fun start() {
JoyConPadTest.RecordingWriter.pads.clear()
ReflectionHelpers.setStaticField(FakeInputWriter::class.java, "padType", FakeInputWriter.PAD_TYPE_DECK)
SessionState.deckPad = true
bridge = PadBridge(directory)
assertTrue(bridge.start())
}
@After fun stop() {
bridge.stop()
SessionState.deckPad = false
directory.deleteRecursively()
}
@Test fun onlyNativeQamControllersAcceptAHeldBack() {
for (type in listOf(-1, FakeInputWriter.PAD_TYPE_DECK, FakeInputWriter.PAD_TYPE_STEAM,
FakeInputWriter.PAD_TYPE_XBOX, FakeInputWriter.PAD_TYPE_DS4, FakeInputWriter.PAD_TYPE_DUALSENSE)) {
ReflectionHelpers.setStaticField(FakeInputWriter::class.java, "padType", type)
val supported = type <= FakeInputWriter.PAD_TYPE_STEAM
assertEquals(supported, bridge.canHoldQam())
bridge.setBackQamPressed(true)
bridge.applyExternal({}, false)
assertEquals(supported, pad().isDown(PadState.QAM))
bridge.setBackQamPressed(false)
}
SessionState.deckPad = false
assertFalse(bridge.canHoldQam())
}
@Test fun qamAndOtherButtonsShareAReportUntilBackIsReleased() {
bridge.setBackQamPressed(true)
advanceTime()
bridge.applyExternal({ it.press(PadState.X, true); it.press(PadState.SELECT, true) }, true)
assertTrue(pad().isDown(PadState.QAM))
assertTrue(pad().isDown(PadState.X))
assertTrue(pad().isDown(PadState.SELECT))
bridge.setBackQamPressed(false)
assertFalse(pad().isDown(PadState.QAM))
assertTrue(pad().isDown(PadState.X))
assertTrue(pad().isDown(PadState.SELECT))
}
@Test fun touchQamAndBackHaveIndependentReleases() {
bridge.setBackQamPressed(true)
bridge.setSystemButtons(false, true)
bridge.setBackQamPressed(false)
assertTrue(pad().isDown(PadState.QAM))
bridge.setBackQamPressed(true)
bridge.setSystemButtons(false, false)
assertTrue(pad().isDown(PadState.QAM))
bridge.setBackQamPressed(false)
assertFalse(pad().isDown(PadState.QAM))
}
@Test fun aTimedQamTapCannotReleaseAHeldBack() {
bridge.triggerQam()
bridge.setBackQamPressed(true)
advanceTime()
assertTrue(pad().isDown(PadState.QAM))
bridge.setBackQamPressed(false)
assertFalse(pad().isDown(PadState.QAM))
}
@Test fun releasingAllOrStoppingClearsTheHold() {
bridge.setBackQamPressed(true)
bridge.releaseAll()
assertFalse(pad().isDown(PadState.QAM))
bridge.setBackQamPressed(true)
bridge.stop()
bridge.start()
bridge.applyExternal({}, false)
assertFalse(pad().isDown(PadState.QAM))
}
@Test fun disablingTheBuiltinPadClearsAndRejectsTheHold() {
bridge.setBackQamPressed(true)
bridge.setBuiltinPad(false)
assertFalse(bridge.canHoldQam())
assertFalse(pad().isDown(PadState.QAM))
bridge.setBackQamPressed(true)
bridge.setBuiltinPad(true)
bridge.applyExternal({}, false)
assertFalse(pad().isDown(PadState.QAM))
}
private fun pad() = requireNotNull(JoyConPadTest.RecordingWriter.pads[0])
private fun advanceTime() {
val handler = ReflectionHelpers.getStaticField<Handler>(PadBridge::class.java, "chordHandler")
shadowOf(handler.looper).idleFor(Duration.ofSeconds(2))
}
}
@@ -0,0 +1,151 @@
package com.droiddeck.launcher.runtime
import android.app.Application
import android.app.NotificationManager
import android.content.Context
import android.media.AudioManager
import android.os.Looper
import android.provider.Settings
import java.io.File
import java.time.Duration
import org.json.JSONObject
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.Implements
import org.robolectric.annotation.Implementation
import org.robolectric.shadows.ShadowSettings
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28, 33], manifest = Config.NONE, application = Application::class, shadows = [WriteSettingsShadow::class])
class DeviceControlsComponentTest {
@get:Rule val tmp = TemporaryFolder()
private val context get() = RuntimeEnvironment.getApplication()
private val audio get() = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
private lateinit var component: DeviceControlsComponent
private lateinit var directory: File
private var visible = true
@Before fun start() {
WriteSettingsShadow.writable = true
Settings.System.putInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 128)
val root = tmp.newFolder()
directory = DeviceControlsComponent.directory(root)
component = DeviceControlsComponent(context, root) { visible }
component.publish()
component.start()
shadowOf(Looper.getMainLooper()).idle()
}
@After fun stop() {
component.stop()
shadowOf(Looper.getMainLooper()).idle()
}
private fun tick() = shadowOf(Looper.getMainLooper()).idleFor(Duration.ofMillis(300))
private fun request(name: String, text: String) {
File(directory, "$name.request").writeText(text)
tick()
}
@Test fun androidChangesArePublishedAndRequestsUseDeviceVolumeSteps() {
request("volume", "999")
assertEquals(audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), audio.getStreamVolume(AudioManager.STREAM_MUSIC))
audio.setStreamVolume(AudioManager.STREAM_MUSIC, 3, 0)
tick()
assertEquals("3", File(directory, "volume.state").readText().split(" ").first())
request("volume", "invalid")
assertEquals(3, audio.getStreamVolume(AudioManager.STREAM_MUSIC))
}
@Test fun deniedBrightnessIsReadBackAndAppliedOnlyAfterPermission() {
WriteSettingsShadow.writable = false
request("brightness", "0.75")
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
assertEquals(Settings.ACTION_MANAGE_WRITE_SETTINGS, shadowOf(context).nextStartedActivity.action)
assertFalse(JSONObject(File(directory, "state.json").readText()).getBoolean("brightnessWritable"))
WriteSettingsShadow.writable = true
tick()
assertEquals(192, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
@Test fun brightnessIsBoundedAndExternalChangesAreReadBack() {
for (invalid in listOf("NaN", "Infinity", "-0.1", "1.1")) request("brightness", invalid)
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
request("brightness", "0")
assertEquals(1, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
Settings.System.putInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS, 255)
tick()
assertEquals(1.0, JSONObject(File(directory, "state.json").readText()).getDouble("brightness"), 0.0001)
}
@Test fun hiddenPermissionRequestOffersANotificationAndRetainsTheLatestValue() {
visible = false
WriteSettingsShadow.writable = false
request("brightness", "0.25")
request("brightness", "0.75")
assertNull(shadowOf(context).nextStartedActivity)
val notifications = context.getSystemService(NotificationManager::class.java)
val notification = notifications.activeNotifications.single().notification
notification.contentIntent.send()
assertEquals(Settings.ACTION_MANAGE_WRITE_SETTINGS, shadowOf(context).nextStartedActivity.action)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(121))
WriteSettingsShadow.writable = true
tick()
assertEquals(192, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
assertTrue(notifications.activeNotifications.isEmpty())
}
@Test fun stoppingClearsThePermissionNotificationAndPendingRequest() {
visible = false
WriteSettingsShadow.writable = false
request("brightness", "0.75")
component.stop()
shadowOf(Looper.getMainLooper()).idle()
assertTrue(context.getSystemService(NotificationManager::class.java).activeNotifications.isEmpty())
WriteSettingsShadow.writable = true
tick()
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
@Test fun stoppedSessionsDoNotConsumeNewCommands() {
component.stop()
shadowOf(Looper.getMainLooper()).idle()
request("brightness", "0.2")
assertEquals(128, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
@Test fun rejectedPermissionCanBeRequestedAgainWithoutOpeningRepeatedScreens() {
WriteSettingsShadow.writable = false
request("brightness", "0.75")
assertNotNull(shadowOf(context).nextStartedActivity)
request("brightness", "0.8")
assertNull(shadowOf(context).nextStartedActivity)
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(5))
request("brightness", "0.8")
assertNotNull(shadowOf(context).nextStartedActivity)
}
@Test fun oversizedRequestsAreDiscardedAndDoNotBlockLaterCommands() {
request("brightness", "0".repeat(1000))
assertFalse(File(directory, "brightness.request.processing").exists())
request("brightness", "0.75")
assertEquals(192, Settings.System.getInt(context.contentResolver, Settings.System.SCREEN_BRIGHTNESS))
}
}
@Implements(Settings.System::class)
class WriteSettingsShadow : ShadowSettings.ShadowSystem() {
companion object {
@JvmField var writable = true
@JvmStatic @Implementation fun canWrite(context: Context) = writable
}
}