diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControlsChoice.kt b/app/src/main/java/com/droiddeck/launcher/input/ControlsChoice.kt index ac21ae327..f31bceb8b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ControlsChoice.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/ControlsChoice.kt @@ -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)) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt index 9e81ce261..6f944e514 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -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) diff --git a/app/src/test/java/com/droiddeck/launcher/input/ControlsChoiceTest.kt b/app/src/test/java/com/droiddeck/launcher/input/ControlsChoiceTest.kt index 76a024dc5..e5dcf47e1 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/ControlsChoiceTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/ControlsChoiceTest.kt @@ -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)) + } }