diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index d0893d19f7..8c36f3e5db 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -4650,6 +4650,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde runningGame = { gestureGame }, quick = { drawerActions }, copyRegion = ::copyGameRegion, + holdComposed = ::holdComposedOutput, screenOff = { secondScreenPresentation?.screenOff() }, )) @@ -5967,7 +5968,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde if (zeroCopyActive()) { WaylandCompositor.nativeSetZeroCopy(false) val restore = java.util.concurrent.atomic.AtomicBoolean(false) - val back = { if (restore.compareAndSet(false, true)) WaylandCompositor.nativeSetZeroCopy(true) } + val back = { if (restore.compareAndSet(false, true)) WaylandCompositor.nativeSetZeroCopy(composedHolds == 0) } uiHandler.postDelayed({ capturePausedFrame(manual = true, onDone = back) }, ZERO_COPY_SETTLE_MS) uiHandler.postDelayed({ back() }, ZERO_COPY_RESTORE_MS) } else { @@ -5976,6 +5977,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde SessionService.pause(this) } + private var composedHolds = 0 + + private fun holdComposedOutput(hold: Boolean) { + composedHolds = (composedHolds + if (hold) 1 else -1).coerceAtLeast(0) + if (zeroCopyActive()) WaylandCompositor.nativeSetZeroCopy(composedHolds == 0) + } + private fun zeroCopyActive(): Boolean = CompositorHost.isStarted && !SessionState.hdr && SessionPrefs.zeroCopy(this, SessionState.mode) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomAchievementsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomAchievementsPage.kt index e0dc4559e6..b174df90aa 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomAchievementsPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomAchievementsPage.kt @@ -21,17 +21,9 @@ import com.droiddeck.launcher.stores.SteamAchievements import kotlinx.coroutines.delay import kotlinx.coroutines.isActive -private const val GAME_POLL_MS = 2_000L - @Composable internal fun BottomAchievementsPage(runningGame: () -> String?) { - var appId by remember { mutableStateOf(runningGame()?.toLongOrNull()) } - LaunchedEffect(Unit) { - while (isActive) { - appId = runningGame()?.toLongOrNull() - delay(GAME_POLL_MS) - } - } + val appId = rememberShownGame(runningGame) val game = remember(appId) { appId?.let(SteamAchievements::game) } val state = rememberAchievements(game) if (game == null || state == null) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomGuidesPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomGuidesPage.kt index 0513e82fee..db0006d760 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomGuidesPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomGuidesPage.kt @@ -56,8 +56,6 @@ import kotlinx.coroutines.withContext import java.io.File import kotlin.coroutines.resume -private const val GAME_POLL_MS = 2_000L - private sealed class GuideList { object Loading : GuideList() object Failed : GuideList() @@ -67,13 +65,7 @@ private sealed class GuideList { @Composable internal fun BottomGuidesPage(runningGame: () -> String?) { val context = LocalContext.current - var appId by remember { mutableStateOf(runningGame()?.toLongOrNull()) } - LaunchedEffect(Unit) { - while (isActive) { - appId = runningGame()?.toLongOrNull() - delay(GAME_POLL_MS) - } - } + val appId = rememberShownGame(runningGame) val id = appId if (id == null) { PageNote(stringResource(R.string.bottom_guides_no_game)) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomIdleHome.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomIdleHome.kt index 95e8876f9a..403307de77 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomIdleHome.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomIdleHome.kt @@ -58,7 +58,30 @@ internal object IdleHome { return games.filter { it.lastPlayed > 0 }.maxByOrNull { it.lastPlayed }?.let { it to false } } + class Overview(val name: String, val minutes: Long, val lastPlayed: Long) + + fun overviewExpression(appId: Long): String = "(() => { const o = globalThis.appStore?.GetAppOverviewByAppID?.($appId); " + + "return o ? JSON.stringify({name: o.display_name, minutes: o.minutes_playtime_forever, last: o.rt_last_time_played}) : null; })()" + + fun parseOverview(json: String?): Overview? = runCatching { + val o = org.json.JSONObject(json ?: return null) + Overview(o.optString("name").ifEmpty { return null }, o.optLong("minutes"), o.optLong("last")) + }.getOrNull() + + private fun overview(context: Context, appId: Long): Overview? = runCatching { + val answer = AgentGuest.steamEval(context, overviewExpression(appId), 4_000) + if (!answer.optBoolean("ok")) null else parseOverview(answer.opt("value")?.takeIf { it != org.json.JSONObject.NULL }?.toString()) + }.getOrNull() + fun load(context: Context, games: List, selected: Long?): IdleGame? { + if (selected != null) overview(context, selected)?.let { o -> + val id = selected.toInt() + val art = games.firstOrNull { it.appId == id }?.let { it.hero ?: it.art } ?: runCatching { + Library.steamCacheImage(File(SessionPrefs.activeSteamRoot(context), "appcache/librarycache"), id, + listOf("library_hero.jpg", "library_600x900.jpg", "header.jpg")) + }.getOrNull() + return IdleGame(id, o.name, art, o.minutes, o.lastPlayed, highlighted = true) + } val (game, highlighted) = pick(games, selected) ?: return null val stats = runCatching { SteamLocalConfig.appStats(SessionPrefs.activeSteamRoot(context), game.appId) }.getOrNull() return IdleGame( @@ -72,6 +95,20 @@ internal object IdleHome { private const val SELECTION_MS = 2_000L +@Composable +internal fun rememberShownGame(runningGame: () -> String?): Long? { + val context = LocalContext.current + var appId by remember { mutableStateOf(runningGame()?.toLongOrNull()) } + LaunchedEffect(Unit) { + while (isActive) { + appId = runningGame()?.toLongOrNull() + ?: withContext(Dispatchers.IO) { runCatching { AgentGuest.selectedSteamAppId(context) }.getOrNull() } + delay(SELECTION_MS) + } + } + return appId +} + @Composable internal fun BottomIdleHome(modifier: Modifier = Modifier) { val context = LocalContext.current diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomMirrorPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomMirrorPage.kt index fd547eed20..583248f56f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomMirrorPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomMirrorPage.kt @@ -18,6 +18,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -73,6 +74,7 @@ internal object MirrorRegion { } private const val FRAME_MS = 66L +private const val COMPOSED_SETTLE_MS = 250L private const val EDIT_WIDTH = 960 private const val EDIT_HEIGHT = 540 private const val LIVE_LONGEST = 720 @@ -85,6 +87,10 @@ internal fun BottomMirrorPage(actions: BottomPageActions) { MirrorNote(stringResource(R.string.bottom_mirror_no_frame)) return } + DisposableEffect(Unit) { + actions.holdComposed(true) + onDispose { actions.holdComposed(false) } + } val game = actions.runningGame() ?: "steam" var region by remember(game) { mutableStateOf(MirrorRegion.parse(SessionPrefs.mirrorRegion(context, game))) } var editing by remember(game) { mutableStateOf(region == null) } @@ -139,6 +145,7 @@ private fun MirrorEditor( var end by remember { mutableStateOf(null) } var picked by remember { mutableStateOf(current) } LaunchedEffect(Unit) { + delay(COMPOSED_SETTLE_MS) val bitmap = capture(copy, null, EDIT_WIDTH, EDIT_HEIGHT) failed = bitmap == null still = bitmap?.asImageBitmap() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt index bb639f546a..2eff0d0c5a 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomPages.kt @@ -35,6 +35,7 @@ class BottomPageActions( val quick: () -> DrawerActions? = { null }, val screenOff: (() -> Unit)? = null, val copyRegion: ((android.graphics.RectF?, Int, Int, (android.graphics.Bitmap?) -> Unit) -> Unit)? = null, + val holdComposed: (Boolean) -> Unit = {}, ) internal const val BOTTOM_PAGE_TELEMETRY = "telemetry" diff --git a/app/src/test/java/com/droiddeck/launcher/ui/BottomIdleHomeTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/BottomIdleHomeTest.kt index 6fb77a62b0..97aeff6ae9 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/BottomIdleHomeTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/BottomIdleHomeTest.kt @@ -4,7 +4,12 @@ import com.droiddeck.launcher.frontend.Library import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) class BottomIdleHomeTest { private val games = listOf( Library.SteamGame(620, "Portal 2", null, "", lastPlayed = 100), @@ -31,4 +36,16 @@ class BottomIdleHomeTest { fun nothingPlayedAndNothingSelected() { assertNull(IdleHome.pick(listOf(Library.SteamGame(1, "A", null, "")), null)) } + + @Test + fun readsSteamsOverviewOfTheHighlightedGame() { + val o = IdleHome.parseOverview("""{"name":"Sonic Mania","minutes":864,"last":1764000000}""")!! + assertEquals("Sonic Mania", o.name) + assertEquals(864L, o.minutes) + assertEquals(1764000000L, o.lastPlayed) + assertNull(IdleHome.parseOverview(null)) + assertNull(IdleHome.parseOverview("""{"name":""}""")) + assertNull(IdleHome.parseOverview("nope")) + assertEquals(true, IdleHome.overviewExpression(584400).contains("GetAppOverviewByAppID?.(584400)")) + } }