v0.5.1 console key releases: Linux VT and FreeBSD vt(4), with a guard

* linux console source: releases from the console's own keyboard, typed by its keymap

SetKeyReport with KeySourceConsole, on a Linux virtual console the game runs
on, switches the keyboard to raw keycodes (K_MEDIUMRAW) while releases are
reported. The kernel allows the switch on the caller's controlling tty, so no
root is needed; a console that refuses is not asked again and the protocol is
next. Going back to presses, Fini and EmergencyReset switch it back.

Each keycode is typed as the console types it cooked, after
drivers/tty/vt/keyboard.c, by the console's own tables read at the switch
(KDGKBENT, KDGKBSENT, KDGKBDIACRUC, KDGKBMETA): modifiers and their left and
right groups, Caps and Num Lock with their lights kept in step (KDSKBLED),
sticky and locked modifiers, Unicode entries, dead keys and compose, the
keypad by Num Lock, function strings, Meta as ESC prefix or bit 7, console
switching by Alt+Fn and Alt+arrows to consoles in use, and from kernel 7.1
cursor and CSI keys with their modifiers. Translation starts from the
console's modifiers and locks (TIOCLINUX 6, KDGKBLED). Each key is held by
its keycode; a key typing several keys taps all but its last. Switching to
another console ends every hold Lost. Mouse reports are off while the
keyboard is raw, since the console types them into the same input, and come
back after it.

The guard: before the first switch the package runs the binary again, named
console-guard, holding the console and the read end of a pipe only the game
writes. If the pipe ends while the keyboard is raw (kill -9, the OOM killer, a
Go fatal error), the guard switches the keyboard back, restores the termios
the game found and shows the cursor, without a reset that would clear the
game's last words. A game leading its console's session takes the console
with it as it dies; its guard then leads a session of its own and opens the
console again once the kernel has hung it up, to restore it.

Checked in QEMU on a real VT, kernels 6.8 and 7.2, the game as uid 1000:
the same key strokes read identically raw and cooked (Shift released first,
Ctrl, Alt, AltGr, Caps and Num Lock, the keypad, function keys, modified
cursor keys on 7.2; dead grave and tilde, compose, a euro sign and Cyrillic
from a loaded keymap), Alt+F2 away ended the hold Lost and Alt+F1 came back,
and the keyboard came back to K_UNICODE with ICANON and ECHO after Fini, a
panic with EmergencyReset, kill -9, and kill -9 of a game leading its session
(left raw without the guard's own session).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3zCVjspRDuEuMrDTumVhp

* freebsd console source: releases from vt(4)'s keyboard, typed by its keymap

On FreeBSD's vt(4) console the keyboard switches to keycodes (K_CODE),
which vt allows any process with the console open, no root needed. Each
keycode types what vt types for it cooked: the keyboard's keymap and
accent map (GIO_KEYMAP, GIO_DEADKEYMAP) run through a port of the
kernel's genkbd_keyaction, kbdmux's left Alt keypad codes, and
vt_processkey's xterm sequences, ESC for Alt and console switching. The
locks go back to the keyboard and its lights with KDSKBSTATE. Scroll Lock
is inert while raw; with the console's history mode on (Scroll Lock lit)
the keyboard stays as it is and the ask reports presses.

Shared with the Linux source now: the restore EmergencyReset runs, the
guard, which opens again a console revoked or hung up with its session's
leader, and what a key asks of the console. A console that can't go raw
just now (Scroll Lock on, its guard gone, output not drained) is tried
again at the next ask rather than never; a refusal at prepare still
holds for good.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3zCVjspRDuEuMrDTumVhp

* example-keys: show each key event, for trying key releases

A viewer for the key events a terminal or console reports: F2 asks for
releases from the console or the terminal's protocol, or gives them
back, and the report each ask came to shows above the events.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B3zCVjspRDuEuMrDTumVhp

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
LixenandClaude Opus 5.5 authored and GitHub committed 2026-10-21 11:16:27 -04:00
1 parent 97f66b1f1c
commit e9aeed0be0
23 files changed
+3580 -9

No files matched your search

+47
View File
@@ -281,6 +281,53 @@ While releases are reported:
Releases come from the first of these sources a game allows that the
terminal has:
- `KeySourceConsole`: the kernel console's own keyboard, on the console the
game runs on: a Linux VT (`/dev/ttyN`) or FreeBSD's vt(4) (`/dev/ttyvN`).
While releases are reported the package switches the keyboard to raw
keycodes, which the kernel allows without root (Linux's `K_MEDIUMRAW` on
the game's controlling tty, FreeBSD's `K_CODE` on a console the game has
open), and switches it back when the game goes back to presses, at `Fini`
and in `EmergencyReset`. Each key types what the console types for it
cooked, by the console's own keymap read at the switch. Switching to
another console ends the holds. The console's mouse reports pause while the
keyboard is raw, and a mouse paste then reads as keys. Never over ssh,
where the keyboard is the far terminal's.
On Linux that is `loadkeys` layouts, dead keys and compose, the locks and
their lights, Alt+F*n* and Alt+arrows switching consoles, and cursor keys
with modifiers from Linux 7.1. Left out while raw: codes typed with Alt on
the keypad, braille, Ctrl+Alt+Del, and Scroll Lock, which only lets stopped
output go on.
On FreeBSD it is `kbdmap` layouts and their accents, the locks and their
lights, Alt+F*n* and Print Screen switching consoles, codes typed with left
Alt on the keypad, and Alt as ESC before a character (right Alt too with
`kern.vt.enable_altgr=0`). Left out while raw: the machine's keys
(Ctrl+Alt+Del, the debugger, suspend), Shift+Insert's paste, and Scroll
Lock. With Scroll Lock on, the console's history mode takes every key, so
the keyboard stays as it is and the ask reports presses; an ask after
Scroll Lock goes off tries again. syscons (`kern.vty=sc`) has no console
source.
Before the first switch the package runs the game's binary again as
`console-guard`, which switches the keyboard back, restores the tty and
shows the cursor if the game dies where nothing of its own runs: `kill -9`,
the OOM killer, a Go fatal error. A game leading its console's session
(on Linux, run with `exec` from the login shell or by an autologin) takes
the console with it as it dies; the guard then opens it again to restore
it. The package's `init` turns the guard's run aside before `main`; the
`init`s of packages initialized before it run in the guard too, so keep
side effects out of them. On FreeBSD the guard goes by the game's name, so
`pkill -9` by that name ends it too: kill the game by its PID. Should the
guard be killed as well, switch the keyboard back from elsewhere: on Linux
`sudo kbd_mode -u -C /dev/ttyN`; FreeBSD has no command for it, so build
one:
```sh
printf '#include <sys/ioctl.h>\n#include <sys/kbio.h>\nint main(void) { return ioctl(0, KDSKBMODE, K_XLATE); }\n' |
cc -x c -o /tmp/kbdxlate - && /tmp/kbdxlate < /dev/ttyvN
```
- `KeySourceProtocol`: the terminal's key event protocol, CSI u with event
types. The package asks the terminal whether it has the protocol (`CSI ? u`,
fenced by a DA1 request every terminal answers), pushes its flags and