fix(input): choosing the Gamepad layout in Steam mode keeps its gamepad actions (#2954)

Choosing a layout from the layout menu took the first actions saved for it.
Desktop and Steam mode both draw the stock Gamepad, and Desktop's actions
(stick moves the pointer, face buttons click and press Enter and Esc,
D-pad sends arrow keys) could come first, so Steam mode's on-screen pad
became a mouse and keyboard. A choice now takes the mode's own actions,
or any the other mode doesn't use, and a Steam-mode selection already
holding Desktop's actions is dropped on upgrade.
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-20 15:31:42 -05:00
1 parent 089c18b49d
commit 33761c30ab
3 files changed
+46 -2

No files matched your search

@@ -125,7 +125,7 @@ object ControlsChoice {
val preferred = listOfNotNull(own(context, library, game), library.activePart(desktop, InputRole.PRIMARY).bindings,
library.profile(library.activeId(desktop)).binding(InputRole.PRIMARY))
preferred.firstNotNullOfOrNull { id -> relatedPreset(library, available, id, layout.id) }
?: available.firstOrNull()?.id ?: newInputId().also { newId ->
?: available.firstOrNull { it.id != InputLibraries.primaryOf(library, !desktop) }?.id ?: newInputId().also { newId ->
InputLibraries.update(context) { it.withPreset(SavedInputBindings(newId, layout.name, false, layout.id)) }
}
}
@@ -10,7 +10,7 @@ import org.json.JSONObject
import java.util.UUID
object InputLibraries {
private const val VERSION = 15
private const val VERSION = 16
private const val KEY = "library"
private const val RECOVERY = "library-recovery."
const val OVERLAY = "builtin.overlay"
@@ -33,6 +33,7 @@ object InputLibraries {
if (version < 13) library = LegacyScreenKeyMigration.pin(library)
if (version < 14) library = deckGamepadSurfaces(context, library)
if (version < 15) library = kitGamepads(context, library, version)
if (version < 16) library = ownModeSelections(library)
if (version < 6 || !p.contains(LegacyScreenKeyMigration.BACKUP)) {
val legacy = LegacyScreenKeyMigration.snapshot(context)
if (legacy != null) {
@@ -311,6 +312,18 @@ object InputLibraries {
}
}
internal fun primaryOf(library: InputLibrary, desktop: Boolean) =
library.profiles.firstOrNull { it.id == library.activeId(desktop) }?.parts?.get(InputRole.PRIMARY)?.bindings
/** Choosing a layout used to take the first actions saved for it, which could be the other mode's mouse and keys. */
private fun ownModeSelections(library: InputLibrary): InputLibrary = library.copy(selections = library.selections.mapValues { (mode, selection) ->
val desktop = mode == InputProfile.DESKTOP
val chosen = selection.parts[InputRole.PRIMARY]
if (chosen != null && chosen == primaryOf(library, !desktop) && chosen != primaryOf(library, desktop))
selection.copy(parts = selection.parts - InputRole.PRIMARY)
else selection
})
private fun migrationBindingId(library: InputLibrary, binding: String, layout: String): String {
val seed = JSONArray(listOf(binding, layout)).toString().toByteArray(Charsets.UTF_8)
val base = "migrated.bindings." + UUID.nameUUIDFromBytes(seed)
@@ -198,4 +198,35 @@ class ControlsChoiceTest {
assertEquals(InputLibraries.OVERLAY, library.profile(InputProfile.GAME).parts.getValue(InputRole.PRIMARY).layout)
assertEquals(InputLibraries.OVERLAY, ControlsChoice.current(context, null, steamMode = true))
}
private fun desktopMouseFirst(): String {
val mouse = SavedInputBindings("desktop.mouse", "Desktop / primary", false, InputLibraries.OVERLAY,
controls = mapOf(InputControl.A to InputAction.MouseButton(272), InputControl.LS to InputAction.Pointer()))
InputLibraries.update(context) { it.copy(bindings = listOf(mouse) + it.bindings).chooseBindings(InputProfile.DESKTOP, InputRole.PRIMARY, mouse.id) }
return mouse.id
}
@Test fun choosingTheGamepadKeepsEachModesOwnActions() {
val mouse = desktopMouseFirst()
val game = InputLibraries.read(context).profile(InputProfile.GAME).parts.getValue(InputRole.PRIMARY).bindings
ControlsChoice.choose(context, null, InputLibraries.OVERLAY, desktop = false)
ControlsChoice.choose(context, null, InputLibraries.OVERLAY, desktop = true)
val library = InputLibraries.read(context)
assertNotEquals(mouse, library.activePart(false, InputRole.PRIMARY).bindings)
assertEquals(game, library.activePart(false, InputRole.PRIMARY).bindings)
assertEquals(mouse, library.activePart(true, InputRole.PRIMARY).bindings)
ControlsChoice.choose(context, 7, InputLibraries.OVERLAY, desktop = false)
assertNotEquals(mouse, GameControls.bindings(context, 7))
}
@Test fun aGameSelectionHoldingDesktopActionsIsDroppedOnUpgrade() {
val mouse = desktopMouseFirst()
val broken = InputLibraries.read(context).selectPart(false, InputRole.PRIMARY, mouse)
val raw = org.json.JSONObject(InputLibraries.encode(broken)).put("version", 15).toString()
context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("library", raw).commit()
val library = InputLibraries.read(context)
assertNull(library.selection(false).parts[InputRole.PRIMARY])
assertEquals(mouse, library.activePart(true, InputRole.PRIMARY).bindings)
assertEquals(InputLibraries.OVERLAY, ControlsChoice.current(context, null, steamMode = true))
}
}