fix(input): keep kit bindings, D-pad directions and Steam touch tint through the redesign

This commit is contained in:
Kurt Himebauch committed 2026-11-18 20:02:37 -05:00
1 parent 695ae21f93
commit 6f564caba6
13 files changed
+118 -22

No files matched your search

@@ -2036,7 +2036,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
}
private fun loadFrameGenFor(appId: Long?) {
if (appId != controlsGame) { controlsGame = appId; uiHandler.post { refreshInputProfile(); updateOnScreenControls() } }
val layoutGame = controlsGameId()
if (layoutGame != controlsGame) { controlsGame = layoutGame; uiHandler.post { refreshInputProfile(); updateOnScreenControls() } }
val profileId = appId?.toString()
frameGen = FrameGen.mode(this, profileId)
frameGenHasGameOverride = appId?.let {
@@ -5892,6 +5893,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (!screenOff) {
gestureSettingsOpen = false
inputSettingsOpen = false
layoutEditor = null
}
releasePads()
heldKeys.clear()
@@ -6269,6 +6271,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
if (event.actionMasked == MotionEvent.ACTION_DOWN) {
drawerControllerActive = false
gestureTouchStreamGate.reset()
if (controllerInUse && event.isFromSource(InputDevice.SOURCE_TOUCHSCREEN)) { controllerInUse = false; updateOnScreenControls() }
}
if (!gestureTouchStreamGate.accepts(GestureTouchStreamGate.Source.SURFACE, event)) return false
return onTouchEvent(event)
@@ -130,9 +130,9 @@ object InputLibraries {
InputStockLayouts.layouts().firstOrNull { it.id == origin }?.let { return it.elements to it.aspect }
if (origin == OVERLAY) {
val (surfaceContext, metrics) = primaryDisplay(context)
val w = maxOf(metrics.widthPixels, metrics.heightPixels).coerceAtLeast(1)
val h = minOf(metrics.widthPixels, metrics.heightPixels).coerceAtLeast(1)
return KitLayout.elements(surfaceContext, PadKit.current(context), w, h) to w.toFloat() / h
val w = metrics.widthPixels.coerceAtLeast(1)
val h = metrics.heightPixels.coerceAtLeast(1)
return KitLayout.elements(surfaceContext, PadKit.current(context), w, h, scale = layout.scale) to w.toFloat() / h
}
return library.layouts.firstOrNull { it.id == origin && it.id != layout.id }?.let { it.elements to it.aspect }
}
@@ -147,10 +147,9 @@ object InputLibraries {
private fun upgradeOverlay(context: Context, library: InputLibrary): InputLibrary {
if (library.layouts.none { it.template == InputLayoutTemplate.OVERLAY }) return library
val settings = ControllerPrefs.read(context)
val elements = emptyList<InputSurfaceElement>()
val style = InputSurfaceStyle(opacity = settings.opacity)
val replacements = library.layouts.filter { it.template == InputLayoutTemplate.OVERLAY }.associate { old ->
old.id to old.copy(name = "Gamepad", elements = elements, template = InputLayoutTemplate.CUSTOM, style = style)
old.id to old.copy(name = "Gamepad", elements = emptyList(), template = InputLayoutTemplate.CUSTOM, style = style)
}
var result = library.copy(layouts = library.layouts.map { replacements[it.id] ?: it })
val migrated = mutableMapOf<Pair<String, String>, String>()
@@ -161,9 +160,8 @@ object InputLibraries {
val id = migrated.getOrPut(key) {
val old = library.preset(part.bindings)
val newId = migrationBindingId(result, old.id, layout.id)
val keyboard = elements.filter { it.id == "builtin:keyboard" }.associate { it.id to InputAction.Command(InputCommand.KEYBOARD) }
val clicks = if (settings.stickClick) old.clicks else old.clicks + elements.filter { it.hasClick }.associate { it.id to InputAction.Disabled }
result = result.withPreset(old.copy(id = newId, layoutId = layout.id, elements = old.elements + keyboard, clicks = clicks))
val clicks = if (settings.stickClick) old.clicks else old.clicks + KitLayout.clickIds.associateWith { InputAction.Disabled }
result = result.withPreset(old.copy(id = newId, layoutId = layout.id, clicks = clicks))
newId
}
part.copy(bindings = id)
@@ -271,8 +271,8 @@ data class InputLibrary(
require(preset.physical || preset.keys.isEmpty())
val layout = preset.layoutId?.let { id -> layouts.first { it.id == id } }
require(!preset.physical || layout == null && preset.elements.isEmpty() && preset.clicks.isEmpty())
require(preset.elements.all { (id, action) -> layout?.elements?.firstOrNull { it.id == id }?.let { action.validFor(it) } == true })
require(preset.clicks.all { (id, action) -> layout?.elements?.any { it.id == id && it.hasClick } == true && action.validForClick() })
require(preset.elements.all { (id, action) -> layout?.element(id)?.let { action.validFor(it) } == true })
require(preset.clicks.all { (id, action) -> layout?.element(id)?.hasClick == true && action.validForClick() })
require(preset.keys.all { it.key.device.isNotBlank() && it.key.keyCode >= 0 && it.key.scanCode >= 0 &&
it.action.validFor(InputControl.A) && (it.action !is InputAction.Native || it.action.target == null) })
}
@@ -312,3 +312,7 @@ internal fun InputAction.validFor(element: InputSurfaceElement): Boolean = when
element.kind == InputElementKind.TRIGGER -> target == InputControl.LT || target == InputControl.RT
else -> target.dimensions == 1 && target != InputControl.LT && target != InputControl.RT
}
/** A saved element, or, in a gamepad that keeps no kit positions, the kit control that [id] names. */
internal fun SavedInputLayout.element(id: String): InputSurfaceElement? =
elements.firstOrNull { it.id == id } ?: KitLayout.element(id)?.takeIf { OnScreenLayout.generated(this) }
@@ -3,6 +3,9 @@ package com.droiddeck.launcher.input
class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole, val element: InputSurfaceElement) {
private val router = pad.inputRouter
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()
private val deck = DeckControls.source(role, routed = true)
private val clickDeck = DeckControls.source(role, routed = true)
private val clickOwner = Any()
@@ -55,6 +58,14 @@ class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole
fingerDown = down
if (latched) 1f else 0f
} else value
if (arms.isNotEmpty() && route?.action is InputAction.Native) {
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) }
nativePad = toPad.isNotEmpty()
InputTiming.end(timing)
return
}
val target = if (router == null) element.control else route?.update(x, y, touching)
val toPad = target?.isPad == true
val toDeck = target != null && !target.isPad
@@ -112,13 +123,13 @@ class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole
closed = true
router?.removeSnapshot(snapshot)
releaseNative(); resetLatch()
route?.close(); clickRoute?.close(); gestureWheel?.close(); gestureRight?.close(); gestureMiddle?.close(); deck.close(); clickDeck.close()
route?.close(); arms.forEach { it.close() }; clickRoute?.close(); gestureWheel?.close(); gestureRight?.close(); gestureMiddle?.close(); deck.close(); clickDeck.close()
}
fun release() {
if (closed) return
releaseNative(); resetLatch()
route?.release(); clickRoute?.release(); gestureWheel?.release(); gestureRight?.release(); gestureMiddle?.release()
route?.release(); arms.forEach { it.release() }; clickRoute?.release(); gestureWheel?.release(); gestureRight?.release(); gestureMiddle?.release()
}
}
@@ -17,6 +17,18 @@ object KitLayout {
/** Whether [id] names one of a kit's own controls rather than a key or control the player added. */
fun isKit(id: String) = id.startsWith("legacy:") || id == DPAD_ID || id == KEYBOARD_ID
/** The control a kit id stands for in a layout that keeps no kit positions, so bindings saved for it stay valid. */
fun element(id: String): InputSurfaceElement? = when {
id == DPAD_ID -> InputSurfaceElement(InputControl.LS, 0f, 0f, .1f, .1f, id, InputElementKind.DPAD)
id == KEYBOARD_ID -> InputSurfaceElement(InputControl.A, 0f, 0f, .1f, .1f, id)
id.startsWith("legacy:") -> InputControl.entries.firstOrNull { "legacy:${it.id}" == id }
?.let { InputSurfaceElement(it, 0f, 0f, .1f, .1f, id, InputElementKind.from(it)) }
else -> null
}
/** Every kit control that has a click, across all virtual controllers. */
val clickIds = listOf(InputControl.LS, InputControl.RS, InputControl.LEFT_PAD, InputControl.RIGHT_PAD).map { "legacy:${it.id}" }
private const val REF_W = 1600f
private const val REF_H = 900f
/** The widest pair of side controls on any kit: a larger size never pushes the two halves, or the rows, into each other. */
@@ -241,7 +241,10 @@ class SteamTouchControls(
private fun cx(e: Element) = e.x * width
private fun cy(e: Element) = e.y * height
private fun reach(e: Element, x: Float, y: Float) = hypot((x - cx(e)) / radius(e), (y - cy(e)) / radiusY(e))
private fun reach(e: Element, x: Float, y: Float): Float {
val dx = (x - cx(e)) / radius(e); val dy = (y - cy(e)) / radiusY(e)
return if (e.type in TRACKPADS) max(abs(dx), abs(dy)) else hypot(dx, dy)
}
private fun hit(list: List<Element>, x: Float, y: Float): Element? =
list.filter { reach(it, x, y) <= 1.15f }.minByOrNull { reach(it, x, y) }
@@ -282,6 +285,8 @@ class SteamTouchControls(
val r = radius(e)
val pressed = !editing && e.type in pressedTypes()
val alpha = (255 * alphaScale * if (faded) FADED else 1f).toInt().coerceIn(30, 255)
stroke.color = Color.rgb(Color.red(tint), Color.green(tint), Color.blue(tint)); stroke.strokeWidth = lineWidth()
fill.color = Color.argb(217, 230, 233, 236)
if (alpha < 255) canvas.saveLayerAlpha(x - r * 1.5f, y - r * 1.5f, x + r * 1.5f, y + r * 1.5f, alpha)
when (e.type) {
SteamTouchConfig.DPAD -> if (visuals[SteamTouchConfig.DPAD].orEmpty().any { it.binding?.hasIcon == true }) drawDpad(canvas, e, x, y, r) else {
@@ -304,8 +309,8 @@ class SteamTouchControls(
}
SteamTouchConfig.TRACKPAD_LEFT, SteamTouchConfig.TRACKPAD_RIGHT, SteamTouchConfig.TRACKPAD_CENTER -> {
val f = fingers.values.firstOrNull { it.element?.type == e.type }.takeIf { !editing }
canvas.save(); canvas.translate(x - r * 1.3f, y - r * .85f)
art.trackpad(canvas, r * 2.6f, r * 1.7f, f != null, (f?.x ?: x) - x + r * 1.3f, (f?.y ?: y) - y + r * .85f)
canvas.save(); canvas.translate(x - r, y - r)
art.trackpad(canvas, r * 2f, r * 2f, f != null, (f?.x ?: x) - x + r, (f?.y ?: y) - y + r)
canvas.restore()
if (editing) art.text(canvas, context.getString(R.string.deck_surface_trackpad), x, y, r * .3f, Color.argb(128, 255, 255, 255))
}
@@ -354,7 +359,7 @@ class SteamTouchControls(
val icon = binding?.icon?.takeIf { it.isNotEmpty() }?.let(::iconOrLoad)
val custom = binding != null && binding.background.isNotEmpty() && !binding.background.equals(SteamTouchBindings.DEFAULT_BACKGROUND, true)
art.glass(canvas, shape, pressed && !custom, if (custom) parseColor(binding!!.background, SteamTouchBindings.DEFAULT_BACKGROUND) else 0x850E1014.toInt())
val ink = if (pressed && !custom) com.droiddeck.launcher.ui.PadArt.ON_ACCENT else com.droiddeck.launcher.ui.PadArt.INK
val ink = if (pressed && !custom) com.droiddeck.launcher.ui.PadArt.ON_ACCENT else if (tint != Color.WHITE) tint else com.droiddeck.launcher.ui.PadArt.INK
if (icon != null) {
val s = r * 0.62f
if (custom) canvas.drawBitmap(tinted(binding!!.icon, icon, binding.foreground), null, RectF(x - s, y - s, x + s, y + s), Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG))
@@ -1444,6 +1449,7 @@ class SteamTouchControls(
var onSaved: () -> Unit = {}
companion object {
private val TRACKPADS = setOf(SteamTouchConfig.TRACKPAD_LEFT, SteamTouchConfig.TRACKPAD_RIGHT, SteamTouchConfig.TRACKPAD_CENTER)
private const val TAG = "SteamTouch"
private const val POLL_MS = 150L
private const val MOVE_REDRAW_MS = 33L
@@ -647,7 +647,9 @@ object SessionPrefs {
}
fun setOscMode(context: Context, mode: String) {
prefs(context).edit().putString("osc", mode).apply()
val editor = prefs(context).edit()
if (!prefs(context).contains("steamTouchLayout")) editor.putBoolean("steamTouchLayout", steamTouchLayout(context))
editor.putString("osc", mode).apply()
}
/** Draw Steam's touch layout for the game instead of DroidDeck's own. */
@@ -169,7 +169,10 @@ private fun InputSettingsContent(onBack: () -> Unit, onChanged: () -> Unit, embe
?: b.controls[editing.control] ?: InputAction.Native()
InputActionEditor(editing.control, current, if (editing.click) stringResource(R.string.input_click_name, editing.element!!.name)
else editing.element?.name ?: editing.key?.label ?: editing.control.label,
properties = { action -> if (!editing.click) elementProperties?.let { e -> InputElementProperties(e, action) { elementProperties = it } } },
properties = { action ->
if (!editing.click && layout?.elements?.any { it.id == editing.element?.id } == true)
elementProperties?.let { e -> InputElementProperties(e, action) { elementProperties = it } }
},
onBack = { edit = null }, element = editing.element, nativeTargets = editing.key == null, click = editing.click,
detail = if (library.usersOfBindings(b.id).size > 1) stringResource(R.string.input_shared_detail, b.name, library.usersOfBindings(b.id).size)
else "${profile.name} · ${roleLabel(role)}", onSave = { action ->
@@ -233,6 +233,11 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
(remote || target == null) && draft.properties && element != null -> propertiesEditor(element)
target == null -> SettingsPage(rememberMenuHost(), savedLayout.name, ::leave) {
Text(stringResource(R.string.input_display_unavailable))
if (draft.confirmDiscard) {
Text(stringResource(R.string.input_editor_discard), style = MaterialTheme.typography.titleSmall)
SecondaryButton(stringResource(R.string.input_keep_editing)) { draft.confirmDiscard = false }
SecondaryButton(stringResource(R.string.input_discard), onClick = onDismiss)
}
PrimaryButton(stringResource(R.string.input_editor_done)) { save() }
}
remote -> SettingsPage(rememberMenuHost(), stringResource(R.string.input_editor_remote), ::leave,
@@ -362,9 +367,17 @@ private fun EditorRow(title: String, action: String, onClick: () -> Unit) {
}
}
@Composable
private fun CloseRow(draft: SurfaceDraft) {
Row(Modifier.fillMaxWidth().padding(horizontal = 10.dp, vertical = 4.dp), horizontalArrangement = Arrangement.End) {
SecondaryButton(stringResource(R.string.common_close), compact = true) { draft.popover = null }
}
}
@Composable
private fun AddRows(draft: SurfaceDraft) {
val context = LocalContext.current
CloseRow(draft)
for (control in listOf(InputControl.L3, InputControl.R3).filter { c -> draft.elements.none { it.id == "legacy:${c.id}" } })
EditorRow(control.label, stringResource(R.string.input_editor_add)) { draft.add(InputSurfaceElement(control, .47f, .45f, .06f, .1f, buttonShape = InputButtonShape.CIRCLE)) }
for (kind in InputElementKind.entries) EditorRow(stringResource(kind.label()), stringResource(R.string.input_editor_add)) {
@@ -378,6 +391,7 @@ private fun AddRows(draft: SurfaceDraft) {
@Composable
private fun HiddenRows(draft: SurfaceDraft) {
val hidden = draft.hiddenDefaults()
CloseRow(draft)
if (hidden.isEmpty()) Text(stringResource(R.string.input_nothing_hidden), Modifier.padding(12.dp), style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant)
for (e in hidden) EditorRow(e.name, stringResource(R.string.input_restore)) { draft.add(e) }
@@ -155,6 +155,7 @@ class InputSurfaceView(
/** Shows [layout]; a layout that has no elements of its own gets the virtual controller's defaults at this view's size. */
fun showLayout(layout: SavedInputLayout?) {
generated = layout?.takeIf(OnScreenLayout::generated)
kit = PadKit.current(context)
updateStyle(layout?.style ?: InputSurfaceStyle())
updateCustomLayout(generated?.let { OnScreenLayout.elements(context, it, width, height, android.graphics.Rect(), null) } ?: layout?.elements.orEmpty())
}
@@ -313,7 +314,8 @@ class InputSurfaceView(
val lit = if (element.toggle) source?.latched == true else down
val w = width.toFloat(); val h = height.toFloat()
val c = element.control
val native = element.id == "legacy:${c.id}" && element.showLabel && element.icon == 0 && bitmap == null
val native = element.id == "legacy:${c.id}" && element.showLabel && element.icon == 0 && bitmap == null &&
(element.name == c.label || element.name == kit.name(c)) && (c !in FACE || element.buttonShape == InputButtonShape.CIRCLE)
when {
element.id == KitLayout.KEYBOARD_ID && element.icon == InputButtonIcons.KEYBOARD -> art.system(canvas, w, h, PadArt.Shape.ROUND, PadArt.Icon.KEYBOARD, lit)
native && c in FACE -> art.face(canvas, w / 2f, h / 2f, min(w, h) / 2f - art.dp(2f), c, kit, lit)
@@ -476,7 +478,7 @@ class InputSurfaceView(
private var knobY = 0f
val adaptive get() = adaptive(element)
private fun radius() = min(width, height) / 2f - 3 * density
private fun radius() = (min(width, height) / 2f - 3 * density).coerceAtLeast(1f)
fun reach() = radius() * STICK_REACH
@@ -61,11 +61,13 @@ class InputLayoutToolsTest {
val (defaults, aspect) = InputLibraries.defaultPositions(context, library, gamepad)!!
assertTrue(gamepad.elements.isEmpty())
val (display, metrics) = InputLibraries.primaryDisplay(context)
val w = maxOf(metrics.widthPixels, metrics.heightPixels); val h = minOf(metrics.widthPixels, metrics.heightPixels)
val w = metrics.widthPixels; val h = metrics.heightPixels
assertEquals(KitLayout.elements(display, PadKit.current(context), w, h), defaults)
assertEquals(w.toFloat() / h, aspect, .0001f)
val copy = SavedInputLayout("mine", "Mine", origin = InputLibraries.OVERLAY)
assertEquals(defaults, InputLibraries.defaultPositions(context, library, copy)!!.first)
assertEquals(KitLayout.elements(display, PadKit.current(context), w, h, scale = 1.2f),
InputLibraries.defaultPositions(context, library, gamepad.copy(scale = 1.2f))!!.first)
assertNull(InputLibraries.defaultPositions(context, library, SavedInputLayout("new", "New")))
}
@@ -171,4 +171,20 @@ class InputSurfaceSourceTest {
surface.click(true); surface.click(false)
assertEquals(listOf("button:272:true", "button:272:false"), events)
}
@Test fun theKitDpadPressesOnlyTheHeldDirectionAndKeepsEachDirectionsBinding() {
val dpad = InputSurfaceElement(InputControl.LS, .1f, .1f, .3f, .3f, KitLayout.DPAD_ID, InputElementKind.DPAD)
bridge.setInputProfile(InputProfile("test", bindings = mapOf(InputRole.SECONDARY to mapOf(InputControl.LEFT to InputAction.Keys(listOf(30))))))
val pad = source(dpad)
pad.update(1f, 0f)
assertTrue(state().right)
assertFalse(state().up || state().down || state().left)
pad.update(0f, 0f)
assertFalse(state().right)
pad.update(-1f, 0f)
assertEquals("key:30:true", events.last())
assertFalse(state().left)
pad.update(0f, 0f)
assertEquals("key:30:false", events.last())
}
}
@@ -126,4 +126,27 @@ class OnScreenControlPrefsTest {
assertEquals(listOf(key.id), upgraded.layouts.single { it.id == InputLibraries.OVERLAY }.elements.map { it.id }.filter { it.startsWith("imported:") })
assertEquals(InputAction.Keys(listOf(29, 33)), upgraded.compileActive(false).elementAction(InputRole.PRIMARY, key.id, InputControl.A))
}
@Test fun bindingsSavedForKitControlsSurviveTheKitUpgrade() {
storeAt(14) { lib ->
val overlay = lib.layouts.single { it.id == InputLibraries.OVERLAY }
val bindings = lib.preset(lib.activePart(false, InputRole.PRIMARY).bindings)
lib.withLayout(overlay.copy(elements = KitLayout.elements(context, PadKit.STEAM_DECK, 1920, 1080), aspect = 16f / 9))
.withPreset(bindings.copy(elements = bindings.elements + ("legacy:a" to InputAction.Keys(listOf(57))),
clicks = bindings.clicks + ("legacy:ls" to InputAction.Disabled)))
}
val upgraded = InputLibraries.read(context)
assertTrue(OnScreenLayout.generated(upgraded.layouts.single { it.id == InputLibraries.OVERLAY }))
val profile = upgraded.compileActive(false)
assertEquals(InputAction.Keys(listOf(57)), profile.elementAction(InputRole.PRIMARY, "legacy:a", InputControl.A))
assertEquals(InputAction.Disabled, profile.clicks[InputRole.PRIMARY]?.get("legacy:ls"))
}
@Test fun changingShowKeepsTheSteamLayoutAnOlderBuildStoredThere() {
val prefs = context.getSharedPreferences("session", Context.MODE_PRIVATE)
prefs.edit().clear().putString("osc", SessionPrefs.OSC_STEAM_TOUCH).commit()
SessionPrefs.setOscMode(context, SessionPrefs.OSC_ALWAYS)
assertEquals(SessionPrefs.OSC_ALWAYS, SessionPrefs.oscMode(context))
assertTrue(SessionPrefs.steamTouchLayout(context))
}
}