From 6ef042487a06f5febb70984f494574e53a467003 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Tue, 20 Oct 2026 00:34:01 -0400 Subject: [PATCH] fix: find and save Steam touch layouts of non-Steam shortcuts (#1171) The touch layout editor opened "App - Layout" for a non-Steam shortcut (an added game, the Epic Games Launcher) with LB, LT, RT, RB, both sticks and LS/RS greyed out, so they could not be placed. Steam keys a shortcut's controller config by the shortcut's lowercase name and logs its app id unsigned, but the editor looked both up by the signed app id, found neither, and fell back to a blank layout with nothing bound. SteamTouchConfig now reads the shortcut's name from shortcuts.vdf, loads and saves its layout under that name the way Steam does (autosave folder and configset entry), and matches Steam's log line by the unsigned id. The editor is titled with the game's name, and the app id in the panel is shown unsigned. A name that is not a plain folder name (a path separator, "." or "..", a quote, a backslash or a control character) keeps the numeric app id. Existing configset entries are matched without regard to case, as Steam reads them. --- .../launcher/frontend/SteamAppInfo.kt | 2 +- .../launcher/input/SteamTouchConfig.kt | 70 +++++++++++++------ .../launcher/input/SteamTouchControls.kt | 2 +- .../input/SteamTouchConfigFilesTest.kt | 56 +++++++++++++++ 4 files changed, 106 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/SteamAppInfo.kt b/app/src/main/java/com/droiddeck/launcher/frontend/SteamAppInfo.kt index d43c4bafb0..ba1a424200 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/SteamAppInfo.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/SteamAppInfo.kt @@ -153,7 +153,7 @@ object SteamAppInfo { return String(bytes, Charsets.UTF_8) } - private fun readMap(b: ByteBuffer, strings: List?): Map { + internal fun readMap(b: ByteBuffer, strings: List?): Map { val out = LinkedHashMap() while (b.hasRemaining()) { val type = b.get().toInt() and 0xFF diff --git a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt index 12692bfa9d..f1479588c0 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchConfig.kt @@ -2,10 +2,14 @@ package com.droiddeck.launcher.input import android.content.Context import android.util.Log +import com.droiddeck.launcher.frontend.SteamAppInfo import com.droiddeck.launcher.runtime.LinuxRuntime import java.io.ByteArrayOutputStream import java.io.File import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.util.Locale object SteamTouchConfig { private const val TAG = "SteamTouch" @@ -125,17 +129,37 @@ object SteamTouchConfig { private val DEFAULT_SET = setOf(DPAD, A, B, X, Y, SELECT, START) private val ALWAYS = setOf(STEAM, THUMB, KEYBOARD) + fun steamId(appId: Int): String = Integer.toUnsignedString(appId) + fun layoutIdOf(actionSet: Int) = if (actionSet <= 0) 1 else actionSet private fun presetOf(actionSet: Int) = layoutIdOf(actionSet) - 1 - fun gameName(context: Context, appId: Int): String? = try { + fun gameName(context: Context, appId: Int): String? = if (appId < 0) shortcutName(context, appId) else try { val kv = KeyValues.parse(File(steamRoot(context), "steamapps/appmanifest_$appId.acf").readText()) (kv.child("AppState") ?: kv).string("name") } catch (_: Exception) { null } + fun steamKey(context: Context, appId: Int): String = + if (appId < 0) shortcutName(context, appId)?.lowercase(Locale.ROOT) + ?.takeIf { name -> name != "." && name != ".." && name.none { it == '"' || it == '\\' || it == '/' || it < ' ' } } ?: steamId(appId) + else steamId(appId) + + private fun shortcutName(context: Context, appId: Int): String? { + val account = accountId(context) ?: return null + return try { + val bytes = File(steamRoot(context), "userdata/$account/config/shortcuts.vdf").readBytes() + val root = SteamAppInfo.readMap(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN), null) + (root["shortcuts"] as? Map<*, *>)?.values?.filterIsInstance>() + ?.firstOrNull { (it["appid"] as? String)?.toIntOrNull() == appId } + ?.let { (it["AppName"] ?: it["appname"]) as? String }?.takeIf { it.isNotEmpty() } + } catch (_: Exception) { + null + } + } + fun steamRoot(context: Context) = com.droiddeck.launcher.session.SessionPrefs.activeSteamRoot(context) fun configDir(context: Context): File? { @@ -158,10 +182,11 @@ object SteamTouchConfig { fun load(context: Context, appId: Int): Config { val dir = configDir(context) - val file = dir?.let { deviceConfig(context, it, appId) } - ?: dir?.let { autosaveConfig(it, appId) } + val key = steamKey(context, appId) + val file = dir?.let { deviceConfig(context, it, key) } + ?: dir?.let { autosaveConfig(it, key) } ?: loggedConfig(context, appId)?.takeIf { isTouchConfig(it) } - ?: dir?.let { configsetConfig(context, it, appId) } + ?: dir?.let { configsetConfig(context, it, key) } if (file == null) { Log.i(TAG, "steam touch: app $appId has no touch config on disk; default layout") return Config(null, null, null, emptyMap()) @@ -193,7 +218,7 @@ object SteamTouchConfig { } val touchIndex = Regex("""!! Controller (\d+) attributes:\s*\n\s*Type: 43""").findAll(tail).lastOrNull()?.groupValues?.get(1) if (touchIndex == null) null else { - val loaded = Regex("""Loaded Config for [^\n]*? Path for App ID $appId, Controller $touchIndex: ([^\n]+)""") + val loaded = Regex("""Loaded Config for [^\n]*? Path for App ID ${steamId(appId)}, Controller $touchIndex: ([^\n]+)""") .findAll(tail).lastOrNull()?.groupValues?.get(1)?.trim() loaded?.let { guestPath -> File(LinuxRuntime.rootDir(context), guestPath.trimStart('/').replace("//", "/")) } } @@ -201,22 +226,22 @@ object SteamTouchConfig { null } - private fun autosaveConfig(dir: File, appId: Int): File? = try { - val set = KeyValues.parse(File(dir, CONFIGSET).readText()).child("controller_config")?.child(appId.toString()) - if (set?.string("autosave") == "1") File(dir, "$appId/controller_mobile_touch.vdf").takeIf { it.isFile } else null + private fun autosaveConfig(dir: File, key: String): File? = try { + val set = KeyValues.parse(File(dir, CONFIGSET).readText()).child("controller_config")?.child(key) + if (set?.string("autosave") == "1") File(dir, "$key/controller_mobile_touch.vdf").takeIf { it.isFile } else null } catch (_: Exception) { null } - private fun configsetConfig(context: Context, dir: File, appId: Int): File? = entryConfig(context, dir, CONFIGSET, appId) + private fun configsetConfig(context: Context, dir: File, key: String): File? = entryConfig(context, dir, CONFIGSET, key) - private fun deviceConfig(context: Context, dir: File, appId: Int): File? = entryConfig(context, dir, DEVICE_CONFIGSET, appId) + private fun deviceConfig(context: Context, dir: File, key: String): File? = entryConfig(context, dir, DEVICE_CONFIGSET, key) - private fun entryConfig(context: Context, dir: File, configset: String, appId: Int): File? = try { - val set = KeyValues.parse(File(dir, configset).readText()).child("controller_config")?.child(appId.toString()) + private fun entryConfig(context: Context, dir: File, configset: String, key: String): File? = try { + val set = KeyValues.parse(File(dir, configset).readText()).child("controller_config")?.child(key) when { set == null -> null - set.string("autosave") == "1" -> File(dir, "$appId/controller_mobile_touch.vdf") + set.string("autosave") == "1" -> File(dir, "$key/controller_mobile_touch.vdf") set.string("workshop") != null -> workshopConfig(context, set.string("workshop")!!) set.string("template") != null -> File(steamRoot(context), "controller_base/templates/" + set.string("template")) else -> null @@ -364,7 +389,8 @@ object SteamTouchConfig { val source = bindings.entries.fold(config.text ?: return null) { text, (ref, change) -> SteamTouchBindings.rewrite(text, ref, change) ?: text } - val target = File(dir, "$appId/controller_mobile_touch.vdf") + val key = steamKey(context, appId) + val target = File(dir, "$key/controller_mobile_touch.vdf") val old = config.layouts val keep = old?.layouts?.filter { it.actionSet !in sets.keys }.orEmpty() val written = sets.map { (id, elements) -> @@ -382,27 +408,27 @@ object SteamTouchConfig { val tmp = File(target.path + ".tmp") tmp.writeText(text) if (!tmp.renameTo(target)) return null - selectAutosave(dir, appId) - dropDeviceEntry(dir, appId) + selectAutosave(dir, key) + dropDeviceEntry(dir, key) Log.i(TAG, "steam touch: saved layouts ${sets.keys} for app $appId to ${target.path}") return target } - private fun dropDeviceEntry(dir: File, appId: Int) { + private fun dropDeviceEntry(dir: File, key: String) { val file = File(dir, DEVICE_CONFIGSET) if (!file.isFile) return val text = file.readText() - val block = Regex("""\n\s*"$appId"\s*\{[^}]*\}""") + val block = Regex("""\n\s*"${Regex.escape(key)}"\s*\{[^}]*\}""", RegexOption.IGNORE_CASE) if (!block.containsMatchIn(text)) return File(file.path + ".tmp").apply { writeText(text.replace(block, "")) }.renameTo(file) } - private fun selectAutosave(dir: File, appId: Int) { + private fun selectAutosave(dir: File, key: String) { val file = File(dir, CONFIGSET) val text = if (file.isFile) file.readText() else "\"controller_config\"\n{\n}\n" - val entry = "\t\"$appId\"\n\t{\n\t\t\"autosave\"\t\t\"1\"\n\t}" - val block = Regex("""\n\s*"$appId"\s*\{[^}]*\}""") - val next = if (block.containsMatchIn(text)) text.replace(block, "\n$entry") + val entry = "\t\"$key\"\n\t{\n\t\t\"autosave\"\t\t\"1\"\n\t}" + val block = Regex("""\n\s*"${Regex.escape(key)}"\s*\{[^}]*\}""", RegexOption.IGNORE_CASE) + val next = if (block.containsMatchIn(text)) text.replace(block) { "\n$entry" } else text.substring(0, text.lastIndexOf('}')).trimEnd() + "\n$entry\n}\n" File(file.path + ".tmp").apply { writeText(next) }.renameTo(file) } diff --git a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchControls.kt b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchControls.kt index dee5107870..1046e3b343 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/SteamTouchControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/SteamTouchControls.kt @@ -965,7 +965,7 @@ class SteamTouchControls( BIG_PICTURE -> "Steam" else -> gameNames[appId] ?: run { val app = appId - gameNames[app] = context.getString(R.string.touch_app_id, app) + gameNames[app] = context.getString(R.string.touch_app_id, Integer.toUnsignedLong(app)) loader.execute { val name = SteamTouchConfig.gameName(context, app) ?: return@execute handler.post { gameNames[app] = name; invalidate() } diff --git a/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt index eb48d09642..96ff9a1c4e 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/SteamTouchConfigFilesTest.kt @@ -57,4 +57,60 @@ class SteamTouchConfigFilesTest { assertTrue(device.contains("\"8400\"")) assertEquals(saved, SteamTouchConfig.load(context, 204360).file) } + + private val template get() = File(steam, "controller_base/templates/controller_mobile_touch_gamepad_joystick.vdf") + + private fun shortcut(name: String): Int { + val shortcut = 2702387189L.toInt() + template.apply { parentFile!!.mkdirs(); writeText(touch("Gamepad with Joystick")) } + File(steam, "logs/console_log.txt").apply { parentFile!!.mkdirs() }.writeText( + "!! Controller 0 attributes:\n Type: 43\n" + + "Loaded Config for Last Resort Path for App ID 2702387189, Controller 0: /root/.local/share/Steam//controller_base/templates/controller_mobile_touch_gamepad_joystick.vdf\n", + ) + File(steam, "config/loginusers.vdf").apply { parentFile!!.mkdirs() } + .writeText("\"users\"\n{\n\t\"76561197960265851\"\n\t{\n\t\t\"MostRecent\"\t\t\"1\"\n\t}\n}\n") + val vdf = java.io.ByteArrayOutputStream().apply { + write("\u0000shortcuts\u0000\u00000\u0000\u0002appid\u0000".toByteArray()) + write(java.nio.ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN).putInt(shortcut).array()) + write("\u0001AppName\u0000$name\u0000\u0008\u0008\u0008".toByteArray()) + } + File(steam, "userdata/123/config/shortcuts.vdf").apply { parentFile!!.mkdirs() }.writeBytes(vdf.toByteArray()) + return shortcut + } + + @Test fun aShortcutFindsSteamsConfigAndSavesUnderItsName() { + val shortcut = shortcut("Epic Games Launcher") + val config = SteamTouchConfig.load(context, shortcut) + assertEquals(template, config.file) + assertEquals("Epic Games Launcher", SteamTouchConfig.gameName(context, shortcut)) + val saved = SteamTouchConfig.saveLayouts(context, shortcut, config, mapOf(1 to emptyList())) + assertEquals(File(dir, "epic games launcher/controller_mobile_touch.vdf"), saved) + assertTrue(File(dir, "configset_controller_mobile_touch.vdf").readText().contains("\"epic games launcher\"")) + assertEquals(saved, SteamTouchConfig.load(context, shortcut).file) + } + + @Test fun aShortcutNameThatIsNoPlainFolderNameUsesTheAppId() { + for (name in listOf("../escape", "a/b", "..", "Tab\tName")) { + assertEquals(name, "2702387189", SteamTouchConfig.steamKey(context, shortcut(name))) + } + } + + @Test fun aShortcutNameWithADollarSavesAgain() { + val shortcut = shortcut("Game $1") + val config = SteamTouchConfig.load(context, shortcut) + SteamTouchConfig.saveLayouts(context, shortcut, config, mapOf(1 to emptyList())) + val saved = SteamTouchConfig.saveLayouts(context, shortcut, SteamTouchConfig.load(context, shortcut), mapOf(1 to emptyList())) + assertEquals(File(dir, "game $1/controller_mobile_touch.vdf"), saved) + assertEquals(1, Regex("\"game \\$1\"").findAll(File(dir, "configset_controller_mobile_touch.vdf").readText()).count()) + } + + @Test fun aMixedCaseEntryOfTheShortcutIsReplacedNotDuplicated() { + val shortcut = shortcut("Epic Games Launcher") + configset("configset_controller_mobile_touch.vdf", "Epic Games Launcher" to "\"template\"\t\t\"controller_mobile_touch_gamepad_joystick.vdf\"") + configset("configset_MT-DROIDDECK0001.vdf", "Epic Games Launcher" to "\"workshop\"\t\t\"1\"") + SteamTouchConfig.saveLayouts(context, shortcut, SteamTouchConfig.load(context, shortcut), mapOf(1 to emptyList())) + val entries = Regex("\"epic games launcher\"", RegexOption.IGNORE_CASE) + assertEquals(1, entries.findAll(File(dir, "configset_controller_mobile_touch.vdf").readText()).count()) + assertEquals(0, entries.findAll(File(dir, "configset_MT-DROIDDECK0001.vdf").readText()).count()) + } }