fix(input): count every Winlator entry and refuse key-only files over 64 controls

This commit is contained in:
Kurt Himebauch committed 2026-12-04 15:24:31 -05:00
1 parent 26dcce168a
commit 56ac92de85
2 files changed
+23 -5

No files matched your search

@@ -27,10 +27,12 @@ object WinlatorLayouts {
*/
fun needsConversion(raw: String): Boolean = runCatching {
val elements = JSONObject(raw).getJSONArray("elements")
(0 until elements.length()).any { i ->
val e = elements.optJSONObject(i) ?: return@any false
val bindings = e.optJSONArray("bindings")?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty()
e.optString("type") in setOf("TRACKPAD", "RANGE_BUTTON") || bindings.any { it.isNotBlank() && it != "NONE" && !InputLayoutFile.knows(it) }
val bound = (0 until elements.length()).mapNotNull { i ->
elements.optJSONObject(i)?.let { e -> e.optString("type") to e.optJSONArray("bindings")?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty() }
}.filter { (type, bindings) -> type in setOf("TRACKPAD", "RANGE_BUTTON") || bindings.any { it.isNotBlank() && it != "NONE" } }
// Over 64 controls: the converter refuses the file, where the other importer would quietly keep the first 64.
bound.size > 64 || bound.any { (type, bindings) ->
type in setOf("TRACKPAD", "RANGE_BUTTON") || bindings.any { it.isNotBlank() && it != "NONE" && !InputLayoutFile.knows(it) }
}
}.getOrDefault(false)
@@ -121,7 +123,7 @@ object WinlatorLayouts {
val w = halfW * .01f; val h = halfH * .01f * aspect
placeRect(kind, name, cx - w, cy - h, 2 * w, 2 * h, action, shape, toggle)
}
for (i in 0 until minOf(source.length(), 128)) {
for (i in 0 until source.length()) {
val e = source.optJSONObject(i) ?: continue
val bindings = e.optJSONArray("bindings")?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty()
val scale = e.optDouble("scale", 1.0).toFloat().coerceIn(.2f, 5f)
@@ -133,6 +133,22 @@ class WinlatorLayoutsTest {
assertNull(WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 1920, 1080))
}
@Test fun aKeyOnlyFileOver64ControlsGoesToTheConverterWhichRefusesIt() {
val buttons = (0 until 65).joinToString(",") { """{"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_A"],"scale":0.5,"x":0.5,"y":0.5}""" }
val raw = """{"cursorSpeed":1,"elements":[$buttons]}"""
assertTrue(WinlatorLayouts.needsConversion(raw))
assertNull(WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 1920, 1080))
}
@Test fun controlsAfterManyEmptyEntriesStillCount() {
val empty = (0 until 127).joinToString(",") { """{"type":"BUTTON","bindings":["NONE"],"x":0.5,"y":0.5}""" }
val bound = (0 until 64).joinToString(",") { """{"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_B"],"scale":0.5,"x":0.5,"y":0.5}""" }
val raw = """{"cursorSpeed":1,"elements":[{"type":"BUTTON","bindings":["KEY_A"],"x":0.5,"y":0.5},$empty,$bound]}"""
assertNull(WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 1920, 1080))
val fits = """{"cursorSpeed":1,"elements":[$empty,{"type":"BUTTON","bindings":["KEY_A"],"x":0.5,"y":0.5}]}"""
assertEquals(1, WinlatorLayouts.import(fits, "x", InputLayoutScreen.MAIN, 1920, 1080)!!.first.elements.size)
}
@Test fun aKeyboardTrackpadKeepsItsKeys() {
val raw = """{"cursorSpeed":1,"elements":[{"type":"TRACKPAD","shape":"CIRCLE","bindings":["KEY_W","KEY_D","KEY_S","KEY_A"],"scale":1,"x":0.5,"y":0.5}]}"""
val (layout, bindings) = WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 1920, 1080)!!