BlueGit

ict rusty-teams

Files

rusty-teams / scripts / headless-shot.sh

headless-shot.sh

headless-shot.sh 3.2 KiB Download Raw
1#!/usr/bin/env bash
2# Usage: scripts/headless-shot.sh OUT.png [--size 1280x800] [--window WxH] [--wait 10] [--settle 1] [--bin TARGET_DIR/debug/teams] [--] [xdotool command]...
3# Runs the Linux build with --demo on a private Xvfb display and saves a screenshot.
4# Example: scripts/headless-shot.sh shot.png -- "mousemove 450 740" "click 1" "type hello" "paste ```\\ncode\\n```"
5# `type` cannot send a backtick (X11 treats it as a dead key and input stops); use `paste`, which expands \n.
6set -euo pipefail
7
8out=${1:?output png}
9shift
10size=1280x800
11wait_seconds=10
12window_size=
13settle_seconds=1
14binary="$(cargo metadata --no-deps --format-version 1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["target_directory"])')/debug/teams"
15while [[ $# -gt 0 ]]; do
16 case $1 in
17 --size) size=$2; shift 2 ;;
18 --window) window_size=$2; shift 2 ;;
19 --settle) settle_seconds=$2; shift 2 ;;
20 --wait) wait_seconds=$2; shift 2 ;;
21 --bin) binary=$2; shift 2 ;;
22 --) shift; break ;;
23 *) break ;;
24 esac
25done
26
27display_number=$((100 + RANDOM % 800))
28Xvfb ":$display_number" -screen 0 "${size}x24" -nolisten tcp >/dev/null 2>&1 &
29xvfb_pid=$!
30app_pid=
31cleanup() {
32 [[ -n $app_pid ]] && kill "$app_pid" 2>/dev/null || true
33 kill "$xvfb_pid" 2>/dev/null || true
34}
35trap cleanup EXIT
36sleep 1
37
38export DISPLAY=":$display_number"
39unset WAYLAND_DISPLAY
40export VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.json
41"$binary" --demo >/dev/null 2>&1 &
42app_pid=$!
43sleep "$wait_seconds"
44
45if [[ -n $window_size ]]; then
46 window_width=${window_size%x*}
47 window_height=${window_size#*x}
48 window_id=$(xdotool search --sync --onlyvisible --pid "$app_pid" --name '^Rusty Teams$' | head -n 1)
49 resized=false
50 for _ in 1 2 3 4 5 6 7 8; do
51 xdotool windowmove "$window_id" 0 0
52 xdotool windowsize "$window_id" "$window_width" "$window_height"
53 sleep 1
54 eval "$(xdotool getwindowgeometry --shell "$window_id")"
55 if [[ $WIDTH == "$window_width" && $HEIGHT == "$window_height" ]]; then
56 resized=true
57 break
58 fi
59 done
60 if [[ $resized != true ]]; then
61 echo "window never reached ${window_size}, last geometry ${WIDTH}x${HEIGHT}" >&2
62 exit 1
63 fi
64fi
65
66for command in "$@"; do
67 subcommand=${command%% *}
68 rest=${command#"$subcommand"}
69 rest=${rest# }
70 if [[ $subcommand == click || $subcommand == key ]]; then
71 # Xvfb has no window manager: keyboard focus only follows an explicit windowfocus.
72 eval "$(xdotool getmouselocation --shell)"
73 xdotool windowfocus --sync "$WINDOW"
74 fi
75 if [[ $subcommand == type ]]; then
76 xdotool type --delay 20 -- "$rest"
77 elif [[ $subcommand == key && $rest == *+* ]]; then
78 # GPUI drops the key when the modifier and the key arrive in the same instant.
79 modifier=${rest%%+*}
80 xdotool keydown "$modifier"
81 sleep 0.15
82 xdotool key "${rest#*+}"
83 sleep 0.15
84 xdotool keyup "$modifier"
85 elif [[ $subcommand == paste ]]; then
86 printf '%b' "$rest" | xclip -selection clipboard
87 xdotool key ctrl+v
88 else
89 read -r -a arguments <<<"$rest"
90 xdotool "$subcommand" "${arguments[@]}"
91 fi
92 sleep 0.4
93done
94sleep "$settle_seconds"
95if [[ -n $window_size ]]; then
96 import -window root -crop "${window_size}+0+0" +repage "$out"
97else
98 import -window root "$out"
99fi
100echo "$out"