From b99cbb6bab361b38c62042fb4e5024a8c56cd39c Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sat, 5 Dec 2026 01:42:41 -0500 Subject: [PATCH] Read and write Winlator layouts as landscape whichever way the device is held --- .../com/droiddeck/launcher/input/InputLayoutFile.kt | 9 +++++++-- .../com/droiddeck/launcher/input/WinlatorLayouts.kt | 3 ++- .../droiddeck/launcher/input/WinlatorLayoutsTest.kt | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt index 828151654c..eb156345c2 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLayoutFile.kt @@ -32,9 +32,13 @@ internal object InputLayoutFile { /** Whether [parse] keeps a binding with this name. */ internal fun knows(binding: String) = binding in CODES + /** Winlator lays out every .icp for a landscape screen, whichever way the device is held when it is read or written. */ + internal fun landscape(width: Int, height: Int) = if (height > width) height to width else width to height + fun fileName(layout: SavedInputLayout) = layout.name.replace(Regex("[^A-Za-z0-9 _.-]"), "_").trim().ifEmpty { "droiddeck-layout" } + ".icp" - fun export(layout: SavedInputLayout, bindings: SavedInputBindings?, width: Int, height: Int): String? { + fun export(layout: SavedInputLayout, bindings: SavedInputBindings?, screenWidth: Int, screenHeight: Int): String? { + val (width, height) = landscape(screenWidth, screenHeight) val snap = width.coerceAtLeast(1) / 100f val elements = JSONArray() for (e in layout.elements) { @@ -116,7 +120,8 @@ internal object InputLayoutFile { } fun import(context: Context, raw: String, fallbackName: String, screen: InputLayoutScreen, style: InputSurfaceStyle, - width: Int, height: Int, density: Float, id: String = newInputId()): Pair? { + screenWidth: Int, screenHeight: Int, density: Float, id: String = newInputId()): Pair? { + val (width, height) = landscape(screenWidth, screenHeight) val keys = parse(raw, width, density)?.takeIf { it.isNotEmpty() } ?: return null val converted = runCatching { LegacyScreenKeyMigration.convert(context, keys, width, height, density) }.getOrNull() ?: return null val name = runCatching { JSONObject(raw).optString("name") }.getOrNull().orEmpty().trim().take(80).ifEmpty { fallbackName.trim().take(80) } diff --git a/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt b/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt index 73b07557ae..0121784034 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt @@ -101,9 +101,10 @@ object WinlatorLayouts { }.lowercase().replaceFirstChar { it.uppercase() }.take(80) /** Converts an `.icp` file for a [width]×[height] screen into a layout and the actions of its controls. */ - fun import(raw: String, fallbackName: String, screen: InputLayoutScreen, width: Int, height: Int, + fun import(raw: String, fallbackName: String, screen: InputLayoutScreen, screenWidth: Int, screenHeight: Int, id: String = newInputId(), bindingsId: String = newInputId()): Pair? = runCatching { require(raw.length <= 1_000_000) + val (width, height) = InputLayoutFile.landscape(screenWidth, screenHeight) val doc = JSONObject(raw) val source = doc.getJSONArray("elements") val aspect = (width.coerceAtLeast(1).toFloat() / height.coerceAtLeast(1)).coerceIn(.05f, 20f) diff --git a/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt index 70cffc7ee5..7e6aee792d 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt @@ -32,6 +32,17 @@ class WinlatorLayoutsTest { assertFalse(WinlatorLayouts.isIcp("not json")) } + @Test fun aLayoutImportedInPortraitMatchesTheLandscapeImport() { + val landscape = WinlatorLayouts.import(profile, "x", InputLayoutScreen.MAIN, 2400, 1080, id = "l", bindingsId = "b")!! + val portrait = WinlatorLayouts.import(profile, "x", InputLayoutScreen.MAIN, 1080, 2400, id = "l", bindingsId = "b")!! + assertEquals(2400f / 1080f, portrait.first.aspect, .001f) + assertEquals(landscape.first.elements.map { it.copy(id = "") }, portrait.first.elements.map { it.copy(id = "") }) + val exported = InputLayoutFile.export(landscape.first, landscape.second, 1000, 600)!! + assertEquals(exported, InputLayoutFile.export(landscape.first, landscape.second, 600, 1000)) + val back = InputLayoutFile.import(context, exported, "x", InputLayoutScreen.MAIN, InputSurfaceStyle(), 600, 1000, 2f)!! + assertTrue(back.first.aspect > 1f) + } + @Test fun convertsEachKindOfControl() { val (layout, bindings) = WinlatorLayouts.import(profile, "fallback", InputLayoutScreen.MAIN, 2400, 1080)!! assertEquals("Shooter", layout.name)