diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 5e7a39b639..4d1c728745 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -545,6 +545,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private val cardboardPrefListener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, key -> if (key == SessionPrefs.CARDBOARD_KEY) uiHandler.post { applyCardboard(SessionPrefs.cardboard(this)) } } + private val screenEffectsProfilePrefListener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, key -> + if (key == SessionPrefs.SCREEN_EFFECTS_ACTIVE_PROFILE_KEY) uiHandler.post { + val profileId = SessionPrefs.activeScreenEffectsProfileId(this@SessionActivity) + effects = SessionPrefs.screenEffects(this@SessionActivity, profileId) + effects.push(upscaler) + } + } private val shortcutPrefListener = android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, key -> if (key == null || key == PhysicalShortcuts.KEY) { physicalShortcuts.bindings = PhysicalShortcuts.read(this) @@ -1342,7 +1349,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde WaylandCompositor.nativeSetUpscaleSharpness(pct) }, onEffects = { e -> - SessionPrefs.setScreenEffects(this@SessionActivity, e); effects = e + SessionPrefs.setScreenEffects(this@SessionActivity, currentScreenEffectsProfileId(), e) + effects = e e.push(upscaler) }, onTextureAnisotropy = { value -> @@ -1966,6 +1974,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde }, "drawer-game-no-steam").start() } + private fun currentScreenEffectsProfileId(): String? { + if (SessionState.running) return SessionPrefs.activeScreenEffectsProfileId(this) + val mode = intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM + return SessionPrefs.screenEffectsProfileId( + mode, + intent.getStringExtra(SessionService.EXTRA_GAME_ID), + intent.getStringExtra(SessionService.EXTRA_PROGRAM), + ) + } + private fun loadingMode(): String = if (SessionState.running) SessionState.mode else intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM @@ -2062,7 +2080,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde hudOpacity = SessionPrefs.hudOpacity(this) upscaler = SessionPrefs.upscaler(this) upscaleSharpness = SessionPrefs.upscaleSharpness(this) - effects = SessionPrefs.screenEffects(this) + effects = SessionPrefs.screenEffects(this, currentScreenEffectsProfileId()) applyCardboard(SessionPrefs.cardboard(this)) if (SessionPrefs.cardboardSourceBeforeSteamVr(this) >= 0 && !steamVrSwitchedPicture) { steamVrSwitchedPicture = true @@ -2300,7 +2318,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde WaylandCompositor.nativeSetScaleMode(SCALE_FIT, ALIGN_CENTER) WaylandCompositor.nativeSetUpscaler(SessionPrefs.upscaler(this)) WaylandCompositor.nativeSetUpscaleSharpness(SessionPrefs.upscaleSharpness(this)) - SessionPrefs.screenEffects(this).push(SessionPrefs.upscaler(this)) + SessionPrefs.screenEffects(this, currentScreenEffectsProfileId()).push(SessionPrefs.upscaler(this)) activeCardboard = effectiveCardboard(SessionPrefs.cardboard(this)) pushCardboard(activeCardboard) WaylandCompositor.nativeSetCardboardPoseFile(File(filesDir, ".wayland-rt/droiddeck-headpose").path) @@ -5020,6 +5038,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (::pip.isInitialized) pip.resumed() if (CompositorHost.isStarted) applyFrameGen() SessionPrefs.sessionPrefs(this).registerOnSharedPreferenceChangeListener(cardboardPrefListener) + SessionPrefs.sessionPrefs(this).registerOnSharedPreferenceChangeListener(screenEffectsProfilePrefListener) } override fun onPause() { @@ -5062,6 +5081,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde updatePadMotion() keyboard?.takeIf { it.shown }?.hide() SessionPrefs.sessionPrefs(this).unregisterOnSharedPreferenceChangeListener(cardboardPrefListener) + SessionPrefs.sessionPrefs(this).unregisterOnSharedPreferenceChangeListener(screenEffectsProfilePrefListener) ControllerPrefs.prefs(this).unregisterOnSharedPreferenceChangeListener(shortcutPrefListener) updateHeadTracker() super.onPause() diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index ae64425b77..4730e1299f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -1437,15 +1437,58 @@ object SessionPrefs { } + private const val SCREEN_EFFECTS_PROFILES_KEY = "screenEffects.profiles" + const val SCREEN_EFFECTS_ACTIVE_PROFILE_KEY = "screenEffects.activeProfile" + + fun screenEffectsProfileId(mode: String, gameId: String?, program: String?): String? { + if (mode == SessionService.MODE_DESKTOP) return "desktop" + val id = gameId?.trim()?.takeIf { it.isNotEmpty() } + if (id != null) return "game:$id" + return if (mode == SessionService.MODE_RUN) { + program?.trim()?.takeIf { it.isNotEmpty() }?.let { "program:$it" } + } else null + } + + fun activeScreenEffectsProfileId(context: Context): String? = + prefs(context).getString(SCREEN_EFFECTS_ACTIVE_PROFILE_KEY, null)?.takeIf { it.isNotBlank() } + + fun setActiveScreenEffectsProfile(context: Context, profileId: String?) { + prefs(context).edit().putString(SCREEN_EFFECTS_ACTIVE_PROFILE_KEY, profileId.orEmpty()).apply() + } + fun screenEffects(context: Context): ScreenEffects { val text = prefs(context).getString("screenEffects", null) ?: return ScreenEffects.OFF return runCatching { ScreenEffects.decode(JSONObject(text)) }.getOrDefault(ScreenEffects.OFF) } + fun screenEffects(context: Context, profileId: String?): ScreenEffects { + if (profileId.isNullOrBlank()) return screenEffects(context) + val storedProfiles = prefs(context).getString(SCREEN_EFFECTS_PROFILES_KEY, null) + val profile = storedProfiles?.let { raw -> + runCatching { JSONObject(raw).optJSONObject(profileId) }.getOrNull() + } + return profile?.let { + runCatching { ScreenEffects.decode(it) }.getOrDefault(ScreenEffects.OFF) + } ?: screenEffects(context) + } + fun setScreenEffects(context: Context, effects: ScreenEffects) { prefs(context).edit().putString("screenEffects", effects.encode().toString()).apply() } + fun setScreenEffects(context: Context, profileId: String?, effects: ScreenEffects) { + if (profileId.isNullOrBlank()) { + setScreenEffects(context, effects) + return + } + val sessionPrefs = prefs(context) + val profiles = runCatching { + JSONObject(sessionPrefs.getString(SCREEN_EFFECTS_PROFILES_KEY, null) ?: "{}") + }.getOrDefault(JSONObject()) + profiles.put(profileId, effects.encode()) + sessionPrefs.edit().putString(SCREEN_EFFECTS_PROFILES_KEY, profiles.toString()).apply() + } + const val CARDBOARD_KEY = "cardboard" fun cardboard(context: Context): Cardboard { 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 8da979cc39..15d857f757 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -372,6 +372,10 @@ class SessionService : Service() { SessionState.gameId = intent?.getStringExtra(EXTRA_GAME_ID) SessionState.steamUi = intent?.getStringExtra(EXTRA_STEAM_UI) SessionState.steamUrl = intent?.getStringExtra(EXTRA_STEAM_URL) + SessionPrefs.setActiveScreenEffectsProfile( + this, + SessionPrefs.screenEffectsProfileId(SessionState.mode, SessionState.gameId, SessionState.program), + ) com.droiddeck.launcher.stores.battlenet.BattleNetClient.clearRequests(LinuxRuntime.sessionRoot(this)) val gen = synchronized(stopLock) { SessionState.steamClientArch = null @@ -1501,6 +1505,10 @@ class SessionService : Service() { } val accepted = gameId!! if (pendingSteamGameId != null) Log.i(TAG, "replacing a queued game launch with $accepted") + SessionState.gameId = accepted + val effectsProfileId = SessionPrefs.screenEffectsProfileId(MODE_STEAM, accepted, null) + SessionPrefs.setActiveScreenEffectsProfile(this, effectsProfileId) + SessionPrefs.screenEffects(this, effectsProfileId).push(SessionPrefs.upscaler(this)) pendingSteamGameId = accepted pendingSteamGameGeneration = sessionGen if (SessionState.suspended) resumeSession() @@ -1546,7 +1554,7 @@ class SessionService : Service() { } private fun watchGameExit(gameId: String, gen: Int) { - if (!SessionPrefs.returnAfterGame(this)) return + val returnToLauncher = SessionPrefs.returnAfterGame(this) val token = ++gameExitWatchToken Thread({ val startBy = android.os.SystemClock.elapsedRealtime() + GAME_START_TIMEOUT_MS @@ -1555,18 +1563,22 @@ class SessionService : Service() { while (token == gameExitWatchToken && gen == sessionGen && SessionState.running && !SessionState.stopRequested) { val now = android.os.SystemClock.elapsedRealtime() if (steamGameRunning()) { - if (!seen) Log.i(TAG, "game $gameId started; returning to the launcher when it closes") + if (!seen) Log.i(TAG, "game $gameId started; tracking its exit") seen = true goneSince = 0L } else if (!seen) { if (now > startBy) { - Log.i(TAG, "game $gameId did not start; not watching for its exit") + Log.i(TAG, "game $gameId did not start; clearing its screen effects profile") + mainHandler.post { clearActiveScreenEffectsProfile(gameId, gen) } return@Thread } } else if (goneSince == 0L) { goneSince = now } else if (now - goneSince >= GAME_EXIT_GRACE_MS) { - mainHandler.post { returnAfterGame(gameId, gen, token) } + mainHandler.post { + if (returnToLauncher) returnAfterGame(gameId, gen, token) + else clearActiveScreenEffectsProfile(gameId, gen) + } return@Thread } try { Thread.sleep(GAME_EXIT_POLL_MS) } catch (e: InterruptedException) { return@Thread } @@ -1574,6 +1586,14 @@ class SessionService : Service() { }, "game-exit-watch").start() } + private fun clearActiveScreenEffectsProfile(gameId: String, gen: Int) { + if (gen != sessionGen || !SessionState.running || SessionState.stopRequested) return + val gameProfileId = SessionPrefs.screenEffectsProfileId(MODE_STEAM, gameId, null) + if (SessionPrefs.activeScreenEffectsProfileId(this) != gameProfileId) return + SessionPrefs.setActiveScreenEffectsProfile(this, null) + SessionPrefs.screenEffects(this, null).push(SessionPrefs.upscaler(this)) + } + private fun returnAfterGame(gameId: String, gen: Int, token: Int) { if (token != gameExitWatchToken || gen != sessionGen || !SessionState.running || SessionState.stopRequested || SessionState.mode != MODE_STEAM || !SessionPrefs.returnAfterGame(this)) return