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 ----
|
||||
|
||||
@@ -56,27 +56,28 @@ inline const char* GetStatusText(StatusCode statusCode) {
|
||||
}
|
||||
}
|
||||
|
||||
// Shared with C (libc.c), so no nullptr.
|
||||
inline const char* GetStatusExplanationText(StatusCode statusCode) {
|
||||
const char* explanationText[] = {
|
||||
nullptr, // 0
|
||||
nullptr, // 1
|
||||
nullptr, // 2
|
||||
nullptr, // 3
|
||||
0, // 0
|
||||
0, // 1
|
||||
0, // 2
|
||||
0, // 3
|
||||
"The path is protected by the system and cannot be modified.", // 4
|
||||
nullptr, // 5
|
||||
nullptr, // 6
|
||||
nullptr, // 7
|
||||
nullptr, // 8
|
||||
nullptr, // 9
|
||||
nullptr, // 10
|
||||
nullptr, // 11
|
||||
nullptr, // 12
|
||||
0, // 5
|
||||
0, // 6
|
||||
0, // 7
|
||||
0, // 8
|
||||
0, // 9
|
||||
0, // 10
|
||||
0, // 11
|
||||
0, // 12
|
||||
"The buffer supplied by the caller is too small.", // 13
|
||||
nullptr, // 14
|
||||
0, // 14
|
||||
};
|
||||
|
||||
if (statusCode > 0 && statusCode <= 14) {
|
||||
if (explanationText[statusCode] != nullptr) {
|
||||
if (explanationText[statusCode] != 0) {
|
||||
return explanationText[statusCode];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
#include <Api/Syscall.hpp>
|
||||
#include <montauk/statuscodes.h>
|
||||
#include <montauk/utf8.h>
|
||||
|
||||
namespace montauk {
|
||||
|
||||
@@ -222,7 +223,23 @@ namespace montauk {
|
||||
|
||||
// Keyboard
|
||||
inline bool is_key_available() { return (bool)syscall0(montauk::abi::SYS_ISKEYAVAILABLE); }
|
||||
inline void getkey(montauk::abi::KeyEvent* out) { syscall1(montauk::abi::SYS_GETKEY, (uint64_t)out); }
|
||||
// Scancode to use when checking for navigation keys (arrows, Home/End,
|
||||
// PgUp/PgDn, Insert/Delete). The keypad shares those scancodes with its
|
||||
// digit and '.' keys, so a keypad key that types a character reports 0
|
||||
// here: numpad 4 is text, not Left. Compare against this, not .scancode.
|
||||
inline uint8_t nav_scancode(const montauk::abi::KeyEvent& key) {
|
||||
uint8_t sc = key.scancode;
|
||||
if (!key.extended && (sc & 0x7F) >= 0x47 && (sc & 0x7F) <= 0x53 &&
|
||||
montauk::utf8::key_codepoint(key) != 0)
|
||||
return 0;
|
||||
return sc;
|
||||
}
|
||||
|
||||
// Fills the full event, including the Unicode codepoint the key types.
|
||||
inline void getkey(montauk::abi::KeyEvent* out) {
|
||||
syscall2(montauk::abi::SYS_GETKEY_EX, (uint64_t)out, sizeof(*out));
|
||||
}
|
||||
// Text keys arrive as UTF-8, one byte per call.
|
||||
inline char getchar() { return (char)syscall0(montauk::abi::SYS_GETCHAR); }
|
||||
// Returns 0 rather than blocking when no character is pending.
|
||||
inline char getchar_nb() { return (char)syscall0(montauk::abi::SYS_GETCHAR_NB); }
|
||||
@@ -496,7 +513,8 @@ namespace montauk {
|
||||
return (int)syscall3(montauk::abi::SYS_CHILDIO_WRITE, (uint64_t)childPid, (uint64_t)data, (uint64_t)len);
|
||||
}
|
||||
inline int childio_writekey(int childPid, const montauk::abi::KeyEvent* key) {
|
||||
return (int)syscall2(montauk::abi::SYS_CHILDIO_WRITEKEY, (uint64_t)childPid, (uint64_t)key);
|
||||
return (int)syscall3(montauk::abi::SYS_CHILDIO_WRITEKEY_EX, (uint64_t)childPid,
|
||||
(uint64_t)key, sizeof(*key));
|
||||
}
|
||||
inline int childio_settermsz(int childPid, int cols, int rows) {
|
||||
return (int)syscall3(montauk::abi::SYS_CHILDIO_SETTERMSZ, (uint64_t)childPid, (uint64_t)cols, (uint64_t)rows);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <montauk/string.h>
|
||||
#include <montauk/heap.h>
|
||||
#include <montauk/config.h>
|
||||
#include <montauk/utf8.h>
|
||||
#include <bearssl_hash.h>
|
||||
|
||||
namespace montauk {
|
||||
@@ -107,7 +108,7 @@ namespace user {
|
||||
bytes_to_hex(hash, 32, out_hex64);
|
||||
}
|
||||
|
||||
inline bool verify_password(const char* password, const char* salt_hex, const char* stored_hex64) {
|
||||
inline bool verify_password_bytes(const char* password, const char* salt_hex, const char* stored_hex64) {
|
||||
char computed[65];
|
||||
hash_password(password, salt_hex, computed);
|
||||
|
||||
@@ -118,6 +119,26 @@ namespace user {
|
||||
return diff == 0;
|
||||
}
|
||||
|
||||
// Passwords are hashed as UTF-8. Before the switch to UTF-8, password
|
||||
// fields stored typed characters as Windows-1252 bytes, so a non-ASCII
|
||||
// password set back then only matches its 1252 form; try that too.
|
||||
inline bool verify_password(const char* password, const char* salt_hex, const char* stored_hex64) {
|
||||
if (verify_password_bytes(password, salt_hex, stored_hex64)) return true;
|
||||
|
||||
char legacy[256];
|
||||
int o = 0;
|
||||
bool non_ascii = false;
|
||||
for (const char* p = password; *p;) {
|
||||
uint32_t cp = montauk::utf8::next(&p);
|
||||
uint8_t b = montauk::utf8::to_cp1252(cp);
|
||||
if (b == 0 || o >= (int)sizeof(legacy) - 1) return false;
|
||||
if (b >= 0x80) non_ascii = true;
|
||||
legacy[o++] = (char)b;
|
||||
}
|
||||
legacy[o] = '\0';
|
||||
return non_ascii && verify_password_bytes(legacy, salt_hex, stored_hex64);
|
||||
}
|
||||
|
||||
// ---- User database I/O ----
|
||||
|
||||
inline int load_users(UserInfo* buf, int max) {
|
||||
|
||||
@@ -0,0 +1,399 @@
|
||||
/*
|
||||
* utf8.h
|
||||
* UTF-8 text encoding helpers with a Windows-1252 fallback
|
||||
* Copyright (c) 2026 Daniel Hammer
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
// MontaukOS text is UTF-8. Before this it was single-byte Windows-1252, so
|
||||
// older files (and anything else that is not valid UTF-8) still turn up. The
|
||||
// decoder never fails: a byte that does not start a well-formed sequence is
|
||||
// read as ONE Windows-1252 character. Valid UTF-8 and plain ASCII are
|
||||
// unaffected, and legacy text keeps rendering exactly as it used to.
|
||||
//
|
||||
// Character boundaries follow the same rule, so cursor movement, deletion and
|
||||
// truncation all agree with what the renderer draws.
|
||||
|
||||
namespace montauk::utf8 {
|
||||
|
||||
inline constexpr uint32_t REPLACEMENT = 0xFFFD;
|
||||
inline constexpr int MAX_BYTES = 4;
|
||||
|
||||
// Windows-1252 byte -> Unicode codepoint. 0x80-0x9F carry the typographic
|
||||
// punctuation; every other byte is identical to Latin-1 / Unicode.
|
||||
inline constexpr uint32_t from_cp1252(uint8_t b) {
|
||||
switch (b) {
|
||||
case 0x80: return 0x20AC; // Euro Sign
|
||||
case 0x82: return 0x201A; // Single Low-9 Quotation Mark
|
||||
case 0x83: return 0x0192; // Latin Small Letter F With Hook
|
||||
case 0x84: return 0x201E; // Double Low-9 Quotation Mark
|
||||
case 0x85: return 0x2026; // Horizontal Ellipsis
|
||||
case 0x86: return 0x2020; // Dagger
|
||||
case 0x87: return 0x2021; // Double Dagger
|
||||
case 0x88: return 0x02C6; // Modifier Letter Circumflex Accent
|
||||
case 0x89: return 0x2030; // Per Mille Sign
|
||||
case 0x8A: return 0x0160; // Latin Capital Letter S With Caron
|
||||
case 0x8B: return 0x2039; // Single Left-Pointing Angle Quotation Mark
|
||||
case 0x8C: return 0x0152; // Latin Capital Ligature OE
|
||||
case 0x8E: return 0x017D; // Latin Capital Letter Z With Caron
|
||||
case 0x91: return 0x2018; // Left Single Quotation Mark
|
||||
case 0x92: return 0x2019; // Right Single Quotation Mark
|
||||
case 0x93: return 0x201C; // Left Double Quotation Mark
|
||||
case 0x94: return 0x201D; // Right Double Quotation Mark
|
||||
case 0x95: return 0x2022; // Bullet
|
||||
case 0x96: return 0x2013; // En Dash
|
||||
case 0x97: return 0x2014; // Em Dash
|
||||
case 0x98: return 0x02DC; // Small Tilde
|
||||
case 0x99: return 0x2122; // Trade Mark Sign
|
||||
case 0x9A: return 0x0161; // Latin Small Letter S With Caron
|
||||
case 0x9B: return 0x203A; // Single Right-Pointing Angle Quotation Mark
|
||||
case 0x9C: return 0x0153; // Latin Small Ligature OE
|
||||
case 0x9E: return 0x017E; // Latin Small Letter Z With Caron
|
||||
case 0x9F: return 0x0178; // Latin Capital Letter Y With Diaeresis
|
||||
default: return b;
|
||||
}
|
||||
}
|
||||
|
||||
// Unicode codepoint -> Windows-1252 byte, or 0 if it has none. Used where a
|
||||
// single byte is still required (PDF WinAnsi output, legacy byte tables).
|
||||
inline constexpr uint8_t to_cp1252(uint32_t cp) {
|
||||
if (cp < 0x80 || (cp >= 0xA0 && cp <= 0xFF)) return (uint8_t)cp;
|
||||
for (int b = 0x80; b <= 0x9F; b++) {
|
||||
uint32_t m = from_cp1252((uint8_t)b);
|
||||
if (m != (uint32_t)b && m == cp) return (uint8_t)b;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Length of the well-formed UTF-8 sequence starting at s (at most `avail`
|
||||
// bytes, stopping early at a NUL), or 0 if it is not one. Rejects overlong
|
||||
// forms, surrogates and values above U+10FFFF.
|
||||
inline int sequence_length(const char* s, int avail, uint32_t* out_cp = nullptr) {
|
||||
if (avail <= 0) return 0;
|
||||
const uint8_t* u = (const uint8_t*)s;
|
||||
uint8_t b0 = u[0];
|
||||
if (b0 < 0x80) {
|
||||
if (out_cp) *out_cp = b0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int n;
|
||||
uint32_t cp, min;
|
||||
if (b0 >= 0xC2 && b0 <= 0xDF) { n = 2; cp = b0 & 0x1F; min = 0x80; }
|
||||
else if (b0 >= 0xE0 && b0 <= 0xEF) { n = 3; cp = b0 & 0x0F; min = 0x800; }
|
||||
else if (b0 >= 0xF0 && b0 <= 0xF4) { n = 4; cp = b0 & 0x07; min = 0x10000; }
|
||||
else return 0;
|
||||
|
||||
if (n > avail) return 0;
|
||||
for (int i = 1; i < n; i++) {
|
||||
uint8_t c = u[i];
|
||||
if ((c & 0xC0) != 0x80) return 0; // also catches NUL
|
||||
cp = (cp << 6) | (c & 0x3F);
|
||||
}
|
||||
if (cp < min || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) return 0;
|
||||
if (out_cp) *out_cp = cp;
|
||||
return n;
|
||||
}
|
||||
|
||||
// Decode one character from a buffer of `avail` bytes. Returns the codepoint
|
||||
// and stores the number of bytes consumed (always >= 1 when avail > 0).
|
||||
inline uint32_t decode(const char* s, int avail, int* consumed) {
|
||||
if (avail <= 0) { if (consumed) *consumed = 0; return 0; }
|
||||
uint32_t cp;
|
||||
int n = sequence_length(s, avail, &cp);
|
||||
if (n == 0) {
|
||||
n = 1;
|
||||
cp = from_cp1252((uint8_t)s[0]);
|
||||
}
|
||||
if (consumed) *consumed = n;
|
||||
return cp;
|
||||
}
|
||||
|
||||
// Decode one character from a NUL-terminated string and advance *p past it.
|
||||
// Returns 0 (without advancing) at the terminator.
|
||||
inline uint32_t next(const char** p) {
|
||||
const char* s = *p;
|
||||
if (!*s) return 0;
|
||||
int n;
|
||||
// A sequence never spans the terminator: sequence_length stops at NUL
|
||||
// because NUL is not a continuation byte.
|
||||
uint32_t cp = decode(s, MAX_BYTES, &n);
|
||||
*p = s + n;
|
||||
return cp;
|
||||
}
|
||||
|
||||
// Encode a codepoint as UTF-8 into out (room for 4 bytes, NOT terminated).
|
||||
// Returns the byte count. Invalid codepoints encode U+FFFD.
|
||||
inline int encode(uint32_t cp, char* out) {
|
||||
if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) cp = REPLACEMENT;
|
||||
uint8_t* o = (uint8_t*)out;
|
||||
if (cp < 0x80) { o[0] = (uint8_t)cp; return 1; }
|
||||
if (cp < 0x800) {
|
||||
o[0] = (uint8_t)(0xC0 | (cp >> 6));
|
||||
o[1] = (uint8_t)(0x80 | (cp & 0x3F));
|
||||
return 2;
|
||||
}
|
||||
if (cp < 0x10000) {
|
||||
o[0] = (uint8_t)(0xE0 | (cp >> 12));
|
||||
o[1] = (uint8_t)(0x80 | ((cp >> 6) & 0x3F));
|
||||
o[2] = (uint8_t)(0x80 | (cp & 0x3F));
|
||||
return 3;
|
||||
}
|
||||
o[0] = (uint8_t)(0xF0 | (cp >> 18));
|
||||
o[1] = (uint8_t)(0x80 | ((cp >> 12) & 0x3F));
|
||||
o[2] = (uint8_t)(0x80 | ((cp >> 6) & 0x3F));
|
||||
o[3] = (uint8_t)(0x80 | (cp & 0x3F));
|
||||
return 4;
|
||||
}
|
||||
|
||||
inline int encoded_length(uint32_t cp) {
|
||||
if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) return 3; // U+FFFD
|
||||
return cp < 0x80 ? 1 : cp < 0x800 ? 2 : cp < 0x10000 ? 3 : 4;
|
||||
}
|
||||
|
||||
// ==== Case mapping ====
|
||||
// Simple one-to-one case mapping for the scripts MontaukOS keyboard layouts
|
||||
// cover: ASCII, Latin-1, Latin Extended-A, Greek and Cyrillic. Anything else
|
||||
// is returned unchanged.
|
||||
|
||||
inline constexpr uint32_t to_upper(uint32_t c) {
|
||||
if (c >= 'a' && c <= 'z') return c - 0x20;
|
||||
if (c < 0x80) return c;
|
||||
if (c >= 0xE0 && c <= 0xFE && c != 0xF7) return c - 0x20;
|
||||
if (c == 0xFF) return 0x178;
|
||||
if (c == 0xB5) return 0x39C; // micro -> Mu
|
||||
// Latin Extended-A: pairs alternate upper/lower, except the two ranges
|
||||
// where the alignment shifts by one (L-stroke..N-apostrophe, Z-acute..).
|
||||
if (c == 0x131) return 'I'; // dotless i
|
||||
if (c >= 0x100 && c <= 0x137 && c != 0x130) return (c & 1) ? c - 1 : c;
|
||||
if (c >= 0x139 && c <= 0x148) return (c & 1) ? c : c - 1;
|
||||
if (c >= 0x14A && c <= 0x177) return (c & 1) ? c - 1 : c;
|
||||
if (c >= 0x179 && c <= 0x17E) return (c & 1) ? c : c - 1;
|
||||
if (c == 0x17F) return 'S'; // long s
|
||||
// Greek
|
||||
if (c >= 0x3B1 && c <= 0x3C9 && c != 0x3C2) return c - 0x20;
|
||||
if (c == 0x3C2) return 0x3A3; // final sigma
|
||||
if (c == 0x3AC) return 0x386;
|
||||
if (c >= 0x3AD && c <= 0x3AF) return c - 0x25;
|
||||
if (c == 0x3CC) return 0x38C;
|
||||
if (c == 0x3CD || c == 0x3CE) return c - 0x3F;
|
||||
// Cyrillic
|
||||
if (c >= 0x430 && c <= 0x44F) return c - 0x20;
|
||||
if (c >= 0x450 && c <= 0x45F) return c - 0x50;
|
||||
return c;
|
||||
}
|
||||
|
||||
inline constexpr uint32_t to_lower(uint32_t c) {
|
||||
if (c >= 'A' && c <= 'Z') return c + 0x20;
|
||||
if (c < 0x80) return c;
|
||||
if (c >= 0xC0 && c <= 0xDE && c != 0xD7) return c + 0x20;
|
||||
if (c == 0x178) return 0xFF;
|
||||
if (c == 0x130) return 'i'; // dotted I
|
||||
if (c >= 0x100 && c <= 0x137) return (c & 1) ? c : c + 1;
|
||||
if (c >= 0x139 && c <= 0x148) return (c & 1) ? c + 1 : c;
|
||||
if (c >= 0x14A && c <= 0x177) return (c & 1) ? c : c + 1;
|
||||
if (c >= 0x179 && c <= 0x17E) return (c & 1) ? c + 1 : c;
|
||||
if (c >= 0x391 && c <= 0x3A9 && c != 0x3A2) return c + 0x20;
|
||||
if (c == 0x386) return 0x3AC;
|
||||
if (c >= 0x388 && c <= 0x38A) return c + 0x25;
|
||||
if (c == 0x38C) return 0x3CC;
|
||||
if (c == 0x38E || c == 0x38F) return c + 0x3F;
|
||||
if (c >= 0x410 && c <= 0x42F) return c + 0x20;
|
||||
if (c >= 0x400 && c <= 0x40F) return c + 0x50;
|
||||
return c;
|
||||
}
|
||||
|
||||
// True if lower/upper are the two cases of one letter.
|
||||
inline constexpr bool is_case_pair(uint32_t lower, uint32_t upper) {
|
||||
return lower != upper && to_upper(lower) == upper && to_lower(upper) == lower;
|
||||
}
|
||||
|
||||
// Text a key event types, as a codepoint (0 for control/non-text keys).
|
||||
// KeyEvent::codepoint is filled by the kernel and by layout translation;
|
||||
// events built by hand may leave it 0, so fall back to `ascii` (a
|
||||
// Windows-1252 byte).
|
||||
template <typename Key>
|
||||
inline uint32_t key_codepoint(const Key& key) {
|
||||
if (key.codepoint) {
|
||||
uint32_t cp = key.codepoint;
|
||||
return (cp < 0x20 || cp == 0x7F || (cp >= 0x80 && cp < 0xA0)) ? 0 : cp;
|
||||
}
|
||||
uint8_t b = (uint8_t)key.ascii;
|
||||
if (b < 0x20 || b == 0x7F) return 0;
|
||||
return from_cp1252(b);
|
||||
}
|
||||
|
||||
// Same, encoded as UTF-8 into out (4 bytes). Returns the length, 0 if none.
|
||||
template <typename Key>
|
||||
inline int key_utf8(const Key& key, char* out) {
|
||||
uint32_t cp = key_codepoint(key);
|
||||
return cp ? encode(cp, out) : 0;
|
||||
}
|
||||
|
||||
// Byte offset of the character after the one at `pos` (len = buffer length).
|
||||
inline int next_boundary(const char* s, int len, int pos) {
|
||||
if (pos >= len) return len;
|
||||
if (pos < 0) return 0;
|
||||
int n;
|
||||
decode(s + pos, len - pos, &n);
|
||||
return pos + n;
|
||||
}
|
||||
|
||||
// Byte offset of the character before `pos`. A run of continuation bytes is
|
||||
// one character only if it completes a well-formed sequence ending exactly at
|
||||
// pos; otherwise the previous byte stands alone (Windows-1252 fallback).
|
||||
inline int prev_boundary(const char* s, int pos) {
|
||||
if (pos <= 0) return 0;
|
||||
for (int back = 2; back <= MAX_BYTES && back <= pos; back++) {
|
||||
if (((uint8_t)s[pos - back + 1] & 0xC0) != 0x80) break;
|
||||
int start = pos - back;
|
||||
if (sequence_length(s + start, back) == back) return start;
|
||||
}
|
||||
return pos - 1;
|
||||
}
|
||||
|
||||
// Snap `pos` back onto a character boundary. Use before truncating a buffer
|
||||
// or splitting at an arbitrary byte offset.
|
||||
inline int floor_boundary(const char* s, int len, int pos) {
|
||||
if (pos <= 0) return 0;
|
||||
if (pos >= len) return len;
|
||||
if (((uint8_t)s[pos] & 0xC0) != 0x80) return pos;
|
||||
// pos is a continuation byte: it is inside a character only if a
|
||||
// well-formed sequence starting up to 3 bytes earlier covers it.
|
||||
for (int back = 1; back < MAX_BYTES && back <= pos; back++) {
|
||||
int start = pos - back;
|
||||
int n = sequence_length(s + start, len - start);
|
||||
if (n > back) return start;
|
||||
if (((uint8_t)s[start] & 0xC0) != 0x80) break;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
// Number of characters in a NUL-terminated string.
|
||||
inline int count(const char* s) {
|
||||
int n = 0;
|
||||
while (*s) { next(&s); n++; }
|
||||
return n;
|
||||
}
|
||||
|
||||
// Number of characters in the first `len` bytes.
|
||||
inline int count(const char* s, int len) {
|
||||
int n = 0, pos = 0;
|
||||
while (pos < len) { pos = next_boundary(s, len, pos); n++; }
|
||||
return n;
|
||||
}
|
||||
|
||||
// Byte offset of the `index`th character (clamped to len).
|
||||
inline int offset_of(const char* s, int len, int index) {
|
||||
int pos = 0;
|
||||
while (index-- > 0 && pos < len) pos = next_boundary(s, len, pos);
|
||||
return pos;
|
||||
}
|
||||
|
||||
// True if the buffer is entirely well-formed UTF-8.
|
||||
inline bool valid(const char* s, int len) {
|
||||
for (int pos = 0; pos < len;) {
|
||||
int n = sequence_length(s + pos, len - pos);
|
||||
if (n == 0) return false;
|
||||
pos += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Rewrite a buffer as well-formed UTF-8 (each stray byte becomes the
|
||||
// character the Windows-1252 fallback would display). Returns the output
|
||||
// length, truncated on a character boundary to fit cap - 1, NUL-terminated.
|
||||
inline int normalize(const char* in, int len, char* out, int cap) {
|
||||
if (cap <= 0) return 0;
|
||||
int o = 0;
|
||||
for (int pos = 0; pos < len;) {
|
||||
int n;
|
||||
uint32_t cp = decode(in + pos, len - pos, &n);
|
||||
int need = encoded_length(cp);
|
||||
if (o + need > cap - 1) break;
|
||||
o += encode(cp, out + o);
|
||||
pos += n;
|
||||
}
|
||||
out[o] = '\0';
|
||||
return o;
|
||||
}
|
||||
|
||||
// Copy src into dst (cap bytes incl. NUL) without splitting a character.
|
||||
inline int copy(char* dst, int cap, const char* src) {
|
||||
if (cap <= 0) return 0;
|
||||
int len = 0;
|
||||
while (src[len]) len++;
|
||||
int n = len < cap - 1 ? len : floor_boundary(src, len, cap - 1);
|
||||
for (int i = 0; i < n; i++) dst[i] = src[i];
|
||||
dst[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
// ==== Editing a NUL-terminated buffer with a byte cursor ====
|
||||
// `len` is the current length, `cap` the buffer size including the NUL. The
|
||||
// cursor is a byte offset that is always kept on a character boundary.
|
||||
|
||||
// Insert n bytes at *cursor. Returns false (buffer unchanged) if they don't fit.
|
||||
inline bool insert(char* buf, int* len, int cap, int* cursor,
|
||||
const char* s, int n) {
|
||||
if (n <= 0 || *len + n >= cap) return false;
|
||||
int at = *cursor < 0 ? 0 : (*cursor > *len ? *len : *cursor);
|
||||
for (int i = *len; i >= at; i--) buf[i + n] = buf[i]; // moves the NUL
|
||||
for (int i = 0; i < n; i++) buf[at + i] = s[i];
|
||||
*len += n;
|
||||
*cursor = at + n;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Insert the text a key event types. Returns false if it types nothing or
|
||||
// does not fit.
|
||||
template <typename Key>
|
||||
inline bool insert_key(char* buf, int* len, int cap, int* cursor, const Key& key) {
|
||||
char t[MAX_BYTES];
|
||||
int n = key_utf8(key, t);
|
||||
return n > 0 && insert(buf, len, cap, cursor, t, n);
|
||||
}
|
||||
|
||||
// Remove the bytes [start, end).
|
||||
inline void erase(char* buf, int* len, int start, int end) {
|
||||
if (start < 0) start = 0;
|
||||
if (end > *len) end = *len;
|
||||
if (start >= end) return;
|
||||
int n = end - start;
|
||||
for (int i = start; i + n <= *len; i++) buf[i] = buf[i + n]; // moves the NUL
|
||||
*len -= n;
|
||||
}
|
||||
|
||||
// Backspace: delete the character before the cursor.
|
||||
inline bool erase_before(char* buf, int* len, int* cursor) {
|
||||
if (*cursor <= 0) return false;
|
||||
int start = prev_boundary(buf, *cursor);
|
||||
erase(buf, len, start, *cursor);
|
||||
*cursor = start;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Delete: remove the character at the cursor.
|
||||
inline bool erase_at(char* buf, int* len, int cursor) {
|
||||
if (cursor >= *len) return false;
|
||||
erase(buf, len, cursor, next_boundary(buf, *len, cursor));
|
||||
return true;
|
||||
}
|
||||
|
||||
// Append typed text at the end (simple line inputs without a cursor).
|
||||
template <typename Key>
|
||||
inline bool append_key(char* buf, int* len, int cap, const Key& key) {
|
||||
int cursor = *len;
|
||||
return insert_key(buf, len, cap, &cursor, key);
|
||||
}
|
||||
|
||||
// Backspace for append-only inputs: drop the last character.
|
||||
inline bool erase_last(char* buf, int* len) {
|
||||
int cursor = *len;
|
||||
return erase_before(buf, len, &cursor);
|
||||
}
|
||||
|
||||
} // namespace montauk::utf8
|
||||
Reference in new issue
Block a user