#!/bin/bash
# Hidden nested Wayland session for tests: Xvfb -> niri (winit backend, an X11 client of Xvfb) -> a
# private Wayland socket with the protocols the user's niri has (wlr virtual pointer, virtual keyboard for wtype,
# input-method-v2, screencopy for grim). Nothing appears on the user's screen; everything renders with llvmpipe.
#
# tools/nested-session.sh start
[WxH] start; writes /env (source it in clients), logs, pid files
# tools/nested-session.sh stop stop it and remove its runtime dir
#
# WxH (default 1280x800) is the nested output's size (scale 1). Several sessions can run at once (own X display, own runtime
# dir). NIRI_RULES is added to niri's config, such as 'window-rule { open-floating true; }'. /env exports WAYLAND_DISPLAY, XDG_RUNTIME_DIR, NIRI_SOCKET (so `niri msg` talks to the nested niri,
# never the user's) and the software-GL variables clients need: Xvfb has no DRI3, so the nested niri can't import
# GPU dmabufs and clients must render with software GL / shared memory too.
set -u
cmd=${1:?usage: nested-session.sh start|stop [WxH]}
dir=${2:?usage: nested-session.sh start|stop [WxH]}
mkdir -p "$dir"; dir=$(cd "$dir" && pwd)
case $cmd in
start)
size=${3:-1280x800}
w=${size%x*} h=${size#*x}
# The socket path must stay short (SUN_LEN), so the runtime dir lives in /run/user/.
rt=$(mktemp -d "/run/user/$(id -u)/ns-XXXXXX") || exit 1
chmod 700 "$rt"
echo "$rt" >"$dir/runtime"
# niri's winit window opens at 1280x800 and ignores an output `mode`; xresize.py resizes it (Xvfb has no
# window manager to refuse), so the X screen is made a bit larger than the output.
exec 3<>"$dir/display"
Xvfb -displayfd 3 -screen 0 "$((w + 64))x$((h + 64))x24" -nolisten tcp -noreset >"$dir/xvfb.log" 2>&1 &
echo $! >"$dir/xvfb.pid"
display=
for _ in $(seq 50); do display=$(head -n1 "$dir/display" 2>/dev/null); [ -n "$display" ] && break; sleep 0.1; done
exec 3>&-
[ -n "$display" ] || { echo "nested-session: Xvfb did not start (see $dir/xvfb.log)" >&2; exit 1; }
cat >"$dir/niri.kdl" <"$dir/niri.log" 2>&1 &
echo $! >"$dir/niri.pid"
wl=
for _ in $(seq 75); do wl=$(ls "$rt" 2>/dev/null | grep -m1 '^wayland-[0-9]*$'); [ -n "$wl" ] && break; sleep 0.2; done
[ -n "$wl" ] || { echo "nested-session: niri did not start (see $dir/niri.log)" >&2; "$0" stop "$dir"; exit 1; }
sock=$(ls -d "$rt"/niri*.sock 2>/dev/null | head -n1)
if [ "$size" != 1280x800 ]; then
python3 -I "$(dirname "$0")/lib/xresize.py" ":$display" "$w" "$h" >>"$dir/niri.log" 2>&1
for _ in $(seq 50); do
env XDG_RUNTIME_DIR="$rt" NIRI_SOCKET="$sock" niri msg outputs 2>/dev/null | grep -q "Logical size: ${w}x${h}" && break
sleep 0.1
done
fi
cat >"$dir/env" </dev/null
done
for p in niri xvfb; do
[ -f "$dir/$p.pid" ] && tail --pid="$(cat "$dir/$p.pid")" -f /dev/null 2>/dev/null &
done
wait
rm -f "$dir/niri.pid" "$dir/xvfb.pid"
rt=$(cat "$dir/runtime" 2>/dev/null)
case $rt in /run/user/*/ns-*) rm -rf "$rt";; esac
rm -f "$dir/runtime";;
*) echo "usage: nested-session.sh start|stop [WxH]" >&2; exit 2;;
esac