* 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>
145 lines
4.0 KiB
Go
145 lines
4.0 KiB
Go
package terminal
|
|
|
|
// The console source: a kernel console's keyboard switched to raw keycodes.
|
|
// Each key types what the console types for it cooked, read as a legacy
|
|
// terminal's bytes are, and is held by its keycode.
|
|
|
|
// keyConsole is a kernel console whose keyboard the reader can switch to raw
|
|
// keycodes: the Linux console, FreeBSD's vt(4). The reader calls it from its
|
|
// goroutine; close comes from Fini, on another.
|
|
type keyConsole interface {
|
|
// prepare reads what translating keys needs and readies the switch: the
|
|
// keymap, the guard, output a Scroll Lock stopped. False when the console
|
|
// can't go raw.
|
|
prepare() bool
|
|
|
|
// raw switches the keyboard to raw keycodes, false when the console refuses
|
|
raw() bool
|
|
|
|
// cooked switches the keyboard back to the mode raw found
|
|
cooked()
|
|
|
|
// keys decodes raw keycodes, keeping a partial one for the next call, and
|
|
// reports each key event with what the console types for it
|
|
keys(data []byte, key func(code uint32, action KeyAction, typed []byte))
|
|
|
|
// front is whether the console is the one on screen, the one the
|
|
// keyboard types into
|
|
front() bool
|
|
|
|
// close switches the keyboard back if it is raw and ends the guard
|
|
close()
|
|
}
|
|
|
|
// consoleAct is what a key asks of the console beside typing
|
|
type consoleAct struct {
|
|
vt int // a console to switch to, from 1, or vtPrev or vtNext; zero for none
|
|
leds bool // the lock flags changed
|
|
start bool // restart output stopped with Scroll Lock
|
|
}
|
|
|
|
// Console switches a key asks for beside switching to a console by number
|
|
const (
|
|
vtPrev = -1 // the console before this one
|
|
vtNext = -2 // the console after this one
|
|
)
|
|
|
|
// consoleBackend is a backend on a kernel console
|
|
type consoleBackend interface {
|
|
keyConsole() keyConsole
|
|
}
|
|
|
|
// backendConsole is the console b runs on, nil for none
|
|
func backendConsole(b Backend) keyConsole {
|
|
if c, ok := b.(consoleBackend); ok {
|
|
return c.keyConsole()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// rawConsole switches the console's keyboard raw for releases; a console that
|
|
// refuses is not asked again, or not until the next ask when it may not
|
|
// refuse then
|
|
func (r *inputReader) rawConsole() {
|
|
// An ESC waiting for its timeout was Escape, pressed before the switch
|
|
if ev, ok := loneEscape[string(r.buf)]; ok {
|
|
r.sendKey(0, ev)
|
|
}
|
|
r.buf = r.buf[:0]
|
|
if r.keysRaw == nil {
|
|
r.consoleHas = -1
|
|
return
|
|
}
|
|
if ok, again := r.keysRaw(); !ok {
|
|
r.consoleHas = -1
|
|
if again {
|
|
r.consoleHas = -2
|
|
}
|
|
return
|
|
}
|
|
r.holds = KeyHolds{}
|
|
r.source, r.report = KeySourceConsole, KeyReportReleases
|
|
}
|
|
|
|
// consoleRead reads raw keycodes, and ends every hold while another console
|
|
// has the keyboard: their releases go there
|
|
func (r *inputReader) consoleRead(data []byte) {
|
|
if len(data) > 0 {
|
|
r.console.keys(data, r.consoleKey)
|
|
}
|
|
if !r.console.front() {
|
|
r.holds.ReleaseAll(r.sendEvent)
|
|
}
|
|
}
|
|
|
|
// consoleKey sends a key event as what the console types for it reads: the
|
|
// last key held by the keycode, any before it as taps, as a dead key's
|
|
// character the key types before its own
|
|
func (r *inputReader) consoleKey(code uint32, action KeyAction, typed []byte) {
|
|
if action == KeyRelease {
|
|
r.sendKey(code, Event{Type: EventKey, Action: KeyRelease})
|
|
return
|
|
}
|
|
var last Event
|
|
have := false
|
|
r.typedKeys(typed, func(ev Event) {
|
|
if have {
|
|
r.sendKey(0, last)
|
|
}
|
|
last, have = ev, true
|
|
})
|
|
if have {
|
|
last.Action = action
|
|
r.sendKey(code, last)
|
|
}
|
|
}
|
|
|
|
// typedKeys reads what a console types for one key as a legacy terminal's
|
|
// input reads, the key whole: an ESC it ends with is Escape
|
|
func (r *inputReader) typedKeys(typed []byte, key func(Event)) {
|
|
if len(typed) == 0 {
|
|
return
|
|
}
|
|
p := r.typer
|
|
if p == nil {
|
|
// A parser of its own: no paste, no protocol, no stop
|
|
p = &inputReader{eventCh: make(chan Event, 512)}
|
|
r.typer = p
|
|
}
|
|
p.dropCSI = false
|
|
n := p.parseInput(typed)
|
|
if ev, ok := loneEscape[string(typed[n:])]; ok {
|
|
p.sendKey(0, ev)
|
|
}
|
|
for {
|
|
select {
|
|
case ev := <-p.eventCh:
|
|
if ev.Type == EventKey && ev.Key != KeyNone {
|
|
key(ev)
|
|
}
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
}
|