BlueGit

ict rusty-teams

Files

rusty-teams / scripts / headless-shot.sh

headless-shot.sh

headless-shot.sh 1.8 KiB Download Raw
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.
6set -euo pipefail
7
8out=${1:?output png}
9shift
10size=1280x800
11wait_seconds=10
12binary=target/debug/teams
13while [[ $# -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
21done
22
23display_number=$((100 + RANDOM % 800))
24Xvfb ":$display_number" -screen 0 "${size}x24" -nolisten tcp >/dev/null 2>&1 &
25xvfb_pid=$!
26app_pid=
27cleanup() {
28 [[ -n $app_pid ]] && kill "$app_pid" 2>/dev/null || true
29 kill "$xvfb_pid" 2>/dev/null || true
30}
31trap cleanup EXIT
32sleep 1
33
34export DISPLAY=":$display_number"
35unset WAYLAND_DISPLAY
36export VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/lvp_icd.json
37"$binary" --demo >/dev/null 2>&1 &
38app_pid=$!
39sleep "$wait_seconds"
40
41for 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
60done
61sleep 1
62import -window root "$out"
63echo "$out"