joeylib2/scripts/verify-dos-input.sh

110 lines
4.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# verify-dos-input.sh - Headless typed-character acceptance gate for the
# DOS port. Runs KEYS.EXE in DOSBox Staging under Xvfb, types the
# RetroNet definition-of-done string with xdotool (real X key events,
# shift and all, so ':' exercises the INT 9 shift path), takes a RAW
# native-resolution screenshot, and decodes the example's verification
# scanline (row 198) from the exact VGA DAC colors. KEYS gives all 16
# palette entries distinct colors precisely so this decode is exact.
# Then the named keys a second key gives (scripts/lib/keysGate.sh): the
# keypad's arrows and Enter, and the right Ctrl and Alt (the INT 9
# handler's 0xE0-prefixed codes). A mapper file binds just these keys and
# puts the screenshot on F11 (scripts/lib/dosboxKeys.sh): DOSBox's Ctrl+F5
# would hand KEYS a Ctrl press.
#
# Usage: scripts/verify-dos-input.sh
# Requires: DOSBox Staging (install.sh install emulator:dos), Xvfb, xdotool;
# build/dos/bin/KEYS.EXE (make dos).
set -euo pipefail
repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
source "$repo/scripts/lib/joeyEnv.sh"
source "$repo/scripts/lib/dosboxKeys.sh"
source "$repo/scripts/lib/keysGate.sh"
joeyNeed DOSBOX dosbox
bin_dir=$repo/build/dos/bin
conf=$repo/scripts/dosbox-386sx16.conf
target="192.168.1.10:6510"
[ -f "$bin_dir/KEYS.EXE" ] || { echo "verify-dos-input: build KEYS.EXE first (make dos)" >&2; exit 2; }
work=$(mktemp -d -t joeylib-dos-input.XXXXXX)
display_num=$((RANDOM % 500 + 300))
cleanup() {
joeyStop "${dosbox_pid:-}"
[ -n "${xvfb_pid:-}" ] && kill "$xvfb_pid" 2>/dev/null
rm -rf "$work"
}
trap cleanup EXIT
mkdir -p "$work/shots"
dosboxKeysMapper "$work/mapper.txt" "${DOSBOX_KEYS_TYPING[@]}" "${DOSBOX_KEYS_NAMED[@]}"
Xvfb ":$display_num" -screen 0 1024x768x24 >/dev/null 2>&1 &
xvfb_pid=$!
sleep 1
XDG_CONFIG_HOME="$work/xdg" DISPLAY=":$display_num" "$DOSBOX" "${JOEY_DOSBOX_ISOLATE[@]}" \
--conf "$conf" \
--set "capture_dir=$work/shots" \
--set "mapperfile=$work/mapper.txt" \
-c "MOUNT C $bin_dir" -c "C:" -c "KEYS.EXE" \
>"$work/dosbox.log" 2>&1 &
dosbox_pid=$!
# Let DOS boot and KEYS reach its poll loop on the simulated 386SX-16.
sleep 12
# Staging titles its window "<PROG> - <n> cycles/ms"; it is the only
# client on this private display, so match any named window.
win=$(joeyFindWindow ":$display_num" "$dosbox_pid")
if [ -z "$win" ]; then
echo "verify-dos-input: FAIL - DOSBox window not found" >&2
exit 1
fi
DISPLAY=":$display_num" xdotool windowactivate --sync "$win" 2>/dev/null || true
DISPLAY=":$display_num" xdotool type --window "$win" --delay 150 -- "$target"
sleep 2
png="$work/typed.png"
if ! dosboxShot ":$display_num" "$win" "$work/shots" "$png"; then
echo "verify-dos-input: FAIL - no screenshot captured" >&2
exit 1
fi
"$(joeyTool dosRow)" input "$png" "$target"
# The stage in the raw screenshot: KEYS's row 198 found where it is.
found=$("$(joeyTool keysRow)" "$png" "$target") || true
if [[ ! "$found" =~ row\ at\ \(([0-9]+),([0-9]+)\)\ at\ ([0-9]+)x ]]; then
echo "verify-dos-input: FAIL - KEYS's stage is not in the screenshot: $found" >&2
exit 1
fi
scale=${BASH_REMATCH[3]}
stageAt="${BASH_REMATCH[1]},$((BASH_REMATCH[2] - 198 * scale))"
# X's names for the keys (scripts/lib/keysGate.sh's).
declare -A xKey=([up]=Up [kp8]=KP_Up [kp2]=KP_Down [kp4]=KP_Left [kp6]=KP_Right [kpenter]=KP_Enter [rctrl]=Control_R [ralt]=Alt_R)
KEYS_LABEL=verify-dos-input
keysPress() {
DISPLAY=":$display_num" xdotool keydown --window "$win" "${xKey[$1]}"
}
keysRelease() {
DISPLAY=":$display_num" xdotool keyup --window "$win" "${xKey[$1]}"
}
keysRead() {
if dosboxShot ":$display_num" "$win" "$work/shots" "$work/cells.png"; then
"$(joeyTool keysCells)" "$work/cells.png" --at "$stageAt" --scale "$scale"
else
echo "no screenshot"
fi
}
keysRunSteps kp8 kp2 kp4 kp6 kpenter rctrl ralt
if [ -n "$KEYS_FAILS" ]; then
echo "verify-dos-input: FAIL - $KEYS_FAILS" >&2
exit 1
fi
echo "verify-dos-input: PASS (typed \"$target\", all ${#target} chars arrived via jlInputGetChar; $KEYS_STEPS named-key steps: the keypad's arrows and Enter, right Ctrl and Alt, Up held with its second key)"