#!/bin/sh # Run a command on a private headless Wayland compositor: sway on wlroots' # headless backend (it has a seat, which GPUI's Wayland backend requires and # weston's headless backend lacks), its socket in its own XDG_RUNTIME_DIR, # and a private D-Bus session bus, so nothing reaches the desktop the # command is started from. No X display is set (no XWayland). # # spikes/gpui/headless-wayland.sh [-r pixman|gles2|vulkan] [-s WIDTHxHEIGHT] -- COMMAND... # # -r: the compositor's renderer (default pixman, the CPU; gles2 or vulkan # use the GPU of a render node); -s: the output size (default 3840x2160). # Run it inside shell.sh; keys go in with wtype (virtual keyboard). State # lives in $SPIKE_STATE as for headless.sh. set -eu renderer=pixman size=3840x2160 while [ $# -gt 0 ]; do case $1 in -r) renderer=$2; shift 2 ;; -s) size=$2; shift 2 ;; --) shift; break ;; *) break ;; esac done [ $# -gt 0 ] || { echo "headless-wayland.sh: no command" >&2; exit 2; } root=$(cd "$(dirname "$0")/../.." && pwd) SPIKE_STATE=${SPIKE_STATE:-$root/.scratch/gpui-spikes-state} # Short: a socket path has at most 107 bytes (sway's IPC socket is # sway-ipc...sock in it). runtime=$root/.scratch/wl mkdir -p "$runtime" "$SPIKE_STATE/config" "$SPIKE_STATE/data" "$SPIKE_STATE/cache" "$SPIKE_STATE/state" chmod 700 "$runtime" # A compositor that died uncleanly leaves its socket, which the wait below # would take for the new one's. rm -f "$runtime"/wayland-* "$runtime"/sway-ipc.* # sway-unwrapped: nixpkgs' sway wrapper starts sway under a dbus-run-session # of its own when no bus is set, and that child outlives the kill below. tools=$(nix build --option substituters "https://cache.nixos.org/ https://nix-community.cachix.org" \ --no-link --print-out-paths nixpkgs#sway-unwrapped nixpkgs#wtype) for t in $tools; do PATH="$t/bin:$PATH"; done unset DISPLAY WAYLAND_DISPLAY WAYLAND_SOCKET XDG_SESSION_TYPE DBUS_SESSION_BUS_ADDRESS SWAYSOCK export XDG_RUNTIME_DIR="$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" cat >"$SPIKE_STATE/sway.conf" <"$SPIKE_STATE/sway.log" 2>&1 & compositor=$! trap 'kill $compositor 2>/dev/null; wait $compositor 2>/dev/null || true' EXIT INT TERM i=0 until socket=$(ls "$runtime" 2>/dev/null | grep '^wayland-[0-9]*$' | head -1) && [ -n "$socket" ]; do i=$((i + 1)) [ $i -lt 100 ] || { echo "sway did not start" >&2; cat "$SPIKE_STATE/sway.log" >&2; exit 1; } sleep 0.1 done export WAYLAND_DISPLAY=$socket cat >"$SPIKE_STATE/dbus-session-wayland.conf" < session unix:path=$runtime/bus EXTERNAL CONF dbus-run-session --config-file="$SPIKE_STATE/dbus-session-wayland.conf" -- "$@"