diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 2027dc7c77..1f94f3a7e9 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -290,6 +290,7 @@ object SessionFiles { "stage session=$startsSession pending=$force choice=${SessionPrefs.bootMovie(context)} movie=${startupMovie?.path} " + "staged=$bigPictureMovieInstalled done=$done steam=${home.path} config: $before -> ${startupMoviePath(config)}", ) + if (startsSession) disableIdleSleep(home) if (!done) allDone = false if (!bigPictureMovieInstalled) allInstalled = false if (done && home.path.contains(".droiddeck-x64") == activeIsX64) seen = if (bigPictureMovieInstalled) BOOT_MOVIE_PATH else "" @@ -528,6 +529,43 @@ object SessionFiles { } } + private val IDLE_SLEEP_TIMERS = listOf("IdleSuspendBatterySeconds", "IdleSuspendACSeconds") + + /** + * Sets Steam's Power > "Sleep after" (battery and plugged in) to Disabled at every session start: Android + * owns the device's sleep, and a client that suspends inside the session only shows a black screen. Dimming stays the user's. Runs + * only before the client starts, which rewrites its config on exit. + */ + internal fun disableIdleSleep(steamHome: File): Boolean { + val config = File(steamHome, "config/config.vdf") + val text = runCatching { config.readText() }.getOrNull() ?: return false + val newline = if (text.contains("\r\n")) "\r\n" else "\n" + val steam = findSteamBlock(text) ?: return false + val system = findObject(text, "system", steam.open + 1, steam.close) + val updated = if (system != null) { + val block = text.substring(system.keyStart, system.close + 1) + text.replaceRange(system.keyStart, system.close + 1, IDLE_SLEEP_TIMERS.fold(block) { acc, key -> setScalar(acc, key, "0", newline) }) + } else { + val lineStart = text.lastIndexOf('\n', steam.close).let { if (it < 0) 0 else it + 1 } + val indent = text.substring(lineStart, steam.close).takeWhile { it == '\t' || it == ' ' } + "\t" + val section = (listOf(indent + "\"system\"", indent + "{") + + IDLE_SLEEP_TIMERS.map { "$indent\t\"$it\"\t\t\"0\"" } + (indent + "}")).joinToString(newline) + newline + text.substring(0, lineStart) + section + text.substring(lineStart) + } + val staged = File(config.parentFile, config.name + ".staged") + return try { + if (updated != text) { + staged.writeText(updated) + if (!staged.renameTo(config)) { staged.delete(); return false } + } + true + } catch (e: Exception) { + staged.delete() + Log.w(TAG, "could not disable Steam's idle sleep", e) + false + } + } + /** Adds the `Customization` section Steam reads, next to the `Software` section, when the config has none. */ private fun withCustomization(text: String, path: String, newline: String): String { val root = findObject(text, "InstallConfigStore", 0, text.length) ?: return text diff --git a/app/src/test/java/com/droiddeck/launcher/session/SteamIdleSleepTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SteamIdleSleepTest.kt new file mode 100644 index 0000000000..05fc9c2a6b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SteamIdleSleepTest.kt @@ -0,0 +1,56 @@ +package com.droiddeck.launcher.session + +import org.junit.Assert.* +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class SteamIdleSleepTest { + @get:Rule val temp = TemporaryFolder() + + private val keys = listOf( + "IdleSuspendBatterySeconds", "IdleSuspendACSeconds", + ) + + private fun home(vararg steamBody: String): File = temp.newFolder().also { + File(it, "config").mkdirs() + File(it, "config/config.vdf").writeText( + "\"InstallConfigStore\"\n{\n\t\"Software\"\n\t{\n\t\t\"Valve\"\n\t\t{\n\t\t\t\"Steam\"\n\t\t\t{\n" + + steamBody.joinToString("") + "\t\t\t}\n\t\t}\n\t}\n}\n", + ) + } + + private fun text(home: File) = File(home, "config/config.vdf").readText() + private fun value(text: String, key: String) = Regex("\"$key\"\\s+\"([^\"]*)\"").find(text)?.groupValues?.get(1) + + @Test fun freshClientGetsBothSleepTimersDisabled() { + val home = home() + assertTrue(SessionFiles.disableIdleSleep(home)) + keys.forEach { assertEquals(it, "0", value(text(home), it)) } + } + + @Test fun existingSleepTimersAreReplacedBesideTheOtherSettings() { + val home = home( + "\t\t\t\t\"system\"\n\t\t\t\t{\n\t\t\t\t\t\"IdleSuspendACSeconds\"\t\t\"900\"\n" + + "\t\t\t\t\t\"IdleScreensaverACSeconds\"\t\t\"300\"\n\t\t\t\t\t\"IdleBacklightDimBatterySeconds\"\t\t\"120\"\n\t\t\t\t}\n", + ) + assertTrue(SessionFiles.disableIdleSleep(home)) + keys.forEach { assertEquals(it, "0", value(text(home), it)) } + assertEquals("300", value(text(home), "IdleScreensaverACSeconds")) + assertEquals("120", value(text(home), "IdleBacklightDimBatterySeconds")) + } + + @Test fun aChoiceMadeInSteamIsResetAtTheNextSession() { + val home = home() + SessionFiles.disableIdleSleep(home) + File(home, "config/config.vdf").writeText(text(home).replace("\"IdleSuspendACSeconds\"\t\t\"0\"", "\"IdleSuspendACSeconds\"\t\t\"600\"")) + assertTrue(SessionFiles.disableIdleSleep(home)) + assertEquals("0", value(text(home), "IdleSuspendACSeconds")) + } + + @Test fun clientWithoutAConfigIsRetriedNextSession() { + val home = temp.newFolder() + assertFalse(SessionFiles.disableIdleSleep(home)) + } +}