#!/bin/sh # Run a command on a private headless X display: Xvfb, a window manager # (openbox: fullscreen, activation and close requests need one) and a # private D-Bus session bus, so nothing reaches the desktop the command is # started from (its Wayland or X display, its session bus, its portals). # # spikes/gpui/headless.sh [-d DISPLAY_NUMBER] [-s WIDTHxHEIGHT] [-n] -- COMMAND... # # -d: the display number (default 91); -s: the screen size (default # 3840x2160); -n: no window manager. Run it inside shell.sh (Xvfb, openbox # and dbus come from there). The display, the window manager's and the # command's state live in $SPIKE_STATE (default: .scratch/gpui-spikes-state # of the checkout, which git ignores). set -eu number=91 size=3840x2160 wm=1 while [ $# -gt 0 ]; do case $1 in -d) number=$2; shift 2 ;; -s) size=$2; shift 2 ;; -n) wm=0; shift ;; --) shift; break ;; *) break ;; esac done [ $# -gt 0 ] || { echo "headless.sh: no command" >&2; exit 2; } root=$(cd "$(dirname "$0")/../.." && pwd) SPIKE_STATE=${SPIKE_STATE:-$root/.scratch/gpui-spikes-state} mkdir -p "$SPIKE_STATE/runtime" "$SPIKE_STATE/config" "$SPIKE_STATE/data" "$SPIKE_STATE/cache" "$SPIKE_STATE/state" chmod 700 "$SPIKE_STATE/runtime" if [ -e "/tmp/.X11-unix/X$number" ]; then echo "headless.sh: display :$number is taken" >&2 exit 1 fi Xvfb ":$number" -screen 0 "${size}x24" -nolisten tcp -noreset >"$SPIKE_STATE/xvfb-$number.log" 2>&1 & xvfb=$! trap 'kill $wm_pid $xvfb 2>/dev/null; wait $xvfb 2>/dev/null || true' EXIT INT TERM wm_pid= i=0 until [ -e "/tmp/.X11-unix/X$number" ]; do i=$((i + 1)) [ $i -lt 100 ] || { echo "headless.sh: Xvfb did not start" >&2; exit 1; } sleep 0.1 done # Never the desktop's display, bus or portals. unset WAYLAND_DISPLAY WAYLAND_SOCKET XDG_SESSION_TYPE DBUS_SESSION_BUS_ADDRESS export DISPLAY=":$number" export XDG_RUNTIME_DIR="$SPIKE_STATE/runtime" XDG_CONFIG_HOME="$SPIKE_STATE/config" export XDG_DATA_HOME="$SPIKE_STATE/data" XDG_CACHE_HOME="$SPIKE_STATE/cache" # The Slint app keeps its logs in XDG_STATE_HOME: never the desktop's. export XDG_STATE_HOME="$SPIKE_STATE/state" export GPUI_X11_SCALE_FACTOR=${GPUI_X11_SCALE_FACTOR:-1} if [ $wm = 1 ]; then openbox --sm-disable >"$SPIKE_STATE/openbox-$number.log" 2>&1 & wm_pid=$! sleep 0.5 fi # A session bus of its own that starts no services: the desktop's portals # (and their backends, which look for a compositor) stay out of the run. cat >"$SPIKE_STATE/dbus-session.conf" < session unix:path=$SPIKE_STATE/runtime/bus EXTERNAL CONF dbus-run-session --config-file="$SPIKE_STATE/dbus-session.conf" -- "$@"