fix: added games are handed to Steam right away and retried only if they never take focus (#994)

This commit is contained in:
Resul Övür authored and GitHub committed 2026-10-18 14:59:39 -04:00
1 parent a97a7d9058
commit 8178bf5099
2 files changed
+83 -84

No files matched your search

@@ -593,41 +593,37 @@ case $mode in
done < <(xprop -spy -root "${atoms[@]}" 2>/dev/null)
) &
steam_hold_launch() {
_bl_launch_ready=
local game=$1 shortcut= waited=0 status=0
_bl_focused() { xprop -root GAMESCOPE_FOCUSED_APP 2>/dev/null | sed -n 's/.*= *//p'; }
_bl_hand_off() {
printf '%s\n' "$game" > "$BL_LAUNCH_DIR/steam-game.tmp" && mv -f "$BL_LAUNCH_DIR/steam-game.tmp" "$BL_LAUNCH_DIR/steam-game"
}
_bl_end=$((SECONDS + 120))
until [ "$(xprop -root GAMESCOPE_FOCUSED_APP 2>/dev/null | sed -n 's/.*= *//p')" = 769 ] || [ "$SECONDS" -ge "$_bl_end" ]; do
until [ "$(_bl_focused)" = 769 ] || [ "$SECONDS" -ge "$_bl_end" ]; do
sleep 1
done
# With a large library the overview can stay missing for 30+ minutes while Steam already takes the launch URL.
_bl_overview_status=1
_bl_overview_tries=0
while [ "$_bl_overview_tries" -lt 2 ]; do
if /usr/local/bin/droiddeck-agent --wait-steam-shortcut-overview "$1" 45; then
_bl_overview_status=0
else
_bl_overview_status=$?
fi
if [ "$_bl_overview_status" -ne 1 ]; then
break
fi
_bl_overview_tries=$((_bl_overview_tries + 1))
echo "== steam: shortcut overview not ready yet (attempt $_bl_overview_tries of 2)"
sleep 2
# Shortcut CGameIDs are 64-bit; Steam app ids fit in 32 bits and keep the old short delay.
[ "${#game}" -gt 10 ] && shortcut=1
[ -n "$shortcut" ] || sleep 4
echo "== steam: interface up; launching the requested game $game"
_bl_hand_off
[ -n "$shortcut" ] || return 0
# Steam can drop a shortcut launch it gets before the shortcut's overview exists (large libraries).
# Hand it over again only if the game never took focus, so a launch that worked is not started twice.
while [ "$(_bl_focused)" = 769 ] && [ "$waited" -lt 20 ]; do
sleep 1
waited=$((waited + 1))
done
if [ "$_bl_overview_status" -eq 0 ]; then
echo "== steam: shortcut overview ready; launching the requested game $1"
_bl_launch_ready=1
elif [ "$_bl_overview_status" -eq 3 ]; then
echo "== steam: not a shortcut CGameID; keeping the existing launch path"
sleep 4
_bl_launch_ready=1
[ "$(_bl_focused)" = 769 ] || return 0
echo "== steam: the game is not in front after about 20 s; waiting for the shortcut overview"
/usr/local/bin/droiddeck-agent --wait-steam-shortcut-overview "$game" 45 || status=$?
if [ "$status" -eq 3 ]; then
echo "== steam: not a shortcut CGameID; not launching again"
elif [ "$(_bl_focused)" = 769 ]; then
echo "== steam: launching the requested game again $game (overview status $status)"
_bl_hand_off
else
echo "== steam: shortcut overview not ready after about 90 s; launching the requested game anyway $1"
_bl_launch_ready=1
fi
if [ "$_bl_launch_ready" = 1 ]; then
echo "== steam: interface up; launching the requested game $1"
printf '%s\n' "$1" > "$BL_LAUNCH_DIR/steam-game.tmp" && mv -f "$BL_LAUNCH_DIR/steam-game.tmp" "$BL_LAUNCH_DIR/steam-game"
echo "== steam: the game took focus while waiting; not launching again"
fi
}
if [ -n "${BL_LAUNCH_DIR:-}" ] && [ -z "${BL_DESKTOP:-}" ]; then
+57 -54
View File
@@ -1,4 +1,4 @@
"""Cold shortcut launches wait for Steam's CGameID-to-app overview mapping."""
"""Requested Steam games are handed over as soon as the interface is up; a shortcut is handed over again only if it never took focus."""
import os
from pathlib import Path
import subprocess
@@ -12,6 +12,7 @@ START = SOURCE.index(" steam_hold_launch() {")
END = SOURCE.index(' if [ -s "$HOME/.bl-autolaunch" ]; then', START)
assert START >= 0
LAUNCH_BLOCK = textwrap.dedent(SOURCE[START:END])
SHORTCUT = str((3551182975 << 32) | 0x02000000)
class SteamColdLaunchTest(unittest.TestCase):
@@ -26,93 +27,95 @@ class SteamColdLaunchTest(unittest.TestCase):
self.agent_args = self.root / "agent-args"
self.sleep_args = self.root / "sleep-args"
self.agent_count = self.root / "agent-count"
self.focus_count = self.root / "focus-count"
# FOCUS lists the focused app per xprop call; the last value repeats.
xprop = self.bin / "xprop"
xprop.write_text("#!/bin/sh\necho 'GAMESCOPE_FOCUSED_APP(CARDINAL) = 769'\n")
xprop.write_text(
"#!/bin/sh\n"
"n=$(cat \"$FOCUS_COUNT\" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > \"$FOCUS_COUNT\"\n"
"i=0; last=769\n"
"for f in $FOCUS; do i=$((i + 1)); last=$f; [ \"$i\" -eq \"$n\" ] && break; done\n"
"echo \"GAMESCOPE_FOCUSED_APP(CARDINAL) = $last\"\n")
xprop.chmod(0o755)
agent = self.bin / "droiddeck-agent"
agent.write_text(
"#!/bin/sh\n"
"printf '%s\\n' \"$@\" > \"$AGENT_ARGS\"\n"
"printf '%s\n' \"$@\" > \"$AGENT_ARGS\"\n"
"n=$(cat \"$AGENT_COUNT\" 2>/dev/null || echo 0); n=$((n + 1)); echo $n > \"$AGENT_COUNT\"\n"
"i=0; last=1\n"
"for s in $AGENT_STATUS; do i=$((i + 1)); last=$s; [ \"$i\" -eq \"$n\" ] && exit \"$s\"; done\n"
"exit \"$last\"\n")
"exit \"$AGENT_STATUS\"\n")
agent.chmod(0o755)
sleep = self.bin / "sleep"
sleep.write_text("#!/bin/sh\nprintf '%s\\n' \"$@\" >> \"$SLEEP_ARGS\"\n")
sleep.write_text("#!/bin/sh\nprintf '%s\n' \"$@\" >> \"$SLEEP_ARGS\"\n")
sleep.chmod(0o755)
self.block = LAUNCH_BLOCK.replace("/usr/local/bin/droiddeck-agent", str(agent))
def run_launch(self, target, helper_status):
def run_launch(self, target, focus="769", helper_status=0):
env = dict(os.environ, HOME=str(self.root), BL_LAUNCH_DIR=str(self.launch_dir),
PATH=str(self.bin) + os.pathsep + os.environ["PATH"],
AGENT_ARGS=str(self.agent_args), SLEEP_ARGS=str(self.sleep_args),
AGENT_COUNT=str(self.agent_count), AGENT_STATUS=str(helper_status))
script = self.block + "\nprintf 'ARG:%s\\n' \"$@\"\nwait\n"
AGENT_COUNT=str(self.agent_count), AGENT_STATUS=str(helper_status),
FOCUS=focus, FOCUS_COUNT=str(self.focus_count))
script = self.block + "\nprintf 'ARG:%s\n' \"$@\"\nwait\n"
url = target if target.startswith("steam://") else "steam://rungameid/" + target
result = subprocess.run(["bash", "-c", script, "cold-launch", url],
env=env, text=True, capture_output=True, timeout=10)
self.assertEqual(0, result.returncode, result.stderr)
return result
def test_shortcut_overview_gate_releases_original_64_bit_cgameid_without_fixed_sleep(self):
game_id = str((3551182975 << 32) | 0x02000000)
result = self.run_launch(game_id, 0)
self.assertEqual("steam-game", (self.launch_dir / "steam-game").name)
self.assertEqual(game_id, (self.launch_dir / "steam-game").read_text().strip())
self.assertEqual(["--wait-steam-shortcut-overview", game_id, "45"], self.agent_args.read_text().splitlines())
self.assertFalse(self.sleep_args.exists(), result.stdout)
def hand_offs(self, result):
return [l for l in result.stdout.splitlines() if "launching the requested game" in l]
def test_native_app_id_keeps_legacy_bounded_launch_path(self):
game_id = "413150"
self.run_launch(game_id, 3)
self.assertEqual(game_id, (self.launch_dir / "steam-game").read_text().strip())
self.assertEqual(["--wait-steam-shortcut-overview", game_id, "45"], self.agent_args.read_text().splitlines())
def test_a_shortcut_that_takes_focus_is_handed_over_once_without_waiting_for_the_overview(self):
result = self.run_launch(SHORTCUT, focus="769 769 4242")
self.assertEqual(SHORTCUT, (self.launch_dir / "steam-game").read_text().strip())
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertFalse(self.agent_args.exists(), result.stdout)
self.assertEqual(["1"], self.sleep_args.read_text().split())
def test_a_shortcut_that_never_takes_focus_is_handed_over_again_after_the_overview_wait(self):
result = self.run_launch(SHORTCUT, focus="769", helper_status=0)
self.assertEqual(2, len(self.hand_offs(result)), result.stdout)
self.assertIn("launching the requested game again", result.stdout)
self.assertEqual(["--wait-steam-shortcut-overview", SHORTCUT, "45"], self.agent_args.read_text().splitlines())
self.assertEqual(["1"] * 20, self.sleep_args.read_text().split())
def test_a_timed_out_overview_still_gets_one_more_hand_off(self):
result = self.run_launch(SHORTCUT, focus="769", helper_status=1)
self.assertEqual(2, len(self.hand_offs(result)), result.stdout)
self.assertIn("overview status 1", result.stdout)
def test_a_game_that_takes_focus_during_the_overview_wait_is_not_started_twice(self):
focus = " ".join(["769"] * 23) + " 4242"
result = self.run_launch(SHORTCUT, focus=focus, helper_status=0)
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertIn("took focus while waiting", result.stdout)
self.assertNotIn("launching the requested game again", result.stdout)
def test_native_app_id_keeps_the_short_delay_and_is_handed_over_once(self):
result = self.run_launch("413150")
self.assertEqual("413150", (self.launch_dir / "steam-game").read_text().strip())
self.assertEqual(1, len(self.hand_offs(result)), result.stdout)
self.assertEqual("4", self.sleep_args.read_text().strip())
self.assertFalse(self.agent_args.exists())
def test_a_shortcut_whose_overview_never_appears_is_launched_after_a_bounded_wait(self):
game_id = str((3551182975 << 32) | 0x02000000)
result = self.run_launch(game_id, 1)
self.assertEqual(game_id, (self.launch_dir / "steam-game").read_text().strip(), result.stdout)
self.assertEqual(["--wait-steam-shortcut-overview", game_id, "45"], self.agent_args.read_text().splitlines())
self.assertEqual("2", self.agent_count.read_text().strip())
self.assertEqual(["2", "2"], self.sleep_args.read_text().split())
self.assertIn("launching the requested game anyway", result.stdout)
def test_a_late_overview_is_still_waited_for_once_more(self):
game_id = str((3551182975 << 32) | 0x02000000)
result = self.run_launch(game_id, "1 0")
self.assertEqual(game_id, (self.launch_dir / "steam-game").read_text().strip(), result.stdout)
self.assertEqual("2", self.agent_count.read_text().strip())
self.assertEqual(["2"], self.sleep_args.read_text().split())
self.assertIn("attempt 1 of 2", result.stdout)
self.assertNotIn("anyway", result.stdout)
def test_an_agent_failure_other_than_a_timeout_launches_without_retrying(self):
game_id = str((3551182975 << 32) | 0x02000000)
result = self.run_launch(game_id, 2)
self.assertEqual(game_id, (self.launch_dir / "steam-game").read_text().strip(), result.stdout)
self.assertEqual("1", self.agent_count.read_text().strip())
self.assertFalse(self.sleep_args.exists())
def test_a_restart_for_a_game_waits_for_its_overview_too(self):
game_id = str((3551182975 << 32) | 0x02000000)
def test_a_restart_for_a_game_uses_the_same_hand_off(self):
loop = SOURCE[SOURCE.index(' if [ -s "$HOME/.bl-restart-game" ]; then'):SOURCE.index(' if [ -f "$HOME/.bl-restart-once" ]; then')]
self.assertIn('steam_hold_launch "$_bl_again" &', loop)
self.assertNotIn("set -- \"steam://rungameid/", loop)
script = self.block + '\nsteam_hold_launch "%s"\n' % game_id
script = self.block + '\nsteam_hold_launch "%s"\n' % SHORTCUT
env = dict(os.environ, HOME=str(self.root), BL_LAUNCH_DIR=str(self.launch_dir),
PATH=str(self.bin) + os.pathsep + os.environ["PATH"],
AGENT_ARGS=str(self.agent_args), SLEEP_ARGS=str(self.sleep_args),
AGENT_COUNT=str(self.agent_count), AGENT_STATUS="1 0")
AGENT_COUNT=str(self.agent_count), AGENT_STATUS="1",
FOCUS="769", FOCUS_COUNT=str(self.focus_count))
result = subprocess.run(["bash", "-c", script, "restart"], env=env, text=True, capture_output=True, timeout=10, check=True)
self.assertEqual(game_id, (self.launch_dir / "steam-game").read_text().strip(), result.stdout)
self.assertEqual(["--wait-steam-shortcut-overview", game_id, "45"], self.agent_args.read_text().splitlines())
self.assertEqual(SHORTCUT, (self.launch_dir / "steam-game").read_text().strip(), result.stdout)
self.assertEqual(2, len(self.hand_offs(result)), result.stdout)
def test_non_game_steam_urls_remain_on_the_original_startup_arguments(self):
url = "steam://open/library"
result = self.run_launch(url, 0)
result = self.run_launch(url)
self.assertIn("ARG:" + url, result.stdout)
self.assertFalse((self.launch_dir / "steam-game").exists())
self.assertFalse(self.agent_args.exists())