Medlar/tools/hidden.sh
astar76 529c509c05 S003: screenshot and hidden-run tools, notes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:28:08 -06:00

71 lines
3.5 KiB
Shell
Executable file

#!/bin/bash
# Hidden run of Medlar: Xvfb -> nested niri (tools/nested-session.sh) -> target/release/medlar with a throwaway home.
# Nothing shows on the user's screen. Software rendering: judge layout here, not frame rate.
#
# tools/hidden.sh <outdir> [driver.sh] [WxH]
#
# outdir gets medlar.log, driver.log, grabs, session/ and home/. The driver is sourced once the window is up, with:
# click X Y [right] move X Y scroll X Y N real pointer input, logical pixels of the nested output
# key ... type TEXT wtype arguments, or plain text
# grab NAME screenshot to outdir/NAME.png
# cpu SECONDS Medlar's CPU use over that time, in percent of one core
# quit asks the window to close
# Without a driver: one grab called `window` and `cpu 5`. MEDLAR_BIN picks another binary, MEDLAR_ARGS adds arguments.
# Exits non-zero if Medlar panicked or crashed, or the driver failed.
set -u
root=$(cd "$(dirname "$0")/.." && pwd)
out=${1:?usage: hidden.sh <outdir> [driver.sh] [WxH]}
driver=${2:+$(realpath "$2")}
bin=${MEDLAR_BIN:-$root/target/release/medlar}
[ -x "$bin" ] || { echo "hidden: $bin is missing, build it first" >&2; exit 2; }
mkdir -p "$out"; out=$(cd "$out" && pwd)
input=$root/tools/wl-input/target/release/wl-input
[ -x "$input" ] || nice cargo build --release -q -j 2 --manifest-path "$root/tools/wl-input/Cargo.toml" || exit 2
session=$out/session
"$root/tools/nested-session.sh" start "$session" "${3:-1280x800}" >"$out/nested.log" 2>&1 || { cat "$out/nested.log"; exit 1; }
trap '"$root/tools/nested-session.sh" stop "$session"' EXIT
source "$session/env"
export HOME=$out/home XDG_CONFIG_HOME=$out/home/.config XDG_CACHE_HOME=$out/home/.cache
export XDG_DATA_HOME=$out/home/.local/share XDG_STATE_HOME=$out/home/.local/state
unset MEDLAR_HOME
mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME"
export RUST_BACKTRACE=1
# shellcheck disable=SC2086
dbus-run-session -- "$bin" ${MEDLAR_ARGS:-} >"$out/medlar.log" 2>&1 &
run=$!
windows() { niri msg --json windows 2>/dev/null | grep -c '"app_id":"medlar"'; }
up=0
for _ in $(seq 100); do
[ "$(windows)" -gt 0 ] && { up=1; break; }
kill -0 $run 2>/dev/null || break
sleep 0.1
done
[ $up = 1 ] || { echo "hidden: no window came up"; cat "$out/medlar.log"; kill $run 2>/dev/null; exit 1; }
sleep 1
pid=$(pgrep -P $run -x medlar || echo $run)
ticks() { awk '{sub(/.*\) /, ""); print $12 + $13}' "/proc/$pid/stat"; }
driver_run() (
set -e
click() { "$input" winit click "$@"; }
move() { "$input" winit move "$@"; }
scroll() { "$input" winit scroll "$@"; }
key() { wtype "$@"; }
type() { wtype -- "$1"; }
grab() { grim "$out/$1.png"; }
cpu() { local a; a=$(ticks); sleep "$1"; awk -v a="$a" -v b="$(ticks)" -v s="$1" -v hz="$(getconf CLK_TCK)" 'BEGIN { printf "cpu %.2f %%\n", (b - a) * 100 / hz / s }'; }
quit() { niri msg action close-window; }
if [ -n "$driver" ]; then source "$driver"; else grab window; cpu 5; fi
)
driver_run >"$out/driver.log" 2>&1
status=$?
[ $status -ne 0 ] && echo "driver failed"
cat "$out/driver.log"
kill -0 $run 2>/dev/null && niri msg action close-window >/dev/null 2>&1
timeout 10 tail --pid=$run -f /dev/null || { echo "did not close"; status=1; kill "$pid"; }
wait $run; code=$?
grep -q panicked "$out/medlar.log" && { echo panic; status=1; }
[ $code -ne 0 ] && [ $status -eq 0 ] && { echo "exit code $code"; status=1; }
echo "out: $out"
exit $status