feat: switch text to UTF-8; add Unicode key events, dead keys & Czech layout; fix numpad navigation; add NumLock support
This commit is contained in:
1 parent
32660ddd1a
commit
2aa6360b3d
101 files changed
+3733
-1639
No files matched your search
@@ -8,27 +8,34 @@
|
||||
|
||||
#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_LAYOUTS = 8;
|
||||
inline constexpr int MAX_KEYS = 64;
|
||||
inline constexpr int MAX_DEAD_KEYS = 16;
|
||||
|
||||
// One overridden key. Characters are Windows-1252 bytes to match the
|
||||
// single-byte GUI text stack; 0 means "no override, keep the kernel's value".
|
||||
// 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;
|
||||
uint8_t base;
|
||||
uint8_t shift;
|
||||
uint8_t altgr;
|
||||
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)
|
||||
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 {
|
||||
@@ -40,8 +47,71 @@ 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;
|
||||
@@ -54,10 +124,9 @@ inline void build_key(char* out, int cap, const char* prefix,
|
||||
out[pos] = '\0';
|
||||
}
|
||||
|
||||
// Read an array of byte values from the layout table.
|
||||
// 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) {
|
||||
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;
|
||||
@@ -70,6 +139,23 @@ inline int read_bytes(const toml::Doc& doc, const char* key,
|
||||
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.
|
||||
@@ -85,10 +171,15 @@ inline bool load_layout(Layout* out, const char* id, const toml::Doc& doc) {
|
||||
montauk::strncpy(out->short_name, doc.get_string(key, id),
|
||||
sizeof(out->short_name));
|
||||
|
||||
uint8_t scancodes[MAX_KEYS];
|
||||
uint8_t base[MAX_KEYS];
|
||||
uint8_t shift[MAX_KEYS];
|
||||
uint8_t altgr[MAX_KEYS] = {};
|
||||
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);
|
||||
@@ -99,11 +190,11 @@ inline bool load_layout(Layout* out, const char* id, const toml::Doc& doc) {
|
||||
}
|
||||
|
||||
build_key(key, sizeof(key), "layouts.", id, ".base");
|
||||
if (read_bytes(doc, key, base, MAX_KEYS) != count) return false;
|
||||
if (read_codepoints(doc, key, base, MAX_KEYS) != count) return false;
|
||||
build_key(key, sizeof(key), "layouts.", id, ".shift");
|
||||
if (read_bytes(doc, key, shift, MAX_KEYS) != count) return false;
|
||||
if (read_codepoints(doc, key, shift, MAX_KEYS) != count) return false;
|
||||
build_key(key, sizeof(key), "layouts.", id, ".altgr");
|
||||
int altgr_count = read_bytes(doc, key, altgr, MAX_KEYS);
|
||||
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++) {
|
||||
@@ -174,6 +265,7 @@ inline Registry load_registry() {
|
||||
// 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");
|
||||
@@ -237,57 +329,117 @@ inline int next_enabled(const State& state, int current) {
|
||||
}
|
||||
|
||||
// Caps Lock is derived rather than declared per key: it applies only where
|
||||
// base and shift are a Windows-1252 lower/upper letter pair. That covers the
|
||||
// accented letters (aa 0xE5 / AA 0xC5) without wrongly upper-casing keys such
|
||||
// as 2 / " where the shifted value is unrelated punctuation.
|
||||
inline constexpr bool is_letter_pair(uint8_t base, uint8_t shift) {
|
||||
// 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;
|
||||
if (base < 0x61) return false;
|
||||
if (base > 0x7A && base < 0xE0) return false;
|
||||
if (base == 0xF7) return false; // division sign sits inside the range
|
||||
return shift == (uint8_t)(base - 0x20);
|
||||
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(0xE6, 0xC6)); // ae / AE
|
||||
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(0xF7, 0xD7)); // divide / multiply
|
||||
static_assert(!is_letter_pair(0x11B, '2')); // Czech number row
|
||||
|
||||
inline void translate(const State& state, abi::KeyEvent* key) {
|
||||
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;
|
||||
if (state.active < 0 || state.active >= state.registry.count) return;
|
||||
const Layout& layout = state.registry.items[state.active];
|
||||
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 (key->extended) return;
|
||||
if (layout && !key->extended) {
|
||||
for (int i = 0; i < layout->key_count; i++) {
|
||||
const KeyMap& mapping = layout->keys[i];
|
||||
if (mapping.scancode != scancode) continue;
|
||||
|
||||
uint8_t scancode = key->scancode & 0x7F;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_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) {
|
||||
key->ascii = (char)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;
|
||||
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 ----
|
||||
|
||||
Reference in new issue
Block a user