feat: Add physical controller button shortcuts (#1202)

* feat(controller): add physical button shortcuts

* fix(controller): improve shortcut capture and assignment feedback

* refactor(controller): simplify menu button assignment UI

* feat(controller): save menu assignments on button release

* refactor(controller): remove obsolete shortcut UI resources

* fix(controller): place drawer remaps below Back

* refactor(controller): simplify shortcut capture wording

* fix(controller): refresh active shortcut bindings on changes

* fix(controller): preserve drawer confirmation for mapped A

* fix: preserve controller shortcut navigation and cancel

* test: align shortcut dialog assertions

---------

Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-10-30 07:08:59 -04:00
1 parent f69e2d0171
commit 4770d11e39
42 files changed
+1316

No files matched your search

@@ -75,6 +75,8 @@ import com.droiddeck.launcher.input.ControllerPrefs
import com.droiddeck.launcher.input.SessionClipboard
import com.droiddeck.launcher.input.OnScreenControls
import com.droiddeck.launcher.input.PadBridge
import com.droiddeck.launcher.input.PhysicalShortcuts
import com.droiddeck.launcher.input.PhysicalShortcutRouter
import com.droiddeck.launcher.input.PadMouse
import com.droiddeck.launcher.input.PadState
import com.droiddeck.launcher.input.ScreenKeys
@@ -162,6 +164,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private lateinit var hud: FusionHudHost
private lateinit var memoryHud: com.droiddeck.launcher.session.MemoryHud
private var padBridge: PadBridge? = null
private val physicalShortcuts = PhysicalShortcutRouter()
private var joyConVr: com.droiddeck.launcher.input.JoyConVr? = null
private var handCamera: com.droiddeck.launcher.handtracking.HandCamera? = null
private var padMotion: com.droiddeck.launcher.input.PadMotion? = null
@@ -433,6 +436,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
private val cardboardPrefListener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == SessionPrefs.CARDBOARD_KEY) uiHandler.post { applyCardboard(SessionPrefs.cardboard(this)) }
}
private val shortcutPrefListener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == null || key == PhysicalShortcuts.KEY) {
physicalShortcuts.bindings = PhysicalShortcuts.read(this)
}
}
private var textureAnisotropy by mutableStateOf(0)
private var textureLodBias by mutableStateOf(TextureFiltering.LOD_BIAS_OFF)
private var drawerTextureAnisotropy by mutableStateOf<Int?>(null)
@@ -519,6 +527,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
updateHandCamera()
}
override fun onInputDeviceRemoved(deviceId: Int) {
physicalShortcuts.onDeviceRemoved(deviceId)
Log.i(TAG, "input device $deviceId disconnected")
backQamInput.onDeviceRemoved(deviceId)
joyConVr?.refresh()
@@ -1828,6 +1837,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
steamDownloadsInBackground = SessionPrefs.steamDownloadsInBackground(this)
oscMode = SessionPrefs.oscMode(this)
backActionsInverted = SessionPrefs.backActionsInverted(this)
physicalShortcuts.bindings = PhysicalShortcuts.read(this)
backDoubleTapMs = SessionPrefs.backDoubleTapMs(this)
lowLatency = SessionPrefs.lowLatency(this)
evdevMouseOn = SessionPrefs.evdevMouse(this)
@@ -2456,6 +2466,26 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
if (pipUi) return true
if (screenKeyTextFocus) return super.dispatchKeyEvent(event)
if (physicalShortcuts.onKey(
event,
enabled = SessionState.running && !SessionState.suspended &&
!(drawerOpen && event.keyCode == KeyEvent.KEYCODE_BUTTON_A) &&
!(drawerOpen && PhysicalShortcuts.isDrawerNavigationKey(event)) &&
(event.deviceId to event.keyCode) !in resumeKeysDown &&
!(::loading.isInitialized && (loading.visible || loading.ended)) &&
!pcKeyboardOpen && !achievementsOpen && !gestureSettingsOpen,
steam = SessionState.mode == SessionService.MODE_STEAM,
) { action ->
pendingBackAction?.let(uiHandler::removeCallbacks)
pendingBackAction = null
drawerControllerActive = true
Log.i(TAG, "physical shortcut ${action.name} device=${event.deviceId} key=${event.keyCode} scan=${event.scanCode}")
when (action) {
PhysicalShortcuts.Action.STEAM -> sendSteamGuide()
PhysicalShortcuts.Action.QAM -> triggerSteamQam()
PhysicalShortcuts.Action.DROIDDECK -> drawerOpen = !drawerOpen
}
}) return true
val pad = com.droiddeck.launcher.input.JoyCon.normalize(event)
if (pad !== event) return dispatchKeyEvent(pad)
val fromController = PadBridge.isControllerKeyEvent(event)
@@ -3892,6 +3922,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
updatePointerCapture()
if (!pipUi) sessionClipboard.start()
updatePadMotion()
ControllerPrefs.prefs(this).registerOnSharedPreferenceChangeListener(shortcutPrefListener)
readPrefs()
refreshControls()
if (::pip.isInitialized) pip.resumed()
@@ -3902,6 +3933,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
override fun onPause() {
com.droiddeck.launcher.input.HomeButtonShortcuts.detach(this)
if (runtimeRemovalBlocked) { super.onPause(); return }
physicalShortcuts.clear()
backQamInput.cancel()
capturePausedFrame()
sessionClipboard.stop()
@@ -3925,6 +3957,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
updatePadMotion()
keyboard?.takeIf { it.shown }?.hide()
SessionPrefs.sessionPrefs(this).unregisterOnSharedPreferenceChangeListener(cardboardPrefListener)
ControllerPrefs.prefs(this).unregisterOnSharedPreferenceChangeListener(shortcutPrefListener)
updateHeadTracker()
super.onPause()
}
@@ -0,0 +1,145 @@
package com.droiddeck.launcher.input
import android.content.Context
import android.util.Log
import android.view.InputDevice
import android.view.KeyEvent
import androidx.annotation.StringRes
import com.droiddeck.launcher.R
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
object PhysicalShortcuts {
internal const val KEY = "physicalShortcuts"
private const val TAG = "PhysicalShortcuts"
enum class Action(@StringRes val label: Int) {
STEAM(R.string.drawer_steam_menu),
QAM(R.string.drawer_open_qam),
DROIDDECK(R.string.shortcuts_dd_menu),
}
data class Button(
val device: String,
val name: String,
val keyCode: Int,
val scanCode: Int,
) {
fun matches(event: KeyEvent): Boolean =
event.device?.let { device == identity(it) } == true &&
keyCode == event.keyCode && scanCode == event.scanCode
fun sameInput(other: Button): Boolean =
device == other.device && keyCode == other.keyCode && scanCode == other.scanCode
val label: String
get() = KeyEvent.keyCodeToString(keyCode).removePrefix("KEYCODE_").removePrefix("BUTTON_")
}
data class Binding(val button: Button, val action: Action)
private fun identity(device: InputDevice): String =
"${device.vendorId}:${device.productId}:${device.descriptor.ifBlank { device.name }}"
fun isVolumeKey(event: KeyEvent): Boolean = when (event.keyCode) {
KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_MUTE -> true
else -> false
}
fun isDrawerNavigationKey(event: KeyEvent): Boolean = when (event.keyCode) {
KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN,
KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_RIGHT -> true
else -> false
}
fun button(event: KeyEvent): Button? {
val device = event.device ?: return null
if (device.isVirtual || isVolumeKey(event)) return null
when (event.keyCode) {
KeyEvent.KEYCODE_HOME, KeyEvent.KEYCODE_POWER, KeyEvent.KEYCODE_SLEEP,
KeyEvent.KEYCODE_WAKEUP, KeyEvent.KEYCODE_APP_SWITCH -> return null
}
if (event.keyCode == KeyEvent.KEYCODE_UNKNOWN && event.scanCode <= 0) return null
return Button(identity(device), device.name, event.keyCode, event.scanCode)
}
fun read(context: Context): List<Binding> {
val raw = ControllerPrefs.prefs(context).getString(KEY, null) ?: return emptyList()
return try {
val array = JSONArray(raw)
buildList {
for (i in 0 until array.length()) {
try {
val entry = array.getJSONObject(i)
val action = Action.entries.firstOrNull { it.name == entry.getString("action") }
val code = entry.getInt("keyCode")
val scan = entry.getInt("scanCode")
val device = entry.getString("device")
if (action == null || device.isBlank() || code < 0 || scan < 0) {
Log.w(TAG, "Ignoring invalid shortcut at index $i")
continue
}
val binding = Binding(Button(device, entry.getString("name"), code, scan), action)
if (none { it.button.sameInput(binding.button) }) add(binding)
} catch (e: JSONException) {
Log.w(TAG, "Ignoring malformed shortcut at index $i", e)
}
}
}
} catch (e: JSONException) {
Log.w(TAG, "Could not read physical shortcuts; reset shortcuts to restore defaults", e)
emptyList()
}
}
fun assign(context: Context, button: Button, action: Action) {
val bindings = read(context).filterNot {
it.button.sameInput(button) || (it.button.device == button.device && it.action == action)
} + Binding(button, action)
write(context, bindings)
}
fun remove(context: Context, binding: Binding) {
write(context, read(context).filterNot { it == binding })
}
private fun write(context: Context, bindings: List<Binding>) {
val array = JSONArray()
bindings.forEach {
array.put(JSONObject().put("device", it.button.device).put("name", it.button.name)
.put("keyCode", it.button.keyCode).put("scanCode", it.button.scanCode).put("action", it.action.name))
}
ControllerPrefs.prefs(context).edit().putString(KEY, array.toString()).apply()
}
}
class PhysicalShortcutRouter {
var bindings: List<PhysicalShortcuts.Binding> = emptyList()
private data class Press(val device: Int, val keyCode: Int, val scanCode: Int)
private val held = mutableSetOf<Press>()
fun onKey(event: KeyEvent, enabled: Boolean, steam: Boolean, perform: (PhysicalShortcuts.Action) -> Unit): Boolean {
val press = Press(event.deviceId, event.keyCode, event.scanCode)
if (event.action == KeyEvent.ACTION_UP) return held.remove(press)
if (event.action != KeyEvent.ACTION_DOWN) return false
if (press in held) return true
if (!enabled || bindings.isEmpty()) return false
val button = PhysicalShortcuts.button(event) ?: return false
val binding = bindings.firstOrNull { it.button.sameInput(button) } ?: return false
if (!steam && binding.action != PhysicalShortcuts.Action.DROIDDECK) return false
held.add(press)
if (event.repeatCount == 0) perform(binding.action)
return true
}
fun onDeviceRemoved(deviceId: Int) {
held.removeAll { it.device == deviceId }
}
fun clear() {
held.clear()
}
}
@@ -93,6 +93,9 @@ fun ControllerMappingPage(mapping: Map<String, String>, onPick: (String, String)
lede = stringResource(R.string.ctrl_mapping_lede),
onBack = onBack,
) {
SettingsGroup(stringResource(R.string.shortcuts_title)) {
PhysicalShortcutRows()
}
SettingsGroup(stringResource(R.string.ctrl_onscreen_buttons)) {
val targets = ControllerPrefs.targets.map { (id, label) -> id to stringResource(label) }
for ((id, name) in ControllerPrefs.mappable) {
@@ -306,6 +306,7 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
if (steam && s.steamController in listOf(SessionPrefs.CONTROLLER_DECK, SessionPrefs.CONTROLLER_STEAM)) {
Note(stringResource(R.string.back_native_qam_hint))
}
PhysicalShortcutRows()
}
}
if (tab == SheetTab.AUDIO && steam) {
@@ -0,0 +1,189 @@
package com.droiddeck.launcher.ui
import android.util.Log
import android.view.KeyEvent
import android.view.Window
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.DialogWindowProvider
import com.droiddeck.launcher.R
import com.droiddeck.launcher.input.PhysicalShortcuts
@Composable
internal fun PhysicalShortcutRows(
onChanged: () -> Unit = {},
modifierFor: (PhysicalShortcuts.Action) -> Modifier = { Modifier },
) {
val context = LocalContext.current
var bindings by remember { mutableStateOf(PhysicalShortcuts.read(context)) }
var editing by remember { mutableStateOf<PhysicalShortcuts.Action?>(null) }
Column {
for (action in PhysicalShortcuts.Action.entries) {
val assigned = bindings.filter { it.action == action }
SettingsRow(
stringResource(action.label),
assigned.joinToString { "${it.button.label} · ${it.button.name}" }.ifEmpty { null },
) {
SecondaryButton(
stringResource(if (assigned.isEmpty()) R.string.game_settings_default else R.string.ctrl_custom),
compact = true,
modifier = modifierFor(action).width(LocalChipMinWidth.current)
.testTag("shortcuts-assign-${action.name}"),
) { editing = action }
}
}
}
editing?.let { action ->
PhysicalShortcutsDialog(action, onDismiss = { editing = null }, onChanged = {
bindings = PhysicalShortcuts.read(context)
onChanged()
})
}
}
@Composable
internal fun PhysicalShortcutsDialog(
action: PhysicalShortcuts.Action,
onDismiss: () -> Unit,
onChanged: () -> Unit = {},
) {
val context = LocalContext.current
val bindings = remember { PhysicalShortcuts.read(context) }
var candidate by remember { mutableStateOf<PhysicalShortcuts.Button?>(null) }
var listening by remember { mutableStateOf(true) }
var capturedDevice by remember { mutableStateOf(-1) }
Dialog(
onDismissRequest = onDismiss,
properties = DialogProperties(dismissOnBackPress = !listening, usePlatformDefaultWidth = false),
) {
CaptureShortcutKeys { event ->
if (listening && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0 &&
(event.keyCode == KeyEvent.KEYCODE_BACK || event.keyCode == KeyEvent.KEYCODE_BUTTON_B)) {
listening = false
onDismiss()
true
} else if (!listening || PhysicalShortcuts.isVolumeKey(event)) {
false
} else {
if (candidate == null && event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) {
PhysicalShortcuts.button(event)?.let { button ->
candidate = button
capturedDevice = event.deviceId
Log.i("PhysicalShortcuts", "capture device=${button.name} key=${event.keyCode} " +
"scan=${event.scanCode} source=0x${event.source.toString(16)}")
}
} else if (event.action == KeyEvent.ACTION_UP && event.deviceId == capturedDevice &&
candidate?.matches(event) == true) {
listening = false
candidate?.let { button ->
if (bindings.none { it.button.sameInput(button) && it.action != action }) {
PhysicalShortcuts.assign(context, button, action)
onChanged()
onDismiss()
}
}
}
true
}
}
CompositionLocalProvider(LocalFrontFocus provides null) {
Surface(
shape = RoundedCornerShape(16.dp),
modifier = Modifier.padding(12.dp).widthIn(max = 560.dp).fillMaxWidth()
.heightIn(max = (LocalConfiguration.current.screenHeightDp - 24).dp),
) {
Column(
Modifier.verticalScroll(rememberScrollState()).padding(18.dp).controllerBack(onDismiss),
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
Text(stringResource(R.string.shortcuts_capture, stringResource(action.label)),
style = MaterialTheme.typography.titleLarge)
val button = candidate
if (button == null) {
Text(stringResource(R.string.shortcuts_press))
} else {
Text(stringResource(R.string.shortcuts_detected, button.name, button.label))
if (listening) {
Text(stringResource(R.string.shortcuts_release))
} else {
val replacements = bindings.filter {
it.button.sameInput(button) || (it.button.device == button.device && it.action == action)
}
for (conflict in replacements) {
Text("${conflict.button.label} · ${conflict.button.name}")
Text(stringResource(R.string.shortcuts_conflict, stringResource(conflict.action.label)))
}
SecondaryButton(stringResource(R.string.shortcuts_save), modifier = Modifier.testTag("shortcuts-save")) {
PhysicalShortcuts.assign(context, button, action)
onChanged()
onDismiss()
}
}
SecondaryButton(stringResource(R.string.shortcuts_retry), modifier = Modifier.testTag("shortcuts-retry")) {
candidate = null
capturedDevice = -1
listening = true
}
}
Text(stringResource(R.string.shortcuts_limitations), style = MaterialTheme.typography.bodySmall)
for (binding in bindings.filter { it.action == action }) {
ActionRow(
stringResource(R.string.shortcuts_detected, binding.button.name, binding.button.label),
null, stringResource(R.string.ctrl_reset), {
PhysicalShortcuts.remove(context, binding)
onChanged()
onDismiss()
},
modifier = Modifier.testTag("shortcuts-remove"),
)
}
SecondaryButton(stringResource(R.string.common_close), modifier = Modifier.testTag("shortcuts-cancel"),
onClick = onDismiss)
}
}
}
}
}
@Composable
private fun CaptureShortcutKeys(onKey: (KeyEvent) -> Boolean) {
val currentOnKey by rememberUpdatedState(onKey)
val window = (LocalView.current.parent as DialogWindowProvider).window
DisposableEffect(window) {
val previous = window.callback
// Capture before dialog navigation so Back and gamepad buttons can be assigned.
window.callback = object : Window.Callback by previous {
override fun dispatchKeyEvent(event: KeyEvent): Boolean =
currentOnKey(event) || previous.dispatchKeyEvent(event)
}
onDispose { window.callback = previous }
}
}
@@ -635,6 +635,7 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
if (a.steam) ChoiceRow(host, "back-double-tap", stringResource(R.string.back_double_tap_window), null,
SessionPrefs.BACK_DOUBLE_TAP_MS_OPTIONS.map { it to stringResource(R.string.back_tap_ms, it) },
a.backDoubleTapMs, chipModifier = focus.track(page, "back-double-tap"), onPick = a.onBackDoubleTapMs)
PhysicalShortcutRows(modifierFor = { focus.track(page, "physical-shortcut-${it.name}") })
if (a.steam && a.unfoldedControls != null) ToggleRow(host, "unfolded-controls", stringResource(R.string.unfolded_controls),
stringResource(R.string.unfolded_controls_hint), a.unfoldedControls,
chipModifier = focus.track(page, "unfolded-controls"), onChange = a.onUnfoldedControls)