fix(input): D-pad keeps direction overrides, kit bindings survive layout edits and an emptied gamepad stays empty

This commit is contained in:
Kurt Himebauch committed 2026-11-18 20:41:54 -05:00
1 parent fdcb7eb3c4
commit 952f363b45
11 files changed
+50 -11

No files matched your search

@@ -4479,6 +4479,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
return SessionPrefs.controlsBelowWanted(this, SessionState.mode, sessionRoot.width, sessionRoot.height,
pipUi || isInPictureInPictureMode)
}
@@ -26,8 +26,8 @@ object ControlsPresenter {
state.mode == SessionPrefs.OSC_AUTO && state.desktop -> Plan(Place.NONE)
else -> Plan(
when {
state.steamLayout -> Place.STEAM
state.bottomScreen -> Place.BOTTOM
state.steamLayout -> Place.STEAM
else -> Place.MAIN
},
when {
@@ -277,7 +277,7 @@ object InputLibraries {
.put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel).put("alwaysShown", e.alwaysShown))
layouts.put(JSONObject().put("id", layout.id).put("name", layout.name).put("template", layout.template.name).put("elements", elements)
.put("tint", layout.style.tint ?: JSONObject.NULL).put("opacity", layout.style.opacity).put("screen", layout.screen.name).put("aspect", layout.aspect.toDouble()).put("scale", layout.scale.toDouble())
.put("hidden", JSONArray(layout.style.hidden.map { it.name }.sorted())).put("origin", layout.origin ?: JSONObject.NULL))
.put("hidden", JSONArray(layout.style.hidden.map { it.name }.sorted())).put("origin", layout.origin ?: JSONObject.NULL).put("kitless", layout.kitless))
}
val bindings = JSONArray()
for (b in library.bindings) {
@@ -321,7 +321,7 @@ object InputLibraries {
InputSurfaceStyle(if (!layout.has("tint")) ControllerPrefs.STEAM_BLUE else if (layout.isNull("tint")) null else layout.getInt("tint"), layout.optInt("opacity", 100),
layout.optJSONArray("hidden")?.let { hidden -> (0 until hidden.length()).map { InputCommand.valueOf(hidden.getString(it)) }.toSet() }.orEmpty()),
InputLayoutScreen.valueOf(layout.optString("screen", InputLayoutScreen.BOTH.name)), layout.optDouble("aspect", 0.0).toFloat(), layout.optionalString("origin"),
layout.optDouble("scale", 1.0).toFloat())
layout.optDouble("scale", 1.0).toFloat(), layout.optBoolean("kitless", false))
}
val bindings = doc.getJSONArray("bindings").objects(512).map { b ->
val controls = b.getJSONObject("controls").actions(28).mapKeys { requireNotNull(InputControl.fromId(it.key)) }
@@ -22,6 +22,8 @@ data class SavedInputLayout(
val aspect: Float = 0f,
val origin: String? = null,
val scale: Float = 1f,
/** The player removed every kit control from this gamepad, so it must not follow the virtual controller again. */
val kitless: Boolean = false,
) {
val editable get() = template == InputLayoutTemplate.CUSTOM
val quickButtons get() = QUICK_BUTTONS.filter { button -> elements.any { it.quickButton == button } }
@@ -136,8 +138,8 @@ data class InputLibrary(
fun withPreset(value: SavedInputBindings) = copy(bindings = bindings.filterNot { it.id == value.id } + value)
fun withLayout(value: SavedInputLayout) = copy(layouts = layouts.filterNot { it.id == value.id } + value,
bindings = bindings.map { preset -> if (preset.layoutId != value.id) preset else preset.copy(
elements = preset.elements.filter { (id, action) -> value.elements.any { it.id == id && action.validFor(it) } },
clicks = preset.clicks.filter { (id, action) -> value.elements.any { it.id == id && it.hasClick && action.validForClick() } }) })
elements = preset.elements.filter { (id, action) -> value.element(id)?.let { action.validFor(it) } == true },
clicks = preset.clicks.filter { (id, action) -> value.element(id)?.hasClick == true && action.validForClick() }) })
fun duplicateProfile(id: String, name: String, newId: String = newInputId(), sharePhysical: Boolean = false): InputLibrary {
require(profiles.size < 64)
@@ -5,7 +5,8 @@ class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole
private val route = router?.channel(role, element.control, element = element)
/** The kit's D-pad keeps each direction's own binding, as the separate direction buttons it replaced did. */
private val arms = if (element.id == KitLayout.DPAD_ID && router != null)
listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT).map { router.channel(role, it) } else emptyList()
listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT).map { router.channel(role, it, element = KitLayout.element("legacy:${it.id}")) }
else emptyList()
private val deck = DeckControls.source(role, routed = true)
private val clickDeck = DeckControls.source(role, routed = true)
private val clickOwner = Any()
@@ -59,6 +60,7 @@ class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole
if (latched) 1f else 0f
} else value
if (arms.isNotEmpty() && route?.action is InputAction.Native) {
route.update(x, y, touching)
val values = listOf(y < -.5f, x > .5f, y > .5f, x < -.5f).map { if (it) 1f else 0f }
val toPad = arms.mapIndexed { i, arm -> arm.update(values[i])?.takeIf { it.isPad }?.let { it to values[i] } }.filterNotNull()
if (toPad.isNotEmpty() || nativePad) writePad(this, null) { state -> state.clear(); for ((t, v) in toPad) state.add(t, v) }
@@ -86,6 +88,7 @@ class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole
fun cancel() {
if (closed) return
route?.takeIf { it.action is InputAction.Command }?.release()
arms.filter { it.action is InputAction.Command }.forEach { it.release() }
update(0f)
}
@@ -24,7 +24,7 @@ object OnScreenLayout {
* the layout stores only the keys and controls the player added on top.
*/
fun generated(layout: SavedInputLayout) = (layout.id == InputLibraries.OVERLAY || layout.origin == InputLibraries.OVERLAY) &&
layout.elements.none { KitLayout.isKit(it.id) }
!layout.kitless && layout.elements.none { KitLayout.isKit(it.id) }
fun elements(context: Context, layout: SavedInputLayout?, width: Int, height: Int, safe: Rect, dock: Dock? = null): List<InputSurfaceElement> {
layout ?: return emptyList()
@@ -165,8 +165,8 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
val p = latest.preset(savedBindings.id)
val generated = draft.kitAtDefaults && OnScreenLayout.generated(l.copy(elements = emptyList()))
val extras = draft.elements.filterNot { KitLayout.isKit(it.id) }
latest.withLayout(if (generated) l.copy(elements = extras, aspect = if (extras.isEmpty()) 0f else draft.aspect, scale = draft.layoutScale)
else l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale)).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } },
latest.withLayout(if (generated) l.copy(elements = extras, aspect = if (extras.isEmpty()) 0f else draft.aspect, scale = draft.layoutScale, kitless = false)
else l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale, kitless = draft.elements.none { KitLayout.isKit(it.id) })).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } },
clicks = draft.clicks.filterKeys { id -> draft.elements.any { it.id == id && it.hasClick } }))
}
}.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) }
@@ -576,6 +576,7 @@ class InputSurfaceView(
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.actionMasked == MotionEvent.ACTION_CANCEL) { source?.cancel(); held = 0; invalidate(); return true }
val bits = when (event.actionMasked) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
val dx = event.getX(0) - width / 2f
@@ -27,7 +27,7 @@ class ControlsPresenterTest {
assertFalse(plan.shown)
continue
}
val place = if (state.steamLayout) Place.STEAM else if (state.bottomScreen) Place.BOTTOM else Place.MAIN
val place = if (state.bottomScreen) Place.BOTTOM else if (state.steamLayout) Place.STEAM else Place.MAIN
val hidden = if (state.hiddenByTap) Hidden.BY_TAP else if (state.mode == SessionPrefs.OSC_AUTO && state.controllerInUse) Hidden.BY_CONTROLLER else null
assertEquals(state.toString(), ControlsPresenter.Plan(place, hidden), plan)
assertEquals(state.toString(), hidden == null, plan.shown)
@@ -49,7 +49,7 @@ class ControlsPresenterTest {
}
@Test fun theSteamLayoutWinsOverTheBottomScreen() {
assertEquals(Place.STEAM, ControlsPresenter.plan(playing.copy(steamLayout = true, bottomScreen = true)).place)
assertEquals(Place.BOTTOM, ControlsPresenter.plan(playing.copy(steamLayout = true, bottomScreen = true)).place)
assertEquals(Place.BOTTOM, ControlsPresenter.plan(playing.copy(bottomScreen = true)).place)
}
@@ -198,4 +198,19 @@ class InputSurfaceSourceTest {
assertEquals(0, physical)
assertTrue(player > 0)
}
@Test fun theKitDpadUsesSavedDirectionButtonsAndCancellingNeverRunsADirectionCommand() {
val dpad = InputSurfaceElement(InputControl.LS, .1f, .1f, .3f, .3f, KitLayout.DPAD_ID, InputElementKind.DPAD)
bridge.setInputProfile(InputProfile("test", elements = mapOf(InputRole.SECONDARY to mapOf("legacy:up" to InputAction.Keys(listOf(103)),
"legacy:down" to InputAction.Command(InputCommand.KEYBOARD)))))
val pad = source(dpad)
pad.update(0f, -1f)
assertEquals("key:103:true", events.last())
pad.update(0f, 0f)
assertEquals("key:103:false", events.last())
events.clear()
pad.update(0f, 1f)
pad.cancel()
assertTrue(events.none { it.startsWith("KEYBOARD") })
}
}
@@ -149,4 +149,21 @@ class OnScreenControlPrefsTest {
assertEquals(SessionPrefs.OSC_ALWAYS, SessionPrefs.oscMode(context))
assertTrue(SessionPrefs.steamTouchLayout(context))
}
@Test fun aGamepadWithEveryKitControlRemovedStaysEmptyAndKeepsKitBindingsOnOtherEdits() {
val library = InputLibraries.read(context)
val overlay = library.layouts.single { it.id == InputLibraries.OVERLAY }
val key = InputSurfaceElement(InputControl.A, .4f, .4f, .1f, .1f, "imported:9", name = "Key")
val emptied = overlay.copy(elements = listOf(key), aspect = 16f / 9, kitless = true)
InputLibraries.update(context) { it.withLayout(emptied) }
val stored = InputLibraries.read(context).layouts.single { it.id == InputLibraries.OVERLAY }
assertFalse(OnScreenLayout.generated(stored))
assertEquals(listOf("imported:9"), OnScreenLayout.elements(context, stored, 1920, 1080, android.graphics.Rect()).map { it.id })
val bindings = library.preset(library.activePart(false, InputRole.PRIMARY).bindings)
val withKey = InputLibraries.update(context) { lib ->
lib.withLayout(overlay).withPreset(bindings.copy(elements = bindings.elements + ("legacy:a" to InputAction.Keys(listOf(30)))))
}
val dimmed = withKey.withLayout(overlay.copy(style = overlay.style.copy(opacity = 50)))
assertEquals(InputAction.Keys(listOf(30)), dimmed.preset(bindings.id).elements["legacy:a"])
}
}