joeylib2/scripts/lib/keysGate.sh

78 lines
2.9 KiB
Bash

# shellcheck shell=bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# Shared by the input gates (scripts/verify-*-input.sh) that check the named
# keys a second physical key gives: the keypad's 8, 2, 4, and 6 as the
# arrows, its Enter as Return, and the right Ctrl and Alt as Ctrl and Alt,
# each held alone, and Up and the keypad's 8 held together with either let
# go first, which must leave Up down (src/core/inputInternal.h,
# INPUT_KEY_SECOND). Each step holds its keys (letting go of every other),
# waits KEYS_SETTLE_SECONDS, and reads KEYS's Return, Ctrl, Alt, Up, Down,
# Left, and Right cells (examples/keys) as seven digits, keysCells's.
#
# The gate sets KEYS_LABEL (its name, for the lines it prints) and defines,
# by the key names below:
# keysPress KEY hold a key down
# keysRelease KEY let it go
# keysRead print the seven digits for what is on the screen now
# then calls keysRunSteps with the second keys its machine has, and puts
# KEYS_FAILS (empty when every step read right) and KEYS_STEPS (how many
# ran) in its verdict.
#
# Key names: up, and the second keys kp8, kp2, kp4, kp6, kpenter, rctrl,
# ralt.
KEYS_SETTLE_SECONDS="${KEYS_SETTLE_SECONDS:-0.5}"
KEYS_FAILS=""
KEYS_STEPS=0
KEYS_HELD=()
# keysHold NAME WANT KEY... -- hold exactly KEYs, then read the cells, which
# must be WANT.
keysHold() {
local name="$1"
local want="$2"
local key
local lit
shift 2
for key in "${KEYS_HELD[@]}"; do
[[ " $* " == *" $key "* ]] || keysRelease "$key"
done
for key in "$@"; do
[[ " ${KEYS_HELD[*]} " == *" $key "* ]] || keysPress "$key"
done
KEYS_HELD=("$@")
sleep "$KEYS_SETTLE_SECONDS"
lit=$(keysRead)
KEYS_STEPS=$((KEYS_STEPS + 1))
echo "$KEYS_LABEL: $name: Return/Ctrl/Alt/Up/Down/Left/Right $lit"
[ "$lit" = "$want" ] || KEYS_FAILS="${KEYS_FAILS:+$KEYS_FAILS; }$name: lit $lit, want $want"
}
# keysRunSteps KEY... -- every step for these second keys (kp8 among them:
# the both-held steps use it).
keysRunSteps() {
local key
keysHold "rest" 0000000
for key in "$@"; do
case "$key" in
kp8) keysHold "keypad 8 as Up" 0001000 kp8 ;;
kp2) keysHold "keypad 2 as Down" 0000100 kp2 ;;
kp4) keysHold "keypad 4 as Left" 0000010 kp4 ;;
kp6) keysHold "keypad 6 as Right" 0000001 kp6 ;;
kpenter) keysHold "keypad Enter as Return" 1000000 kpenter ;;
rctrl) keysHold "right Ctrl as Ctrl" 0100000 rctrl ;;
ralt) keysHold "right Alt as Alt" 0010000 ralt ;;
*) KEYS_FAILS="${KEYS_FAILS:+$KEYS_FAILS; }no step for $key" ;;
esac
done
keysHold "Up" 0001000 up
keysHold "Up and keypad 8" 0001000 up kp8
keysHold "Up, keypad 8 let go" 0001000 up
keysHold "keypad 8, Up let go" 0001000 kp8
keysHold "rest again" 0000000
}