From 41186dad1e1d33b0ec91661b33d95431f769da91 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Wed, 20 Nov 2024 13:01:24 +0000
Subject: [PATCH] launch-options.sh: Wrap GUIs in reaper and
 steam-launch-wrapper

In particular this makes the Gamescope session on Steam Deck behave as
if they are part of the actual game, allowing the window to be displayed.
It isn't controller-friendly (using the touchscreen is necessary)
but it's better than nothing.

steamrt/tasks#602

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 bin/launch-options.sh | 41 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 3 deletions(-)

diff --git a/bin/launch-options.sh b/bin/launch-options.sh
index 7af90d66b..4bbcd0010 100755
--- a/bin/launch-options.sh
+++ b/bin/launch-options.sh
@@ -32,6 +32,41 @@ log () {
     echo "${me-steam-runtime-launch-options}[$$]: $*" >&2 || :
 }
 
+# run_like_game [exec] COMMAND ARGS
+# Run COMMAND ARGS, but wrap it as if it was the actual game for the
+# purposes of systemd resource control, Gamescope metadata and so on.
+#
+# If the first argument is "exec", replace the shell with the COMMAND
+# instead of running it as a subprocess.
+run_like_game () {
+    use_exec=
+
+    if [ "$1" = exec ]; then
+        use_exec=yes
+        shift
+    fi
+
+    app_id="${STEAM_COMPAT_APP_ID-${SteamAppId-}}"
+
+    if [ -n "$app_id" ]; then
+        if [ -x ~/.steam/root/ubuntu12_32/reaper ]; then
+            set -- ~/.steam/root/ubuntu12_32/reaper SteamLaunch AppId="$app_id" -- "$@"
+        else
+            log "warning: ~/.steam/root/ubuntu12_32/reaper not found"
+        fi
+    else
+        log "warning: Steam app ID not found in environment"
+    fi
+
+    if [ -x ~/.steam/root/ubuntu12_32/steam-launch-wrapper ]; then
+        set -- ~/.steam/root/ubuntu12_32/steam-launch-wrapper -- "$@"
+    else
+        log "warning: ~/.steam/root/ubuntu12_32/steam-launch-wrapper not found"
+    fi
+
+    ${use_exec:+exec} "$@"
+}
+
 main () {
     me="$(readlink -f "$0")"
     here="${me%/*}"
@@ -107,7 +142,7 @@ main () {
 
 $result"
             fi
-            ~/.steam/root/steam-dialog --error --width=500 --text="$result"
+            run_like_game ~/.steam/root/steam-dialog --error --width=500 --text="$result"
             exit 125
         fi
 
@@ -139,11 +174,11 @@ $result"
             text="$result"
         fi
 
-        "$run" "${STEAM_ZENITY:-zenity}" --error --width 500 --text "$text" || :
+        run_like_game "$run" "${STEAM_ZENITY:-zenity}" --error --width 500 --text "$text" || :
         exit 125
     fi
 
-    if command_line="$(exec "$script" --command-line-fd=1 "$@")" && [ -n "$command_line" ]; then
+    if command_line="$(run_like_game exec "$script" --command-line-fd=1 "$@")" && [ -n "$command_line" ]; then
         log "info: exec $command_line"
         eval "exec $command_line" || exit 125
     else
-- 
GitLab