diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index be228331ce..7f33dfdf05 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -566,6 +566,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private var cardboard by mutableStateOf(Cardboard.OFF) private var headTracker: HeadTracker? = null private var steamVrStatus by mutableStateOf(null) + private var steamVrPercent by mutableStateOf(null) private var steamVrSwitchedPicture by mutableStateOf(false) private var steamVrSeenRunning = false private val steamVrRecovery by lazy { com.droiddeck.launcher.gpu.SteamVrRecovery(this) { @@ -1315,9 +1316,12 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde val request = steamVrRequestGeneration val session = SessionState.sessionId val now = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { - SteamVr.status(this@SessionActivity) + SteamVr.report(this@SessionActivity) + } + if (request == steamVrRequestGeneration && session == SessionState.sessionId) { + steamVrPercent = now.percent + onSteamVrStatus(now.status) } - if (request == steamVrRequestGeneration && session == SessionState.sessionId) onSteamVrStatus(now) kotlinx.coroutines.delay(3000) } } @@ -1430,6 +1434,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde com.droiddeck.launcher.glasses.GlassesMenuMirror.recenter() }, steamVr = steamVrStatus, + steamVrPercent = steamVrPercent, immersiveSteamVr = com.droiddeck.launcher.session.QuestExperimental.isSupported(), onSteamVr = { action -> steamVrAction(action) }, textureAnisotropy = if (drawerGameProfileId == null) textureAnisotropy else drawerTextureAnisotropy, diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/SteamVr.kt b/app/src/main/java/com/droiddeck/launcher/gpu/SteamVr.kt index b06498339d..851a0b6294 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/SteamVr.kt +++ b/app/src/main/java/com/droiddeck/launcher/gpu/SteamVr.kt @@ -32,9 +32,21 @@ object SteamVr { } } - fun status(context: Context): Status = runCatching { - parse(AgentGuest.call(context, JSONObject().put("kind", "vr").put("action", "status"), STATUS_TIMEOUT_MS)) - }.getOrDefault(Status.UNAVAILABLE) + data class Report(val status: Status, val percent: Int? = null) + + fun status(context: Context): Status = report(context).status + + fun report(context: Context): Report = runCatching { + report(AgentGuest.call(context, JSONObject().put("kind", "vr").put("action", "status"), STATUS_TIMEOUT_MS)) + }.getOrDefault(Report(Status.UNAVAILABLE)) + + fun report(answer: JSONObject): Report { + val status = parse(answer) + val size = answer.optLong("size", 0L) + val done = answer.optLong("downloaded", -1L) + val percent = if (status == Status.INSTALLING && size > 0 && done in 0 until size) (done * 100 / size).toInt() else null + return Report(status, percent) + } fun run(context: Context, action: String): Boolean = runCatching { require(action in setOf("install", "start", "stop")) diff --git a/app/src/main/java/com/droiddeck/launcher/session/EnhancedSessionLogShare.kt b/app/src/main/java/com/droiddeck/launcher/session/EnhancedSessionLogShare.kt index b437f3ae4e..daeea620c8 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/EnhancedSessionLogShare.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/EnhancedSessionLogShare.kt @@ -948,7 +948,7 @@ internal object EnhancedSessionLogShare { "droiddeck", "droiddeck-esync", "session.log", "app.log", "wayland.log", "audio.log", "device.txt", "network.txt", "events.jsonl", "memory.csv", "capture.json", "crash.log", "frame-trace.log", "process-trace.log", "game-lifecycle.log", "launches.log", "tools.tsv", "wanted.tsv", "prefixes.json", - "battlenet.log", "spawned.log", "flatpak-install.log", "cef_log.txt", "cef_log.previous.txt", + "battlenet.log", "spawned.log", "flatpak-install.log", "droiddeck-steamvr-install.log", "cef_log.txt", "cef_log.previous.txt", "console_log.txt", "webhelper_js.txt", "controller_support.txt", "systemperfmanager.txt", ) private val KNOWN_COMPONENT_PATTERNS = listOf( diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index 09e169e73b..d2b16e4d71 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -250,6 +250,7 @@ class DrawerActions( val cardboard: Cardboard = Cardboard.OFF, val cardboardSensor: Boolean = false, val steamVr: SteamVr.Status? = null, + val steamVrPercent: Int? = null, val glassesPresent: Boolean = false, val glassesOutput: Boolean = false, val immersiveSteamVr: Boolean = false, @@ -1251,7 +1252,7 @@ private fun SteamVrGroup(a: DrawerActions, track: (String) -> Modifier) { } when (a.steamVr) { SteamVr.Status.NOT_INSTALLED -> button(vrLabel, stringResource(R.string.cardboard_steamvr_install), "install") - SteamVr.Status.INSTALLING -> SettingsRow(vrLabel, stringResource(R.string.cardboard_steamvr_installing)) {} + SteamVr.Status.INSTALLING -> SteamVrInstalling(vrLabel, a.steamVrPercent) SteamVr.Status.READY -> button(vrLabel, enterLabel, "start") SteamVr.Status.RUNNING -> { if (a.immersiveSteamVr) button(vrLabel, enterLabel, "start") @@ -1266,12 +1267,19 @@ private fun SteamVrGroup(a: DrawerActions, track: (String) -> Modifier) { } } +@Composable +private fun SteamVrInstalling(label: String, percent: Int?) { + SettingsRow(label, stringResource(R.string.cardboard_steamvr_installing)) { + if (percent != null) Text(stringResource(R.string.ctrl_percent, percent), fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) + } +} + @Composable private fun SteamVrRow(a: DrawerActions) { val vrLabel = stringResource(R.string.cardboard_steamvr) when (a.steamVr) { SteamVr.Status.NOT_INSTALLED -> ActionRow(vrLabel, null, stringResource(R.string.cardboard_steamvr_install), { a.onSteamVr("install") }) - SteamVr.Status.INSTALLING -> SettingsRow(vrLabel, stringResource(R.string.cardboard_steamvr_installing)) {} + SteamVr.Status.INSTALLING -> SteamVrInstalling(vrLabel, a.steamVrPercent) SteamVr.Status.READY -> ActionRow(vrLabel, null, stringResource(R.string.cardboard_steamvr_start), { a.onSteamVr("start") }) SteamVr.Status.RUNNING -> ActionRow(vrLabel, null, stringResource(R.string.cardboard_steamvr_stop), { a.onSteamVr("stop") }) SteamVr.Status.UNAVAILABLE -> SettingsRow(vrLabel, stringResource(R.string.cardboard_steamvr_unavailable)) {} diff --git a/app/src/test/java/com/droiddeck/launcher/gpu/CardboardTest.kt b/app/src/test/java/com/droiddeck/launcher/gpu/CardboardTest.kt index 27ea94c926..f588df5bd2 100644 --- a/app/src/test/java/com/droiddeck/launcher/gpu/CardboardTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/gpu/CardboardTest.kt @@ -64,6 +64,22 @@ class CardboardTest { SteamVr.parse(answer("ok" to true, "installed" to true, "driver" to true).put("exitCode", 1))) } + @Test fun steamVrReportsDownloadPercentOnlyWhileInstalling() { + fun answer(installing: Boolean, downloaded: Long?, size: Long?) = JSONObject().apply { + put("ok", true) + put("installing", installing) + put("driver", true) + downloaded?.let { put("downloaded", it) } + size?.let { put("size", it) } + } + assertEquals(SteamVr.Report(SteamVr.Status.INSTALLING, 0), SteamVr.report(answer(true, 0, 571_309_057))) + assertEquals(SteamVr.Report(SteamVr.Status.INSTALLING, 49), SteamVr.report(answer(true, 285_654_528, 571_309_057))) + assertEquals(SteamVr.Report(SteamVr.Status.INSTALLING, 99), SteamVr.report(answer(true, 571_309_056, 571_309_057))) + assertEquals(SteamVr.Report(SteamVr.Status.INSTALLING, null), SteamVr.report(answer(true, 571_309_057, 571_309_057))) + assertEquals(SteamVr.Report(SteamVr.Status.INSTALLING, null), SteamVr.report(answer(true, null, null))) + assertEquals(SteamVr.Report(SteamVr.Status.NOT_INSTALLED, null), SteamVr.report(answer(false, 1_000, 571_309_057))) + } + @Test fun steamVrActionsSucceedOnlyWhenTheHelperDid() { fun answer(ok: Boolean, exitCode: Int? = null, started: Boolean? = null) = JSONObject().apply { put("ok", ok) diff --git a/docs/cardboard-vr.md b/docs/cardboard-vr.md index 8fd74154db..e5d959099c 100644 --- a/docs/cardboard-vr.md +++ b/docs/cardboard-vr.md @@ -55,7 +55,10 @@ the lens exactly. Valve's ARM64 SteamVR, the build the Steam Frame runs, comes from Valve's public SteamOS package server (`deckard-steamvr`, pinned by version and SHA-256 in `droiddeck-steamvr`; the same hash [steam-frame-nixos](https://github.com/y0usaf/steam-frame-nixos) pins). `droiddeck-steamvr install` -unpacks it to `/opt/steamvr`. The drawer's **Install** button runs that (571 MB). **Start** then +unpacks it to `/opt/steamvr`. The drawer's **Install** button runs that (571 MB) and shows the +download percentage. A download that stays under 1 KB/s for 60 seconds is stopped and resumed, up to +five tries without progress; each step is written to Steam's logs as `droiddeck-steamvr-install.log`, +which the session log share includes. **Start** then switches the picture to side-by-side and runs SteamVR with three DroidDeck pieces (`tools/vr`): 1. **`driver_droiddeck.so`**, an OpenVR driver with one HMD: diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent b/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent index 114e608e33..9d1da9a555 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-agent @@ -1043,6 +1043,8 @@ def steamvr(req): key, _, value = part.partition("=") if key in ("installed", "running", "installing", "driver"): result[key] = value == "1" + elif key in ("downloaded", "size") and value.isdigit(): + result[key] = int(value) return result diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr index 176f92aad3..8b155ebcf6 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steamvr @@ -13,6 +13,15 @@ chmod 700 "$state" PKG_URL=https://steamdeck-packages.steamos.cloud/archlinux-deckard-hotfixes/release/0.3.x/deckard-steamvr-rel-r25358740%2B28b72a4f-1-aarch64.pkg.tar.zst PKG_SHA256=90a600775eb3e8856dc475f83166a93a290a0b1f3b4e82a69c8e2a8f06b9ddc9 +PKG_SIZE=571309057 +pkg=$HOME/.cache/droiddeck-vr/steamvr.pkg.tar.zst +install_log=$logs/droiddeck-steamvr-install.log + +bytes() { stat -c %s "$1" 2>/dev/null || echo 0; } +note() { + mkdir -p "$logs" + printf '%s %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$install_log" +} running() { pgrep -x vrserver >/dev/null 2>&1; } installed() { [ -x "$steamvr/bin/linuxarm64/vrstartup" ]; } @@ -90,7 +99,7 @@ status) if idle; then unregister; fi n=0; [ -f "$state/installing" ] && n=1 d=0; [ -f "$driver/bin/linuxarm64/driver_droiddeck.so" ] && [ -f "$layer/libVkLayer_droiddeck_vr.so" ] && d=1 - echo "installed=$i running=$r installing=$n driver=$d" + echo "installed=$i running=$r installing=$n driver=$d downloaded=$(bytes "$pkg") size=$PKG_SIZE" ;; install) exec 7>"$state/install.lock" @@ -98,18 +107,30 @@ install) installed && { echo "SteamVR is already in $steamvr"; exit 0; } : > "$state/installing" trap 'rm -f "$state/installing"' EXIT - cache=$HOME/.cache/droiddeck-vr + cache=$(dirname "$pkg") mkdir -p "$cache" - pkg=$cache/steamvr.pkg.tar.zst - echo "downloading SteamVR (571 MB) from Valve's package server" - curl -fsSL --retry 5 -C - -o "$pkg" "$PKG_URL" || { echo "download failed"; exit 1; } - echo "$PKG_SHA256 $pkg" | sha256sum -c - >/dev/null 2>&1 || { echo "checksum mismatch: removing the download"; rm -f "$pkg"; exit 1; } + attempt=1 + while [ "$(bytes "$pkg")" -lt "$PKG_SIZE" ]; do + before=$(bytes "$pkg") + note "downloading SteamVR from Valve's package server: attempt $attempt, $before of $PKG_SIZE bytes" + curl -fsSL --connect-timeout 30 --speed-limit 1024 --speed-time 60 -C - -o "$pkg" "$PKG_URL" 2>>"$install_log" + code=$? + note "curl exited $code with $(bytes "$pkg") of $PKG_SIZE bytes" + [ "$code" = 0 ] && break + [ "$(bytes "$pkg")" -gt "$before" ] && attempt=0 + [ "$attempt" -lt 5 ] || { note "download failed"; exit 1; } + attempt=$((attempt + 1)) + sleep 5 + done + note "checking sha256" + echo "$PKG_SHA256 $pkg" | sha256sum -c - >/dev/null 2>&1 || { note "checksum mismatch: removing the download"; rm -f "$pkg"; exit 1; } tmp=$cache/extract rm -rf "$tmp" && mkdir -p "$tmp" - zstd -dc "$pkg" | tar -x -C "$tmp" opt/steamvr || { echo "extract failed"; rm -rf "$tmp"; exit 1; } + note "unpacking SteamVR" + zstd -dc "$pkg" 2>>"$install_log" | tar -x -C "$tmp" opt/steamvr 2>>"$install_log" || { note "extract failed"; rm -rf "$tmp"; exit 1; } mkdir -p "$(dirname "$steamvr")" - rm -rf "$steamvr" && mv "$tmp/opt/steamvr" "$steamvr" && rm -rf "$tmp" "$pkg" - echo "SteamVR installed in $steamvr" + rm -rf "$steamvr" && mv "$tmp/opt/steamvr" "$steamvr" && rm -rf "$tmp" "$pkg" || { note "could not move SteamVR into $steamvr"; exit 1; } + note "SteamVR installed in $steamvr" ;; stop) stop || exit 1 diff --git a/tools/tests/test_droiddeck_agent.py b/tools/tests/test_droiddeck_agent.py index a84a54ca8b..2aa6bbd03d 100644 --- a/tools/tests/test_droiddeck_agent.py +++ b/tools/tests/test_droiddeck_agent.py @@ -179,6 +179,11 @@ class AgentTest(unittest.TestCase): self.assertEqual((True, False, False, True), (answer["installed"], answer["running"], answer["installing"], answer["driver"])) + def test_vr_status_reports_download_progress(self): + self.vr_helper('echo "installed=0 running=0 installing=1 driver=1 downloaded=1234 size=571309057"\n') + answer = self.ask({"kind": "vr", "action": "status"}) + self.assertEqual((True, 1234, 571309057), (answer["installing"], answer["downloaded"], answer["size"])) + def test_vr_runs_only_its_own_actions(self): log = self.dir / "args" self.vr_helper('echo "$@" > %s\n' % log) diff --git a/tools/tests/test_steamvr_helper.py b/tools/tests/test_steamvr_helper.py index f99f5cbdc2..359f3c698d 100644 --- a/tools/tests/test_steamvr_helper.py +++ b/tools/tests/test_steamvr_helper.py @@ -1,9 +1,12 @@ +import hashlib +import io import os import json import shutil import socket import subprocess import sys +import tarfile import tempfile import time import unittest @@ -249,5 +252,102 @@ class SteamVrHelperTest(unittest.TestCase): self.assertTrue(xr.is_symlink()) + def package(self): + payload = io.BytesIO() + with tarfile.open(fileobj=payload, mode='w') as archive: + body = b'#!/bin/sh\n' + os.urandom(6000).hex().encode() + b'\n' + entry = tarfile.TarInfo('opt/steamvr/bin/linuxarm64/vrstartup') + entry.size = len(body) + entry.mode = 0o755 + archive.addfile(entry, io.BytesIO(body)) + data = payload.getvalue() + source = self.root / 'package' + source.write_bytes(data) + text = self.helper.read_text() + for old, new in (('PKG_SIZE=571309057', 'PKG_SIZE=%d' % len(data)), + ('PKG_SHA256=90a600775eb3e8856dc475f83166a93a290a0b1f3b4e82a69c8e2a8f06b9ddc9', + 'PKG_SHA256=' + hashlib.sha256(data).hexdigest())): + self.assertIn(old, text) + text = text.replace(old, new) + self.helper.write_text(text, encoding='utf-8') + self.executable(self.commands / 'zstd', 'exec cat "$2"') + self.environment['STEAMVR'] = str(self.root / 'fresh/steamvr') + return source, len(data) + + def fake_curl(self, source, chunk, exit_code): + self.executable(self.commands / 'curl', '\n'.join(( + 'out=; previous=', + 'for argument in "$@"; do [ "$previous" = -o ] && out=$argument; previous=$argument; done', + 'echo "$*" >> %s' % (self.root / 'curl-calls'), + 'have=$(stat -c %s "$out" 2>/dev/null || echo 0)', + 'tail -c +$((have + 1)) %s | head -c %d >> "$out"' % (source, chunk), + '[ "$(stat -c %%s "$out")" -lt "$(stat -c %%s %s)" ] || exit 0' % source, + 'echo "curl: (28) Operation too slow" >&2', + 'exit %d' % exit_code, + ))) + + def install(self): + return subprocess.run(['sh', str(self.helper), 'install'], env=self.environment, + capture_output=True, text=True, timeout=30) + + def install_log(self): + return (self.home / '.local/share/Steam/logs/droiddeck-steamvr-install.log').read_text() + + def test_install_resumes_a_stalled_download_and_logs_each_step(self): + source, size = self.package() + self.fake_curl(source, size // 8 + 1, 28) + result = self.install() + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertTrue((self.root / 'fresh/steamvr/bin/linuxarm64/vrstartup').is_file()) + calls = (self.root / 'curl-calls').read_text().splitlines() + self.assertEqual(len(calls), 8) + for call in calls: + self.assertIn('--speed-limit 1024 --speed-time 60', call) + self.assertIn('--connect-timeout 30', call) + self.assertIn('-C -', call) + log = self.install_log() + self.assertIn('curl exited 28', log) + self.assertIn('curl: (28) Operation too slow', log) + self.assertIn('curl exited 0 with %d of %d bytes' % (size, size), log) + self.assertIn('checking sha256', log) + self.assertIn('unpacking SteamVR', log) + self.assertIn('SteamVR installed in', log) + self.assertFalse((self.runtime / 'droiddeck-steamvr/installing').exists()) + self.assertFalse((self.home / '.cache/droiddeck-vr/steamvr.pkg.tar.zst').exists()) + + def test_install_gives_up_when_the_download_makes_no_progress(self): + source, size = self.package() + self.fake_curl(source, 0, 28) + result = self.install() + self.assertEqual(result.returncode, 1) + self.assertEqual(len((self.root / 'curl-calls').read_text().splitlines()), 5) + self.assertIn('download failed', self.install_log()) + self.assertFalse((self.runtime / 'droiddeck-steamvr/installing').exists()) + self.assertFalse((self.root / 'fresh/steamvr').exists()) + + def test_install_removes_a_corrupt_download(self): + source, size = self.package() + self.fake_curl(source, size, 0) + corrupt = self.home / '.cache/droiddeck-vr/steamvr.pkg.tar.zst' + corrupt.parent.mkdir(parents=True) + corrupt.write_bytes(b'x' * size) + result = self.install() + self.assertEqual(result.returncode, 1) + self.assertFalse((self.root / 'curl-calls').exists()) + self.assertIn('checksum mismatch', self.install_log()) + self.assertFalse(corrupt.exists()) + + def test_status_reports_download_progress(self): + partial = self.home / '.cache/droiddeck-vr/steamvr.pkg.tar.zst' + partial.parent.mkdir(parents=True) + partial.write_bytes(b'x' * 1234) + result = subprocess.run(['sh', str(self.helper), 'status'], env=self.environment, + capture_output=True, text=True, timeout=5, check=True) + self.assertIn('downloaded=1234 size=571309057', result.stdout) + partial.unlink() + result = subprocess.run(['sh', str(self.helper), 'status'], env=self.environment, + capture_output=True, text=True, timeout=5, check=True) + self.assertIn('downloaded=0 size=571309057', result.stdout) + if __name__ == '__main__': unittest.main()