| 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. |
| 6 | set -euo pipefail |
| 7 | |
| 8 | out=${1:?output png} |
| 9 | shift |
| 10 | size=1280x800 |
| 11 | wait_seconds=10 |
| 12 | window_size= |
| 13 | settle_seconds=1 |
| 14 | binary="$(cargo metadata --no-deps --format-version 1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["target_directory"])')/debug/teams" |
| 15 | while [[ $# -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 |
| 25 | done |
| 26 | |
| 27 | display_number=$((100 + RANDOM % 800)) |
| 28 | Xvfb ":$display_number" -screen 0 "${size}x24" -nolisten tcp >/dev/null 2>&1 & |
| 29 | xvfb_pid=$! |
| 30 | app_pid= |
| 31 | cleanup() { |
| 32 | [[ -n $app_pid ]] && kill "$app_pid" 2>/dev/null || true |
| 33 | kill "$xvfb_pid" 2>/dev/null || true |
| 34 | } |
| 35 | trap cleanup EXIT |
| 36 | sleep 1 |
| 37 | |
| 38 | export DISPLAY=":$display_number" |
| 39 | unset WAYLAND_DISPLAY |
| 40 | export VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.json |
| 41 | "$binary" --demo >/dev/null 2>&1 & |
| 42 | app_pid=$! |
| 43 | sleep "$wait_seconds" |
| 44 | |
| 45 | if [[ -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 |
| 64 | fi |
| 65 | |
| 66 | for 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 |
| 93 | done |
| 94 | sleep "$settle_seconds" |
| 95 | if [[ -n $window_size ]]; then |
| 96 | import -window root -crop "${window_size}+0+0" +repage "$out" |
| 97 | else |
| 98 | import -window root "$out" |
| 99 | fi |
| 100 | echo "$out" |