479 lines
18 KiB
C++
479 lines
18 KiB
C++
/*
|
|
* keyboard.h
|
|
* Keyboard layout registry, per-user selection, and scan-code translation
|
|
* Copyright (c) 2026 Daniel Hammer
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <montauk/config.h>
|
|
#include <montauk/string.h>
|
|
#include <montauk/utf8.h>
|
|
|
|
namespace montauk::keyboard {
|
|
|
|
inline constexpr int MAX_LAYOUTS = 8;
|
|
inline constexpr int MAX_KEYS = 64;
|
|
inline constexpr int MAX_DEAD_KEYS = 16;
|
|
|
|
// One overridden key. Characters are Unicode codepoints; translation stores
|
|
// them in KeyEvent::codepoint (and the Windows-1252 byte, if any, in ascii).
|
|
// 0 means "no override, keep the kernel's value".
|
|
struct KeyMap {
|
|
uint8_t scancode;
|
|
uint32_t base;
|
|
uint32_t shift;
|
|
uint32_t altgr;
|
|
};
|
|
|
|
struct Layout {
|
|
char id[8];
|
|
char name[48];
|
|
char short_name[8];
|
|
KeyMap keys[MAX_KEYS];
|
|
int key_count; // 0 = passthrough (the kernel's US table)
|
|
// Characters that act as dead keys in this layout: pressing one types
|
|
// nothing and modifies the next character (see compose()).
|
|
uint32_t dead_keys[MAX_DEAD_KEYS];
|
|
int dead_count;
|
|
};
|
|
|
|
struct Registry {
|
|
Layout items[MAX_LAYOUTS];
|
|
int count;
|
|
};
|
|
|
|
struct State {
|
|
Registry registry;
|
|
bool enabled[MAX_LAYOUTS];
|
|
int active;
|
|
uint32_t pending_dead; // dead key waiting for the next character, 0 = none
|
|
};
|
|
|
|
// ==== Dead-key composition ====
|
|
// Keyed by the spacing form of the accent, which is what a layout puts on the
|
|
// dead key itself (and what dead key + Space types).
|
|
|
|
struct ComposeEntry {
|
|
uint16_t dead;
|
|
uint16_t base;
|
|
uint16_t result;
|
|
};
|
|
|
|
inline constexpr ComposeEntry COMPOSE[] = {
|
|
// acute
|
|
{0x00B4,'a',0xE1},{0x00B4,'A',0xC1},{0x00B4,'e',0xE9},{0x00B4,'E',0xC9},
|
|
{0x00B4,'i',0xED},{0x00B4,'I',0xCD},{0x00B4,'o',0xF3},{0x00B4,'O',0xD3},
|
|
{0x00B4,'u',0xFA},{0x00B4,'U',0xDA},{0x00B4,'y',0xFD},{0x00B4,'Y',0xDD},
|
|
{0x00B4,'c',0x107},{0x00B4,'C',0x106},{0x00B4,'l',0x13A},{0x00B4,'L',0x139},
|
|
{0x00B4,'n',0x144},{0x00B4,'N',0x143},{0x00B4,'r',0x155},{0x00B4,'R',0x154},
|
|
{0x00B4,'s',0x15B},{0x00B4,'S',0x15A},{0x00B4,'z',0x17A},{0x00B4,'Z',0x179},
|
|
// caron
|
|
{0x02C7,'c',0x10D},{0x02C7,'C',0x10C},{0x02C7,'d',0x10F},{0x02C7,'D',0x10E},
|
|
{0x02C7,'e',0x11B},{0x02C7,'E',0x11A},{0x02C7,'l',0x13E},{0x02C7,'L',0x13D},
|
|
{0x02C7,'n',0x148},{0x02C7,'N',0x147},{0x02C7,'r',0x159},{0x02C7,'R',0x158},
|
|
{0x02C7,'s',0x161},{0x02C7,'S',0x160},{0x02C7,'t',0x165},{0x02C7,'T',0x164},
|
|
{0x02C7,'z',0x17E},{0x02C7,'Z',0x17D},
|
|
// diaeresis
|
|
{0x00A8,'a',0xE4},{0x00A8,'A',0xC4},{0x00A8,'e',0xEB},{0x00A8,'E',0xCB},
|
|
{0x00A8,'i',0xEF},{0x00A8,'I',0xCF},{0x00A8,'o',0xF6},{0x00A8,'O',0xD6},
|
|
{0x00A8,'u',0xFC},{0x00A8,'U',0xDC},{0x00A8,'y',0xFF},{0x00A8,'Y',0x178},
|
|
// ring above
|
|
{0x00B0,'u',0x16F},{0x00B0,'U',0x16E},{0x00B0,'a',0xE5},{0x00B0,'A',0xC5},
|
|
// grave
|
|
{0x0060,'a',0xE0},{0x0060,'A',0xC0},{0x0060,'e',0xE8},{0x0060,'E',0xC8},
|
|
{0x0060,'i',0xEC},{0x0060,'I',0xCC},{0x0060,'o',0xF2},{0x0060,'O',0xD2},
|
|
{0x0060,'u',0xF9},{0x0060,'U',0xD9},
|
|
// circumflex
|
|
{0x005E,'a',0xE2},{0x005E,'A',0xC2},{0x005E,'e',0xEA},{0x005E,'E',0xCA},
|
|
{0x005E,'i',0xEE},{0x005E,'I',0xCE},{0x005E,'o',0xF4},{0x005E,'O',0xD4},
|
|
{0x005E,'u',0xFB},{0x005E,'U',0xDB},
|
|
// tilde
|
|
{0x007E,'a',0xE3},{0x007E,'A',0xC3},{0x007E,'n',0xF1},{0x007E,'N',0xD1},
|
|
{0x007E,'o',0xF5},{0x007E,'O',0xD5},
|
|
// breve
|
|
{0x02D8,'a',0x103},{0x02D8,'A',0x102},{0x02D8,'g',0x11F},{0x02D8,'G',0x11E},
|
|
// ogonek
|
|
{0x02DB,'a',0x105},{0x02DB,'A',0x104},{0x02DB,'e',0x119},{0x02DB,'E',0x118},
|
|
{0x02DB,'i',0x12F},{0x02DB,'I',0x12E},{0x02DB,'u',0x173},{0x02DB,'U',0x172},
|
|
// dot above
|
|
{0x02D9,'z',0x17C},{0x02D9,'Z',0x17B},{0x02D9,'e',0x117},{0x02D9,'E',0x116},
|
|
{0x02D9,'I',0x130},
|
|
// double acute
|
|
{0x02DD,'o',0x151},{0x02DD,'O',0x150},{0x02DD,'u',0x171},{0x02DD,'U',0x170},
|
|
// cedilla
|
|
{0x00B8,'c',0xE7},{0x00B8,'C',0xC7},{0x00B8,'s',0x15F},{0x00B8,'S',0x15E},
|
|
};
|
|
|
|
// The composed character, or 0 if the accent does not combine with base.
|
|
inline uint32_t compose(uint32_t dead, uint32_t base) {
|
|
for (const ComposeEntry& e : COMPOSE)
|
|
if (e.dead == dead && e.base == base) return e.result;
|
|
return 0;
|
|
}
|
|
|
|
inline void build_key(char* out, int cap, const char* prefix,
|
|
const char* id, const char* suffix = nullptr) {
|
|
int pos = 0;
|
|
const char* parts[3] = {prefix, id, suffix};
|
|
for (int part = 0; part < 3; part++) {
|
|
const char* text = parts[part];
|
|
if (!text) continue;
|
|
while (*text && pos < cap - 1) out[pos++] = *text++;
|
|
}
|
|
out[pos] = '\0';
|
|
}
|
|
|
|
// Read an array of byte values (scancodes) from the layout table.
|
|
// Returns the element count, or -1 if the key is missing or malformed.
|
|
inline int read_bytes(const toml::Doc& doc, const char* key, uint8_t* out, int cap) {
|
|
toml::Value* arr = doc.get_array(key);
|
|
if (!arr) return -1;
|
|
if (arr->array.count > cap) return -1;
|
|
for (int i = 0; i < arr->array.count; i++) {
|
|
toml::Value* value = arr->array.items[i];
|
|
if (!value || value->type != toml::Type::Int) return -1;
|
|
if (value->ival < 0 || value->ival > 0xFF) return -1;
|
|
out[i] = (uint8_t)value->ival;
|
|
}
|
|
return arr->array.count;
|
|
}
|
|
|
|
// Read an array of characters (Unicode codepoints). 0x80-0x9F are C1 control
|
|
// codes in Unicode, never key output, so they are read as the Windows-1252
|
|
// bytes older layout files used (0x80 = euro sign).
|
|
inline int read_codepoints(const toml::Doc& doc, const char* key, uint32_t* out, int cap) {
|
|
toml::Value* arr = doc.get_array(key);
|
|
if (!arr) return -1;
|
|
if (arr->array.count > cap) return -1;
|
|
for (int i = 0; i < arr->array.count; i++) {
|
|
toml::Value* value = arr->array.items[i];
|
|
if (!value || value->type != toml::Type::Int) return -1;
|
|
int64_t v = value->ival;
|
|
if (v < 0 || v > 0x10FFFF || (v >= 0xD800 && v <= 0xDFFF)) return -1;
|
|
out[i] = (v >= 0x80 && v <= 0x9F) ? utf8::from_cp1252((uint8_t)v) : (uint32_t)v;
|
|
}
|
|
return arr->array.count;
|
|
}
|
|
|
|
// Parse one [layouts.<id>] table. Returns false if the layout declares key
|
|
// overrides but they are inconsistent, in which case the caller skips it: a
|
|
// malformed table must never produce a half-applied layout.
|
|
inline bool load_layout(Layout* out, const char* id, const toml::Doc& doc) {
|
|
if (!out) return false;
|
|
*out = {};
|
|
montauk::strncpy(out->id, id, sizeof(out->id));
|
|
|
|
char key[64];
|
|
build_key(key, sizeof(key), "layouts.", id, ".name");
|
|
montauk::strncpy(out->name, doc.get_string(key, id), sizeof(out->name));
|
|
build_key(key, sizeof(key), "layouts.", id, ".short_name");
|
|
montauk::strncpy(out->short_name, doc.get_string(key, id),
|
|
sizeof(out->short_name));
|
|
|
|
build_key(key, sizeof(key), "layouts.", id, ".dead_keys");
|
|
int dead = read_codepoints(doc, key, out->dead_keys, MAX_DEAD_KEYS);
|
|
if (dead < 0 && doc.get_array(key)) return false; // present but malformed
|
|
out->dead_count = dead < 0 ? 0 : dead;
|
|
|
|
uint8_t scancodes[MAX_KEYS];
|
|
uint32_t base[MAX_KEYS];
|
|
uint32_t shift[MAX_KEYS];
|
|
uint32_t altgr[MAX_KEYS] = {};
|
|
|
|
build_key(key, sizeof(key), "layouts.", id, ".scancodes");
|
|
int count = read_bytes(doc, key, scancodes, MAX_KEYS);
|
|
if (count < 0) {
|
|
// No override table at all: a passthrough layout such as "en".
|
|
out->key_count = 0;
|
|
return true;
|
|
}
|
|
|
|
build_key(key, sizeof(key), "layouts.", id, ".base");
|
|
if (read_codepoints(doc, key, base, MAX_KEYS) != count) return false;
|
|
build_key(key, sizeof(key), "layouts.", id, ".shift");
|
|
if (read_codepoints(doc, key, shift, MAX_KEYS) != count) return false;
|
|
build_key(key, sizeof(key), "layouts.", id, ".altgr");
|
|
int altgr_count = read_codepoints(doc, key, altgr, MAX_KEYS);
|
|
if (altgr_count >= 0 && altgr_count != count) return false;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
out->keys[i].scancode = scancodes[i];
|
|
out->keys[i].base = base[i];
|
|
out->keys[i].shift = shift[i];
|
|
out->keys[i].altgr = altgr[i];
|
|
}
|
|
out->key_count = count;
|
|
return true;
|
|
}
|
|
|
|
// The compiled-in base layout. The kernel's scancode table is already US
|
|
// English, so this overrides nothing; it exists so the registry is never
|
|
// empty and input keeps working even with no data file on disk.
|
|
inline void add_base_layout(Registry* registry) {
|
|
if (!registry || registry->count >= MAX_LAYOUTS) return;
|
|
Layout& layout = registry->items[registry->count++];
|
|
layout = {};
|
|
montauk::strcpy(layout.id, "en");
|
|
montauk::strcpy(layout.name, "English (US)");
|
|
montauk::strcpy(layout.short_name, "en");
|
|
layout.key_count = 0;
|
|
}
|
|
|
|
inline int find_layout(const Registry& registry, const char* id) {
|
|
for (int i = 0; i < registry.count; i++)
|
|
if (montauk::streq(registry.items[i].id, id)) return i;
|
|
return -1;
|
|
}
|
|
|
|
inline Registry load_registry() {
|
|
Registry registry = {};
|
|
toml::Doc doc = montauk::data::load("keyboard-layouts");
|
|
|
|
toml::Value* order = doc.get_array("registry.layouts");
|
|
if (order) {
|
|
for (int i = 0; i < order->array.count; i++) {
|
|
toml::Value* value = order->array.items[i];
|
|
if (!value || value->type != toml::Type::String || !value->str)
|
|
continue;
|
|
if (registry.count >= MAX_LAYOUTS) break;
|
|
if (find_layout(registry, value->str) >= 0) continue;
|
|
Layout candidate;
|
|
if (!load_layout(&candidate, value->str, doc)) continue;
|
|
registry.items[registry.count++] = candidate;
|
|
}
|
|
}
|
|
doc.destroy();
|
|
|
|
// Guarantee a working layout even if the data file is missing, malformed,
|
|
// or simply omits "en".
|
|
if (find_layout(registry, "en") < 0) {
|
|
if (registry.count >= MAX_LAYOUTS) registry.count = MAX_LAYOUTS - 1;
|
|
for (int i = registry.count; i > 0; i--)
|
|
registry.items[i] = registry.items[i - 1];
|
|
registry.count++;
|
|
Registry base = {};
|
|
add_base_layout(&base);
|
|
registry.items[0] = base.items[0];
|
|
}
|
|
return registry;
|
|
}
|
|
|
|
// Re-read only the user's selection, leaving the registry alone. The registry
|
|
// is read-only OS data that cannot change while the machine is running, so
|
|
// callers polling for layout changes should use this rather than load_user:
|
|
// it reads one small file instead of re-parsing the whole layout table.
|
|
inline void refresh_selection(State* state, const char* username) {
|
|
if (!state) return;
|
|
state->pending_dead = 0; // a dead key never carries over a layout switch
|
|
for (int i = 0; i < state->registry.count; i++) state->enabled[i] = false;
|
|
|
|
toml::Doc doc = config::load_user(username, "keyboard");
|
|
for (int i = 0; i < state->registry.count; i++) {
|
|
char key[48];
|
|
build_key(key, sizeof(key), "layouts.", state->registry.items[i].id);
|
|
state->enabled[i] = doc.get_bool(key, i == 0);
|
|
}
|
|
const char* active_id = doc.get_string("selection.active", "en");
|
|
state->active = find_layout(state->registry, active_id);
|
|
doc.destroy();
|
|
|
|
if (state->registry.count == 0) return;
|
|
if (state->active < 0 || !state->enabled[state->active]) {
|
|
state->active = 0;
|
|
while (state->active < state->registry.count
|
|
&& !state->enabled[state->active])
|
|
state->active++;
|
|
}
|
|
if (state->active >= state->registry.count) {
|
|
state->active = 0;
|
|
state->enabled[0] = true;
|
|
}
|
|
}
|
|
|
|
inline State load_user(const char* username) {
|
|
State state = {};
|
|
state.registry = load_registry();
|
|
refresh_selection(&state, username);
|
|
return state;
|
|
}
|
|
|
|
inline bool save_user(const char* username, const State& state) {
|
|
toml::Doc doc;
|
|
doc.init();
|
|
for (int i = 0; i < state.registry.count; i++) {
|
|
char key[48];
|
|
build_key(key, sizeof(key), "layouts.", state.registry.items[i].id);
|
|
config::set_bool(&doc, key, state.enabled[i]);
|
|
}
|
|
int active = state.active >= 0 && state.active < state.registry.count
|
|
? state.active : 0;
|
|
config::set_string(&doc, "selection.active", state.registry.items[active].id);
|
|
int result = config::save_user(username, "keyboard", &doc);
|
|
doc.destroy();
|
|
return result == 0;
|
|
}
|
|
|
|
// NOTE: layouts are per-user by design. The login screen runs before there is
|
|
// a user, so it stays US-English until it grows its own layout switcher; there
|
|
// is deliberately no machine-wide "current layout" for it to read, because a
|
|
// wrong guess there is unrecoverable (you cannot type your password to fix it).
|
|
|
|
inline int next_enabled(const State& state, int current) {
|
|
if (state.registry.count <= 0) return 0;
|
|
for (int step = 1; step <= state.registry.count; step++) {
|
|
int candidate = (current + step) % state.registry.count;
|
|
if (state.enabled[candidate]) return candidate;
|
|
}
|
|
return current;
|
|
}
|
|
|
|
// Caps Lock is derived rather than declared per key: it applies only where
|
|
// base and shift are the lower/upper case of one letter. That covers accented
|
|
// letters in any script (aa/AA, e-caron/E-caron) without wrongly upper-casing
|
|
// keys such as 2 / " where the shifted value is unrelated punctuation.
|
|
inline constexpr bool is_letter_pair(uint32_t base, uint32_t shift) {
|
|
if (base == 0 || shift == 0) return false;
|
|
return utf8::is_case_pair(base, shift);
|
|
}
|
|
|
|
static_assert(is_letter_pair(0xE5, 0xC5)); // aa / AA
|
|
static_assert(is_letter_pair(0xF8, 0xD8)); // oe / OE
|
|
static_assert(is_letter_pair(0x11B, 0x11A)); // e-caron
|
|
static_assert(is_letter_pair('a', 'A'));
|
|
static_assert(!is_letter_pair('2', '"'));
|
|
static_assert(!is_letter_pair(0xF7, 0xD7)); // divide / multiply
|
|
static_assert(!is_letter_pair(0x11B, '2')); // Czech number row
|
|
|
|
inline bool is_dead_key(const Layout& layout, uint32_t cp) {
|
|
if (cp == 0) return false;
|
|
for (int i = 0; i < layout.dead_count; i++)
|
|
if (layout.dead_keys[i] == cp) return true;
|
|
return false;
|
|
}
|
|
|
|
// Keys that may be pressed between a dead key and its letter without
|
|
// cancelling it (Shift for a capital, AltGr, Caps Lock, ...).
|
|
inline constexpr bool is_modifier_scancode(uint8_t sc) {
|
|
return sc == 0x2A || sc == 0x36 || sc == 0x1D || sc == 0x38 ||
|
|
sc == 0x3A || sc == 0x45 || sc == 0x46 || sc == 0x5B || sc == 0x5C;
|
|
}
|
|
|
|
// Apply the active layout to a raw key event: the character it types goes to
|
|
// key->codepoint, its Windows-1252 byte (or KEY_ASCII_SUBSTITUTE) to
|
|
// key->ascii. Dead keys type nothing and modify the following character, so
|
|
// the state is updated on presses.
|
|
inline void translate(State& state, abi::KeyEvent* key) {
|
|
if (!key) return;
|
|
const Layout* layout = (state.active >= 0 && state.active < state.registry.count)
|
|
? &state.registry.items[state.active] : nullptr;
|
|
|
|
uint8_t a = (uint8_t)key->ascii;
|
|
uint32_t cp = key->codepoint ? key->codepoint
|
|
: ((a >= 0x20 && a < 0x7F) ? a : 0);
|
|
uint8_t scancode = key->scancode & 0x7F;
|
|
|
|
// Extended keys carry a main-block scancode with the E0 prefix stripped
|
|
// (keypad "/" arrives as 0x35, the same as the main "/"), so translating
|
|
// them would turn keypad "/" into whatever the layout puts on that key.
|
|
if (layout && !key->extended) {
|
|
for (int i = 0; i < layout->key_count; i++) {
|
|
const KeyMap& mapping = layout->keys[i];
|
|
if (mapping.scancode != scancode) continue;
|
|
|
|
uint32_t out;
|
|
if (key->altgr) {
|
|
out = mapping.altgr;
|
|
} else {
|
|
bool upper = key->shift;
|
|
if (is_letter_pair(mapping.base, mapping.shift))
|
|
upper = key->shift != key->capslock;
|
|
out = upper ? mapping.shift : mapping.base;
|
|
}
|
|
if (out != 0) {
|
|
cp = out;
|
|
// An AltGr key that produced a character is text, not a
|
|
// shortcut. Apps gate insertion on !alt (and alt is
|
|
// LeftAlt||RightAlt), so leaving it set would silently swallow
|
|
// every AltGr character.
|
|
if (key->altgr) key->alt = false;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool dead = layout && !key->extended && is_dead_key(*layout, cp);
|
|
bool shortcut = key->ctrl || (key->alt && !key->altgr);
|
|
if (dead && shortcut) dead = false;
|
|
|
|
if (key->pressed && !shortcut) {
|
|
if (dead) {
|
|
if (state.pending_dead == cp) {
|
|
state.pending_dead = 0; // pressed twice: type the accent
|
|
dead = false;
|
|
} else {
|
|
state.pending_dead = cp; // wait for the next character
|
|
}
|
|
} else if (state.pending_dead) {
|
|
if (cp == ' ') {
|
|
cp = state.pending_dead; // accent + Space: the accent
|
|
state.pending_dead = 0;
|
|
} else if (cp) {
|
|
// The composed letter, or just the letter when the accent does
|
|
// not apply to it (one event can only carry one character).
|
|
uint32_t composed = compose(state.pending_dead, cp);
|
|
if (composed) cp = composed;
|
|
state.pending_dead = 0;
|
|
} else if (!is_modifier_scancode(scancode)) {
|
|
state.pending_dead = 0; // Enter, arrows, ... cancel it
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dead) {
|
|
key->codepoint = 0;
|
|
key->ascii = 0;
|
|
return;
|
|
}
|
|
key->codepoint = cp;
|
|
if (cp != 0) {
|
|
uint8_t b = utf8::to_cp1252(cp);
|
|
key->ascii = b ? (char)b : abi::KEY_ASCII_SUBSTITUTE;
|
|
}
|
|
}
|
|
|
|
// ---- Direct keyboard readers ----
|
|
// The desktop translates events before routing them to windows, but programs
|
|
// that read the kernel buffer themselves (terminal, login) never pass through
|
|
// it and would otherwise always get the US layout. They translate through this
|
|
// instead. The registry is parsed once; only the small per-user selection file
|
|
// is re-read, at most once a second, so switching layout in the panel reaches
|
|
// them shortly afterwards without re-parsing the whole layout table.
|
|
|
|
struct DirectInput {
|
|
State state;
|
|
bool loaded;
|
|
uint64_t last_poll;
|
|
};
|
|
|
|
inline DirectInput& direct_input() {
|
|
static DirectInput input; // zero-initialised POD, so no guard variable
|
|
return input;
|
|
}
|
|
|
|
inline void translate_direct(abi::KeyEvent* key, const char* username) {
|
|
DirectInput& input = direct_input();
|
|
uint64_t now = montauk::get_milliseconds();
|
|
if (!input.loaded) {
|
|
input.state = load_user(username);
|
|
input.loaded = true;
|
|
input.last_poll = now;
|
|
} else if (now - input.last_poll >= 1000) {
|
|
refresh_selection(&input.state, username);
|
|
input.last_poll = now;
|
|
}
|
|
translate(input.state, key);
|
|
}
|
|
|
|
} // namespace montauk::keyboard
|