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
@@ -271,6 +271,12 @@ namespace montauk::abi {
|
||||
static constexpr uint64_t SYS_CONSENT_NEXT = 192; // (ConsentRequest*) -> 1 delivered, 0 none queued
|
||||
static constexpr uint64_t SYS_CONSENT_REPLY = 193; // (requestId, CONSENT_ALLOW/DENY) -> 0, -1 stale id
|
||||
|
||||
/* Unicode key events. Size-versioned: the caller passes sizeof(its
|
||||
KeyEvent); the kernel copies min(size, its own) bytes and zero-fills the
|
||||
rest, so the struct can grow without another syscall. */
|
||||
static constexpr uint64_t SYS_GETKEY_EX = 194; // (KeyEvent*, size) -> 0; blocks like SYS_GETKEY
|
||||
static constexpr uint64_t SYS_CHILDIO_WRITEKEY_EX = 195; // (childPid, const KeyEvent*, size) -> status
|
||||
|
||||
/* Kernel-owned process capabilities. User identities may namespace
|
||||
per-user resources, but never participate in authorization decisions. */
|
||||
static constexpr uint64_t CAP_PROCESS_ADMIN = 1ULL << 0;
|
||||
@@ -531,7 +537,10 @@ namespace montauk::abi {
|
||||
uint32_t buildNumber; // monotonic kernel build number
|
||||
};
|
||||
|
||||
struct KeyEvent {
|
||||
// Legacy 9-byte key event, read by SYS_GETKEY and written by
|
||||
// SYS_CHILDIO_WRITEKEY. Kept so binaries built before codepoints still work.
|
||||
// `ascii` is a Windows-1252 byte.
|
||||
struct KeyEventV1 {
|
||||
uint8_t scancode;
|
||||
char ascii;
|
||||
bool pressed;
|
||||
@@ -547,6 +556,32 @@ namespace montauk::abi {
|
||||
bool extended;
|
||||
};
|
||||
|
||||
// Key event with the Unicode text it types (SYS_GETKEY_EX,
|
||||
// SYS_CHILDIO_WRITEKEY_EX, WinEvent). Its first 9 bytes ARE a KeyEventV1, so
|
||||
// a legacy reader sees a valid event. `codepoint` is the character the key
|
||||
// produces (0 for control/navigation keys and dead keys). `ascii` is its
|
||||
// Windows-1252 byte, or KEY_ASCII_SUBSTITUTE when 1252 has none.
|
||||
struct KeyEvent {
|
||||
uint8_t scancode;
|
||||
char ascii;
|
||||
bool pressed;
|
||||
bool shift;
|
||||
bool ctrl;
|
||||
bool alt;
|
||||
bool capslock;
|
||||
bool altgr;
|
||||
bool extended;
|
||||
uint8_t _reserved[3];
|
||||
uint32_t codepoint;
|
||||
};
|
||||
static_assert(sizeof(KeyEventV1) == 9, "KeyEventV1 is frozen ABI");
|
||||
static_assert(sizeof(KeyEvent) == 16, "KeyEvent must fit the WinEvent union");
|
||||
|
||||
// ASCII SUB: `ascii` of a text key whose character has no Windows-1252 byte.
|
||||
// A control character, so legacy code that only accepts ascii >= 0x20 skips
|
||||
// it, while code testing `ascii == 0` for navigation keys is not confused.
|
||||
static constexpr char KEY_ASCII_SUBSTITUTE = 0x1A;
|
||||
|
||||
struct MouseState {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
@@ -584,12 +619,13 @@ namespace montauk::abi {
|
||||
uint8_t type; // 0=key, 1=mouse, 2=resize, 3=close, 4=scale
|
||||
uint8_t _pad[3];
|
||||
union {
|
||||
KeyEvent key;
|
||||
KeyEvent key; // 16 bytes: the union was already that size (mouse)
|
||||
struct { int32_t x, y, scroll; uint8_t buttons, prev_buttons; } mouse;
|
||||
struct { int32_t w, h; } resize;
|
||||
struct { int32_t scale; } scale;
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(WinEvent) == 20, "WinEvent layout is shared with existing binaries");
|
||||
|
||||
struct WinInfo {
|
||||
int32_t id;
|
||||
|
||||
@@ -165,9 +165,9 @@ struct Canvas {
|
||||
if (!str || !font_data || scale <= 0) return;
|
||||
uint32_t pixel = c.to_pixel();
|
||||
int cx = x;
|
||||
for (int i = 0; str[i]; i++, cx += FONT_WIDTH * scale) {
|
||||
for (const char* p = str; *p; cx += FONT_WIDTH * scale) {
|
||||
const uint8_t* glyph =
|
||||
&font_data[(unsigned char)str[i] * FONT_HEIGHT];
|
||||
&font_data[cp437_index(montauk::utf8::next(&p)) * FONT_HEIGHT];
|
||||
for (int row = 0; row < FONT_HEIGHT; row++) {
|
||||
uint8_t bits = glyph[row];
|
||||
if (!bits) continue;
|
||||
|
||||
@@ -9,12 +9,32 @@
|
||||
#include <montauk/syscall.h>
|
||||
#include <montauk/string.h>
|
||||
#include <montauk/heap.h>
|
||||
#include <montauk/utf8.h>
|
||||
|
||||
namespace gui {
|
||||
|
||||
// The clipboard carries UTF-8. Text that is not well-formed (legacy
|
||||
// Windows-1252 bytes) is converted on the way in and on the way out, so
|
||||
// every reader can assume valid UTF-8.
|
||||
inline char* clipboard_utf8_copy(const char* text, int len, int* out_len) {
|
||||
int cap = len * 3 + 1; // each stray byte grows to at most 3
|
||||
char* out = (char*)montauk::malloc(cap);
|
||||
if (!out) return nullptr;
|
||||
*out_len = montauk::utf8::normalize(text, len, out, cap);
|
||||
return out;
|
||||
}
|
||||
|
||||
inline bool clipboard_set_text(const char* text, int len) {
|
||||
if (len < 0) return false;
|
||||
if (len > 0 && text == nullptr) return false;
|
||||
if (len > 0 && !montauk::utf8::valid(text, len)) {
|
||||
int fixed_len = 0;
|
||||
char* fixed = clipboard_utf8_copy(text, len, &fixed_len);
|
||||
if (!fixed) return false;
|
||||
bool ok = montauk::clipboard_set_text(fixed, (uint32_t)fixed_len) == 0;
|
||||
montauk::mfree(fixed);
|
||||
return ok;
|
||||
}
|
||||
return montauk::clipboard_set_text(text, (uint32_t)len) == 0;
|
||||
}
|
||||
|
||||
@@ -53,6 +73,14 @@ namespace gui {
|
||||
}
|
||||
|
||||
text[actual_len] = '\0';
|
||||
if (!montauk::utf8::valid(text, (int)actual_len)) {
|
||||
int fixed_len = 0;
|
||||
char* fixed = clipboard_utf8_copy(text, (int)actual_len, &fixed_len);
|
||||
montauk::mfree(text);
|
||||
if (!fixed) return nullptr;
|
||||
text = fixed;
|
||||
actual_len = (uint32_t)fixed_len;
|
||||
}
|
||||
if (out_len) *out_len = (int)actual_len;
|
||||
if (out_serial) *out_serial = serial;
|
||||
return text;
|
||||
|
||||
@@ -19,6 +19,34 @@ static constexpr int FONT_HEIGHT = 16;
|
||||
// TrueType fonts, while login/desktop can recover when TrueType setup fails.
|
||||
extern const uint8_t font_data[256 * 16] __attribute__((weak));
|
||||
|
||||
// The bitmap fallback font is Code Page 437. Map a Unicode codepoint to its
|
||||
// CP437 cell ('?' when it has none) so UTF-8 text still draws sensibly,
|
||||
// including the box-drawing characters terminal programs use.
|
||||
inline uint8_t cp437_index(uint32_t cp) {
|
||||
if (cp < 0x80) return (uint8_t)cp;
|
||||
static constexpr uint16_t high[128] = {
|
||||
0x00C7,0x00FC,0x00E9,0x00E2,0x00E4,0x00E0,0x00E5,0x00E7,
|
||||
0x00EA,0x00EB,0x00E8,0x00EF,0x00EE,0x00EC,0x00C4,0x00C5,
|
||||
0x00C9,0x00E6,0x00C6,0x00F4,0x00F6,0x00F2,0x00FB,0x00F9,
|
||||
0x00FF,0x00D6,0x00DC,0x00A2,0x00A3,0x00A5,0x20A7,0x0192,
|
||||
0x00E1,0x00ED,0x00F3,0x00FA,0x00F1,0x00D1,0x00AA,0x00BA,
|
||||
0x00BF,0x2310,0x00AC,0x00BD,0x00BC,0x00A1,0x00AB,0x00BB,
|
||||
0x2591,0x2592,0x2593,0x2502,0x2524,0x2561,0x2562,0x2556,
|
||||
0x2555,0x2563,0x2551,0x2557,0x255D,0x255C,0x255B,0x2510,
|
||||
0x2514,0x2534,0x252C,0x251C,0x2500,0x253C,0x255E,0x255F,
|
||||
0x255A,0x2554,0x2569,0x2566,0x2560,0x2550,0x256C,0x2567,
|
||||
0x2568,0x2564,0x2565,0x2559,0x2558,0x2552,0x2553,0x256B,
|
||||
0x256A,0x2518,0x250C,0x2588,0x2584,0x258C,0x2590,0x2580,
|
||||
0x03B1,0x00DF,0x0393,0x03C0,0x03A3,0x03C3,0x00B5,0x03C4,
|
||||
0x03A6,0x0398,0x03A9,0x03B4,0x221E,0x03C6,0x03B5,0x2229,
|
||||
0x2261,0x00B1,0x2265,0x2264,0x2320,0x2321,0x00F7,0x2248,
|
||||
0x00B0,0x2219,0x00B7,0x221A,0x207F,0x00B2,0x25A0,0x00A0,
|
||||
};
|
||||
for (int i = 0; i < 128; i++)
|
||||
if (high[i] == cp) return (uint8_t)(0x80 + i);
|
||||
return '?';
|
||||
}
|
||||
|
||||
// Dynamic font height: TTF line height or 16 (bitmap fallback)
|
||||
inline int system_font_height() {
|
||||
if (fonts::system_font && fonts::system_font->valid)
|
||||
@@ -74,8 +102,8 @@ inline void draw_text(Framebuffer& fb, int x, int y, const char* text, Color fg)
|
||||
fonts::system_font->draw(fb, x, y, text, fg, fonts::UI_SIZE);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; text[i]; i++) {
|
||||
draw_char(fb, x + i * FONT_WIDTH, y, text[i], fg);
|
||||
for (const char* p = text; *p; x += FONT_WIDTH) {
|
||||
draw_char(fb, x, y, (char)cp437_index(montauk::utf8::next(&p)), fg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,18 +112,23 @@ inline void draw_text_bg(Framebuffer& fb, int x, int y, const char* text, Color
|
||||
fonts::system_font->draw_bg(fb, x, y, text, fg, bg, fonts::UI_SIZE);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; text[i]; i++) {
|
||||
draw_char_bg(fb, x + i * FONT_WIDTH, y, text[i], fg, bg);
|
||||
for (const char* p = text; *p; x += FONT_WIDTH) {
|
||||
draw_char_bg(fb, x, y, (char)cp437_index(montauk::utf8::next(&p)), fg, bg);
|
||||
}
|
||||
}
|
||||
|
||||
// Width of the first `len` bytes of a UTF-8 string in the UI font.
|
||||
inline int text_width_n(const char* text, int len) {
|
||||
if (fonts::system_font && fonts::system_font->valid)
|
||||
return fonts::system_font->measure_text_n(text, len, fonts::UI_SIZE);
|
||||
return montauk::utf8::count(text, len) * FONT_WIDTH;
|
||||
}
|
||||
|
||||
inline int text_width(const char* text) {
|
||||
if (fonts::system_font && fonts::system_font->valid) {
|
||||
return fonts::system_font->measure_text(text, fonts::UI_SIZE);
|
||||
}
|
||||
int len = 0;
|
||||
while (text[len]) len++;
|
||||
return len * FONT_WIDTH;
|
||||
return montauk::utf8::count(text) * FONT_WIDTH;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
@@ -296,15 +296,13 @@ inline bool text_input_has_selection(const TextInputState& state, const char* te
|
||||
return text_input_selection_start(state, len) != text_input_selection_end(state, len);
|
||||
}
|
||||
|
||||
// Width of the first `count` bytes (a character boundary). Masked fields
|
||||
// draw one '*' per character, however many bytes it takes.
|
||||
inline int text_input_prefix_width(const char* text, int count, bool masked) {
|
||||
char prefix[512];
|
||||
int len = 0;
|
||||
while (text && text[len] && len < count && len < (int)sizeof(prefix) - 1) {
|
||||
prefix[len] = masked ? '*' : text[len];
|
||||
len++;
|
||||
}
|
||||
prefix[len] = '\0';
|
||||
return text_width(prefix);
|
||||
while (text && text[len] && len < count) len++;
|
||||
if (!masked) return text_width_n(text, len);
|
||||
return montauk::utf8::count(text, len) * text_width("*");
|
||||
}
|
||||
|
||||
inline int text_input_index_from_x(const Rect& bounds, const char* value,
|
||||
@@ -314,11 +312,13 @@ inline int text_input_index_from_x(const Rect& bounds, const char* value,
|
||||
|
||||
int len = text_input_len(value);
|
||||
int prev_w = 0;
|
||||
for (int i = 0; i < len; i++) {
|
||||
int cur_w = text_input_prefix_width(value, i + 1, masked);
|
||||
for (int i = 0; i < len;) {
|
||||
int next = montauk::utf8::next_boundary(value, len, i);
|
||||
int cur_w = text_input_prefix_width(value, next, masked);
|
||||
int midpoint = prev_w + (cur_w - prev_w) / 2;
|
||||
if (rel_x < midpoint) return i;
|
||||
prev_w = cur_w;
|
||||
i = next;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
@@ -360,8 +360,11 @@ inline int text_input_mouse(TextInputState& state,
|
||||
return TEXT_INPUT_NONE;
|
||||
}
|
||||
|
||||
// Bytes >= 0x80 (any non-ASCII character) count as word characters, so word
|
||||
// jumps never stop inside a multi-byte sequence.
|
||||
inline bool text_input_word_char(char ch) {
|
||||
return (ch >= 'a' && ch <= 'z') ||
|
||||
return (uint8_t)ch >= 0x80 ||
|
||||
(ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= 'A' && ch <= 'Z') ||
|
||||
(ch >= '0' && ch <= '9') ||
|
||||
ch == '_';
|
||||
@@ -574,8 +577,10 @@ inline int text_input_key(TextInputState& state,
|
||||
if (!key.pressed || !text || cap <= 0) return TEXT_INPUT_NONE;
|
||||
|
||||
int len = text_input_len(text, cap);
|
||||
state.cursor = text_input_clamp_cursor(state.cursor, len);
|
||||
state.selection_anchor = text_input_clamp_cursor(state.selection_anchor, len);
|
||||
state.cursor = montauk::utf8::floor_boundary(
|
||||
text, len, text_input_clamp_cursor(state.cursor, len));
|
||||
state.selection_anchor = montauk::utf8::floor_boundary(
|
||||
text, len, text_input_clamp_cursor(state.selection_anchor, len));
|
||||
|
||||
if (state.context.open) {
|
||||
context_menu_close(state.context);
|
||||
@@ -606,20 +611,22 @@ inline int text_input_key(TextInputState& state,
|
||||
return TEXT_INPUT_CONSUMED | (changed ? TEXT_INPUT_CHANGED : 0);
|
||||
}
|
||||
|
||||
if (key.scancode == 0x4B || key.scancode == 0x4D ||
|
||||
key.scancode == 0x47 || key.scancode == 0x4F) {
|
||||
if (montauk::nav_scancode(key) == 0x4B || montauk::nav_scancode(key) == 0x4D ||
|
||||
montauk::nav_scancode(key) == 0x47 || montauk::nav_scancode(key) == 0x4F) {
|
||||
int next = state.cursor;
|
||||
if (!key.shift && sel_start != sel_end && key.scancode == 0x4B)
|
||||
if (!key.shift && sel_start != sel_end && montauk::nav_scancode(key) == 0x4B)
|
||||
next = sel_start;
|
||||
else if (!key.shift && sel_start != sel_end && key.scancode == 0x4D)
|
||||
else if (!key.shift && sel_start != sel_end && montauk::nav_scancode(key) == 0x4D)
|
||||
next = sel_end;
|
||||
else if (key.scancode == 0x4B)
|
||||
next = key.ctrl ? text_input_prev_word_boundary(text, state.cursor) : state.cursor - 1;
|
||||
else if (key.scancode == 0x4D)
|
||||
next = key.ctrl ? text_input_next_word_boundary(text, state.cursor) : state.cursor + 1;
|
||||
else if (key.scancode == 0x47)
|
||||
else if (montauk::nav_scancode(key) == 0x4B)
|
||||
next = key.ctrl ? text_input_prev_word_boundary(text, state.cursor)
|
||||
: montauk::utf8::prev_boundary(text, state.cursor);
|
||||
else if (montauk::nav_scancode(key) == 0x4D)
|
||||
next = key.ctrl ? text_input_next_word_boundary(text, state.cursor)
|
||||
: montauk::utf8::next_boundary(text, len, state.cursor);
|
||||
else if (montauk::nav_scancode(key) == 0x47)
|
||||
next = 0;
|
||||
else if (key.scancode == 0x4F)
|
||||
else if (montauk::nav_scancode(key) == 0x4F)
|
||||
next = len;
|
||||
|
||||
text_input_set_cursor(state, len, next, key.shift);
|
||||
@@ -641,15 +648,16 @@ inline int text_input_key(TextInputState& state,
|
||||
changed = true;
|
||||
}
|
||||
} else if (state.cursor > 0) {
|
||||
text_input_delete_range(text, &len, state.cursor - 1, state.cursor);
|
||||
state.cursor--;
|
||||
int start = montauk::utf8::prev_boundary(text, state.cursor);
|
||||
text_input_delete_range(text, &len, start, state.cursor);
|
||||
state.cursor = start;
|
||||
changed = true;
|
||||
}
|
||||
state.selection_anchor = state.cursor;
|
||||
return TEXT_INPUT_CONSUMED | (changed ? TEXT_INPUT_CHANGED : 0);
|
||||
}
|
||||
|
||||
if (key.scancode == 0x53) {
|
||||
if (montauk::nav_scancode(key) == 0x53) {
|
||||
if (sel_start != sel_end) {
|
||||
text_input_delete_range(text, &len, sel_start, sel_end);
|
||||
state.cursor = sel_start;
|
||||
@@ -661,15 +669,22 @@ inline int text_input_key(TextInputState& state,
|
||||
changed = true;
|
||||
}
|
||||
} else if (state.cursor < len) {
|
||||
text_input_delete_range(text, &len, state.cursor, state.cursor + 1);
|
||||
int end = montauk::utf8::next_boundary(text, len, state.cursor);
|
||||
text_input_delete_range(text, &len, state.cursor, end);
|
||||
changed = true;
|
||||
}
|
||||
state.selection_anchor = state.cursor;
|
||||
return TEXT_INPUT_CONSUMED | (changed ? TEXT_INPUT_CHANGED : 0);
|
||||
}
|
||||
|
||||
if (key.ctrl || key.alt || !text_input_char_allowed(key.ascii, filter, userdata))
|
||||
return TEXT_INPUT_NONE;
|
||||
// Typed text is inserted as UTF-8; the filter sees each encoded byte.
|
||||
char typed[montauk::utf8::MAX_BYTES];
|
||||
int typed_len = key.ctrl || key.alt ? 0 : montauk::utf8::key_utf8(key, typed);
|
||||
if (typed_len == 0) return TEXT_INPUT_NONE;
|
||||
for (int i = 0; i < typed_len; i++) {
|
||||
if (!text_input_char_allowed(typed[i], filter, userdata))
|
||||
return TEXT_INPUT_NONE;
|
||||
}
|
||||
|
||||
if (sel_start != sel_end) {
|
||||
text_input_delete_range(text, &len, sel_start, sel_end);
|
||||
@@ -677,11 +692,12 @@ inline int text_input_key(TextInputState& state,
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (len < cap - 1) {
|
||||
if (len + typed_len < cap) {
|
||||
for (int i = len; i >= state.cursor; i--)
|
||||
text[i + 1] = text[i];
|
||||
text[state.cursor++] = key.ascii;
|
||||
len++;
|
||||
text[i + typed_len] = text[i];
|
||||
for (int i = 0; i < typed_len; i++)
|
||||
text[state.cursor++] = typed[i];
|
||||
len += typed_len;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
@@ -1251,22 +1267,34 @@ inline void draw_badge(Canvas& c,
|
||||
c.text(x + theme.gap_sm, y + 2, label, fg);
|
||||
}
|
||||
|
||||
// One '*' per UTF-8 character.
|
||||
inline const char* mask_text(const char* value, char* buffer, int cap) {
|
||||
int len = 0;
|
||||
while (value && value[len] && len < cap - 1) {
|
||||
buffer[len] = '*';
|
||||
len++;
|
||||
const char* p = value ? value : "";
|
||||
while (*p && len < cap - 1) {
|
||||
montauk::utf8::next(&p);
|
||||
buffer[len++] = '*';
|
||||
}
|
||||
buffer[len] = '\0';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// First `count` bytes of value (a character boundary), or one '*' per
|
||||
// character in them when masked.
|
||||
inline void copy_prefix(char* buffer, int cap, const char* value, int count, bool masked) {
|
||||
if (cap <= 0) return;
|
||||
int len = 0;
|
||||
while (value && value[len] && len < count && len < cap - 1) {
|
||||
buffer[len] = masked ? '*' : value[len];
|
||||
len++;
|
||||
if (masked) {
|
||||
const char* p = value ? value : "";
|
||||
while (*p && p - value < count && len < cap - 1) {
|
||||
montauk::utf8::next(&p);
|
||||
buffer[len++] = '*';
|
||||
}
|
||||
} else {
|
||||
while (value && value[len] && len < count && len < cap - 1) {
|
||||
buffer[len] = value[len];
|
||||
len++;
|
||||
}
|
||||
}
|
||||
buffer[len] = '\0';
|
||||
}
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
#include "gui/font.hpp"
|
||||
#include <montauk/syscall.h>
|
||||
#include <montauk/string.h>
|
||||
#include <montauk/utf8.h>
|
||||
#include <Api/Syscall.hpp>
|
||||
#include <montauk/capabilities.h>
|
||||
|
||||
namespace gui {
|
||||
|
||||
struct TermCell {
|
||||
char ch;
|
||||
uint32_t ch; // Unicode codepoint (every cell is one column wide)
|
||||
Color fg;
|
||||
Color bg;
|
||||
};
|
||||
@@ -58,6 +59,11 @@ struct TerminalState {
|
||||
int csi_params[8];
|
||||
int csi_param_count;
|
||||
int csi_current_param;
|
||||
|
||||
// Partial UTF-8 sequence carried across terminal_feed calls (child output
|
||||
// arrives in arbitrary chunks, so a character can straddle two reads).
|
||||
char utf8_pending[4];
|
||||
int utf8_pending_len;
|
||||
};
|
||||
|
||||
// ==== Palette ====
|
||||
@@ -244,6 +250,7 @@ static inline void terminal_init_cells(TerminalState* t, int cols, int rows, int
|
||||
t->csi_private = false;
|
||||
t->csi_param_count = 0;
|
||||
t->csi_current_param = 0;
|
||||
t->utf8_pending_len = 0;
|
||||
t->child_pid = 0;
|
||||
|
||||
int total_cells = (rows + max_sb) * cols;
|
||||
@@ -275,7 +282,7 @@ static inline void terminal_init(TerminalState* t, int cols, int rows) {
|
||||
montauk::childio_settermsz(t->child_pid, cols, rows);
|
||||
}
|
||||
|
||||
static inline void terminal_put_char(TerminalState* t, char ch) {
|
||||
static inline void terminal_put_char(TerminalState* t, uint32_t ch) {
|
||||
if (t->cursor_x >= t->cols) {
|
||||
t->cursor_x = 0;
|
||||
t->cursor_y++;
|
||||
@@ -549,11 +556,61 @@ static inline void terminal_process_csi(TerminalState* t, char cmd) {
|
||||
}
|
||||
}
|
||||
|
||||
// Emit pending bytes that did not form a valid UTF-8 sequence: each one is a
|
||||
// Windows-1252 character, matching how the GUI renders legacy text.
|
||||
static inline void terminal_flush_utf8(TerminalState* t) {
|
||||
for (int i = 0; i < t->utf8_pending_len; i++)
|
||||
terminal_put_char(t, montauk::utf8::from_cp1252((uint8_t)t->utf8_pending[i]));
|
||||
t->utf8_pending_len = 0;
|
||||
}
|
||||
|
||||
static inline int terminal_utf8_expected(uint8_t lead) {
|
||||
if (lead >= 0xC2 && lead <= 0xDF) return 2;
|
||||
if (lead >= 0xE0 && lead <= 0xEF) return 3;
|
||||
if (lead >= 0xF0 && lead <= 0xF4) return 4;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Feed one byte >= 0x80 of printable text through the UTF-8 assembler.
|
||||
static inline void terminal_feed_high(TerminalState* t, uint8_t b) {
|
||||
if (t->utf8_pending_len > 0) {
|
||||
if ((b & 0xC0) == 0x80) {
|
||||
t->utf8_pending[t->utf8_pending_len++] = (char)b;
|
||||
int want = terminal_utf8_expected((uint8_t)t->utf8_pending[0]);
|
||||
uint32_t cp = 0;
|
||||
int n = montauk::utf8::sequence_length(t->utf8_pending,
|
||||
t->utf8_pending_len, &cp);
|
||||
if (n == want) {
|
||||
terminal_put_char(t, cp);
|
||||
t->utf8_pending_len = 0;
|
||||
} else if (t->utf8_pending_len >= want) {
|
||||
terminal_flush_utf8(t); // overlong / surrogate / out of range
|
||||
}
|
||||
return;
|
||||
}
|
||||
terminal_flush_utf8(t);
|
||||
}
|
||||
if (terminal_utf8_expected(b)) {
|
||||
t->utf8_pending[0] = (char)b;
|
||||
t->utf8_pending_len = 1;
|
||||
} else {
|
||||
terminal_put_char(t, montauk::utf8::from_cp1252(b));
|
||||
}
|
||||
}
|
||||
|
||||
static inline void terminal_feed(TerminalState* t, const char* data, int len) {
|
||||
if (len > 0) t->dirty = true;
|
||||
for (int i = 0; i < len; i++) {
|
||||
char ch = data[i];
|
||||
|
||||
if (t->parse_state == TerminalState::STATE_NORMAL) {
|
||||
if ((uint8_t)ch >= 0x80) {
|
||||
terminal_feed_high(t, (uint8_t)ch);
|
||||
continue;
|
||||
}
|
||||
if (t->utf8_pending_len > 0) terminal_flush_utf8(t);
|
||||
}
|
||||
|
||||
switch (t->parse_state) {
|
||||
case TerminalState::STATE_NORMAL:
|
||||
if (ch == '\033') {
|
||||
@@ -575,9 +632,8 @@ static inline void terminal_feed(TerminalState* t, const char* data, int len) {
|
||||
while (t->cursor_x < next) {
|
||||
terminal_put_char(t, ' ');
|
||||
}
|
||||
} else if (ch >= 32 || ch < 0) {
|
||||
// Printable character (also treat high-bit chars as printable)
|
||||
terminal_put_char(t, ch);
|
||||
} else if (ch >= 32) {
|
||||
terminal_put_char(t, (uint8_t)ch);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -641,9 +697,9 @@ static inline void terminal_fill_pixel_rect(uint32_t* pixels, int pw, int ph,
|
||||
}
|
||||
|
||||
static inline void terminal_draw_bitmap_char(uint32_t* pixels, int pw, int ph,
|
||||
int x, int y, unsigned char ch,
|
||||
int x, int y, uint32_t ch,
|
||||
uint32_t color) {
|
||||
const uint8_t* glyph = &font_data[ch * FONT_HEIGHT];
|
||||
const uint8_t* glyph = &font_data[cp437_index(ch) * FONT_HEIGHT];
|
||||
for (int fy = 0; fy < FONT_HEIGHT; fy++) {
|
||||
int dy = y + fy;
|
||||
if (dy >= ph) break;
|
||||
@@ -669,14 +725,14 @@ static inline void terminal_draw_cell_pixels(const TermCell& cell,
|
||||
cell.bg.to_pixel());
|
||||
}
|
||||
|
||||
if (cell.ch <= 32 && cell.ch >= 0) return;
|
||||
if (cell.ch <= 32) return;
|
||||
|
||||
if (use_ttf) {
|
||||
int baseline = py + gc->ascent;
|
||||
fonts::mono->draw_char_to_buffer(pixels, pw, ph,
|
||||
px, baseline, (unsigned char)cell.ch, cell.fg, gc);
|
||||
px, baseline, (int)cell.ch, cell.fg, gc);
|
||||
} else {
|
||||
terminal_draw_bitmap_char(pixels, pw, ph, px, py, (unsigned char)cell.ch,
|
||||
terminal_draw_bitmap_char(pixels, pw, ph, px, py, cell.ch,
|
||||
cell.fg.to_pixel());
|
||||
}
|
||||
}
|
||||
@@ -691,14 +747,14 @@ static inline void terminal_draw_cursor_pixels(const TermCell& cell,
|
||||
terminal_fill_pixel_rect(pixels, pw, ph, px, py, cell_w, cell_h,
|
||||
g_term_palette.cursor.to_pixel());
|
||||
|
||||
if (cell.ch <= 32 && cell.ch >= 0) return;
|
||||
if (cell.ch <= 32) return;
|
||||
|
||||
if (use_ttf) {
|
||||
int baseline = py + gc->ascent;
|
||||
fonts::mono->draw_char_to_buffer(pixels, pw, ph,
|
||||
px, baseline, (unsigned char)cell.ch, g_term_palette.bg, gc);
|
||||
px, baseline, (int)cell.ch, g_term_palette.bg, gc);
|
||||
} else {
|
||||
terminal_draw_bitmap_char(pixels, pw, ph, px, py, (unsigned char)cell.ch,
|
||||
terminal_draw_bitmap_char(pixels, pw, ph, px, py, cell.ch,
|
||||
g_term_palette.bg.to_pixel());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <montauk/syscall.h>
|
||||
#include <montauk/heap.h>
|
||||
#include <montauk/string.h>
|
||||
#include <montauk/utf8.h>
|
||||
#include "gui/gui.hpp"
|
||||
#include "gui/framebuffer.hpp"
|
||||
|
||||
@@ -41,39 +42,12 @@
|
||||
|
||||
namespace gui {
|
||||
|
||||
// Windows-1252 byte -> Unicode codepoint, for the remaining code that still
|
||||
// handles single-byte text (PDF simple-font encodings). UTF-8 strings go
|
||||
// through measure_text/draw, which decode with montauk::utf8.
|
||||
inline int decode_single_byte_codepoint(int codepoint) {
|
||||
if (codepoint < 0 || codepoint > 255) return codepoint;
|
||||
|
||||
switch (codepoint) {
|
||||
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 codepoint;
|
||||
}
|
||||
return (int)montauk::utf8::from_cp1252((uint8_t)codepoint);
|
||||
}
|
||||
|
||||
struct CachedGlyph {
|
||||
@@ -84,8 +58,18 @@ struct CachedGlyph {
|
||||
bool loaded;
|
||||
};
|
||||
|
||||
// Glyphs above U+00FF live in a small open-addressed table so CJK, symbols
|
||||
// and the like cost nothing until they are drawn.
|
||||
struct ExtGlyph {
|
||||
uint32_t codepoint; // 0 = empty slot
|
||||
CachedGlyph glyph;
|
||||
};
|
||||
|
||||
struct GlyphCache {
|
||||
CachedGlyph glyphs[256]; // Single-byte text with Windows-1252 punctuation mapping
|
||||
CachedGlyph glyphs[256]; // Unicode U+0000-U+00FF, indexed directly
|
||||
ExtGlyph* ext; // everything above, hashed by codepoint
|
||||
int ext_cap; // power of two, 0 until first use
|
||||
int ext_count;
|
||||
int pixel_size;
|
||||
float scale;
|
||||
int ascent, descent, line_gap;
|
||||
@@ -142,8 +126,21 @@ struct TrueTypeFont {
|
||||
return true;
|
||||
}
|
||||
|
||||
void init_cache(GlyphCache* gc, int pixel_size) {
|
||||
// Free any existing glyph bitmaps
|
||||
static void free_ext(GlyphCache* gc) {
|
||||
if (gc->ext) {
|
||||
for (int i = 0; i < gc->ext_cap; i++) {
|
||||
if (gc->ext[i].codepoint && gc->ext[i].glyph.bitmap)
|
||||
montauk::mfree(gc->ext[i].glyph.bitmap);
|
||||
}
|
||||
montauk::mfree(gc->ext);
|
||||
}
|
||||
gc->ext = nullptr;
|
||||
gc->ext_cap = 0;
|
||||
gc->ext_count = 0;
|
||||
}
|
||||
|
||||
// Release every glyph bitmap in a cache (the cache stays usable).
|
||||
static void free_cache(GlyphCache* gc) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (gc->glyphs[i].bitmap) {
|
||||
montauk::mfree(gc->glyphs[i].bitmap);
|
||||
@@ -151,6 +148,11 @@ struct TrueTypeFont {
|
||||
gc->glyphs[i].bitmap = nullptr;
|
||||
gc->glyphs[i].loaded = false;
|
||||
}
|
||||
free_ext(gc);
|
||||
}
|
||||
|
||||
void init_cache(GlyphCache* gc, int pixel_size) {
|
||||
free_cache(gc);
|
||||
|
||||
gc->pixel_size = pixel_size;
|
||||
gc->scale = em_scaling
|
||||
@@ -179,11 +181,8 @@ struct TrueTypeFont {
|
||||
return gc;
|
||||
}
|
||||
|
||||
// Evict oldest cache (slot 0) — free its glyph bitmaps
|
||||
for (int g = 0; g < 256; g++) {
|
||||
if (caches[0].glyphs[g].bitmap)
|
||||
montauk::mfree(caches[0].glyphs[g].bitmap);
|
||||
}
|
||||
// Evict oldest cache (slot 0) - free its glyph bitmaps
|
||||
free_cache(&caches[0]);
|
||||
// Shift remaining caches down
|
||||
for (int i = 0; i < 3; i++)
|
||||
montauk::memcpy(&caches[i], &caches[i + 1], sizeof(GlyphCache));
|
||||
@@ -192,13 +191,54 @@ struct TrueTypeFont {
|
||||
return &caches[3];
|
||||
}
|
||||
|
||||
static constexpr int EXT_MAX = 2048; // flush the table beyond this
|
||||
|
||||
CachedGlyph* ext_slot(GlyphCache* gc, uint32_t codepoint) {
|
||||
if (gc->ext_cap == 0 || gc->ext_count * 4 >= gc->ext_cap * 3) {
|
||||
int new_cap = gc->ext_cap ? gc->ext_cap * 2 : 64;
|
||||
if (gc->ext_count >= EXT_MAX) {
|
||||
// A document full of distinct glyphs: start over rather than
|
||||
// grow without bound. Bitmaps are rebuilt on demand.
|
||||
free_ext(gc);
|
||||
new_cap = 64;
|
||||
}
|
||||
ExtGlyph* table = (ExtGlyph*)montauk::malloc(sizeof(ExtGlyph) * new_cap);
|
||||
if (!table) return nullptr;
|
||||
montauk::memset(table, 0, sizeof(ExtGlyph) * new_cap);
|
||||
for (int i = 0; i < gc->ext_cap; i++) {
|
||||
ExtGlyph& e = gc->ext[i];
|
||||
if (!e.codepoint) continue;
|
||||
uint32_t h = (e.codepoint * 2654435761u) & (new_cap - 1);
|
||||
while (table[h].codepoint) h = (h + 1) & (new_cap - 1);
|
||||
table[h] = e;
|
||||
}
|
||||
if (gc->ext) montauk::mfree(gc->ext);
|
||||
gc->ext = table;
|
||||
gc->ext_cap = new_cap;
|
||||
}
|
||||
uint32_t mask = gc->ext_cap - 1;
|
||||
uint32_t h = (codepoint * 2654435761u) & mask;
|
||||
while (gc->ext[h].codepoint && gc->ext[h].codepoint != codepoint)
|
||||
h = (h + 1) & mask;
|
||||
if (!gc->ext[h].codepoint) {
|
||||
gc->ext[h].codepoint = codepoint;
|
||||
montauk::memset(&gc->ext[h].glyph, 0, sizeof(CachedGlyph));
|
||||
gc->ext_count++;
|
||||
}
|
||||
return &gc->ext[h].glyph;
|
||||
}
|
||||
|
||||
// Glyph for a Unicode codepoint. Codepoints the font lacks render as its
|
||||
// .notdef box, so missing characters stay visible instead of vanishing.
|
||||
CachedGlyph* get_glyph(GlyphCache* gc, int codepoint) {
|
||||
if (codepoint < 0 || codepoint >= 256) return nullptr;
|
||||
CachedGlyph* g = &gc->glyphs[codepoint];
|
||||
if (codepoint < 0 || codepoint > 0x10FFFF) return nullptr;
|
||||
CachedGlyph* g = codepoint < 256 ? &gc->glyphs[codepoint]
|
||||
: ext_slot(gc, (uint32_t)codepoint);
|
||||
if (!g) return nullptr;
|
||||
if (g->loaded) return g;
|
||||
|
||||
g->loaded = true;
|
||||
int font_codepoint = decode_single_byte_codepoint(codepoint);
|
||||
int font_codepoint = codepoint;
|
||||
int advance, lsb;
|
||||
stbtt_GetCodepointHMetrics(&info, font_codepoint, &advance, &lsb);
|
||||
g->advance = (int)(advance * gc->scale);
|
||||
@@ -222,12 +262,34 @@ struct TrueTypeFont {
|
||||
return g;
|
||||
}
|
||||
|
||||
// Advance of one codepoint at a size (0 if the font is unusable).
|
||||
int char_advance(int codepoint, int pixel_size) {
|
||||
if (!valid) return 0;
|
||||
CachedGlyph* g = get_glyph(get_cache(pixel_size), codepoint);
|
||||
return g ? g->advance : 0;
|
||||
}
|
||||
|
||||
// Width of the first `len` bytes of a UTF-8 string.
|
||||
int measure_text_n(const char* text, int len, int pixel_size) {
|
||||
if (!valid) return 0;
|
||||
GlyphCache* gc = get_cache(pixel_size);
|
||||
int w = 0;
|
||||
for (int pos = 0; pos < len && text[pos];) {
|
||||
int n;
|
||||
uint32_t cp = montauk::utf8::decode(text + pos, len - pos, &n);
|
||||
CachedGlyph* g = get_glyph(gc, (int)cp);
|
||||
if (g) w += g->advance;
|
||||
pos += n;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
int measure_text(const char* text, int pixel_size) {
|
||||
if (!valid) return 0;
|
||||
GlyphCache* gc = get_cache(pixel_size);
|
||||
int w = 0;
|
||||
for (int i = 0; text[i]; i++) {
|
||||
CachedGlyph* g = get_glyph(gc, (unsigned char)text[i]);
|
||||
for (const char* p = text; *p;) {
|
||||
CachedGlyph* g = get_glyph(gc, (int)montauk::utf8::next(&p));
|
||||
if (g) w += g->advance;
|
||||
}
|
||||
return w;
|
||||
@@ -253,8 +315,8 @@ struct TrueTypeFont {
|
||||
uint32_t src_rgb = 0xFF000000 | ((uint32_t)color.r << 16) |
|
||||
((uint32_t)color.g << 8) | color.b;
|
||||
|
||||
for (int i = 0; text[i]; i++) {
|
||||
CachedGlyph* g = get_glyph(gc, (unsigned char)text[i]);
|
||||
for (const char* p = text; *p;) {
|
||||
CachedGlyph* g = get_glyph(gc, (int)montauk::utf8::next(&p));
|
||||
if (!g) continue;
|
||||
|
||||
if (g->bitmap) {
|
||||
@@ -325,8 +387,8 @@ struct TrueTypeFont {
|
||||
int baseline = y + gc->ascent;
|
||||
bool drew_pixel = false;
|
||||
|
||||
for (int i = 0; text[i]; i++) {
|
||||
CachedGlyph* g = get_glyph(gc, (unsigned char)text[i]);
|
||||
for (const char* p = text; *p;) {
|
||||
CachedGlyph* g = get_glyph(gc, (int)montauk::utf8::next(&p));
|
||||
if (!g) continue;
|
||||
|
||||
if (g->bitmap) {
|
||||
@@ -378,8 +440,8 @@ struct TrueTypeFont {
|
||||
int clip_x2 = clip_x + clip_w;
|
||||
int clip_y2 = clip_y + clip_h;
|
||||
|
||||
for (int i = 0; text[i]; i++) {
|
||||
CachedGlyph* g = get_glyph(gc, (unsigned char)text[i]);
|
||||
for (const char* p = text; *p;) {
|
||||
CachedGlyph* g = get_glyph(gc, (int)montauk::utf8::next(&p));
|
||||
if (!g) continue;
|
||||
|
||||
if (g->bitmap) {
|
||||
@@ -417,7 +479,7 @@ struct TrueTypeFont {
|
||||
}
|
||||
}
|
||||
|
||||
// Draw single character to buffer, returning advance width
|
||||
// Draw one Unicode codepoint to buffer, returning advance width
|
||||
int draw_char_to_buffer(uint32_t* pixels, int buf_w, int buf_h,
|
||||
int x, int baseline, int codepoint,
|
||||
Color color, GlyphCache* gc) {
|
||||
|
||||
@@ -212,11 +212,7 @@ struct TextBox {
|
||||
|
||||
// Draw cursor if focused
|
||||
if (focused) {
|
||||
// Measure text up to cursor position for proportional fonts
|
||||
char prefix[256];
|
||||
for (int i = 0; i < cursor && i < 255; i++) prefix[i] = text[i];
|
||||
prefix[cursor < 255 ? cursor : 255] = '\0';
|
||||
int cx = tx + text_width(prefix);
|
||||
int cx = tx + text_width_n(text, cursor);
|
||||
draw_vline(fb, cx, ty, fh, cursor_color);
|
||||
}
|
||||
}
|
||||
@@ -230,34 +226,35 @@ struct TextBox {
|
||||
void handle_key(const montauk::abi::KeyEvent& key) {
|
||||
if (!focused || !key.pressed) return;
|
||||
|
||||
char typed[montauk::utf8::MAX_BYTES];
|
||||
int typed_len = montauk::utf8::key_utf8(key, typed);
|
||||
|
||||
if (key.ascii == '\b' || key.scancode == 0x0E) {
|
||||
// Backspace
|
||||
// Backspace: remove the whole character before the cursor
|
||||
if (cursor > 0) {
|
||||
for (int i = cursor - 1; i < text_len - 1; i++) {
|
||||
text[i] = text[i + 1];
|
||||
}
|
||||
text_len--;
|
||||
cursor--;
|
||||
text[text_len] = '\0';
|
||||
int start = montauk::utf8::prev_boundary(text, cursor);
|
||||
int n = cursor - start;
|
||||
for (int i = start; i <= text_len - n; i++)
|
||||
text[i] = text[i + n];
|
||||
text_len -= n;
|
||||
cursor = start;
|
||||
}
|
||||
} else if ((unsigned char)key.ascii >= 32
|
||||
&& (unsigned char)key.ascii != 127) {
|
||||
// Printable character
|
||||
if (text_len < 254) {
|
||||
for (int i = text_len; i > cursor; i--) {
|
||||
text[i] = text[i - 1];
|
||||
}
|
||||
text[cursor] = key.ascii;
|
||||
cursor++;
|
||||
text_len++;
|
||||
text[text_len] = '\0';
|
||||
} else if (typed_len > 0) {
|
||||
// Printable character, inserted as UTF-8
|
||||
if (text_len + typed_len <= 254) {
|
||||
for (int i = text_len; i >= cursor; i--)
|
||||
text[i + typed_len] = text[i];
|
||||
for (int i = 0; i < typed_len; i++)
|
||||
text[cursor + i] = typed[i];
|
||||
cursor += typed_len;
|
||||
text_len += typed_len;
|
||||
}
|
||||
} else if (key.scancode == 0x4B) {
|
||||
} else if (montauk::nav_scancode(key) == 0x4B) {
|
||||
// Left arrow
|
||||
if (cursor > 0) cursor--;
|
||||
} else if (key.scancode == 0x4D) {
|
||||
cursor = montauk::utf8::prev_boundary(text, cursor);
|
||||
} else if (montauk::nav_scancode(key) == 0x4D) {
|
||||
// Right arrow
|
||||
if (cursor < text_len) cursor++;
|
||||
cursor = montauk::utf8::next_boundary(text, text_len, cursor);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+196
-194
@@ -23,200 +23,202 @@ extern "C" {
|
||||
Generated from Api/Syscall.hpp by scripts/montauk-syscalls.py.
|
||||
Do not edit by hand; run 'make gen-syscalls' to refresh.
|
||||
Wrapper functions below are hand-written. */
|
||||
#define MTK_SYS_EXIT 0
|
||||
#define MTK_SYS_YIELD 1
|
||||
#define MTK_SYS_SLEEP_MS 2
|
||||
#define MTK_SYS_GETPID 3
|
||||
#define MTK_SYS_PRINT 4
|
||||
#define MTK_SYS_PUTCHAR 5
|
||||
#define MTK_SYS_OPEN 6
|
||||
#define MTK_SYS_READ 7
|
||||
#define MTK_SYS_GETSIZE 8
|
||||
#define MTK_SYS_CLOSE 9
|
||||
#define MTK_SYS_READDIR 10
|
||||
#define MTK_SYS_ALLOC 11
|
||||
#define MTK_SYS_FREE 12
|
||||
#define MTK_SYS_GETTICKS 13
|
||||
#define MTK_SYS_GETMILLISECONDS 14
|
||||
#define MTK_SYS_GETINFO 15
|
||||
#define MTK_SYS_ISKEYAVAILABLE 16
|
||||
#define MTK_SYS_GETKEY 17
|
||||
#define MTK_SYS_GETCHAR 18
|
||||
#define MTK_SYS_PING 19
|
||||
#define MTK_SYS_SPAWN 20
|
||||
#define MTK_SYS_FBINFO 21
|
||||
#define MTK_SYS_FBMAP 22
|
||||
#define MTK_SYS_WAITPID 23
|
||||
#define MTK_SYS_TERMSIZE 24
|
||||
#define MTK_SYS_GETARGS 25
|
||||
#define MTK_SYS_RESET 26
|
||||
#define MTK_SYS_SHUTDOWN 27
|
||||
#define MTK_SYS_GETTIME 28
|
||||
#define MTK_SYS_SOCKET 29
|
||||
#define MTK_SYS_CONNECT 30
|
||||
#define MTK_SYS_BIND 31
|
||||
#define MTK_SYS_LISTEN 32
|
||||
#define MTK_SYS_ACCEPT 33
|
||||
#define MTK_SYS_SEND 34
|
||||
#define MTK_SYS_RECV 35
|
||||
#define MTK_SYS_CLOSESOCK 36
|
||||
#define MTK_SYS_GETNETCFG 37
|
||||
#define MTK_SYS_SETNETCFG 38
|
||||
#define MTK_SYS_SENDTO 39
|
||||
#define MTK_SYS_RECVFROM 40
|
||||
#define MTK_SYS_FWRITE 41
|
||||
#define MTK_SYS_FCREATE 42
|
||||
#define MTK_SYS_TERMSCALE 43
|
||||
#define MTK_SYS_RESOLVE 44
|
||||
#define MTK_SYS_GETRANDOM 45
|
||||
#define MTK_SYS_LOG 46
|
||||
#define MTK_SYS_MOUSESTATE 47
|
||||
#define MTK_SYS_SETMOUSEBOUNDS 48
|
||||
#define MTK_SYS_SPAWN_REDIR 49
|
||||
#define MTK_SYS_CHILDIO_READ 50
|
||||
#define MTK_SYS_CHILDIO_WRITE 51
|
||||
#define MTK_SYS_CHILDIO_WRITEKEY 52
|
||||
#define MTK_SYS_CHILDIO_SETTERMSZ 53
|
||||
#define MTK_SYS_WINCREATE 54
|
||||
#define MTK_SYS_WINDESTROY 55
|
||||
#define MTK_SYS_WINPRESENT 56
|
||||
#define MTK_SYS_WINPOLL 57
|
||||
#define MTK_SYS_WINENUM 58
|
||||
#define MTK_SYS_WINMAP 59
|
||||
#define MTK_SYS_WINSENDEVENT 60
|
||||
#define MTK_SYS_PROCLIST 61
|
||||
#define MTK_SYS_KILL 62
|
||||
#define MTK_SYS_DEVLIST 63
|
||||
#define MTK_SYS_WINRESIZE 64
|
||||
#define MTK_SYS_WINSETSCALE 65
|
||||
#define MTK_SYS_WINGETSCALE 66
|
||||
#define MTK_SYS_MEMSTATS 67
|
||||
#define MTK_SYS_WINSETCURSOR 68
|
||||
#define MTK_SYS_DISKINFO 69
|
||||
#define MTK_SYS_PARTLIST 70
|
||||
#define MTK_SYS_DISKREAD 71
|
||||
#define MTK_SYS_DISKWRITE 72
|
||||
#define MTK_SYS_GPTINIT 73
|
||||
#define MTK_SYS_GPTADD 74
|
||||
#define MTK_SYS_FSMOUNT 75
|
||||
#define MTK_SYS_FSFORMAT 76
|
||||
#define MTK_SYS_FDELETE 77
|
||||
#define MTK_SYS_FMKDIR 78
|
||||
#define MTK_SYS_DRIVELIST 79
|
||||
#define MTK_SYS_AUDIOOPEN 80
|
||||
#define MTK_SYS_AUDIOCLOSE 81
|
||||
#define MTK_SYS_AUDIOWRITE 82
|
||||
#define MTK_SYS_AUDIOCTL 83
|
||||
#define MTK_SYS_BTSCAN 84
|
||||
#define MTK_SYS_BTCONNECT 85
|
||||
#define MTK_SYS_BTDISCONNECT 86
|
||||
#define MTK_SYS_BTLIST 87
|
||||
#define MTK_SYS_BTINFO 88
|
||||
#define MTK_SYS_SUSPEND 89
|
||||
#define MTK_SYS_SETTZ 90
|
||||
#define MTK_SYS_GETTZ 91
|
||||
#define MTK_SYS_SETUSER 92
|
||||
#define MTK_SYS_GETUSER 93
|
||||
#define MTK_SYS_FRENAME 94
|
||||
#define MTK_SYS_GETCWD 95
|
||||
#define MTK_SYS_CHDIR 96
|
||||
#define MTK_SYS_WINUNMAP 97
|
||||
#define MTK_SYS_DUPHANDLE 98
|
||||
#define MTK_SYS_WAIT_HANDLE 99
|
||||
#define MTK_SYS_STREAM_CREATE 100
|
||||
#define MTK_SYS_STREAM_READ 101
|
||||
#define MTK_SYS_STREAM_WRITE 102
|
||||
#define MTK_SYS_MAILBOX_CREATE 103
|
||||
#define MTK_SYS_MAILBOX_SEND 104
|
||||
#define MTK_SYS_MAILBOX_RECV 105
|
||||
#define MTK_SYS_WAITSET_CREATE 106
|
||||
#define MTK_SYS_WAITSET_ADD 107
|
||||
#define MTK_SYS_WAITSET_REMOVE 108
|
||||
#define MTK_SYS_WAITSET_WAIT 109
|
||||
#define MTK_SYS_PROC_OPEN 110
|
||||
#define MTK_SYS_SURFACE_CREATE 111
|
||||
#define MTK_SYS_SURFACE_MAP 112
|
||||
#define MTK_SYS_SURFACE_RESIZE 113
|
||||
#define MTK_SYS_LOAD_LIB 114
|
||||
#define MTK_SYS_UNLOAD_LIB 115
|
||||
#define MTK_SYS_DLSYM 116
|
||||
#define MTK_SYS_GETLIBBASE 117
|
||||
#define MTK_SYS_CRASH_REPORT 118
|
||||
#define MTK_SYS_CLIPBOARD_SET_TEXT 119
|
||||
#define MTK_SYS_CLIPBOARD_GET_INFO 120
|
||||
#define MTK_SYS_CLIPBOARD_GET_TEXT 121
|
||||
#define MTK_SYS_CLIPBOARD_CLEAR 122
|
||||
#define MTK_SYS_INPUT_WAIT 123
|
||||
#define MTK_SYS_DRIVELABEL 124
|
||||
#define MTK_SYS_NETSTATUS 125
|
||||
#define MTK_SYS_WINSETFLAGS 126
|
||||
#define MTK_SYS_DRIVEKIND 127
|
||||
#define MTK_SYS_AUDIOLIST 128
|
||||
#define MTK_SYS_AUDIOWAIT 129
|
||||
#define MTK_SYS_THREAD_SPAWN 130
|
||||
#define MTK_SYS_THREAD_EXIT 131
|
||||
#define MTK_SYS_THREAD_JOIN 132
|
||||
#define MTK_SYS_THREAD_SELF 133
|
||||
#define MTK_SYS_FS_SYNC 134
|
||||
#define MTK_SYS_POWER_REQUEST 135
|
||||
#define MTK_SYS_READDIR_AT 136
|
||||
#define MTK_SYS_BTSETADDR 137
|
||||
#define MTK_SYS_BTBONDS 138
|
||||
#define MTK_SYS_BTFORGET 139
|
||||
#define MTK_SYS_RESERVED_140 140
|
||||
#define MTK_SYS_RESERVED_141 141
|
||||
#define MTK_SYS_RESERVED_142 142
|
||||
#define MTK_SYS_RESERVED_143 143
|
||||
#define MTK_SYS_RESERVED_144 144
|
||||
#define MTK_SYS_RESERVED_145 145
|
||||
#define MTK_SYS_RESERVED_146 146
|
||||
#define MTK_SYS_RESERVED_147 147
|
||||
#define MTK_SYS_RESERVED_148 148
|
||||
#define MTK_SYS_POWERINFO 149
|
||||
#define MTK_SYS_FBFLIP 150
|
||||
#define MTK_SYS_GETEXECPATH 151
|
||||
#define MTK_SYS_STAT 152
|
||||
#define MTK_SYS_SETUNIXTIME 153
|
||||
#define MTK_SYS_DISPLAYINFO 154
|
||||
#define MTK_SYS_DISPLAYMODES 155
|
||||
#define MTK_SYS_DISPLAYSETMODE 156
|
||||
#define MTK_SYS_DISPLAYBRIGHTNESS 157
|
||||
#define MTK_SYS_WIFI_SCAN 158
|
||||
#define MTK_SYS_WIFI_INFO 159
|
||||
#define MTK_SYS_WIFI_CONNECT 160
|
||||
#define MTK_SYS_WIFI_DISCONNECT 161
|
||||
#define MTK_SYS_WIFI_SCAN_START 162
|
||||
#define MTK_SYS_WIFI_RESULTS 163
|
||||
#define MTK_SYS_WIFI_CONNECT_ASYNC 164
|
||||
#define MTK_SYS_NETIFS 165
|
||||
#define MTK_SYS_GETCHAR_NB 166
|
||||
#define MTK_SYS_UTIME 167
|
||||
#define MTK_SYS_MMAP_ANON 168
|
||||
#define MTK_SYS_MUNMAP 169
|
||||
#define MTK_SYS_MPROTECT 170
|
||||
#define MTK_SYS_GETENVIRON 171
|
||||
#define MTK_SYS_SETENVIRON 172
|
||||
#define MTK_SYS_SPAWN_ENV 173
|
||||
#define MTK_SYS_SETSESSION 174
|
||||
#define MTK_SYS_KILLSESSION 175
|
||||
#define MTK_SYS_LOG_WRITE 176
|
||||
#define MTK_SYS_TERMINAL_ATTACHED 177
|
||||
#define MTK_SYS_USB_LIST 178
|
||||
#define MTK_SYS_USB_CLAIM 179
|
||||
#define MTK_SYS_USB_CLOSE 180
|
||||
#define MTK_SYS_USB_CONTROL 181
|
||||
#define MTK_SYS_USB_BULK_IN_START 182
|
||||
#define MTK_SYS_USB_BULK_IN_STOP 183
|
||||
#define MTK_SYS_USB_BULK_IN_READ 184
|
||||
#define MTK_SYS_SPAWN_CAPS 185
|
||||
#define MTK_SYS_SPAWN_REDIR_CAPS 186
|
||||
#define MTK_SYS_ALLOC_EAGER 187
|
||||
#define MTK_SYS_BATTERYINFO 188
|
||||
#define MTK_SYS_GETCAPS 189
|
||||
#define MTK_SYS_VOLINFO 190
|
||||
#define MTK_SYS_CONSENT_REGISTER 191
|
||||
#define MTK_SYS_CONSENT_NEXT 192
|
||||
#define MTK_SYS_CONSENT_REPLY 193
|
||||
#define MTK_SYS_EXIT 0
|
||||
#define MTK_SYS_YIELD 1
|
||||
#define MTK_SYS_SLEEP_MS 2
|
||||
#define MTK_SYS_GETPID 3
|
||||
#define MTK_SYS_PRINT 4
|
||||
#define MTK_SYS_PUTCHAR 5
|
||||
#define MTK_SYS_OPEN 6
|
||||
#define MTK_SYS_READ 7
|
||||
#define MTK_SYS_GETSIZE 8
|
||||
#define MTK_SYS_CLOSE 9
|
||||
#define MTK_SYS_READDIR 10
|
||||
#define MTK_SYS_ALLOC 11
|
||||
#define MTK_SYS_FREE 12
|
||||
#define MTK_SYS_GETTICKS 13
|
||||
#define MTK_SYS_GETMILLISECONDS 14
|
||||
#define MTK_SYS_GETINFO 15
|
||||
#define MTK_SYS_ISKEYAVAILABLE 16
|
||||
#define MTK_SYS_GETKEY 17
|
||||
#define MTK_SYS_GETCHAR 18
|
||||
#define MTK_SYS_PING 19
|
||||
#define MTK_SYS_SPAWN 20
|
||||
#define MTK_SYS_FBINFO 21
|
||||
#define MTK_SYS_FBMAP 22
|
||||
#define MTK_SYS_WAITPID 23
|
||||
#define MTK_SYS_TERMSIZE 24
|
||||
#define MTK_SYS_GETARGS 25
|
||||
#define MTK_SYS_RESET 26
|
||||
#define MTK_SYS_SHUTDOWN 27
|
||||
#define MTK_SYS_GETTIME 28
|
||||
#define MTK_SYS_SOCKET 29
|
||||
#define MTK_SYS_CONNECT 30
|
||||
#define MTK_SYS_BIND 31
|
||||
#define MTK_SYS_LISTEN 32
|
||||
#define MTK_SYS_ACCEPT 33
|
||||
#define MTK_SYS_SEND 34
|
||||
#define MTK_SYS_RECV 35
|
||||
#define MTK_SYS_CLOSESOCK 36
|
||||
#define MTK_SYS_GETNETCFG 37
|
||||
#define MTK_SYS_SETNETCFG 38
|
||||
#define MTK_SYS_SENDTO 39
|
||||
#define MTK_SYS_RECVFROM 40
|
||||
#define MTK_SYS_FWRITE 41
|
||||
#define MTK_SYS_FCREATE 42
|
||||
#define MTK_SYS_TERMSCALE 43
|
||||
#define MTK_SYS_RESOLVE 44
|
||||
#define MTK_SYS_GETRANDOM 45
|
||||
#define MTK_SYS_LOG 46
|
||||
#define MTK_SYS_MOUSESTATE 47
|
||||
#define MTK_SYS_SETMOUSEBOUNDS 48
|
||||
#define MTK_SYS_SPAWN_REDIR 49
|
||||
#define MTK_SYS_CHILDIO_READ 50
|
||||
#define MTK_SYS_CHILDIO_WRITE 51
|
||||
#define MTK_SYS_CHILDIO_WRITEKEY 52
|
||||
#define MTK_SYS_CHILDIO_SETTERMSZ 53
|
||||
#define MTK_SYS_WINCREATE 54
|
||||
#define MTK_SYS_WINDESTROY 55
|
||||
#define MTK_SYS_WINPRESENT 56
|
||||
#define MTK_SYS_WINPOLL 57
|
||||
#define MTK_SYS_WINENUM 58
|
||||
#define MTK_SYS_WINMAP 59
|
||||
#define MTK_SYS_WINSENDEVENT 60
|
||||
#define MTK_SYS_PROCLIST 61
|
||||
#define MTK_SYS_KILL 62
|
||||
#define MTK_SYS_DEVLIST 63
|
||||
#define MTK_SYS_WINRESIZE 64
|
||||
#define MTK_SYS_WINSETSCALE 65
|
||||
#define MTK_SYS_WINGETSCALE 66
|
||||
#define MTK_SYS_MEMSTATS 67
|
||||
#define MTK_SYS_WINSETCURSOR 68
|
||||
#define MTK_SYS_DISKINFO 69
|
||||
#define MTK_SYS_PARTLIST 70
|
||||
#define MTK_SYS_DISKREAD 71
|
||||
#define MTK_SYS_DISKWRITE 72
|
||||
#define MTK_SYS_GPTINIT 73
|
||||
#define MTK_SYS_GPTADD 74
|
||||
#define MTK_SYS_FSMOUNT 75
|
||||
#define MTK_SYS_FSFORMAT 76
|
||||
#define MTK_SYS_FDELETE 77
|
||||
#define MTK_SYS_FMKDIR 78
|
||||
#define MTK_SYS_DRIVELIST 79
|
||||
#define MTK_SYS_AUDIOOPEN 80
|
||||
#define MTK_SYS_AUDIOCLOSE 81
|
||||
#define MTK_SYS_AUDIOWRITE 82
|
||||
#define MTK_SYS_AUDIOCTL 83
|
||||
#define MTK_SYS_BTSCAN 84
|
||||
#define MTK_SYS_BTCONNECT 85
|
||||
#define MTK_SYS_BTDISCONNECT 86
|
||||
#define MTK_SYS_BTLIST 87
|
||||
#define MTK_SYS_BTINFO 88
|
||||
#define MTK_SYS_SUSPEND 89
|
||||
#define MTK_SYS_SETTZ 90
|
||||
#define MTK_SYS_GETTZ 91
|
||||
#define MTK_SYS_SETUSER 92
|
||||
#define MTK_SYS_GETUSER 93
|
||||
#define MTK_SYS_FRENAME 94
|
||||
#define MTK_SYS_GETCWD 95
|
||||
#define MTK_SYS_CHDIR 96
|
||||
#define MTK_SYS_WINUNMAP 97
|
||||
#define MTK_SYS_DUPHANDLE 98
|
||||
#define MTK_SYS_WAIT_HANDLE 99
|
||||
#define MTK_SYS_STREAM_CREATE 100
|
||||
#define MTK_SYS_STREAM_READ 101
|
||||
#define MTK_SYS_STREAM_WRITE 102
|
||||
#define MTK_SYS_MAILBOX_CREATE 103
|
||||
#define MTK_SYS_MAILBOX_SEND 104
|
||||
#define MTK_SYS_MAILBOX_RECV 105
|
||||
#define MTK_SYS_WAITSET_CREATE 106
|
||||
#define MTK_SYS_WAITSET_ADD 107
|
||||
#define MTK_SYS_WAITSET_REMOVE 108
|
||||
#define MTK_SYS_WAITSET_WAIT 109
|
||||
#define MTK_SYS_PROC_OPEN 110
|
||||
#define MTK_SYS_SURFACE_CREATE 111
|
||||
#define MTK_SYS_SURFACE_MAP 112
|
||||
#define MTK_SYS_SURFACE_RESIZE 113
|
||||
#define MTK_SYS_LOAD_LIB 114
|
||||
#define MTK_SYS_UNLOAD_LIB 115
|
||||
#define MTK_SYS_DLSYM 116
|
||||
#define MTK_SYS_GETLIBBASE 117
|
||||
#define MTK_SYS_CRASH_REPORT 118
|
||||
#define MTK_SYS_CLIPBOARD_SET_TEXT 119
|
||||
#define MTK_SYS_CLIPBOARD_GET_INFO 120
|
||||
#define MTK_SYS_CLIPBOARD_GET_TEXT 121
|
||||
#define MTK_SYS_CLIPBOARD_CLEAR 122
|
||||
#define MTK_SYS_INPUT_WAIT 123
|
||||
#define MTK_SYS_DRIVELABEL 124
|
||||
#define MTK_SYS_NETSTATUS 125
|
||||
#define MTK_SYS_WINSETFLAGS 126
|
||||
#define MTK_SYS_DRIVEKIND 127
|
||||
#define MTK_SYS_AUDIOLIST 128
|
||||
#define MTK_SYS_AUDIOWAIT 129
|
||||
#define MTK_SYS_THREAD_SPAWN 130
|
||||
#define MTK_SYS_THREAD_EXIT 131
|
||||
#define MTK_SYS_THREAD_JOIN 132
|
||||
#define MTK_SYS_THREAD_SELF 133
|
||||
#define MTK_SYS_FS_SYNC 134
|
||||
#define MTK_SYS_POWER_REQUEST 135
|
||||
#define MTK_SYS_READDIR_AT 136
|
||||
#define MTK_SYS_BTSETADDR 137
|
||||
#define MTK_SYS_BTBONDS 138
|
||||
#define MTK_SYS_BTFORGET 139
|
||||
#define MTK_SYS_RESERVED_140 140
|
||||
#define MTK_SYS_RESERVED_141 141
|
||||
#define MTK_SYS_RESERVED_142 142
|
||||
#define MTK_SYS_RESERVED_143 143
|
||||
#define MTK_SYS_RESERVED_144 144
|
||||
#define MTK_SYS_RESERVED_145 145
|
||||
#define MTK_SYS_RESERVED_146 146
|
||||
#define MTK_SYS_RESERVED_147 147
|
||||
#define MTK_SYS_RESERVED_148 148
|
||||
#define MTK_SYS_POWERINFO 149
|
||||
#define MTK_SYS_FBFLIP 150
|
||||
#define MTK_SYS_GETEXECPATH 151
|
||||
#define MTK_SYS_STAT 152
|
||||
#define MTK_SYS_SETUNIXTIME 153
|
||||
#define MTK_SYS_DISPLAYINFO 154
|
||||
#define MTK_SYS_DISPLAYMODES 155
|
||||
#define MTK_SYS_DISPLAYSETMODE 156
|
||||
#define MTK_SYS_DISPLAYBRIGHTNESS 157
|
||||
#define MTK_SYS_WIFI_SCAN 158
|
||||
#define MTK_SYS_WIFI_INFO 159
|
||||
#define MTK_SYS_WIFI_CONNECT 160
|
||||
#define MTK_SYS_WIFI_DISCONNECT 161
|
||||
#define MTK_SYS_WIFI_SCAN_START 162
|
||||
#define MTK_SYS_WIFI_RESULTS 163
|
||||
#define MTK_SYS_WIFI_CONNECT_ASYNC 164
|
||||
#define MTK_SYS_NETIFS 165
|
||||
#define MTK_SYS_GETCHAR_NB 166
|
||||
#define MTK_SYS_UTIME 167
|
||||
#define MTK_SYS_MMAP_ANON 168
|
||||
#define MTK_SYS_MUNMAP 169
|
||||
#define MTK_SYS_MPROTECT 170
|
||||
#define MTK_SYS_GETENVIRON 171
|
||||
#define MTK_SYS_SETENVIRON 172
|
||||
#define MTK_SYS_SPAWN_ENV 173
|
||||
#define MTK_SYS_SETSESSION 174
|
||||
#define MTK_SYS_KILLSESSION 175
|
||||
#define MTK_SYS_LOG_WRITE 176
|
||||
#define MTK_SYS_TERMINAL_ATTACHED 177
|
||||
#define MTK_SYS_USB_LIST 178
|
||||
#define MTK_SYS_USB_CLAIM 179
|
||||
#define MTK_SYS_USB_CLOSE 180
|
||||
#define MTK_SYS_USB_CONTROL 181
|
||||
#define MTK_SYS_USB_BULK_IN_START 182
|
||||
#define MTK_SYS_USB_BULK_IN_STOP 183
|
||||
#define MTK_SYS_USB_BULK_IN_READ 184
|
||||
#define MTK_SYS_SPAWN_CAPS 185
|
||||
#define MTK_SYS_SPAWN_REDIR_CAPS 186
|
||||
#define MTK_SYS_ALLOC_EAGER 187
|
||||
#define MTK_SYS_BATTERYINFO 188
|
||||
#define MTK_SYS_GETCAPS 189
|
||||
#define MTK_SYS_VOLINFO 190
|
||||
#define MTK_SYS_CONSENT_REGISTER 191
|
||||
#define MTK_SYS_CONSENT_NEXT 192
|
||||
#define MTK_SYS_CONSENT_REPLY 193
|
||||
#define MTK_SYS_GETKEY_EX 194
|
||||
#define MTK_SYS_CHILDIO_WRITEKEY_EX 195
|
||||
/* @SYSCALLS-END */
|
||||
|
||||
/* Syscall status codes (montauk/statuscodes.h for C++). */
|
||||
|
||||
@@ -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