Restore Steam after idle sleep through session pause and login1 wake

This commit is contained in:
xXJsonDeruloXx committed 2026-10-02 21:32:12 -04:00
1 parent 620db5a8ef
commit a25800149d
6 files changed
+343 -4

No files matched your search

@@ -1160,7 +1160,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
}
return true
}
val resumeKey = event.keyCode == KeyEvent.KEYCODE_BUTTON_A || event.keyCode == KeyEvent.KEYCODE_BUTTON_START
val resumeKey = event.keyCode == KeyEvent.KEYCODE_BUTTON_A || event.keyCode == KeyEvent.KEYCODE_BUTTON_START ||
event.keyCode == KeyEvent.KEYCODE_BUTTON_MODE || event.keyCode == KeyEvent.KEYCODE_HOME
val resumeKeyId = event.deviceId to event.keyCode
if (fromController && resumeKey && (SessionState.suspended || resumeKeyId in resumeKeysDown)) {
if (event.action == KeyEvent.ACTION_DOWN) {
@@ -39,6 +39,7 @@ object SessionFiles {
"usr/local/bin/bannerlator-seed-redists" to "usr/local/bin/bannerlator-seed-redists",
"usr/local/bin/bannerlator-proton-extra" to "usr/local/bin/bannerlator-proton-extra",
"usr/local/bin/bannerlator-netmanager" to "usr/local/bin/bannerlator-netmanager",
"usr/local/bin/bannerlator-login1" to "usr/local/bin/bannerlator-login1",
"usr/local/bin/bannerlator-steam-launch" to "usr/local/bin/bannerlator-steam-launch",
"usr/local/bin/bannerlator-desktop-games" to "usr/local/bin/bannerlator-desktop-games",
"usr/local/bin/bannerlator-steam-shim" to "usr/local/bin/bannerlator-steam-shim",
@@ -74,6 +74,8 @@ class SessionService : Service() {
private var activityVisible = true
private var screenOn = true
private var manualPauseRequested = false
/** An explicit Steam sleep request is independent of the background policy. */
private var steamSleepToken: String? = null
private var suspendOperationPending = false
private var suspendAttemptFailed = false
private var screenReceiverRegistered = false
@@ -84,6 +86,7 @@ class SessionService : Service() {
Intent.ACTION_SCREEN_ON -> screenOn = true
else -> return
}
if (screenOn) resumeSteamSleepOnReturn()
suspendAttemptFailed = false
updateSuspendPolicy()
}
@@ -139,6 +142,7 @@ class SessionService : Service() {
}
ACTION_ACTIVITY_VISIBLE -> {
activityVisible = true
resumeSteamSleepOnReturn()
suspendAttemptFailed = false
updateSuspendPolicy()
return START_NOT_STICKY
@@ -160,6 +164,7 @@ class SessionService : Service() {
activityVisible = true
screenOn = (getSystemService(Context.POWER_SERVICE) as? PowerManager)?.isInteractive ?: screenOn
manualPauseRequested = false
completeSteamSleep()
suspendAttemptFailed = false
updateSuspendPolicy()
return START_NOT_STICKY
@@ -177,6 +182,7 @@ class SessionService : Service() {
activityVisible = true
screenOn = (getSystemService(Context.POWER_SERVICE) as? PowerManager)?.isInteractive ?: true
manualPauseRequested = false
steamSleepToken = null
suspendOperationPending = false
suspendAttemptFailed = false
suspendController = null
@@ -938,9 +944,26 @@ class SessionService : Service() {
private fun watchLaunchRequests(dir: File) {
launchWatcher?.stopWatching()
listOf("steam-sleep", "steam-sleep-state", "steam-sleep-ready").forEach { File(dir, it).delete() }
val gen = sessionGen
@Suppress("DEPRECATION")
val watcher = object : android.os.FileObserver(dir.path, CLOSE_WRITE or MOVED_TO) {
override fun onEvent(event: Int, path: String?) {
if (path == "steam-sleep") {
val request = File(dir, path)
val token = runCatching { request.readText().trim() }.getOrNull() ?: return
request.delete()
if (!token.matches(Regex("[a-f0-9]{32}"))) return
mainHandler.post {
if (gen != sessionGen || !SessionState.running) return@post
steamSleepToken = token
writeSteamSleepState(token, "paused")
suspendAttemptFailed = false
Log.i(TAG, "Steam requested session sleep")
updateSuspendPolicy()
}
return
}
if (path != "steam-launch") return
val file = File(dir, path)
val text = try { file.readText() } catch (e: Exception) { return }
@@ -975,18 +998,23 @@ class SessionService : Service() {
if (suspendPolicy == SessionPrefs.SUSPEND_MANUAL && (!activityVisible || !screenOn)) {
manualPauseRequested = true
}
val shouldSuspend = when (suspendPolicy) {
val shouldSuspend = steamSleepToken != null || when (suspendPolicy) {
SessionPrefs.SUSPEND_AUTO -> !activityVisible || !screenOn
SessionPrefs.SUSPEND_MANUAL -> manualPauseRequested
else -> false
}
val controller = suspendController ?: return
if (suspendOperationPending || suspendAttemptFailed || shouldSuspend == SessionState.suspended) return
val gen = sessionGen
suspendOperationPending = true
SessionEvents.record(if (shouldSuspend) "session.suspend_requested" else "session.resume_requested", mapOf(
"policy" to suspendPolicy, "activityVisible" to activityVisible,
"screenOn" to screenOn, "steamSleep" to (steamSleepToken != null),
))
if (shouldSuspend) {
controller.freeze { success ->
mainHandler.post {
if (!SessionState.running) return@post
if (gen != sessionGen || !SessionState.running) return@post
suspendOperationPending = false
if (success) {
SessionState.suspended = true
@@ -994,6 +1022,7 @@ class SessionService : Service() {
releaseLocks()
refreshNotification()
} else {
completeSteamSleep()
suspendAttemptFailed = true
Log.w(TAG, "could not confirm that the session stopped")
}
@@ -1004,7 +1033,7 @@ class SessionService : Service() {
acquireLocks()
controller.resume { success ->
mainHandler.post {
if (!SessionState.running) return@post
if (gen != sessionGen || !SessionState.running) return@post
suspendOperationPending = false
if (success) {
SessionState.suspended = false
@@ -1020,6 +1049,27 @@ class SessionService : Service() {
}
}
private fun writeSteamSleepState(token: String, state: String) {
val dir = LinuxRuntime.sessionRoot(this)
runCatching {
val staged = File(dir, "steam-sleep-state.tmp")
staged.writeText("$token:$state\n")
check(staged.renameTo(File(dir, "steam-sleep-state")))
}.onFailure { Log.w(TAG, "could not acknowledge Steam sleep", it) }
}
private fun completeSteamSleep() {
val token = steamSleepToken ?: return
// Write before SIGCONT: the guest's login1 sees it immediately on thaw
// and delivers PrepareForSleep(false), restoring Steam's main surface.
writeSteamSleepState(token, "awake")
steamSleepToken = null
}
private fun resumeSteamSleepOnReturn() {
if (suspendPolicy == SessionPrefs.SUSPEND_AUTO && activityVisible && screenOn) completeSteamSleep()
}
private fun finishSessionStop(status: Int, stoppedGen: Int) {
mainHandler.post {
if (stoppedGen != sessionGen || SessionState.running) {