Files
faumarayandClaude Opus 5.5 b6f8f16108 spikes/gpui: headless-wayland.sh stops its compositor
nixpkgs' sway wrapper runs sway under a dbus-run-session of its own when
no session bus is set, so the script's kill reached dbus-run-session and
every run left a headless sway behind. It now runs sway-unwrapped, which
is the process it kills, and clears sockets a dead compositor left in its
private runtime directory before it waits for the new one's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 03:12:16 +03:00

76 lines
3.3 KiB
Bash
Executable File

#!/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.<uid>.<pid>.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" <<CONF
output HEADLESS-1 resolution $size position 0 0
default_border none
focus_follows_mouse no
CONF
WLR_BACKENDS=headless WLR_LIBINPUT_NO_DEVICES=1 WLR_RENDERER=$renderer \
sway --unsupported-gpu -c "$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" <<CONF
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<type>session</type>
<listen>unix:path=$runtime/bus</listen>
<auth>EXTERNAL</auth>
<policy context="default">
<allow send_destination="*" eavesdrop="true"/>
<allow eavesdrop="true"/>
<allow own="*"/>
</policy>
</busconfig>
CONF
dbus-run-session --config-file="$SPIKE_STATE/dbus-session-wayland.conf" -- "$@"