tty: the switch chord is a setting, because only the host knows the answer

Third guess at a chord, and the third one would have been wrong too: a display
manager that grabs Ctrl+Alt+F<n> matches loosely, so Ctrl+Alt+Shift+F2 never
reaches the guest either. Extra modifiers do not get past a loose grab.

The real problem is that this is not knowable from inside. A key the host
swallowed is indistinguishable from a key nobody pressed, so the guest cannot
probe for a free combination, and there is no combination that is free
everywhere:

  the HOST takes    Alt+F4, Alt+F2, Ctrl+Alt+F1..F6 (loosely matched),
                    Ctrl+Alt+digit (QEMU's own console tabs)
  APPLICATIONS want Alt+anything (Meta -- zsh binds Meta-digit as
                    digit-argument), Ctrl+letter, Ctrl+Shift+digit
                    (Ctrl+Shift+2 types ^@), bare F-keys
  the LAYOUT wants  AltGr+letter

So it is a setting. VT_SETCHORD/VT_GETCHORD, and in userspace:

    chvt -c ctrl+shift+F       Ctrl+Shift+F1..F6   (the new default)
    chvt -c ctrl+alt+shift+F
    chvt -c alt+1
    chvt -c off                no chord; chvt N is then the only way
    chvt                       report the active terminal and the chord

`off` is a legitimate setting rather than a failure mode. On a machine whose
host grabs everything worth binding, `chvt N` is the honest answer.

The default moved to Ctrl+Shift+F1..F6: display managers bind Ctrl+Alt+F<n>,
not Ctrl+Shift+F<n>, and Ctrl+Shift with a *function* key is not a character
on any layout -- unlike Ctrl+Shift+digit, which is.

Caught while writing it: VTC_RALT was 0x08 while kbd.h's RALT is (1<<7), since
(1<<3) is CAPSLOCK. A chord set from userspace with AltGr would simply never
have matched, and the only symptom would have been a key that does nothing.
A _Static_assert now holds the two headers together.

Verified: Ctrl+Alt+F2 leaves the screen byte-identical, Ctrl+Shift+F2 switches,
Ctrl+Shift+F1 returns to a pixel-identical tty1, and chvt -c round-trips
through alt+1 and off and back. schfs-check passes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
Nexoniarz 2026-09-13 13:13:38 +02:00
commit 7c0f7c7d01
8 changed files with 256 additions and 60 deletions

View file

@ -2,9 +2,20 @@
#include "x86.h"
#include "defs.h"
#include "kbd.h"
#include "posix.h"
#include "vt.h" /* NVT -- how many terminals the chords reach */
#include "input.h"
// The chord, settable at run time through VT_SETCHORD. Defaults to
// Ctrl+Shift+F1..F6: the host's display manager binds Ctrl+Alt+F<n> and
// matches it loosely, so anything containing Ctrl+Alt never reaches a guest,
// whatever else is held with it. Ctrl+Shift is not a VT chord on any host and
// is not a character on any layout.
//
// Zero modifiers disables the chord entirely; `chvt N` still works.
uint vt_chord_mods = CTL | SHIFT;
uint vt_chord_base = SCAN_F1;
int
kbdgetc(void)
{
@ -60,19 +71,22 @@ kbdgetc(void)
shift |= shiftcode[data];
shift ^= togglecode[data];
// Ctrl+Alt+Shift+F1..F6 before the character tables, because there is no
// character to
// look up: it is a chord that means "show me a different terminal". What it
// means is decided in console.c, which is where the terminals are; this only
// has to name it. See console_vt_hotkey().
// The switch chord, before the character tables, because there is no
// character to look up: it is a combination that means "show me a different
// terminal". What it means is decided in console.c, which is where the
// terminals are; this only has to name it. See console_vt_hotkey().
//
// The modifier test is an equality, not a mask. A mask fires on any superset,
// so a chord aimed at the host would switch terminals behind your back.
//
// The modifier test is an equality, not a mask. `shift & ALT` would fire on
// Ctrl+Alt+1 and on AltGr+1 as well, so a chord aimed at the host -- or a
// Polish keystroke -- would switch terminals behind your back. Exactly left
// Alt, and nothing else held.
if ((shift & (CTL | SHIFT | ALT | RALT)) == (CTL | ALT | SHIFT) &&
data >= SCAN_F1 && data < SCAN_F1 + NVT)
return KEY_VT_SWITCH + (data - SCAN_F1);
if (vt_chord_mods != 0 &&
(shift & (CTL | SHIFT | ALT | RALT)) == vt_chord_mods &&
data >= vt_chord_base && data < vt_chord_base + NVT)
return KEY_VT_SWITCH + (data - vt_chord_base);
c = charcode[shift & (CTL | SHIFT)][data];
if(shift & CAPSLOCK){
@ -89,3 +103,10 @@ kbdintr(void)
{
consoleintr(kbdgetc);
}
// The two headers describe the same bits to two different audiences, and a
// mismatch would be silent: a chord set from userspace would simply never
// match, and the only symptom would be a key that does nothing.
_Static_assert(VTC_SHIFT == SHIFT && VTC_CTRL == CTL &&
VTC_ALT == ALT && VTC_RALT == RALT,
"include/posix.h VTC_* must mirror include/kbd.h modifiers");