diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 688d0c19b..df89589e5 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -227,7 +227,9 @@ if [ -z "${BL_INSIDE:-}" ]; then echo "== gamescope: games at ${nested}x${height} stretched to the ${width}x${height} output" fi fi - if [ "$mode" = steam ]; then + # Off unless Performance asks for it. Compositing the cursor here adds a second + # plane on the phone's compositor; a controller-driven cursor is the reason to turn it on. + if [ "$mode" = steam ] && [ "${BL_CURSOR_COMPOSITE:-0}" = 1 ]; then export gamescope_cursor_composite=2 fi args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$nested" -h "$height") @@ -475,19 +477,30 @@ case $mode in bl_zram_scan step "zRAM: done ($(bl_zram_usage))" } + # The game process exists for the whole Proton launch. Compressing then stalls the + # launch, so the clock starts only once the game window has focus, and waits 20 s more. + bl_game_focused() { + [ -n "${BL_LAUNCH_DIR:-}" ] || return 1 + local id + id=$(sed -n 's/.*"focusedApp"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' \ + "$BL_LAUNCH_DIR/agent/focus.json" 2>/dev/null | head -1) + [ -n "$id" ] && [ "$id" != 769 ] + } _bl_since=-1 _bl_packed=-1 while kill -0 $$ 2>/dev/null; do bl_zram_scan - if [ "$_bl_game" = 1 ]; then + if [ "$_bl_game" = 1 ] && bl_game_focused; then [ "$_bl_since" -ge 0 ] || _bl_since=$SECONDS if [ $((SECONDS - _bl_since)) -ge 20 ] && [ "${#_bl_pids[@]}" -gt 0 ] && { [ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 300 ]; }; then bl_zram_send 1 "compressing the client" _bl_packed=$SECONDS fi - elif [ "$_bl_since" -ge 0 ]; then + elif [ "$_bl_game" != 1 ]; then [ "$_bl_packed" -lt 0 ] || bl_zram_send 0 "game closed, reading the client back" _bl_since=-1 _bl_packed=-1 + else + _bl_since=-1 fi nap 5 done diff --git a/tools/tests/test_client_zram_focus.py b/tools/tests/test_client_zram_focus.py new file mode 100644 index 000000000..52c48b8be --- /dev/null +++ b/tools/tests/test_client_zram_focus.py @@ -0,0 +1,39 @@ +from pathlib import Path +import subprocess +import unittest + + +SCRIPT = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session' + + +class ClientZramFocusTest(unittest.TestCase): + def test_refocus_restarts_the_delay_and_game_exit_restores_a_packed_client(self): + source = SCRIPT.read_text() + start = source.index(' if [ "$_bl_game" = 1 ] && bl_game_focused; then') + stop = source.index(' nap 5', start) + body = source[start:stop] + harness = ''' +_bl_since=-1 _bl_packed=-1 +_bl_pids=(123) +bl_game_focused() { [ "$focused" = 1 ]; } +bl_zram_send() { echo "send:$1"; } +tick() { + SECONDS=$1 _bl_game=$2 focused=$3 +''' + body + ''' +} +tick 0 1 1 +tick 5 1 0 +tick 30 1 1 +echo "refocus:$_bl_packed" +tick 49 1 1 +tick 50 1 1 +tick 55 1 0 +tick 60 0 0 +''' + result = subprocess.run(['bash', '-c', harness], capture_output=True, text=True, timeout=10) + self.assertEqual(0, result.returncode, result.stderr) + self.assertEqual(['refocus:-1', 'send:1', 'send:0'], result.stdout.splitlines()) + + +if __name__ == '__main__': + unittest.main()