diff --git a/.gitignore b/.gitignore index 15e0ac7666..0c602cc24f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,8 @@ __pycache__/ # docs/guide/out is generated by docs/guide/build.sh; the installed copies in # src/ are the tracked ones. docs/guide/out/ + +# an agent's local plans (Hermes), not part of the tree +/.hermes/ +# raw output of tools/mcde-desktop-webshots.cmds; the kept pictures are www/img/ +/www/screenshots/ diff --git a/tools/AGENTS.md b/tools/AGENTS.md index 805702ac25..547f509a3c 100644 --- a/tools/AGENTS.md +++ b/tools/AGENTS.md @@ -99,7 +99,11 @@ MCDE and the logo: - `mcde-desktop.sh` -- starts MCDE through dtlogin on a private Xvfb display, logs in as \$USER, and runs a control script (wait/shot/type/key/click/move/run/ focus/close/exit) to take targeted screenshots for the website; backs up and - restores \$HOME/.dt and /etc/pam.d/dtlogin. + restores \$HOME/.dt and /etc/pam.d/dtlogin. Hazard: for the length of the run + the HOST's dtlogin service is pam_permit (sudo); the trap that puts it back is + set before the switch. `mcde-desktop-webshots.cmds` is the script for the + website's pictures (raw shots in `www/screenshots/`, not tracked; the kept + ones are copied to `www/img/`). - `mcde-xkey.sh` -- presses keys by their plain keycodes (XTest), `mcde-xkey.sh Super_L h`; `xdotool key F8` on Xvfb sends Alt+F8 (header of the script). - `logo-ascii.sh`, `theme-logo.sh` -- the logo as ASCII, and in a palette's diff --git a/tools/mcde-desktop-webshots.cmds b/tools/mcde-desktop-webshots.cmds new file mode 100644 index 0000000000..2da01f5487 --- /dev/null +++ b/tools/mcde-desktop-webshots.cmds @@ -0,0 +1,31 @@ +# Sample control script for mcde-desktop.sh producing the website shots. +# Run: tools/mcde-desktop.sh tools/mcde-desktop-webshots.cmds +# Output goes to www/screenshots/ ; copy the ones you keep into www/img/. + +# 1. the plain desktop (let the desktop fully paint first) +wait 8 +shot www/screenshots/shot-desktop.png + +# 2. open a terminal +run dtterm +wait 2 +shot www/screenshots/shot-dtterm.png + +# 3. open the file manager +run dtfile +wait 2 +shot www/screenshots/shot-dtfile.png + +# 4. open System Settings +run dtsystemsettings +wait 2 +shot www/screenshots/shot-settings.png + +# 5. open the launcher +key Super_L space +wait 2 +shot www/screenshots/shot-dtlaunch.png +key Escape +wait 1 + +exit diff --git a/tools/mcde-desktop.sh b/tools/mcde-desktop.sh new file mode 100755 index 0000000000..ba97d892e6 --- /dev/null +++ b/tools/mcde-desktop.sh @@ -0,0 +1,330 @@ +#!/bin/sh +# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. +# +# mcde-desktop.sh -- start MCDE on a private Xvfb display through dtlogin, +# log in, and control the desktop to take targeted screenshots +# +# usage: tools/mcde-desktop.sh [COMMANDS_FILE] +# +# Starts dtlogin (as root) on a private Xvfb display, logs in as $USER +# (pam_permit is used during the run, so no password is needed), waits for +# the desktop, then runs the control commands from COMMANDS_FILE (or stdin +# if none / if "-" is given) and tears everything down on exit. +# +# Control commands (one per line; lines starting with # are ignored): +# wait SECONDS sleep +# shot OUT.png screenshot of the whole screen +# shot OUT.png X Y W H screenshot of a region (ImageMagick crop) +# type TEXT type TEXT into the focused window +# key KEYSYM [KEYSYM ...] press keys (via tools/mcde-xkey.sh) +# click X Y [BUTTON] click at (X,Y) (default button 1) +# move X Y move the pointer +# run PROGRAM [ARG ...] launch PROGRAM from the dev install in the +# session (background) +# focus NAME focus the first window whose name contains NAME +# close close the focused window (Alt+F4) +# exit stop processing (teardown follows) +# +# Environment: +# MCDE_DESK_DISPLAY X display to use (default :93) +# MCDE_DESK_USER user to log in as (default $USER) +# MCDE_DESK_WAIT seconds to wait for the desktop after login (default 8) +# MCDE_DESK_SETUP shell command run in $HOME before login (optional) +# MCDE_DESK_EXTRA program started beside the session (optional) +# MCDE_THUMB_DEBUG etc. forwarded as in mcde-shot.sh +# +# A new home is NOT created: the session runs as $USER and uses its real +# $HOME, so the dev build under the tree is reachable. $HOME/.dt is backed +# up to build/mcde-desktop/home.dt.bak before the run and restored on exit. + +set -u + +TOP=$(cd "$(dirname "$0")/.." && pwd) +R=$TOP/build/mcde-root +[ -x "$R/dt/bin/dtlogin" ] || { echo "no $R: make mcde-linux MCDE_DIRS=install" >&2; exit 1; } + +DISP=${MCDE_DESK_DISPLAY:-:93} +USERN=${MCDE_DESK_USER:-${USER:-lotherk}} +WAIT=${MCDE_DESK_WAIT:-8} +CMDS=${1:-} + +WORK=$TOP/build/mcde-desktop +mkdir -p "$WORK" +CFG=$WORK/cfg +rm -rf "$CFG" +mkdir -p "$CFG" +cp -r "$R/dt/config/"* "$CFG/" 2>/dev/null + +# Xservers: launch Xvfb ourselves on $DISP +cat > "$CFG/Xservers" < "$CFG/Xconfig" </dev/null + +# PAM: pam_permit for this run, the host's own file put back afterwards. +# The trap is set BEFORE the switch: a run killed in between used to leave the +# host's graphical login open without a password. The backup is taken from +# the file as it is now, every run -- a backup kept from the first run put an +# old file back over a newer one -- unless the file is our pam_permit, left by +# a killed run, and then the backup of that run is the original. +PAM=/etc/pam.d/dtlogin +PAMBAK=/etc/pam.d/dtlogin.orig.makronix +HOME_BAK=$WORK/home.dt.bak +switched= +restored= + +restore() { + [ -n "$restored" ] && return 0 + restored=yes + [ -n "$switched" ] && sudo cp "$PAMBAK" "$PAM" + # $HOME/.dt as it was before the run; the session's copy is set aside in + # $WORK/home.dt.session, never removed before the backup is back + if [ -d "$HOME_BAK" ]; then + rm -rf "$WORK/home.dt.session" + [ -d "$HOME/.dt" ] && mv "$HOME/.dt" "$WORK/home.dt.session" + cp -a "$HOME_BAK" "$HOME/.dt" && rm -rf "$HOME_BAK" + fi + # kill dtlogin / Xvfb + if [ -n "${DTLOGIN_PID:-}" ]; then + sudo kill "$DTLOGIN_PID" 2>/dev/null + fi + # kill any leftover Xvfb on $DISP + pids=$(pgrep -f "Xvfb ${DISP}" 2>/dev/null) + [ -n "$pids" ] && sudo kill $pids 2>/dev/null + sleep 1 +} +trap restore EXIT +trap 'restore; exit 1' HUP INT TERM + +if grep -q pam_permit "$PAM" 2>/dev/null; then + [ -f "$PAMBAK" ] || { echo "$PAM is pam_permit and $PAMBAK is missing: fix $PAM by hand" >&2; exit 1; } +else + sudo cp "$PAM" "$PAMBAK" || { echo "cannot back up $PAM" >&2; exit 1; } +fi +switched=yes +sudo tee "$PAM" >/dev/null <<'PAMEOF' +auth required pam_permit.so +account required pam_permit.so +session required pam_permit.so +password required pam_permit.so +PAMEOF + +# back up $HOME/.dt (a backup left by a killed run is the original: kept) +if [ -d "$HOME/.dt" ] && [ ! -d "$HOME_BAK" ]; then + # whole or not at all: a half copy would be put back over $HOME/.dt + rm -rf "$HOME_BAK.tmp" + cp -a "$HOME/.dt" "$HOME_BAK.tmp" && mv "$HOME_BAK.tmp" "$HOME_BAK" || + { echo "cannot back up $HOME/.dt" >&2; exit 1; } +fi + +# environment for launched programs +export DISPLAY=$DISP +export HOME=$HOME +PATH=$R/dt/bin:$PATH +LD_LIBRARY_PATH=$R/dt/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} +XFILESEARCHPATH=$R/dt/app-defaults/%L/%N:$R/dt/app-defaults/C/%N +DTDATABASESEARCHPATH=$R/dt/appconfig/types/%L,$R/dt/appconfig/types/C +TTPATH=$R/dt/appconfig/tttypes +DTSCREENSAVERLIST="StartDtscreenSwarm StartDtscreenQix StartDtscreenFlame StartDtscreenHop StartDtscreenImage StartDtscreenLife StartDtscreenRotor StartDtscreenPyro StartDtscreenWorm StartDtscreenBlank" +export PATH LD_LIBRARY_PATH XFILESEARCHPATH DTDATABASESEARCHPATH TTPATH DTSCREENSAVERLIST + +Session() { + env -i DISPLAY=$DISPLAY HOME=$HOME PATH=$PATH LANG=${MCDE_LANG:-C.UTF-8} \ + LD_LIBRARY_PATH=$LD_LIBRARY_PATH XFILESEARCHPATH=$XFILESEARCHPATH \ + TTPATH=$TTPATH DTDATABASESEARCHPATH=$DTDATABASESEARCHPATH \ + DTSCREENSAVERLIST="$DTSCREENSAVERLIST" \ + MCDE_THUMB_DEBUG="${MCDE_THUMB_DEBUG:-}" \ + MCDE_DESK_DEBUG="${MCDE_DESK_DEBUG:-}" \ + MCDE_PARENTAL_DEBUG="${MCDE_PARENTAL_DEBUG:-}" \ + MCDE_DEFAULTS_DEBUG="${MCDE_DEFAULTS_DEBUG:-}" "$@" +} + +# start dtlogin +sudo "$R/dt/bin/dtlogin" -config "$CFG/Xconfig" -nodaemon -debug 1 \ + > "$WORK/dtlogin.log" 2>&1 & +DTLOGIN_PID=$! +# wait for greeter +i=0 +while [ $i -lt 60 ]; do + grep -q 'Greeter started' "$WORK/dtlogin.log" 2>/dev/null && break + sleep 1 + i=$((i + 1)) +done +grep -q 'Greeter started' "$WORK/dtlogin.log" 2>/dev/null || { echo "greeter did not start" >&2; exit 1; } +sleep 4 + +# find entry window id (the username text field) +find_entry() { + DISPLAY=$DISP xwininfo -root -tree 2>/dev/null | awk '/222x23/ {print $1; exit}' +} + +UENTRY=$(find_entry) +if [ -z "$UENTRY" ]; then + echo "username entry not found" >&2 + exit 1 +fi +# click the username field at its known position, then type +env DISPLAY=$DISP xdotool mousemove 551 394 click 1 +sleep 0.3 +env DISPLAY=$DISP xdotool type --delay 40 "$USERN" 2>/dev/null +sleep 0.3 +env DISPLAY=$DISP xdotool key Return 2>/dev/null +sleep 2 +# password challenge: the greeter grabs the keyboard for the no-echo field. +# Retry a few times: the grab may not be active yet on the first try. +i=0 +while [ $i -lt 5 ]; do + env DISPLAY=$DISP xdotool type --delay 40 "x" 2>/dev/null + sleep 0.3 + env DISPLAY=$DISP xdotool key Return 2>/dev/null + sleep 2 + if ! DISPLAY=$DISP xdotool search --name login_shell >/dev/null 2>&1; then break; fi + i=$((i + 1)) +done + +# wait for the greeter to go away (login_shell window disappears) +i=0 +while [ $i -lt 30 ]; do + if ! DISPLAY=$DISP xdotool search --name login_shell >/dev/null 2>&1; then break; fi + sleep 1 + i=$((i + 1)) +done +sleep "$WAIT" + +# verify a session actually started: dtlogin log must show Verify() + Client started +if ! grep -q "Verify(): $USERN" "$WORK/dtlogin.log" 2>/dev/null; then + echo "login failed; see $WORK/dtlogin.log" >&2 + exit 1 +fi +if ! grep -q "Client started" "$WORK/dtlogin.log" 2>/dev/null; then + echo "session did not start; see $WORK/dtlogin.log" >&2 + exit 1 +fi +echo "mcde-desktop: desktop up on $DISP (user $USERN)" >&2 + +# helper: run a shell command in the session env +run_session() { + # shellcheck disable=SC2086 + DISPLAY=$DISP HOME=$HOME PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH \ + XFILESEARCHPATH=$XFILESEARCHPATH DTDATABASESEARCHPATH=$DTDATABASESEARCHPATH \ + TTPATH=$TTPATH DTSCREENSAVERLIST="$DTSCREENSAVERLIST" "$@" +} + +# command processor +process_line() { + line=$1 + # strip leading/trailing whitespace + line=$(printf '%s' "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + [ -z "$line" ] && return 0 + case "$line" in + \#*) return 0 ;; + esac + set -- $line + cmd=$1 + shift + case "$cmd" in + wait) + sleep "${1:-1}" + ;; + shot) + out=$1; shift + if [ $# -ge 4 ]; then + x=$1; y=$2; w=$3; h=$4 + env DISPLAY=$DISP import -window root -crop "${w}x${h}+${x}+${y}" "$out" 2>/dev/null + else + env DISPLAY=$DISP import -window root "$out" 2>/dev/null + fi + echo "shot: $out" + ;; + type) + # rest of line as text + text=${line#type } + text=${text# } + env DISPLAY=$DISP xdotool type --delay 30 "$text" 2>/dev/null + ;; + key) + # shellcheck disable=SC2086 + env DISPLAY=$DISP sh "$TOP/tools/mcde-xkey.sh" "$@" 2>/dev/null + ;; + click) + x=$1; y=$2; btn=${3:-1} + env DISPLAY=$DISP xdotool mousemove "$x" "$y" click "$btn" 2>/dev/null + ;; + move) + env DISPLAY=$DISP xdotool mousemove "$1" "$2" 2>/dev/null + ;; + run) + # launch in session env, background + prog=$1; shift + case "$prog" in + */*) ;; + *) prog=$R/dt/bin/$prog ;; + esac + Session "$prog" "$@" >> "$WORK/run.log" 2>&1 & + echo "run: $prog $*" + ;; + focus) + name=$1 + w=$(env DISPLAY=$DISP xdotool search --onlyvisible --name "$name" 2>/dev/null | head -1) + if [ -n "$w" ]; then + env DISPLAY=$DISP xdotool windowactivate "$w" 2>/dev/null + env DISPLAY=$DISP xdotool windowfocus "$w" 2>/dev/null + echo "focus: $name ($w)" + else + echo "focus: no window matching '$name'" >&2 + fi + ;; + close) + env DISPLAY=$DISP xdotool key alt+F4 2>/dev/null + ;; + exit) + return 1 + ;; + *) + echo "unknown command: $cmd" >&2 + ;; + esac + return 0 +} + +# read commands +if [ -n "$CMDS" ] && [ "$CMDS" != "-" ]; then + if [ ! -r "$CMDS" ]; then + echo "cannot read commands file: $CMDS" >&2 + exit 2 + fi + while IFS= read -r line; do + process_line "$line" || break + done < "$CMDS" +else + # stdin + while IFS= read -r line; do + process_line "$line" || break + done +fi + +echo "mcde-desktop: done" >&2 +exit 0