mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 21:35:59 +00:00
fix: Steam touch layout decoding, saved layouts and new sessions
This commit is contained in:
1 parent
ac8455925e
commit
261dee73c4
4 files changed
+93
-35
No files matched your search
@@ -1820,8 +1820,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
|
||||
/** Steam's touch controls, once the session has made the device (SessionService). */
|
||||
private fun steamTouch(): com.droiddeck.launcher.input.SteamTouchControls? {
|
||||
steamTouchControls?.let { return it }
|
||||
val device = com.droiddeck.launcher.input.SteamTouchDevice.current ?: return null
|
||||
steamTouchControls?.let { view ->
|
||||
if (view.device === device) return view
|
||||
// A new session made a new device: the view of the old one goes.
|
||||
view.releaseAll()
|
||||
(view.parent as? android.view.ViewGroup)?.removeView(view)
|
||||
steamTouchControls = null
|
||||
}
|
||||
val root = onScreenControls?.parent as? android.view.ViewGroup ?: return null
|
||||
val view = com.droiddeck.launcher.input.SteamTouchControls(this, device,
|
||||
onMenu = { drawerOpen = true }, onKeyboard = ::togglePcKeyboard)
|
||||
|
||||
@@ -111,7 +111,10 @@ object SteamTouchConfig {
|
||||
/** The config the client has for app [appId] on the touch controller. Blocking: reads files. */
|
||||
fun load(context: Context, appId: Int): Config {
|
||||
val dir = configDir(context)
|
||||
val file = loggedConfig(context, appId)?.takeIf { isTouchConfig(it) }
|
||||
// The app's own saved config is what the client loads when there is one (its configset
|
||||
// entry); otherwise what it logged loading - a template, an official or a workshop config.
|
||||
val file = dir?.let { autosaveConfig(it, appId) }
|
||||
?: loggedConfig(context, appId)?.takeIf { isTouchConfig(it) }
|
||||
?: dir?.let { configsetConfig(context, it, appId) }
|
||||
if (file == null) {
|
||||
Log.i(TAG, "steam touch: app $appId has no touch config on disk; default layout")
|
||||
@@ -155,6 +158,13 @@ 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
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
/** The app's entry in configset_controller_mobile_touch.vdf. */
|
||||
private fun configsetConfig(context: Context, dir: File, appId: Int): File? = try {
|
||||
val set = KeyValues.parse(File(dir, CONFIGSET).readText()).child("controller_config")?.child(appId.toString())
|
||||
@@ -324,8 +334,12 @@ object SteamTouchConfig {
|
||||
fun decodeLayouts(bytes: ByteArray): Layouts {
|
||||
val layouts = mutableListOf<Layout>()
|
||||
val rest = ByteArrayOutputStream()
|
||||
Proto(bytes).forEachField { field, wire, start, end, reader ->
|
||||
if (field == 1 && wire == 2) layouts += decodeLayout(reader.bytes()) else rest.write(bytes, start, end - start)
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val start = p.pos
|
||||
val (field, wire) = p.key()
|
||||
if (field == 1 && wire == 2) layouts += decodeLayout(p.bytes())
|
||||
else { p.skip(wire); rest.write(bytes, start, p.pos - start) }
|
||||
}
|
||||
return Layouts(layouts, rest.toByteArray())
|
||||
}
|
||||
@@ -335,13 +349,15 @@ object SteamTouchConfig {
|
||||
var version: Int? = null
|
||||
var color: FloatArray? = null
|
||||
val elements = mutableListOf<Element>()
|
||||
Proto(bytes).forEachField { field, wire, _, _, r ->
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
when {
|
||||
field == 1 && wire == 0 -> version = r.varint().toInt()
|
||||
field == 2 && wire == 0 -> actionSet = r.varint().toInt()
|
||||
field == 4 && wire == 2 -> elements += decodeElement(r.bytes())
|
||||
field == 5 && wire == 2 -> color = decodeColor(r.bytes())
|
||||
else -> r.skip(wire)
|
||||
field == 1 && wire == 0 -> version = p.varint().toInt()
|
||||
field == 2 && wire == 0 -> actionSet = p.varint().toInt()
|
||||
field == 4 && wire == 2 -> elements += decodeElement(p.bytes())
|
||||
field == 5 && wire == 2 -> color = decodeColor(p.bytes())
|
||||
else -> p.skip(wire)
|
||||
}
|
||||
}
|
||||
return Layout(actionSet, elements, color, version)
|
||||
@@ -354,15 +370,17 @@ object SteamTouchConfig {
|
||||
var y = 0f
|
||||
var sx = 1f
|
||||
var sy = 1f
|
||||
Proto(bytes).forEachField { field, wire, _, _, r ->
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
when {
|
||||
field == 1 && wire == 0 -> type = r.varint().toInt()
|
||||
field == 2 && wire == 0 -> visible = r.varint() != 0L
|
||||
field == 3 && wire == 5 -> x = r.float()
|
||||
field == 4 && wire == 5 -> y = r.float()
|
||||
field == 5 && wire == 5 -> sx = r.float()
|
||||
field == 6 && wire == 5 -> sy = r.float()
|
||||
else -> r.skip(wire)
|
||||
field == 1 && wire == 0 -> type = p.varint().toInt()
|
||||
field == 2 && wire == 0 -> visible = p.varint() != 0L
|
||||
field == 3 && wire == 5 -> x = p.float()
|
||||
field == 4 && wire == 5 -> y = p.float()
|
||||
field == 5 && wire == 5 -> sx = p.float()
|
||||
field == 6 && wire == 5 -> sy = p.float()
|
||||
else -> p.skip(wire)
|
||||
}
|
||||
}
|
||||
return Element(type, visible, x, y, sx, sy)
|
||||
@@ -370,8 +388,10 @@ object SteamTouchConfig {
|
||||
|
||||
private fun decodeColor(bytes: ByteArray): FloatArray {
|
||||
val c = floatArrayOf(1f, 1f, 1f, 1f)
|
||||
Proto(bytes).forEachField { field, wire, _, _, r ->
|
||||
if (field in 1..4 && wire == 5) c[field - 1] = r.float() else r.skip(wire)
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
if (field in 1..4 && wire == 5) c[field - 1] = p.float() else p.skip(wire)
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -450,19 +470,8 @@ object SteamTouchConfig {
|
||||
else -> pos = data.size
|
||||
}
|
||||
}
|
||||
inline fun forEachField(block: (field: Int, wire: Int, start: Int, end: Int, reader: Proto) -> Unit) {
|
||||
while (pos < data.size) {
|
||||
val start = pos
|
||||
val key = varint().toInt()
|
||||
val field = key ushr 3
|
||||
val wire = key and 7
|
||||
val mark = pos
|
||||
// Each handler reads or skips the field; the raw span is measured from a copy.
|
||||
val probe = Proto(data).also { it.pos = mark; it.skip(wire) }
|
||||
block(field, wire, start, probe.pos, this)
|
||||
pos = probe.pos
|
||||
}
|
||||
}
|
||||
fun more() = pos < data.size
|
||||
fun key(): Pair<Int, Int> = varint().toInt().let { (it ushr 3) to (it and 7) }
|
||||
}
|
||||
|
||||
fun hexToBytes(hex: String) = ByteArray(hex.length / 2) { hex.substring(2 * it, 2 * it + 2).toInt(16).toByte() }
|
||||
|
||||
@@ -34,7 +34,7 @@ import kotlin.math.min
|
||||
@SuppressLint("ViewConstructor")
|
||||
class SteamTouchControls(
|
||||
context: Context,
|
||||
private val device: SteamTouchDevice,
|
||||
val device: SteamTouchDevice,
|
||||
private val onMenu: () -> Unit,
|
||||
private val onKeyboard: () -> Unit,
|
||||
) : View(context) {
|
||||
@@ -48,6 +48,7 @@ class SteamTouchControls(
|
||||
private var actionSet = 0
|
||||
private var layers: List<Int> = emptyList()
|
||||
private var actionSeq = -1
|
||||
private var opened = 0
|
||||
private var elements: List<Element> = emptyList()
|
||||
private var tint = Color.WHITE
|
||||
private var alphaScale = 0.45f
|
||||
@@ -93,7 +94,10 @@ class SteamTouchControls(
|
||||
private fun refreshState() {
|
||||
val state = device.state()
|
||||
val app = if (state.appId == 0) BIG_PICTURE else state.appId
|
||||
if (app != configApp) {
|
||||
// The client reads the config afresh when the controller (re)connects: so do we.
|
||||
val reconnected = state.opened > 0 && opened == 0
|
||||
opened = state.opened
|
||||
if (app != configApp || reconnected) {
|
||||
configApp = app
|
||||
reload(app)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class SteamTouchConfigTest {
|
||||
// A layout DroidDeck saved for Geometry Wars on the Thor, as Steam keeps it.
|
||||
private val saved = "0aec0310012218080010011d0000703d255555d53d2d0000803f350000803f2218080110011d6666fe3e255555d53d2d0000803f350000803f2218080210011dba490c3e255c8f423f2d0000803f350000803f2218080410011d9a99593f255c8f423f2d0000803f350000803f2218080510011d8fc2353f25ae47613f2d0000803f350000803f2218080610011d2b87163e25d7a3f03e2d0000803f350000803f2218080710011dba49003f25f958053f2d293ab23f35293ab23f2218080810011d9a99683f25d227dd3e2d0000803f350000803f2218080910011d9a99483f25d227dd3e2d0000803f350000803f2218080a10011d9a99583f254444a43e2d0000803f350000803f2218080b10011d6666ce3e255555d53d2d0000803f350000803f2218080c10011d3333173f255555d53d2d0000803f350000803f2218080d10011d0ad7233e2585eb513e2d0000803f350000803f2218080e10011d3d0a573f2585eb513e2d0000803f350000803f2218080f10011d8fc2753d2585eb513e2d0000803f350000803f2218081010011dd7a3703f2585eb513e2d0000803f350000803f2218081c10011d0000713f255555d53d2d0000803f350000803f2218080310001de17a943e25ae47613f2d0000803f350000803f2a140d0000803f150000803f1d0000803f25cdcccc3e1002"
|
||||
|
||||
@Test
|
||||
fun decodesSavedLayout() {
|
||||
val layouts = SteamTouchConfig.decodeLayouts(SteamTouchConfig.hexToBytes(saved))
|
||||
assertEquals(1, layouts.layouts.size)
|
||||
val layout = layouts.forActionSet(1)!!
|
||||
val a = layout.elements.first { it.type == SteamTouchConfig.A }
|
||||
assertEquals(0.501f, a.x, 0.002f)
|
||||
assertEquals(1.39f, a.xScale, 0.01f)
|
||||
assertFalse(layout.elements.first { it.type == SteamTouchConfig.JOYSTICK_LEFT_BUTTON }.visible)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun roundTrips() {
|
||||
val bytes = SteamTouchConfig.hexToBytes(saved)
|
||||
assertEquals(saved, SteamTouchConfig.bytesToHex(SteamTouchConfig.encodeLayouts(SteamTouchConfig.decodeLayouts(bytes))))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun appliesLayoutToElements() {
|
||||
val layouts = SteamTouchConfig.decodeLayouts(SteamTouchConfig.hexToBytes(saved))
|
||||
val all = (0..31).toSet()
|
||||
val config = SteamTouchConfig.Config(null, null, layouts, mapOf(0 to all))
|
||||
val shown = SteamTouchConfig.elementsFor(config, 0, emptyList())
|
||||
val a = shown.first { it.type == SteamTouchConfig.A }
|
||||
assertEquals(0.501f, a.x, 0.002f)
|
||||
assertTrue(shown.none { it.type == SteamTouchConfig.JOYSTICK_LEFT_BUTTON })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user