mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
From Bannerlator's feat/linux-gamescope-runtime since this repo was cut: - Session teardown (4509d788): proot was SIGKILLed, which never lets its --kill-on-exit cleanup run; its tracees lived on with no tracer, every seccomp-trapped syscall failing with ENOSYS, spinning at a full core for over an hour. Now SIGTERM, a grace for proot's own cleanup, SIGKILL only if it will not go, then a sweep of the tree it had — snapshotted first, each pid checked against its start time. - The Steam client's controller interposer is its own source (fakeinput_steam.cpp, 28922e7c), separate from the one preloaded into Wine. - The seeder runs on a beat so a prefix the client creates mid-session is covered before its install scripts run (6ee86bff). - GE-Proton and proton-cachyos as compatibility tools: the registrar bannerlator-proton-extra and the compat script's adopt_extras (03867656). - space.c in the session shim: a library's own free space, not the phone's. The env-vs-args ordering bug (98d38931) does not apply here: nothing is added after the script name.
237 lines
11 KiB
Bash
Executable File
237 lines
11 KiB
Bash
Executable File
#!/bin/bash
|
|
# The Linux runtime's session. Bannerlator execs this under proot with the output size and mode;
|
|
# the first pass starts gamescope, which execs this script again inside it for the program:
|
|
# bannerlator-session desktop the file manager
|
|
# bannerlator-session run <path> a Linux program (AppImage, script or ELF)
|
|
# bannerlator-session steam [steam-url] the native Steam client
|
|
# BL_WIDTH / BL_HEIGHT size the output, BL_FPS limits it, BL_LOG collects everything said here.
|
|
set -u
|
|
export HOME=${HOME:-/root}
|
|
mode=${1:-desktop}
|
|
shift || true
|
|
# One line per milestone, greppable as "== STEP", so a first run reads as a narrative rather than
|
|
# something to be reconstructed from Steam's own chatter. The app shows these while it waits.
|
|
step() { echo "== STEP $(date -u +%H:%M:%S) $*"; }
|
|
if [ -n "${BL_LOG:-}" ] && [ -z "${BL_INSIDE:-}" ]; then
|
|
mkdir -p "$(dirname "$BL_LOG")" 2>/dev/null
|
|
# The client alone writes megabytes an hour; one earlier log is kept, not every session's.
|
|
if [ "$(stat -c %s "$BL_LOG" 2>/dev/null || echo 0)" -gt 8388608 ]; then
|
|
mv -f "$BL_LOG" "$BL_LOG.1"
|
|
fi
|
|
exec >>"$BL_LOG" 2>&1
|
|
echo "== bannerlator-session $(date) mode=$mode $*"
|
|
echo "== uid=$(id -u) size=${BL_WIDTH:-?}x${BL_HEIGHT:-?} fps=${BL_FPS:-0} icd=${VK_ICD_FILENAMES:-none}"
|
|
echo "== gamescope: $(gamescope --version 2>&1 | head -1)"
|
|
echo "== rootfs: $(cat /.version 2>/dev/null || echo unversioned)"
|
|
fi
|
|
|
|
# Until the runtime is stable, Steam's own logs are copied beside the session log at exit so a
|
|
# failed launch can be handed over as one folder.
|
|
collect_steam_logs() {
|
|
[ -n "${BL_DEBUG_DIR:-}" ] || return 0
|
|
local steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
|
|
mkdir -p "$BL_DEBUG_DIR" 2>/dev/null || return 0
|
|
[ -d "$steam_root/logs" ] && cp -r "$steam_root/logs" "$BL_DEBUG_DIR/steam-logs" 2>/dev/null
|
|
[ -f "$HOME/.steam/steam/logs/steamwebhelper.log" ] && cp "$HOME/.steam/steam/logs/steamwebhelper.log" "$BL_DEBUG_DIR/" 2>/dev/null
|
|
[ -f "$steam_root/steam.pid" ] && cp "$steam_root/steam.pid" "$BL_DEBUG_DIR/" 2>/dev/null
|
|
echo "== steam logs copied to $BL_DEBUG_DIR"
|
|
}
|
|
export XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
|
|
export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
|
|
export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
|
|
mkdir -p "$XDG_DATA_HOME" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME"
|
|
|
|
if [ -z "${BL_INSIDE:-}" ]; then
|
|
# The compositor may still be starting, and a socket left by an earlier session is not
|
|
# a compositor: probe until a connection succeeds.
|
|
for _ in $(seq 1 100); do
|
|
wayland-info >/dev/null 2>&1 && break
|
|
sleep 0.1
|
|
done
|
|
width=${BL_WIDTH:-1280}
|
|
height=${BL_HEIGHT:-720}
|
|
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$width" -h "$height")
|
|
# -r is what gamescope advertises as the refresh rate, and what a game reads as the display's.
|
|
# Left unset it defaults to 60, so a 120 Hz panel offers only 60 Hz in game settings. The FPS
|
|
# limiter wins when it is on (it is a deliberate cap); otherwise use the panel's own rate.
|
|
if [ "${BL_FPS:-0}" -gt 0 ]; then
|
|
args+=(-r "$BL_FPS")
|
|
elif [ "${BL_REFRESH:-0}" -gt 0 ]; then
|
|
args+=(-r "$BL_REFRESH")
|
|
fi
|
|
[ "$mode" = steam ] && args+=(-e)
|
|
export BL_INSIDE=1
|
|
exec gamescope "${args[@]}" -- "$0" "$mode" "$@"
|
|
fi
|
|
|
|
# gamescope manages X windows: toolkits go through Xwayland, not gamescope's own Wayland socket.
|
|
export GDK_BACKEND=x11 QT_QPA_PLATFORM=xcb SDL_VIDEODRIVER=x11
|
|
# gamescope's reaper waits for every process left under it before the compositor comes down. A
|
|
# process a game left behind - a Wine service blocked on a server that is already gone - is
|
|
# reparented there when the client exits, and the screen stays black on it for good. Nothing of
|
|
# the session outlives the program it was started for.
|
|
reap_orphans() {
|
|
local round p stat pid left
|
|
for round in 1 2 3 4 5; do
|
|
left=
|
|
for p in /proc/[0-9]*; do
|
|
pid=${p#/proc/}
|
|
[ "$pid" = "$$" ] && continue
|
|
stat=$(cat "$p/stat" 2>/dev/null) || continue
|
|
stat=${stat##*) }
|
|
set -- $stat
|
|
[ "${2:-}" = "$PPID" ] && left="$left $pid"
|
|
done
|
|
[ -n "$left" ] || return 0
|
|
echo "== leaving behind:$left"
|
|
kill -9 $left 2>/dev/null
|
|
sleep 0.2
|
|
done
|
|
}
|
|
|
|
case $mode in
|
|
desktop)
|
|
exec pcmanfm
|
|
;;
|
|
run)
|
|
target=${1:?program path}
|
|
shift
|
|
case ${target,,} in
|
|
*.appimage)
|
|
export APPIMAGE_EXTRACT_AND_RUN=1
|
|
chmod +x "$target" 2>/dev/null
|
|
exec "$target" "$@"
|
|
;;
|
|
*.sh)
|
|
exec bash "$target" "$@"
|
|
;;
|
|
*)
|
|
chmod +x "$target" 2>/dev/null
|
|
cd "$(dirname "$target")" && exec "$target" "$@"
|
|
;;
|
|
esac
|
|
;;
|
|
steam)
|
|
step "checking the Steam client (first run downloads it, ~1-2 min with nothing on screen)"
|
|
bannerlator-steam-install || exit $?
|
|
step "Steam client ready"
|
|
steam_root=$XDG_DATA_HOME/Steam
|
|
# Register the card library (bound at /mnt/bannerlator-sd) with the client - internal storage is
|
|
# its main library already - and the ARM64 Proton that lets it launch the Windows ones.
|
|
# A request left in ~/.bl-proton-extra installs a third-party Proton before the client starts,
|
|
# so it is in the Compatibility dropdown the first time the user looks. One per line, each the
|
|
# arguments to bannerlator-proton-extra: "ge", "cachyos", "ge --tag GE-Proton11-7", or a URL.
|
|
# Hundreds of megabytes each, so it is only ever done on request, and the line is dropped once
|
|
# it has succeeded - a failed download stays and is retried next session.
|
|
if [ -s "$HOME/.bl-proton-extra" ]; then
|
|
_bl_left=$HOME/.bl-proton-extra.left
|
|
: > "$_bl_left"
|
|
while read -r _bl_extra || [ -n "$_bl_extra" ]; do
|
|
case ${_bl_extra# } in ""|"#"*) continue ;; esac
|
|
step "installing Proton '$_bl_extra' (large download, several minutes)"
|
|
if bannerlator-proton-extra "$steam_root" $_bl_extra; then
|
|
step "installed Proton '$_bl_extra'"
|
|
else
|
|
step "Proton '$_bl_extra' failed; will retry next session"
|
|
echo "$_bl_extra" >> "$_bl_left"
|
|
fi
|
|
done < "$HOME/.bl-proton-extra"
|
|
mv -f "$_bl_left" "$HOME/.bl-proton-extra"
|
|
fi
|
|
# steam:// URLs left in ~/.bl-steam-urls are handed to the client on its command line. The app
|
|
# writes one when the user asks for a Proton that is one of Valve's own depots rather than a
|
|
# tarball: those are not files anywhere, the client fetches them like any other app. One per
|
|
# line, and the file is cleared once they have been passed on - the client either acts on a URL
|
|
# or it does not, and repeating it every session would re-ask forever.
|
|
if [ -s "$HOME/.bl-steam-urls" ]; then
|
|
while read -r _bl_url || [ -n "$_bl_url" ]; do
|
|
case ${_bl_url# } in steam://*) ;; *) continue ;; esac
|
|
step "asking the client for $_bl_url"
|
|
set -- "$@" "$_bl_url"
|
|
done < "$HOME/.bl-steam-urls"
|
|
rm -f "$HOME/.bl-steam-urls"
|
|
fi
|
|
step "registering the app's game library with Steam"
|
|
bannerlator-steam-library "$steam_root" || true
|
|
step "setting up the ARM64 Proton compatibility tool"
|
|
bannerlator-steam-compat "$steam_root" || true
|
|
# Steam runs a game's shared redistributables inside its prefix on first launch; marking
|
|
# them as already run keeps those x86 installers from starting under emulation at all.
|
|
bannerlator-seed-redists "$steam_root" || true
|
|
# The compatibility tool runs Valve's arm64 Proton, which the client fetches like any depot.
|
|
# Asked for on the command line the first time, so a Windows title has it before it is needed.
|
|
if [ ! -f "$steam_root/steamapps/appmanifest_4427310.acf" ]; then
|
|
step "asking the client to fetch the ARM64 Proton"
|
|
set -- "$@" steam://install/4427310
|
|
fi
|
|
# The tool is registered before the client starts and finds the depot at launch time, so
|
|
# nothing here waits for a download. What can still change under a running client is which
|
|
# titles are installed: a title installed this session gets the client's own per-app Proton
|
|
# choice, which it cannot start. Refresh the per-app entries every so often; the registrar is
|
|
# idempotent, so a repeat costs nothing.
|
|
# The seeder has the same problem and a worse consequence. It marks the shared redistributables
|
|
# as already run in each prefix that exists, and a game's prefix is not created until the
|
|
# client launches it - so a title played for the first time this session was never covered, and
|
|
# its install scripts run for real. That is exactly the hang the seeder exists to prevent: Need
|
|
# for Speed Payback sat on "Running install script" with vcredist_x86.exe blocked in pipe_read,
|
|
# holding both ends of a pipe nobody would write to, while the client waited for it to exit.
|
|
# Seeding again on the same beat covers a prefix the moment it appears.
|
|
# The seeder runs on a shorter beat than the registrar because it is racing something. The
|
|
# client creates a prefix when it launches a title and runs that title's install scripts
|
|
# moments later, so the seeder has only that gap to mark the redistributables as already run.
|
|
# It stamps each prefix it has covered and skips it thereafter, so the extra passes cost
|
|
# almost nothing.
|
|
(
|
|
beat=0
|
|
for _ in $(seq 1 720); do
|
|
sleep 5
|
|
bannerlator-seed-redists "$steam_root" >/dev/null 2>&1 || true
|
|
beat=$((beat + 1))
|
|
if [ "$((beat % 3))" -eq 0 ]; then
|
|
bannerlator-steam-compat "$steam_root" >/dev/null 2>&1 || true
|
|
fi
|
|
done
|
|
) &
|
|
# Debug aid while the runtime is being brought up: an app id dropped in ~/.bl-autolaunch is
|
|
# handed to the client as a rungameid URL, so a launch can be reproduced without touching the
|
|
# screen. Removed once the runtime is stable.
|
|
if [ -s "$HOME/.bl-autolaunch" ]; then
|
|
_bl_app=$(cat "$HOME/.bl-autolaunch")
|
|
# A bare app id runs the game; a full steam:// URL is passed through, which is how a
|
|
# validate or an install is driven without touching the screen.
|
|
case $_bl_app in
|
|
steam://*) _bl_url=$_bl_app ;;
|
|
*) _bl_url=steam://rungameid/$_bl_app ;;
|
|
esac
|
|
echo "== autolaunch $_bl_url"
|
|
set -- "$@" "$_bl_url"
|
|
fi
|
|
export STEAM_RUNTIME=1
|
|
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
|
|
export SDL_VIDEO_X11_DGAMOUSE=0
|
|
# Native-Linux x86 games go through Valve's FEX compat tool, which runs FEX with FEX_PORTABLE=1
|
|
# - that makes FEX look for its config next to the binary, while the tool writes one into the
|
|
# app's compatdata instead. FEX therefore starts with no rootfs, every x86_64 library lookup
|
|
# falls through to the host, and the guest shell dies on libreadline before the game is reached.
|
|
# FEX_ROOTFS names it outright and is read whatever the config situation.
|
|
[ -d /usr/share/guestos/fex-mesa ] && export FEX_ROOTFS=/usr/share/guestos/fex-mesa
|
|
# The client exits 42 when it has updated itself and wants a fresh start (steam.sh does this).
|
|
while :; do
|
|
step "starting the Steam client"
|
|
"$steam_root/steamrtarm64/steam" -gamepadui -clientbeta publicbeta \
|
|
-no-cef-sandbox -cef-force-gpu -cef-ozone-platform=x11 \
|
|
-cef-use-gl=angle -cef-use-angle=vulkan "$@"
|
|
rc=$?
|
|
echo "== steam exited rc=$rc"
|
|
collect_steam_logs
|
|
[ "$rc" -eq 42 ] || break
|
|
done
|
|
reap_orphans
|
|
exit "$rc"
|
|
;;
|
|
*)
|
|
echo "bannerlator-session: unknown mode '$mode'" >&2
|
|
exit 64
|
|
;;
|
|
esac
|