From 16ef838316a9c92f6904e0eff0e6e497aeaae42d Mon Sep 17 00:00:00 2001 From: astar76 Date: Wed, 25 Nov 2026 00:28:08 -0600 Subject: [PATCH] S003: screenshot and hidden-run tools, notes Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 3 + .../01 Foundation/S003-shell-motion-tools.md | 16 +- Plans/INDEX.md | 2 +- docs/PROGRESS.md | 11 + tools/contact.sh | 8 + tools/hidden.sh | 71 ++++++ tools/lib/xresize.py | 29 +++ tools/nested-session.sh | 98 ++++++++ tools/wl-input/.gitignore | 1 + tools/wl-input/Cargo.lock | 230 ++++++++++++++++++ tools/wl-input/Cargo.toml | 14 ++ tools/wl-input/src/main.rs | 171 +++++++++++++ 12 files changed, 645 insertions(+), 9 deletions(-) create mode 100755 tools/contact.sh create mode 100755 tools/hidden.sh create mode 100644 tools/lib/xresize.py create mode 100755 tools/nested-session.sh create mode 100644 tools/wl-input/.gitignore create mode 100644 tools/wl-input/Cargo.lock create mode 100644 tools/wl-input/Cargo.toml create mode 100644 tools/wl-input/src/main.rs diff --git a/CLAUDE.md b/CLAUDE.md index 28ea046..79687a3 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -70,6 +70,9 @@ Never modify: everything outside this folder. `~/Projects/*` is read-only refere | Tool | Role | Available after | |---|---|---| | `nice cargo test -j 2` | unit and engine tests | 01 Foundation | +| `MEDLAR_SHOTS=dir nice cargo test -j 2 -p medlar --test shots`, then `tools/contact.sh dir` | screenshots light and dark (`SCENE`, `SIZES=WxH,…`, `HOVER=x,y`, `PRESS=1`); add the view to `SCENES` | S003 | +| `tools/hidden.sh [driver.sh] [WxH]` | the release build in a hidden nested niri with a throwaway home: click, type, grab, cpu | S003 | +| `python3 tools/gen_assets.py icons` | adds icons: put the name in `tools/icons.txt` first | S003 | | `nice cargo build --release -j 2` | release build (never at the same time as the tests) | 01 Foundation | | `nice cargo test -j 2 -p medlar-testserver` / `cargo run -p medlar-testserver -- [--port N] [--tls] [--media] [--proxies]` | local server with faults and proxy stubs; API in the `crates/testserver/src/lib.rs` header | S002 | | `ssh root@192.168.1.71 hub-release` | release notes on Astar Hub (release only, after the user's yes) | now | diff --git a/Plans/01 Foundation/S003-shell-motion-tools.md b/Plans/01 Foundation/S003-shell-motion-tools.md index 76bc8a4..7c70ad3 100644 --- a/Plans/01 Foundation/S003-shell-motion-tools.md +++ b/Plans/01 Foundation/S003-shell-motion-tools.md @@ -14,14 +14,14 @@ An empty but correct window with tokens, colours, springs, icons and fonts, and - ~/Projects/Damson/tools/gen_icons.py ## Steps -- [ ] eframe 0.37 (glow, wayland, x11) window with app id `medlar`, remembered size, minimum 480×360 -- [ ] Tokens module: spacing, radius, type, elevation, motion presets; `motion.rs` copied and extended with a velocity-keeping retarget test -- [ ] Theme: Caelestia scheme, KDE accent or own colour (D-012), live reload, light, dark, AMOLED, contrast levels, scheme variants; a test checks 4.5:1 text and 3:1 icon contrast for 12 seeds in every mode -- [ ] Fonts and Material Symbols embedded with a generator script; app icon drawn as our own SVG (a medlar fruit shape) -- [ ] Motion level (off, reduced, normal, expressive) and repaint only while `busy()` -- [ ] `tests/shots.rs` harness: `MEDLAR_SHOTS=dir`, light and dark, sizes, `HOVER=x,y`; contact-sheet script -- [ ] `tools/hidden.sh`: nested niri run with throwaway home, grab, click, type; rows added to CLAUDE.md §5 -- [ ] `docs/PROGRESS.md` entry, tick `Plans/INDEX.md`, commit `S003: …`, push +- [x] eframe 0.37 (glow, wayland, x11) window with app id `medlar`, remembered size, minimum 480×360 +- [x] Tokens module: spacing, radius, type, elevation, motion presets; `motion.rs` copied and extended with a velocity-keeping retarget test +- [x] Theme: Caelestia scheme, KDE accent or own colour (D-012), live reload, light, dark, AMOLED, contrast levels, scheme variants; a test checks 4.5:1 text and 3:1 icon contrast for 12 seeds in every mode +- [x] Fonts and Material Symbols embedded with a generator script; app icon drawn as our own SVG (an orange disc with a star and arrow, at the user's wish) +- [x] Motion level (off, reduced, normal, expressive) and repaint only while `busy()` +- [x] `tests/shots.rs` harness: `MEDLAR_SHOTS=dir`, light and dark, sizes, `HOVER=x,y`; contact-sheet script +- [x] `tools/hidden.sh`: nested niri run with throwaway home, grab, click, type; rows added to CLAUDE.md §5 +- [x] `docs/PROGRESS.md` entry, tick `Plans/INDEX.md`, commit `S003: …`, push ## End criteria - `nice cargo test -j 2 -p medlar` green diff --git a/Plans/INDEX.md b/Plans/INDEX.md index 688d0a2..0342bbf 100644 --- a/Plans/INDEX.md +++ b/Plans/INDEX.md @@ -8,7 +8,7 @@ Tracker: https://claude.ai/artifact/XouimfVJS6FYVKskGJ8s7H - [x] [S001](01%20Foundation/S001-workspace-store-settings.md) Workspace, store, settings and strings - [x] [S002](01%20Foundation/S002-test-server.md) Test server with faults -- [ ] [S003](01%20Foundation/S003-shell-motion-tools.md) App shell, theme, motion and screenshot tools +- [x] [S003](01%20Foundation/S003-shell-motion-tools.md) App shell, theme, motion and screenshot tools ## 02 Engine diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md index d1cb46e..acf366e 100644 --- a/docs/PROGRESS.md +++ b/docs/PROGRESS.md @@ -22,6 +22,17 @@ Verified: `nice cargo test -j 2 -p medlar-testserver` green (44 tests, ~1 s, one - Proxies: `proxy::Proxy::start_http(auth)` / `start_socks5(auth)`; `seen()` lists kind, target, credentials, allowed; they connect only to loopback or names set with `map_host`. Not done: BLAKE3 sidecar, HTTP/2, a real gzip body, FTP/SFTP servers (their plans). +## S003 — App shell, theme, motion and tools (2026-11-25) +Done: `crates/app` is a library `medlar_app` plus the `medlar` binary (no arguments: window; else the command line). +Verified: `nice cargo test -j 2 -p medlar -p medlar-core` green (19 here; 432 themes for contrast); shots seen in light and dark, hover and press tints change; hidden release run: window drawn, idle 5 s = 0.00 % CPU twice (also after input), click and keys arrive, scheme.json change re-colours it live, 700×500 remembered, 480×360 is the least. +- Tokens (`tokens.rs`): `space` XS–XXL, `radius` XS–XL, FULL, `inner()`, `text` LABEL BODY TITLE HEADLINE DISPLAY, `icon` S M L XL HERO, `size`, `state` (hover, press, drag, disabled), `elevation::floating`, `spring` FAST DEFAULT SLOW EFFECT. +- Colour: `app.theme.` (`theme.rs`); faint text is `on_variant`, `outline` only reaches 3:1. New settings `scheme` and `contrast`; `own_color` default is `#b5622b`. Changing `app.settings` re-themes by itself. +- Motion: `motion::spring(id, target, preset)` inside `App::frame`; it asks for frames by itself. Non-spring motion calls `motion::keep_alive()` only while something changes. Colours use EFFECT. +- Text: `fonts::{ui, med, semi}(size)`; digits are tabular. Icons: `icons::paint(painter, centre, tokens::icon::M, "name", colour)`; add one through `tools/icons.txt`, then `python3 tools/gen_assets.py icons` (`draft`, `queue` don't exist there). +- Shots: add `("name", |app, ui| app.frame(ui, draw))` to `SCENES` in `tests/shots.rs`. Interface tests: `tests/shell.rs` shows a harness without a window. +- Icon: at the user's wish an orange disc with the five-pointed star and arrow, not a fruit. +Not done: `run()` logs settings damage but shows no message; the store is not opened. + ## S004 — HTTP core with resume and crash-safe files (2026-11-24) Done: `core::http` (client, probe, names, fetch, download), `core::disk` (`PartialFile`), `core::part` (`LivePart`, `split`). Choices: D-017 to D-019. Verified: `nice cargo test -j 2 -p medlar-core` green (47 unit, 16 http, on tmpfs and btrfs); 50 kills at random moments × 4 runs, SHA-256 equal; clippy clean for the new code. diff --git a/tools/contact.sh b/tools/contact.sh new file mode 100755 index 0000000..e46f035 --- /dev/null +++ b/tools/contact.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# One sheet of every screenshot in a folder: tools/contact.sh [columns] -> /contact.png +set -eu +dir=${1:?usage: contact.sh [columns]} +cd "$dir" +rm -f contact.png +montage -label '%t' ./*.png -tile "${2:-2}x" -geometry '760x>+8+8' -background '#777' contact.png +echo "$dir/contact.png" diff --git a/tools/hidden.sh b/tools/hidden.sh new file mode 100755 index 0000000..d221032 --- /dev/null +++ b/tools/hidden.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# Hidden run of Medlar: Xvfb -> nested niri (tools/nested-session.sh) -> target/release/medlar with a throwaway home. +# Nothing shows on the user's screen. Software rendering: judge layout here, not frame rate. +# +# tools/hidden.sh [driver.sh] [WxH] +# +# outdir gets medlar.log, driver.log, grabs, session/ and home/. The driver is sourced once the window is up, with: +# click X Y [right] move X Y scroll X Y N real pointer input, logical pixels of the nested output +# key ... type TEXT wtype arguments, or plain text +# grab NAME screenshot to outdir/NAME.png +# cpu SECONDS Medlar's CPU use over that time, in percent of one core +# quit asks the window to close +# Without a driver: one grab called `window` and `cpu 5`. MEDLAR_BIN picks another binary, MEDLAR_ARGS adds arguments. +# Exits non-zero if Medlar panicked or crashed, or the driver failed. +set -u +root=$(cd "$(dirname "$0")/.." && pwd) +out=${1:?usage: hidden.sh [driver.sh] [WxH]} +driver=${2:+$(realpath "$2")} +bin=${MEDLAR_BIN:-$root/target/release/medlar} +[ -x "$bin" ] || { echo "hidden: $bin is missing, build it first" >&2; exit 2; } +mkdir -p "$out"; out=$(cd "$out" && pwd) +input=$root/tools/wl-input/target/release/wl-input +[ -x "$input" ] || nice cargo build --release -q -j 2 --manifest-path "$root/tools/wl-input/Cargo.toml" || exit 2 +session=$out/session +"$root/tools/nested-session.sh" start "$session" "${3:-1280x800}" >"$out/nested.log" 2>&1 || { cat "$out/nested.log"; exit 1; } +trap '"$root/tools/nested-session.sh" stop "$session"' EXIT +source "$session/env" +export HOME=$out/home XDG_CONFIG_HOME=$out/home/.config XDG_CACHE_HOME=$out/home/.cache +export XDG_DATA_HOME=$out/home/.local/share XDG_STATE_HOME=$out/home/.local/state +unset MEDLAR_HOME +mkdir -p "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME" +export RUST_BACKTRACE=1 +# shellcheck disable=SC2086 +dbus-run-session -- "$bin" ${MEDLAR_ARGS:-} >"$out/medlar.log" 2>&1 & +run=$! + +windows() { niri msg --json windows 2>/dev/null | grep -c '"app_id":"medlar"'; } +up=0 +for _ in $(seq 100); do + [ "$(windows)" -gt 0 ] && { up=1; break; } + kill -0 $run 2>/dev/null || break + sleep 0.1 +done +[ $up = 1 ] || { echo "hidden: no window came up"; cat "$out/medlar.log"; kill $run 2>/dev/null; exit 1; } +sleep 1 +pid=$(pgrep -P $run -x medlar || echo $run) + +ticks() { awk '{sub(/.*\) /, ""); print $12 + $13}' "/proc/$pid/stat"; } +driver_run() ( + set -e + click() { "$input" winit click "$@"; } + move() { "$input" winit move "$@"; } + scroll() { "$input" winit scroll "$@"; } + key() { wtype "$@"; } + type() { wtype -- "$1"; } + grab() { grim "$out/$1.png"; } + cpu() { local a; a=$(ticks); sleep "$1"; awk -v a="$a" -v b="$(ticks)" -v s="$1" -v hz="$(getconf CLK_TCK)" 'BEGIN { printf "cpu %.2f %%\n", (b - a) * 100 / hz / s }'; } + quit() { niri msg action close-window; } + if [ -n "$driver" ]; then source "$driver"; else grab window; cpu 5; fi +) +driver_run >"$out/driver.log" 2>&1 +status=$? +[ $status -ne 0 ] && echo "driver failed" +cat "$out/driver.log" +kill -0 $run 2>/dev/null && niri msg action close-window >/dev/null 2>&1 +timeout 10 tail --pid=$run -f /dev/null || { echo "did not close"; status=1; kill "$pid"; } +wait $run; code=$? +grep -q panicked "$out/medlar.log" && { echo panic; status=1; } +[ $code -ne 0 ] && [ $status -eq 0 ] && { echo "exit code $code"; status=1; } +echo "out: $out" +exit $status diff --git a/tools/lib/xresize.py b/tools/lib/xresize.py new file mode 100644 index 0000000..4301d53 --- /dev/null +++ b/tools/lib/xresize.py @@ -0,0 +1,29 @@ +"""Resize every mapped top-level window of an X display (Xvfb has no window manager): xresize.py :N W H. + +tools/nested-session.sh uses it to give niri's winit output the requested size. Plain ctypes over libX11. +""" +import ctypes +import sys + +x = ctypes.CDLL("libX11.so.6") +x.XOpenDisplay.restype = ctypes.c_void_p +x.XOpenDisplay.argtypes = [ctypes.c_char_p] +x.XDefaultRootWindow.restype = ctypes.c_ulong +x.XDefaultRootWindow.argtypes = [ctypes.c_void_p] +x.XQueryTree.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), + ctypes.POINTER(ctypes.POINTER(ctypes.c_ulong)), ctypes.POINTER(ctypes.c_uint)] +x.XResizeWindow.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_uint, ctypes.c_uint] +x.XSync.argtypes = [ctypes.c_void_p, ctypes.c_int] +x.XCloseDisplay.argtypes = [ctypes.c_void_p] + +display, w, h = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) +d = x.XOpenDisplay(display.encode()) +if not d: + sys.exit(f"xresize: can't open {display}") +root, parent, children, n = ctypes.c_ulong(), ctypes.c_ulong(), ctypes.POINTER(ctypes.c_ulong)(), ctypes.c_uint() +x.XQueryTree(d, x.XDefaultRootWindow(d), ctypes.byref(root), ctypes.byref(parent), ctypes.byref(children), ctypes.byref(n)) +for i in range(n.value): + x.XResizeWindow(d, children[i], w, h) +x.XSync(d, 0) +x.XCloseDisplay(d) +print(f"xresize: {n.value} window(s) to {w}x{h}") diff --git a/tools/nested-session.sh b/tools/nested-session.sh new file mode 100755 index 0000000..5d975bc --- /dev/null +++ b/tools/nested-session.sh @@ -0,0 +1,98 @@ +#!/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 diff --git a/tools/wl-input/.gitignore b/tools/wl-input/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/tools/wl-input/.gitignore @@ -0,0 +1 @@ +/target diff --git a/tools/wl-input/Cargo.lock b/tools/wl-input/Cargo.lock new file mode 100644 index 0000000..24ab73b --- /dev/null +++ b/tools/wl-input/Cargo.lock @@ -0,0 +1,230 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "cc" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "errno" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quick-xml" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +dependencies = [ + "memchr", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "smallvec" +version = "1.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "wayland-backend" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38a91b4eaddff87b1cd1074985e3713da4af2c49742d1b356b2c01670a67a078" +dependencies = [ + "cc", + "downcast-rs", + "rustix", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c36a0f861ad76d0901f2800b46321410d9f73f2ea88aac0650d86c32688073" +dependencies = [ + "bitflags", + "rustix", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d0c813de3daa2ed6520af85a3bd49b0e722a3078506899aa9686fea58dc4b6" +dependencies = [ + "bitflags", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-misc" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e9567599ef23e09b8dad6e429e5738d4509dfc46b3b21f32841a304d16b29c8" +dependencies = [ + "bitflags", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb04e52f7836d7c7976c78ca0250d61e33873c34156a2a1fc9474828ec268234" +dependencies = [ + "bitflags", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", +] + +[[package]] +name = "wayland-sys" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8eab23fefc9e41f8e841df4a9c707e8a8c4ed26e944ef69297184de2785e3be" +dependencies = [ + "pkg-config", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wl-input" +version = "0.1.0" +dependencies = [ + "wayland-client", + "wayland-protocols-misc", + "wayland-protocols-wlr", +] diff --git a/tools/wl-input/Cargo.toml b/tools/wl-input/Cargo.toml new file mode 100644 index 0000000..1ac38e0 --- /dev/null +++ b/tools/wl-input/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "wl-input" +version = "0.1.0" +edition = "2024" +publish = false +description = "Test helper: real compositor input (wlr virtual pointer, input-method-v2) for Medlar's hidden runs" + +# Own workspace and own target/ (tools/wl-input/target), never the main crate's target/. +[workspace] + +[dependencies] +wayland-client = "=0.31.15" +wayland-protocols-wlr = { version = "=0.3.12", features = ["client"] } +wayland-protocols-misc = { version = "=0.3.12", features = ["client"] } diff --git a/tools/wl-input/src/main.rs b/tools/wl-input/src/main.rs new file mode 100644 index 0000000..dc679d9 --- /dev/null +++ b/tools/wl-input/src/main.rs @@ -0,0 +1,171 @@ +//! Real compositor input for Medlar's hidden runs (tools/nested-session.sh): a wlr virtual pointer and a test +//! input method (input-method-v2). Keys and text go through `wtype` instead. +//! +//! Usage: wl-input OUTPUT move X Y | click X Y [left|right|middle|side|extra] | scroll X Y NOTCHES | ime TEXT [PREEDIT_SECS] +//! X/Y are pixels on OUTPUT (e.g. winit, DP-1; `-` is the first output); WL_INPUT_FLIP_Y=1 for a nested niri (winit +//! output), which mirrors Y. NOTCHES are wheel steps, positive down. `ime` waits for a focused text field +//! (text-input-v3 enabled), shows TEXT as preedit for PREEDIT_SECS (default 2), then commits it. +//! Build: `cargo build --release --manifest-path tools/wl-input/Cargo.toml` (tools/hidden.sh does it when missing). +use std::time::{Duration, Instant}; + +use wayland_client::protocol::{wl_output, wl_registry, wl_seat}; +use wayland_client::{Connection, Dispatch, QueueHandle, delegate_noop}; +use wayland_protocols_misc::zwp_input_method_v2::client::{zwp_input_method_manager_v2 as imm, zwp_input_method_v2 as im}; +use wayland_protocols_wlr::virtual_pointer::v1::client::{ + zwlr_virtual_pointer_manager_v1 as vpm, zwlr_virtual_pointer_v1 as vp, +}; + +#[derive(Default)] +struct State { + seat: Option, + outputs: Vec<(wl_output::WlOutput, String, (i32, i32))>, + vpm: Option, + imm: Option, + active: bool, + done: u32, + unavailable: bool, +} + +impl Dispatch for State { + fn event(s: &mut Self, r: &wl_registry::WlRegistry, e: wl_registry::Event, _: &(), _: &Connection, qh: &QueueHandle) { + if let wl_registry::Event::Global { name, interface, version } = e { + match interface.as_str() { + "wl_seat" if s.seat.is_none() => s.seat = Some(r.bind(name, version.min(7), qh, ())), + "wl_output" => { + let i = s.outputs.len(); + s.outputs.push((r.bind(name, version.min(4), qh, i), String::new(), (0, 0))); + } + "zwlr_virtual_pointer_manager_v1" => s.vpm = Some(r.bind(name, version.min(2), qh, ())), + "zwp_input_method_manager_v2" => s.imm = Some(r.bind(name, 1, qh, ())), + _ => {} + } + } + } +} + +impl Dispatch for State { + fn event(s: &mut Self, _: &wl_output::WlOutput, e: wl_output::Event, i: &usize, _: &Connection, _: &QueueHandle) { + match e { + wl_output::Event::Name { name } => s.outputs[*i].1 = name, + wl_output::Event::Mode { width, height, flags, .. } + if flags.into_result().is_ok_and(|f| f.contains(wl_output::Mode::Current)) => + { + s.outputs[*i].2 = (width, height) + } + _ => {} + } + } +} + +impl Dispatch for State { + fn event(s: &mut Self, _: &im::ZwpInputMethodV2, e: im::Event, _: &(), _: &Connection, _: &QueueHandle) { + match e { + im::Event::Activate => s.active = true, + im::Event::Deactivate => s.active = false, + im::Event::Done => s.done += 1, + im::Event::Unavailable => s.unavailable = true, + _ => {} + } + } +} + +delegate_noop!(State: ignore wl_seat::WlSeat); +delegate_noop!(State: vpm::ZwlrVirtualPointerManagerV1); +delegate_noop!(State: vp::ZwlrVirtualPointerV1); +delegate_noop!(State: imm::ZwpInputMethodManagerV2); + +fn now_ms(t0: Instant) -> u32 { + t0.elapsed().as_millis() as u32 +} + +fn main() { + let args: Vec = std::env::args().skip(1).collect(); + let usage = "usage: wl-input OUTPUT move X Y | click X Y [left|right|middle|side|extra] | scroll X Y DY | ime TEXT [SECS]"; + let (output_name, cmd) = (args.first().expect(usage), args.get(1).expect(usage).as_str()); + let conn = Connection::connect_to_env().expect("wayland"); + let mut q = conn.new_event_queue(); + let qh = q.handle(); + conn.display().get_registry(&qh, ()); + let mut s = State::default(); + q.roundtrip(&mut s).unwrap(); + q.roundtrip(&mut s).unwrap(); + let seat = s.seat.clone().expect("seat"); + let t0 = Instant::now(); + let num = |i: usize| -> f64 { args.get(i).and_then(|a| a.parse().ok()).expect(usage) }; + + if cmd == "ime" { + let text = args.get(2).expect(usage).clone(); + let secs: f64 = args.get(3).and_then(|a| a.parse().ok()).unwrap_or(2.0); + let m = s.imm.clone().expect("compositor has no input-method-v2").get_input_method(&seat, &qh, ()); + let deadline = Instant::now() + Duration::from_secs(5); + while !(s.active && s.done > 0) { + q.roundtrip(&mut s).unwrap(); + assert!(!s.unavailable, "another input method is running"); + assert!(Instant::now() < deadline, "no focused text field enabled text-input within 5 s"); + std::thread::sleep(Duration::from_millis(50)); + } + m.set_preedit_string(text.clone(), 0, text.len() as i32); + m.commit(s.done); + q.roundtrip(&mut s).unwrap(); + println!("preedit {text}"); + std::thread::sleep(Duration::from_secs_f64(secs)); + q.roundtrip(&mut s).unwrap(); + m.commit_string(text.clone()); + m.commit(s.done); + q.roundtrip(&mut s).unwrap(); + println!("commit {text}"); + m.destroy(); + q.roundtrip(&mut s).unwrap(); + return; + } + + let (out, _, (w, h)) = s.outputs.iter().find(|o| output_name == "-" || &o.1 == output_name).cloned().expect("no such output"); + let p = s.vpm.clone().expect("compositor has no wlr virtual pointer").create_virtual_pointer_with_output( + Some(&seat), + Some(&out), + &qh, + (), + ); + let (x, mut y) = (num(2), num(3)); + // nested niri (winit backend) mirrors absolute motion vertically + if std::env::var("WL_INPUT_FLIP_Y").is_ok_and(|v| v == "1") { + y = h as f64 - y; + } + p.motion_absolute(now_ms(t0), x as u32, y as u32, w as u32, h as u32); + p.frame(); + q.roundtrip(&mut s).unwrap(); + std::thread::sleep(Duration::from_millis(60)); + match cmd { + "move" => {} + "click" => { + let button = match args.get(4).map(String::as_str) { + Some("right") => 0x111, + Some("middle") => 0x112, + Some("side") => 0x113, + Some("extra") => 0x114, + _ => 0x110, + }; + for state in [wayland_client::protocol::wl_pointer::ButtonState::Pressed, wayland_client::protocol::wl_pointer::ButtonState::Released] { + p.button(now_ms(t0), button, state); + p.frame(); + q.roundtrip(&mut s).unwrap(); + std::thread::sleep(Duration::from_millis(40)); + } + } + "scroll" => { + let dy = num(4); + let steps = dy.abs().round().max(1.0) as i32; + for _ in 0..steps { + use wayland_client::protocol::wl_pointer::{Axis, AxisSource}; + p.axis_source(AxisSource::Wheel); + p.axis_discrete(now_ms(t0), Axis::VerticalScroll, 15.0 * dy.signum(), dy.signum() as i32); + p.frame(); + q.roundtrip(&mut s).unwrap(); + std::thread::sleep(Duration::from_millis(30)); + } + } + _ => panic!("{usage}"), + } + p.destroy(); + q.roundtrip(&mut s).unwrap(); +}