kbd: three layouts, and the arrow keys were sitting on top of UTF-8
`∞`, `→`, `≤` and `'` could not be typed. `ą` and `ę` could. That pairing is
the whole diagnosis: KBD_UP was 0xE2 and its neighbours filled 0xE0..0xE9,
which are exactly the UTF-8 lead bytes for three-byte characters. Every
character in U+0800..U+FFFF -- most punctuation, every arrow, every maths
symbol, the whole of CJK -- arrived as a value the console read as an arrow key
and helpfully turned into `ESC [ A`. Two-byte sequences lead with 0xC4 and
nothing claimed 0xC4, so Polish came through and the rest did not.
The chord codes already knew this rule and said so in a comment. The navigation
keys predate them and were never brought in line. Stated once, in kbd.h: a key
produces either a Unicode codepoint or a value above 0x10FFFF, and nothing in
between means anything. KEY_* -> KBD_*, which also settles a silent collision
-- include/input.h defines its own KEY_HOME, KEY_UP and KEY_END as evdev
numbers, and kbd.c includes both headers, so three macros were being quietly
redefined in the one file that used them.
Then the layouts, which is what the codepoint-sized keycodes were for.
kernel/dev/keymap.c: us, pl, de. A layout is four tables -- plain, Shift,
AltGr, Shift+AltGr -- of uint32 codepoints. uint32 and not uchar is the reason
the file exists: `ó` fits in a byte and `ą` does not, and half a Polish
alphabet is not a Polish layout.
Two things deliberately are *not* tables:
- **Ctrl is derived from the plain codepoint.** The table it replaces was
indexed by scancode, which quietly assumed US -- on QWERTZ the Y and Z keys
swap, so Ctrl+Z would have suspended a job only on some layouts.
- **Caps Lock is a case toggle on the result**, so AltGr+a with Caps Lock is
`Ą`. No second table could say that, because Caps Lock combines with AltGr
rather than replacing it. Latin Extended-A alternates upper/lower by
codepoint but not all the same way round -- Ł is U+0141 (odd) and Ą is
U+0104 (even) -- so Polish lands in both halves and the parity matters.
kbdgetc() encodes to UTF-8 and returns the bytes one call at a time.
consoleintr()'s contract is bytes: the serial line hands it UTF-8 already and
it could not tell an encoded codepoint from a byte of the same value.
The layouts live in the kernel. Linux's loadkeys compiles a text keymap and
uploads it a key at a time through KDSKBENT, which puts the data in userspace
-- and a machine whose /usr has not mounted then has no layout at all. Here the
kernel always has a working keyboard and `loadkeys` only names which one.
KB_GETLAYOUT/KB_SETLAYOUT/KB_LISTLAYOUT; /etc/vconsole.conf and an
os.schos.keymap job for the boot-time choice. No dead keys, so French and
Spanish are absent rather than wrong.
/etc/locale.conf and LANG=C.UTF-8 in profile and zshenv. Only the charset half
of that is real -- musl decodes UTF-8 whatever the locale says and ships no
locale data -- but an unset LANG reads as "ASCII only" to everything that asks,
which is its own way of producing `?`.
And two symlink bugs found while chasing
zsh: locking failed for /root/.zsh_history: operation not permitted
on every boot. symlink() is the classic atomic lock -- create the link, and if
it exists someone else holds it -- and zsh distinguishes EEXIST ("contended,
retry") from anything else ("this cannot work, give up"). sys_symlink returned
a bare -1, which is -EPERM, so every zsh on the machine took the second branch
on a filesystem that supports symlinks perfectly well. Same errno class as
commit 75ccf98.
The second one the first was hiding: **divesymlink() has been in fs.c since the
fork and nothing ever called it.** A symlink behaved as a small file containing
its own target, so `cat link` printed the target's *name*. open() and openat()
follow the final component now (O_NOFOLLOW gives ELOOP), and stat() and lstat()
stop being the same function, since which one follows is the only difference
between them. Mid-path symlinks are still not resolved -- that belongs in
namex(), which would re-enter lookup holding a directory lock.
Verified on the emulated keyboard, not just over serial: AltGr+a/e/s gives
ąęś, Shift+AltGr gives ĄĘŚ, Caps Lock+AltGr gives ĄĘŚ, de types zy for the
physical y/z keys and @€µ on AltGr. `∞ 'x' → ≤ • — …` typed over serial now
survives. Three fresh boots: 0 lock failures, 0 faults.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
5618164754
commit
4e3e345b41
15 changed files with 994 additions and 208 deletions
137
include/kbd.h
137
include/kbd.h
|
|
@ -1,4 +1,12 @@
|
|||
// PC keyboard interface constants
|
||||
//
|
||||
// The scancode tables themselves are no longer here. They moved to
|
||||
// kernel/dev/keymap.c when there became more than one of them -- a header
|
||||
// full of `static` arrays gives every file that includes it its own private
|
||||
// copy, which was harmless while exactly one file did.
|
||||
|
||||
#ifndef KBD_H
|
||||
#define KBD_H
|
||||
|
||||
#define KBSTATP 0x64 // kbd controller status port(I)
|
||||
#define KBS_DIB 0x01 // kbd data in buffer
|
||||
|
|
@ -24,25 +32,36 @@
|
|||
// Nothing the kernel binds may involve it.
|
||||
#define RALT (1<<7)
|
||||
|
||||
// Special keycodes
|
||||
#define KEY_HOME 0xE0
|
||||
#define KEY_END 0xE1
|
||||
#define KEY_UP 0xE2
|
||||
#define KEY_DN 0xE3
|
||||
#define KEY_LF 0xE4
|
||||
#define KEY_RT 0xE5
|
||||
#define KEY_PGUP 0xE6
|
||||
#define KEY_PGDN 0xE7
|
||||
#define KEY_INS 0xE8
|
||||
#define KEY_DEL 0xE9
|
||||
// Special keycodes.
|
||||
//
|
||||
// **Above every Unicode codepoint, deliberately**, and they did not use to be:
|
||||
// KBD_UP was 0xE2 and its neighbours filled 0xE0..0xE9. Those are exactly the
|
||||
// UTF-8 lead bytes for three-byte characters, so typing any of U+0800..U+FFFF
|
||||
// -- which is most punctuation, every arrow, every maths symbol, and the whole
|
||||
// of CJK -- produced a keycode the console read as an arrow key and turned
|
||||
// into `ESC [ A`. `∞`, `→`, `≤` and `'` all vanished from the input line while
|
||||
// `ą` and `ę` came through, because those are two-byte sequences whose lead
|
||||
// byte is 0xC4 and nothing claimed 0xC4.
|
||||
//
|
||||
// The chord codes below already knew this rule and said so; the navigation
|
||||
// keys predate them and were never brought in line. The rule, stated once: a
|
||||
// value that a key produces is either a Unicode codepoint or it is above
|
||||
// 0x10FFFF, and nothing in between means anything.
|
||||
#define KBD_SPECIAL 0x110000
|
||||
|
||||
#define KBD_HOME (KBD_SPECIAL + 0x00)
|
||||
#define KBD_END (KBD_SPECIAL + 0x01)
|
||||
#define KBD_UP (KBD_SPECIAL + 0x02)
|
||||
#define KBD_DN (KBD_SPECIAL + 0x03)
|
||||
#define KBD_LF (KBD_SPECIAL + 0x04)
|
||||
#define KBD_RT (KBD_SPECIAL + 0x05)
|
||||
#define KBD_PGUP (KBD_SPECIAL + 0x06)
|
||||
#define KBD_PGDN (KBD_SPECIAL + 0x07)
|
||||
#define KBD_INS (KBD_SPECIAL + 0x08)
|
||||
#define KBD_DEL (KBD_SPECIAL + 0x09)
|
||||
|
||||
// Switch virtual terminal.
|
||||
//
|
||||
// Above 0xFF deliberately. kbdgetc returns an int and consoleintr takes one,
|
||||
// and every value that fits in a byte is already either a character or a
|
||||
// UTF-8 continuation byte. A chord that collided with one would be typed into
|
||||
// the terminal instead of switching away from it.
|
||||
//
|
||||
// *Which* chord is a runtime setting, not a constant here, and that is the
|
||||
// interesting part. Three separate parties have a claim on every candidate:
|
||||
//
|
||||
|
|
@ -76,13 +95,13 @@
|
|||
// and no application asks for it. Right Alt being a separate modifier bit
|
||||
// here is what makes the distinction expressible at all -- it used to share
|
||||
// ALT, which is harmless for typing and useless for this.
|
||||
#define KEY_VT_SWITCH 0x120
|
||||
#define KBD_VT_SWITCH (KBD_SPECIAL + 0x20)
|
||||
|
||||
// Previous / next terminal, on the same modifiers as the chord above. The
|
||||
// minus and equals keys, which sit together and read as - and + -- a relative
|
||||
// move for when you have two terminals and do not care which number they are.
|
||||
#define KEY_VT_PREV 0x130
|
||||
#define KEY_VT_NEXT 0x131
|
||||
#define KBD_VT_PREV (KBD_SPECIAL + 0x30)
|
||||
#define KBD_VT_NEXT (KBD_SPECIAL + 0x31)
|
||||
|
||||
#define SCAN_MINUS 0x0C // - / _
|
||||
#define SCAN_EQUALS 0x0D // = / +
|
||||
|
|
@ -93,82 +112,4 @@
|
|||
// C('A') == Control-A
|
||||
#define C(x) (x - '@')
|
||||
|
||||
static uchar shiftcode[256] =
|
||||
{
|
||||
[0x1D] CTL,
|
||||
[0x2A] SHIFT,
|
||||
[0x36] SHIFT,
|
||||
[0x38] ALT,
|
||||
[0x9D] CTL,
|
||||
[0xB8] RALT // right Alt / AltGr -- see RALT above
|
||||
};
|
||||
|
||||
static uchar togglecode[256] =
|
||||
{
|
||||
[0x3A] CAPSLOCK,
|
||||
[0x45] NUMLOCK,
|
||||
[0x46] SCROLLLOCK
|
||||
};
|
||||
|
||||
static uchar normalmap[256] =
|
||||
{
|
||||
NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
|
||||
'7', '8', '9', '0', '-', '=', '\b', '\t',
|
||||
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10
|
||||
'o', 'p', '[', ']', '\n', NO, 'a', 's',
|
||||
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20
|
||||
'\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
|
||||
'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30
|
||||
NO, ' ', NO, NO, NO, NO, NO, NO,
|
||||
NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
|
||||
'8', '9', '-', '4', '5', '6', '+', '1',
|
||||
'2', '3', '0', '.', NO, NO, NO, NO, // 0x50
|
||||
[0x9C] '\n', // KP_Enter
|
||||
[0xB5] '/', // KP_Div
|
||||
[0xC8] KEY_UP, [0xD0] KEY_DN,
|
||||
[0xC9] KEY_PGUP, [0xD1] KEY_PGDN,
|
||||
[0xCB] KEY_LF, [0xCD] KEY_RT,
|
||||
[0x97] KEY_HOME, [0xCF] KEY_END,
|
||||
[0xD2] KEY_INS, [0xD3] KEY_DEL
|
||||
};
|
||||
|
||||
static uchar shiftmap[256] =
|
||||
{
|
||||
NO, 033, '!', '@', '#', '$', '%', '^', // 0x00
|
||||
'&', '*', '(', ')', '_', '+', '\b', '\t',
|
||||
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10
|
||||
'O', 'P', '{', '}', '\n', NO, 'A', 'S',
|
||||
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20
|
||||
'"', '~', NO, '|', 'Z', 'X', 'C', 'V',
|
||||
'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30
|
||||
NO, ' ', NO, NO, NO, NO, NO, NO,
|
||||
NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
|
||||
'8', '9', '-', '4', '5', '6', '+', '1',
|
||||
'2', '3', '0', '.', NO, NO, NO, NO, // 0x50
|
||||
[0x9C] '\n', // KP_Enter
|
||||
[0xB5] '/', // KP_Div
|
||||
[0xC8] KEY_UP, [0xD0] KEY_DN,
|
||||
[0xC9] KEY_PGUP, [0xD1] KEY_PGDN,
|
||||
[0xCB] KEY_LF, [0xCD] KEY_RT,
|
||||
[0x97] KEY_HOME, [0xCF] KEY_END,
|
||||
[0xD2] KEY_INS, [0xD3] KEY_DEL
|
||||
};
|
||||
|
||||
static uchar ctlmap[256] =
|
||||
{
|
||||
NO, NO, NO, NO, NO, NO, NO, NO,
|
||||
NO, NO, NO, NO, NO, NO, NO, NO,
|
||||
C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
|
||||
C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
|
||||
C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
|
||||
NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
|
||||
C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
|
||||
[0x9C] '\r', // KP_Enter
|
||||
[0xB5] C('/'), // KP_Div
|
||||
[0xC8] KEY_UP, [0xD0] KEY_DN,
|
||||
[0xC9] KEY_PGUP, [0xD1] KEY_PGDN,
|
||||
[0xCB] KEY_LF, [0xCD] KEY_RT,
|
||||
[0x97] KEY_HOME, [0xCF] KEY_END,
|
||||
[0xD2] KEY_INS, [0xD3] KEY_DEL
|
||||
};
|
||||
|
||||
#endif /* KBD_H */
|
||||
|
|
|
|||
Loading…
Reference in a new issue