| 1 | #!/usr/bin/env bash |
| 2 | # Usage: scripts/headless-shot.sh OUT.png [--size 1280x800] [--wait 10] [--bin target/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 | binary=target/debug/teams |
| 13 | while [[ $# -gt 0 ]]; do |
| 14 | case $1 in |
| 15 | --size) size=$2; shift 2 ;; |
| 16 | --wait) wait_seconds=$2; shift 2 ;; |
| 17 | --bin) binary=$2; shift 2 ;; |
| 18 | --) shift; break ;; |
| 19 | *) break ;; |
| 20 | esac |
| 21 | done |
| 22 | |
| 23 | display_number=$((100 + RANDOM % 800)) |
| 24 | Xvfb ":$display_number" -screen 0 "${size}x24" -nolisten tcp >/dev/null 2>&1 & |
| 25 | xvfb_pid=$! |
| 26 | app_pid= |
| 27 | cleanup() { |
| 28 | [[ -n $app_pid ]] && kill "$app_pid" 2>/dev/null || true |
| 29 | kill "$xvfb_pid" 2>/dev/null || true |
| 30 | } |
| 31 | trap cleanup EXIT |
| 32 | sleep 1 |
| 33 | |
| 34 | export DISPLAY=":$display_number" |
| 35 | unset WAYLAND_DISPLAY |
| 36 | export VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.json |
| 37 | "$binary" --demo >/dev/null 2>&1 & |
| 38 | app_pid=$! |
| 39 | sleep "$wait_seconds" |
| 40 | |
| 41 | for command in "$@"; do |
| 42 | subcommand=${command%% *} |
| 43 | rest=${command#"$subcommand"} |
| 44 | rest=${rest# } |
| 45 | if [[ $subcommand == click ]]; then |
| 46 | # Xvfb has no window manager: keyboard focus only follows an explicit windowfocus. |
| 47 | eval "$(xdotool getmouselocation --shell)" |
| 48 | xdotool windowfocus --sync "$WINDOW" |
| 49 | fi |
| 50 | if [[ $subcommand == type ]]; then |
| 51 | xdotool type --delay 20 -- "$rest" |
| 52 | elif [[ $subcommand == paste ]]; then |
| 53 | printf '%b' "$rest" | xclip -selection clipboard |
| 54 | xdotool key ctrl+v |
| 55 | else |
| 56 | read -r -a arguments <<<"$rest" |
| 57 | xdotool "$subcommand" "${arguments[@]}" |
| 58 | fi |
| 59 | sleep 0.4 |
| 60 | done |
| 61 | sleep 1 |
| 62 | import -window root "$out" |
| 63 | echo "$out" |