diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt index 69ccb08c9c..1d4fb5ef55 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AutoComponents.kt @@ -111,10 +111,7 @@ object AutoComponents { @VisibleForTesting internal var gameRunning: () -> Boolean = { gameProcessRunning() } - private fun gameProcessRunning(): Boolean = File("/proc").listFiles()?.any { p -> - p.name.all(Char::isDigit) && runCatching { String(File(p, "cmdline").readBytes()).replace('\u0000', ' ') }.getOrNull() - ?.let { it.contains("SteamLaunch AppId=") || it.contains("proton waitforexitandrun") } == true - } == true + private fun gameProcessRunning(): Boolean = com.droiddeck.launcher.session.SteamGameProcess.running() fun queueInSession(context: Context, game: Library.SteamGame) { if (appKey(game) == null) return diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index ef8344bd90..fee992446c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -1339,17 +1339,7 @@ class SessionService : Service() { /** A Steam game is running: the client runs each one under its reaper, and Proton's script * waits on the game with waitforexitandrun. Read from the command lines, as ComponentsManager does. */ - private fun steamGameRunning(): Boolean { - val procs = File("/proc").listFiles() ?: return false - for (p in procs) { - if (!p.name.all(Char::isDigit)) continue - val cmd = runCatching { File(p, "cmdline").readBytes() }.getOrNull() ?: continue - if (cmd.isEmpty()) continue - val text = String(cmd).replace('\u0000', ' ') - if (text.contains("SteamLaunch AppId=") || text.contains("waitforexitandrun")) return true - } - return false - } + private fun steamGameRunning(): Boolean = SteamGameProcess.running() private fun resumeSession(panelVisible: Boolean = true) { if (SessionState.suspended) SessionState.resumeRequested = true diff --git a/app/src/main/java/com/droiddeck/launcher/session/SteamGameProcess.kt b/app/src/main/java/com/droiddeck/launcher/session/SteamGameProcess.kt new file mode 100644 index 0000000000..891fb57ca8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/SteamGameProcess.kt @@ -0,0 +1,18 @@ +package com.droiddeck.launcher.session + +import java.io.File + +object SteamGameProcess { + fun matches(cmdline: ByteArray): Boolean { + val args = String(cmdline).split('\u0000') + for (i in args.indices) { + if (args[i] == "waitforexitandrun") return true + if (args[i] == "SteamLaunch" && args.getOrNull(i + 1)?.startsWith("AppId=") == true) return true + } + return false + } + + fun running(proc: File = File("/proc")): Boolean = proc.listFiles()?.any { p -> + p.name.all(Char::isDigit) && runCatching { File(p, "cmdline").readBytes() }.getOrNull()?.let(::matches) == true + } == true +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/SteamGameProcessTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SteamGameProcessTest.kt new file mode 100644 index 0000000000..7a1723b95f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SteamGameProcessTest.kt @@ -0,0 +1,38 @@ +package com.droiddeck.launcher.session + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class SteamGameProcessTest { + @get:Rule val tmp = TemporaryFolder() + + private fun cmdline(vararg args: String) = args.joinToString("\u0000", postfix = "\u0000").toByteArray() + + @Test fun theSessionsOwnSearchesAreNotAGame() { + assertFalse(SteamGameProcess.matches(cmdline("pgrep", "-a", "-f", "SteamLaunch AppId=[0-9]"))) + assertFalse(SteamGameProcess.matches(cmdline("pgrep", "-f", "reaper SteamLaunch"))) + assertFalse(SteamGameProcess.matches(cmdline("pgrep", "-f", "reaper SteamLaunch AppId=976310( |$)"))) + assertFalse(SteamGameProcess.matches(cmdline("sh", "-c", "pgrep -f 'proton waitforexitandrun'"))) + assertFalse(SteamGameProcess.matches(cmdline("/usr/bin/steam", "steam://rungameid/976310"))) + } + + @Test fun steamsReaperAndProtonAreAGame() { + assertTrue(SteamGameProcess.matches(cmdline("/root/.local/share/Steam/steamrtarm64/reaper", "SteamLaunch", "AppId=976310", "--", + "/root/.local/share/Steam/compatibilitytools.d/droiddeck-proton-arm64/proton", "waitforexitandrun", "MK11.exe"))) + assertTrue(SteamGameProcess.matches(cmdline("python3", "/proton", "waitforexitandrun", "game.exe"))) + } + + @Test fun runningLooksAtEveryProcess() { + val proc = tmp.newFolder("proc") + fun process(pid: String, vararg args: String) = File(proc, pid).apply { mkdirs() }.resolve("cmdline").writeBytes(cmdline(*args)) + process("100", "pgrep", "-a", "-f", "SteamLaunch AppId=[0-9]") + process("self", "reaper", "SteamLaunch", "AppId=1") + assertFalse(SteamGameProcess.running(proc)) + process("200", "reaper", "SteamLaunch", "AppId=976310", "--", "proton", "waitforexitandrun", "MK11.exe") + assertTrue(SteamGameProcess.running(proc)) + } +}