Nacre/tools/nested-session.sh
astar76 b7d1dbcc2f S046: smoke harness in a hidden nested niri, real selftest input, local media in the sandbox
- tools/nested-session.sh (Xvfb -> nested niri, own display/runtime dir, xresize for SMOKE_SIZE); smoke.sh and
  hidden.sh run in it; wl-input moved to tools/wl-input (own crate)
- selftest: click/rclick/pagescroll via wl-input, pagetype via wtype, resize via the nested niri
- WebKit sandbox: XDG user dirs (+ test pages in scripted runs) read-only so file:// media plays (D-043)
- SMOKE_VISIBLE needs the user's niri window rule, never moves focus (D-044)
- launch, basics, input, keys, chrome green hidden

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-13 06:14:17 -06:00

97 lines
4 KiB
Shell
Executable file

#!/bin/bash
# Hidden nested Wayland session for tests (D-034/D-035): 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 <dir> [WxH] start; writes <dir>/env (source it in clients), logs, pid files
# tools/nested-session.sh stop <dir> 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). <dir>/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 <dir> [WxH]}
dir=${2:?usage: nested-session.sh start|stop <dir> [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/<uid>.
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" <<KDL
input {
keyboard {
xkb {
layout "us"
}
}
}
prefer-no-csd
hotkey-overlay {
skip-at-startup
}
screenshot-path null
animations {
off
}
layout {
gaps 0
border {
off
}
focus-ring {
off
}
default-column-width {
proportion 1.0
}
}
KDL
env -i PATH="$PATH" HOME="$dir" XDG_RUNTIME_DIR="$rt" DISPLAY=":$display" LIBGL_ALWAYS_SOFTWARE=1 RUST_LOG=warn \
niri -c "$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" <<ENV
export XDG_RUNTIME_DIR=$rt WAYLAND_DISPLAY=$wl NIRI_SOCKET=$sock GDK_BACKEND=wayland XDG_SESSION_TYPE=wayland
export LIBGL_ALWAYS_SOFTWARE=1 WEBKIT_DMABUF_RENDERER_FORCE_SHM=1 GSK_RENDERER=gl GTK_A11Y=none NO_AT_BRIDGE=1 GDK_DEBUG=no-portals
export WL_INPUT_FLIP_Y=1
unset DISPLAY SWAYSOCK
ENV
echo "nested-session: :$display wayland=$wl runtime=$rt";;
stop)
for p in niri xvfb; do
[ -f "$dir/$p.pid" ] && kill "$(cat "$dir/$p.pid")" 2>/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 <dir> [WxH]" >&2; exit 2;;
esac