SchOS/kernel/dev/keymap.c
Nexoniarz 4e3e345b41 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>
2026-09-20 01:09:32 +02:00

297 lines
11 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Keyboard layouts. See include/keymap.h for the interface and its limits.
//
// Every table is indexed by set-1 scancode, with the E0-prefixed keys folded
// into 0x80..0xFF by kbd.c before the lookup -- the same numbering the single
// hardcoded table used, so a layout is a diff against a shape that was already
// proven to work rather than a new mapping to get wrong.
//
// The tables are `uint32` and not `uchar`. That is the whole reason this file
// exists: `ą` is U+0105 and does not fit in a byte, and a Polish layout that
// cannot type `ą` is not one.
#include "types.h"
#include "defs.h"
#include "kbd.h"
#include "keymap.h"
// Shared by every layout that does not move the navigation block, which is
// all of them -- the arrow keys are not a national matter. Merged into each
// level table below rather than consulted separately, because a layout is
// allowed to disagree and none currently does.
#define NAV_KEYS \
[0xC8] = KBD_UP, [0xD0] = KBD_DN, \
[0xC9] = KBD_PGUP, [0xD1] = KBD_PGDN, \
[0xCB] = KBD_LF, [0xCD] = KBD_RT, \
[0x97] = KBD_HOME, [0xCF] = KBD_END, \
[0xD2] = KBD_INS, [0xD3] = KBD_DEL, \
[0x9C] = '\n', /* KP_Enter */ \
[0xB5] = '/' /* KP_Div */
// --- US ---------------------------------------------------------------------
static const uint32 us_plain[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
NAV_KEYS
};
static const uint32 us_shift[256] = {
NO, 0x1B, '!', '@', '#', '$', '%', '^', // 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
NAV_KEYS
};
// --- Polish (programmers) ---------------------------------------------------
//
// US QWERTY with nine letters on AltGr. This is the layout essentially every
// Polish developer uses, and the reason is that the alternative -- "Polish
// (214)", a genuine national arrangement -- moves Y, Z and most of the
// punctuation, which makes every shell and every editor shortcut land
// somewhere else. The diacritics go on AltGr and nothing else moves.
//
// AltGr+x is ź and AltGr+z is ż, which looks arbitrary and is not: the letter
// x is unused in Polish, so its key is free, and ź and ż both need one.
static const uint32 pl_altgr[256] = {
[0x1E] = 0x0105, // a -> ą
[0x2E] = 0x0107, // c -> ć
[0x12] = 0x0119, // e -> ę
[0x26] = 0x0142, // l -> ł
[0x31] = 0x0144, // n -> ń
[0x18] = 0x00F3, // o -> ó
[0x1F] = 0x015B, // s -> ś
[0x2D] = 0x017A, // x -> ź
[0x2C] = 0x017C, // z -> ż
};
static const uint32 pl_shiftaltgr[256] = {
[0x1E] = 0x0104, // A -> Ą
[0x2E] = 0x0106, // C -> Ć
[0x12] = 0x0118, // E -> Ę
[0x26] = 0x0141, // L -> Ł
[0x31] = 0x0143, // N -> Ń
[0x18] = 0x00D3, // O -> Ó
[0x1F] = 0x015A, // S -> Ś
[0x2D] = 0x0179, // X -> Ź
[0x2C] = 0x017B, // Z -> Ż
};
// --- German (QWERTZ) --------------------------------------------------------
//
// Y and Z swap, the umlauts and ß take the punctuation keys on the home and
// top rows, and the key between left Shift and Z -- scancode 0x56, which a US
// keyboard does not have at all -- carries < > |.
//
// The acute (0x0D) and circumflex (0x29) keys are dead on a real German
// layout. Here they type the accent on its own. See include/keymap.h.
static const uint32 de_plain[256] = {
NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
'7', '8', '9', '0', 0x00DF, 0x00B4, '\b', '\t', // ß ´
'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', // 0x10
'o', 'p', 0x00FC, '+', '\n', NO, 'a', 's', // ü
'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x00F6, // ö
0x00E4, '^', NO, '#', 'y', '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, // 0x50, 0x56 = <>|
NAV_KEYS
};
static const uint32 de_shift[256] = {
NO, 0x1B, '!', '"', 0x00A7, '$', '%', '&', // 0x00 §
'/', '(', ')', '=', '?', '`', '\b', '\t',
'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', // 0x10
'O', 'P', 0x00DC, '*', '\n', NO, 'A', 'S', // Ü
'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x00D6, // Ö
0x00C4, 0x00B0, NO, '\'', 'Y', '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, // 0x50
NAV_KEYS
};
static const uint32 de_altgr[256] = {
[0x03] = 0x00B2, // 2 -> ²
[0x04] = 0x00B3, // 3 -> ³
[0x08] = '{', // 7
[0x09] = '[', // 8
[0x0A] = ']', // 9
[0x0B] = '}', // 0
[0x0C] = '\\', // ß
[0x10] = '@', // q
[0x12] = 0x20AC, // e -> €
[0x1B] = '~', // +
[0x32] = 0x00B5, // m -> µ
[0x56] = '|',
};
// --- the catalogue ----------------------------------------------------------
//
// Order matters only in that the first entry is the default. US, because a
// machine whose keyboard layout has never been set should behave the way the
// scancodes are printed on the majority of keyboards, and because every
// installation instruction anyone follows is written in ASCII.
static const struct keylayout layouts[] = {
{ "us", "English (US), QWERTY",
{ us_plain, us_shift, 0, 0 } },
{ "pl", "Polish (programmers) -- US QWERTY, diacritics on AltGr",
{ us_plain, us_shift, pl_altgr, pl_shiftaltgr } },
{ "de", "German, QWERTZ -- no dead keys, so the accent keys type the accent",
{ de_plain, de_shift, de_altgr, 0 } },
};
#define NLAYOUTS ((int)(sizeof(layouts) / sizeof(layouts[0])))
static const struct keylayout *active = &layouts[0];
const struct keylayout *
keymap_active(void)
{
return active;
}
const struct keylayout *
keymap_by_index(int i)
{
if (i < 0 || i >= NLAYOUTS)
return 0;
return &layouts[i];
}
int
keymap_set(const char *name)
{
for (int i = 0; i < NLAYOUTS; i++) {
const char *a = layouts[i].name, *b = name;
while (*a && *a == *b) { a++; b++; }
if (*a == 0 && *b == 0) {
active = &layouts[i];
return 0;
}
}
return -1;
}
// A control character from the plain codepoint rather than from a table of
// its own.
//
// The table this replaces was indexed by scancode, which quietly assumed a US
// keyboard: on QWERTZ, Ctrl+Z and Ctrl+Y would have swapped with the letters,
// so suspending a job would have depended on the layout. Deriving from what
// the key actually types is both shorter and correct everywhere.
static uint32
control_of(uint32 cp)
{
if (cp >= 'a' && cp <= 'z') return cp - 'a' + 1;
if (cp >= 'A' && cp <= 'Z') return cp - 'A' + 1;
switch (cp) {
case '@': case ' ': case '2': return 0; // NUL
case '[': return 0x1B; // ESC
case '\\': return 0x1C;
case ']': return 0x1D;
case '^': case '6': return 0x1E;
case '_': case '-': case '/': return 0x1F;
case '?': return 0x7F;
case '\n': case '\r': return '\r';
}
return 0; // no control character on this key
}
// Upper <-> lower for the letters these layouts can produce.
//
// Caps Lock needs this and Shift does not: Shift has its own table, so the
// layout has already said what the shifted key gives. Caps Lock has no table
// -- it applies to whatever came out, including `ą` from the AltGr level --
// so the case pair has to be derived from the codepoint.
//
// Latin Extended-A is laid out in alternating pairs, but not all the same way
// round: U+0100..U+0137 and U+014A..U+0177 have the capital on the even
// codepoint, while U+0139..U+0148 and U+0179..U+017E have it on the odd one.
// L-stroke is U+0141/U+0142 and Z-dot is U+017B/U+017C, so Polish lands in
// both halves and getting the parity wrong would shift one of them to a
// neighbouring letter rather than to its own capital.
static uint32
case_toggle(uint32 cp)
{
if (cp >= 'a' && cp <= 'z') return cp - 'a' + 'A';
if (cp >= 'A' && cp <= 'Z') return cp - 'A' + 'a';
// Latin-1: the accented letters run C0..DE and E0..FE, with multiplication
// and division signs punched out of the middle of each. Eszett (DF) has no
// single-codepoint capital and stays as it is.
if (cp >= 0xC0 && cp <= 0xDE && cp != 0xD7) return cp + 0x20;
if (cp >= 0xE0 && cp <= 0xFE && cp != 0xF7) return cp - 0x20;
if ((cp >= 0x0100 && cp <= 0x0137) || (cp >= 0x014A && cp <= 0x0177))
return (cp & 1) ? cp - 1 : cp + 1; // even is the capital
if ((cp >= 0x0139 && cp <= 0x0148) || (cp >= 0x0179 && cp <= 0x017E))
return (cp & 1) ? cp + 1 : cp - 1; // odd is the capital
return cp;
}
uint32
keymap_lookup(uint scancode, uint mods)
{
const struct keylayout *l = active;
uint32 cp;
if (scancode > 0xFF)
return 0;
// AltGr first. It is the only modifier that selects a whole different table,
// and on a layout that has no AltGr level the lookup falls back rather than
// producing nothing -- otherwise holding AltGr on a US keyboard would make
// every key dead.
int level = KM_PLAIN;
if (mods & RALT) level = (mods & SHIFT) ? KM_SHIFTALTGR : KM_ALTGR;
else if (mods & SHIFT) level = KM_SHIFT;
cp = l->level[level] ? l->level[level][scancode] : 0;
if (cp == 0 && level >= KM_ALTGR) {
level = (mods & SHIFT) ? KM_SHIFT : KM_PLAIN;
cp = l->level[level] ? l->level[level][scancode] : 0;
}
if (cp == 0)
return 0;
// Caps Lock is a letter-case toggle, not a Shift: it applies to whatever
// the table produced, including a letter that only exists on AltGr. `ą`
// with Caps Lock on is `Ą`, and only the layout knows that -- so the case
// pair has to come from the codepoint, not from a second table lookup.
if ((mods & CAPSLOCK) && cp < KBD_SPECIAL)
cp = case_toggle(cp);
// Ctrl is last and overrides: Ctrl+Shift+C is still Ctrl+C.
if (mods & CTL) {
if (cp >= KBD_SPECIAL)
return cp; // Ctrl+arrow stays an arrow
return control_of(cp);
}
return cp;
}