fix(input): keep layout frame edits, bound community downloads and keep DroidDeck exports on their importer

This commit is contained in:
Kurt Himebauch committed 2026-12-04 14:28:58 -05:00
1 parent d6d47d379f
commit f94d5179c3
6 files changed
+45 -3

No files matched your search

@@ -201,7 +201,9 @@ data class InputLibrary(
val before = baseLayout?.elements?.firstOrNull { it.id == e.id }
before == null || before != e || mine.action(e) != theirs.action(e) || (e.hasClick && mine.clickAction(e) != theirs.clickAction(e))
} + if (layout == null) 0 else baseLayout?.elements.orEmpty().count { e -> layout.elements.none { it.id == e.id } }
val styled = if (layout != null && layout.id != baseLayout?.id && layout.style != baseLayout?.style) 1 else 0
val styled = if (layout != null && layout.id != baseLayout?.id && (baseLayout == null || layout.style != baseLayout.style ||
layout.scale != baseLayout.scale || layout.aspect != baseLayout.aspect || layout.rotated != baseLayout.rotated ||
layout.rotatedAspect != baseLayout.rotatedAspect || layout.rotatedScale != baseLayout.rotatedScale)) 1 else 0
controls + keys + styled + if (layout?.id == baseLayout?.id) 0 else elements
}
}
@@ -19,6 +19,19 @@ object WinlatorLayouts {
fun isIcp(raw: String) = runCatching { JSONObject(raw).let { it.has("elements") && it.optJSONArray("elements") != null && it.has("cursorSpeed") } }.getOrDefault(false)
/**
* Whether a file uses something only this converter keeps: mouse or gamepad bindings, trackpads or key ranges.
* DroidDeck's own exports send only keys, so they keep importing through [InputLayoutFile] with the sizes they were saved with.
*/
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" && !it.startsWith("KEY_") }
}
}.getOrDefault(false)
private val keys: Map<String, Int> = mapOf(
"ESC" to 1, "1" to 2, "2" to 3, "3" to 4, "4" to 5, "5" to 6, "6" to 7, "7" to 8, "8" to 9, "9" to 10, "0" to 11,
"MINUS" to 12, "EQUAL" to 13, "BKSP" to 14, "TAB" to 15, "Q" to 16, "W" to 17, "E" to 18, "R" to 19, "T" to 20, "Y" to 21,
@@ -23,7 +23,16 @@ private fun fetchText(url: String): String? = runCatching {
connectTimeout = 10_000; readTimeout = 15_000
try {
if (responseCode != 200) return@run null
inputStream.use { stream -> stream.readBytes().takeIf { it.size <= 1_000_000 }?.toString(Charsets.UTF_8) }
inputStream.use { stream ->
val bytes = java.io.ByteArrayOutputStream()
val buffer = ByteArray(8192)
while (bytes.size() <= 1_000_000) {
val read = stream.read(buffer)
if (read < 0) break
bytes.write(buffer, 0, read)
}
bytes.takeIf { it.size() <= 1_000_000 }?.toString(Charsets.UTF_8.name())
}
} finally { disconnect() }
}
}.getOrNull()
@@ -127,7 +127,7 @@ private fun InputSettingsContent(onBack: () -> Unit, onChanged: () -> Unit, embe
val screen = if (target == InputRole.PRIMARY) InputLayoutScreen.MAIN else InputLayoutScreen.SECONDARY
val name = layoutFileName(context, uri) ?: context.getString(R.string.ctrl_layout)
val imported = raw?.let {
if (WinlatorLayouts.isIcp(it)) WinlatorLayouts.import(it, name, screen, metrics.widthPixels, metrics.heightPixels)
if (WinlatorLayouts.isIcp(it) && WinlatorLayouts.needsConversion(it)) WinlatorLayouts.import(it, name, screen, metrics.widthPixels, metrics.heightPixels)
else InputLayoutFile.import(context, it, name, screen, layout?.style ?: InputSurfaceStyle(), metrics.widthPixels, metrics.heightPixels, metrics.density)
}
if (imported == null) error = context.getString(R.string.input_layout_import_failed)
@@ -107,6 +107,15 @@ class InputLibraryGamesTest {
assertEquals(1, hidden.gameChanges(false, "620", InputRole.PRIMARY))
}
@Test fun scaleAndOtherOrientationEditsInAGamesLayoutCount() {
val forked = library().withGamePart(false, "620", "Portal 2", InputRole.PRIMARY)
val layout = forked.activeLayout(false, InputRole.PRIMARY, "620")!!
if (!layout.editable) return
assertEquals(1, forked.withLayout(layout.copy(scale = layout.scale * 1.1f)).gameChanges(false, "620", InputRole.PRIMARY))
val rotated = layout.copy(rotated = layout.elements.take(1), rotatedAspect = .5f)
assertEquals(1, forked.withLayout(rotated).gameChanges(false, "620", InputRole.PRIMARY))
}
@Test fun aGamesCopiesCantBeDeletedFromUnderIt() {
val forked = library().withGamePart(false, "620", "Portal 2", InputRole.PHYSICAL)
val id = forked.activePart(false, InputRole.PHYSICAL, "620").bindings
@@ -86,6 +86,15 @@ class WinlatorLayoutsTest {
assertEquals("Ctrl l+C", layout.elements[0].name)
}
@Test fun keyOnlyFilesLikeDroidDecksOwnExportsKeepTheirImporter() {
assertTrue(WinlatorLayouts.needsConversion(profile))
val keysOnly = """{"id":0,"name":"Mine","cursorSpeed":1.5,"elements":[
{"type":"BUTTON","shape":"CIRCLE","customColor":0,"bindings":["KEY_SPACE"],"scale":1,"x":0.5,"y":0.5,"swipeable":false},
{"type":"D_PAD","shape":"CIRCLE","bindings":["KEY_W","KEY_D","KEY_S","KEY_A"],"scale":1,"x":0.2,"y":0.5}]}"""
assertFalse(WinlatorLayouts.needsConversion(keysOnly))
assertTrue(WinlatorLayouts.needsConversion("""{"cursorSpeed":1,"elements":[{"type":"TRACKPAD","bindings":["MOUSE_MOVE_UP"]}]}"""))
}
@Test fun rejectsAFileWithNothingToPlace() {
assertNull(WinlatorLayouts.import("""{"cursorSpeed":1,"elements":[]}""", "x", InputLayoutScreen.MAIN, 100, 100))
}