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
+137
-22
@@ -12,6 +12,107 @@
|
||||
#include "Common.hpp"
|
||||
|
||||
namespace montauk::abi {
|
||||
// ---- Key text ----------------------------------------------------------
|
||||
// Keyboard layouts are applied in userspace (desktop, terminal), which send
|
||||
// KeyEvents carrying the Unicode codepoint. Binaries built before that send
|
||||
// 9-byte KeyEventV1s whose `ascii` is a Windows-1252 byte; both kinds share
|
||||
// a redirected child's key mailbox, told apart by message length.
|
||||
|
||||
static uint32_t KeyCp1252ToUnicode(uint8_t b) {
|
||||
static constexpr uint16_t high[32] = {
|
||||
0x20AC, 0x81, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
|
||||
0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x8D, 0x017D, 0x8F,
|
||||
0x90, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
|
||||
0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x9D, 0x017E, 0x0178,
|
||||
};
|
||||
return (b >= 0x80 && b <= 0x9F) ? high[b - 0x80] : b;
|
||||
}
|
||||
|
||||
// The character a key event types, or 0 for control/navigation keys.
|
||||
static uint32_t KeyTextCodepoint(const KeyEvent& ev) {
|
||||
if (ev.codepoint) return ev.codepoint;
|
||||
uint8_t b = (uint8_t)ev.ascii;
|
||||
if (b < 0x20 || b == 0x7F) return 0;
|
||||
return KeyCp1252ToUnicode(b);
|
||||
}
|
||||
|
||||
static int KeyEncodeUtf8(uint32_t cp, uint8_t* out) {
|
||||
if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) cp = 0xFFFD;
|
||||
if (cp < 0x80) { out[0] = (uint8_t)cp; return 1; }
|
||||
if (cp < 0x800) {
|
||||
out[0] = (uint8_t)(0xC0 | (cp >> 6));
|
||||
out[1] = (uint8_t)(0x80 | (cp & 0x3F));
|
||||
return 2;
|
||||
}
|
||||
if (cp < 0x10000) {
|
||||
out[0] = (uint8_t)(0xE0 | (cp >> 12));
|
||||
out[1] = (uint8_t)(0x80 | ((cp >> 6) & 0x3F));
|
||||
out[2] = (uint8_t)(0x80 | (cp & 0x3F));
|
||||
return 3;
|
||||
}
|
||||
out[0] = (uint8_t)(0xF0 | (cp >> 18));
|
||||
out[1] = (uint8_t)(0x80 | ((cp >> 12) & 0x3F));
|
||||
out[2] = (uint8_t)(0x80 | ((cp >> 6) & 0x3F));
|
||||
out[3] = (uint8_t)(0x80 | (cp & 0x3F));
|
||||
return 4;
|
||||
}
|
||||
|
||||
static void KeyFromDriver(const Drivers::PS2::Keyboard::KeyEvent& k, KeyEvent* out) {
|
||||
memset(out, 0, sizeof(KeyEvent));
|
||||
out->scancode = k.Scancode;
|
||||
out->ascii = k.Ascii;
|
||||
out->pressed = k.Pressed;
|
||||
out->shift = k.Shift;
|
||||
out->ctrl = k.Ctrl;
|
||||
out->alt = k.Alt;
|
||||
out->capslock = k.CapsLock;
|
||||
out->altgr = k.AltGr;
|
||||
out->extended = k.Extended;
|
||||
// The kernel's own scancode table is US ASCII.
|
||||
uint8_t a = (uint8_t)k.Ascii;
|
||||
out->codepoint = (a >= 0x20 && a < 0x7F) ? a : 0;
|
||||
}
|
||||
|
||||
// Receive one key from a redirected child's mailbox as a full KeyEvent,
|
||||
// upgrading legacy 9-byte messages. Returns MailboxRecv's result.
|
||||
static int KeyMailboxRecv(Ipc::Mailbox* mailbox, KeyEvent* out) {
|
||||
memset(out, 0, sizeof(KeyEvent));
|
||||
uint16_t len = sizeof(KeyEvent);
|
||||
int rc = Ipc::MailboxRecv(mailbox, nullptr, out, &len, true);
|
||||
if (rc > 0 && len < sizeof(KeyEvent)) {
|
||||
memset((uint8_t*)out + len, 0, sizeof(KeyEvent) - len);
|
||||
out->codepoint = KeyTextCodepoint(*out);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
// Turn a pressed key into the next getchar byte. Characters above ASCII are
|
||||
// returned as UTF-8, one byte per call; the rest is parked in the process.
|
||||
static bool KeyTakeChar(Sched::Process* proc, const KeyEvent& ev, char* out) {
|
||||
if (!ev.pressed) return false;
|
||||
uint32_t cp = KeyTextCodepoint(ev);
|
||||
if (cp >= 0x80 && proc != nullptr) {
|
||||
int n = KeyEncodeUtf8(cp, proc->getcharUtf8);
|
||||
proc->getcharUtf8Len = (uint8_t)n;
|
||||
proc->getcharUtf8Pos = 1;
|
||||
*out = (char)proc->getcharUtf8[0];
|
||||
return true;
|
||||
}
|
||||
if (ev.ascii != 0 && ev.ascii != KEY_ASCII_SUBSTITUTE) {
|
||||
*out = ev.ascii;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool KeyPendingChar(Sched::Process* proc, char* out) {
|
||||
if (proc == nullptr || proc->getcharUtf8Pos >= proc->getcharUtf8Len) return false;
|
||||
*out = (char)proc->getcharUtf8[proc->getcharUtf8Pos++];
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- Syscalls ------------------------------------------------------------
|
||||
|
||||
static bool Sys_IsKeyAvailable() {
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
if (proc && proc->redirected) {
|
||||
@@ -27,8 +128,8 @@ namespace montauk::abi {
|
||||
return Drivers::PS2::Keyboard::IsKeyAvailable();
|
||||
}
|
||||
|
||||
static void Sys_GetKey(KeyEvent* outEvent) {
|
||||
if (outEvent == nullptr) return;
|
||||
// Blocking read of the next key event, in the full format.
|
||||
static void ReadKeyEvent(KeyEvent* outEvent) {
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
if (proc && proc->redirected) {
|
||||
Ipc::HandleSnapshot snapshot;
|
||||
@@ -36,8 +137,7 @@ namespace montauk::abi {
|
||||
if (mailbox != nullptr) {
|
||||
for (;;) {
|
||||
uint64_t observedWake = Sched::ObserveObjectWake(mailbox);
|
||||
uint16_t len = sizeof(KeyEvent);
|
||||
int rc = Ipc::MailboxRecv(mailbox, nullptr, outEvent, &len, true);
|
||||
int rc = KeyMailboxRecv(mailbox, outEvent);
|
||||
if (rc > 0) return;
|
||||
if (rc < 0) {
|
||||
memset(outEvent, 0, sizeof(KeyEvent));
|
||||
@@ -47,20 +147,33 @@ namespace montauk::abi {
|
||||
}
|
||||
}
|
||||
}
|
||||
auto k = Drivers::PS2::Keyboard::GetKey();
|
||||
outEvent->scancode = k.Scancode;
|
||||
outEvent->ascii = k.Ascii;
|
||||
outEvent->pressed = k.Pressed;
|
||||
outEvent->shift = k.Shift;
|
||||
outEvent->ctrl = k.Ctrl;
|
||||
outEvent->alt = k.Alt;
|
||||
outEvent->capslock = k.CapsLock;
|
||||
outEvent->altgr = k.AltGr;
|
||||
outEvent->extended = k.Extended;
|
||||
KeyFromDriver(Drivers::PS2::Keyboard::GetKey(), outEvent);
|
||||
}
|
||||
|
||||
// SYS_GETKEY: legacy 9-byte event. A new-format event's first 9 bytes are
|
||||
// already a valid KeyEventV1 (ascii holds the 1252 byte or SUB).
|
||||
static void Sys_GetKey(KeyEventV1* outEvent) {
|
||||
if (outEvent == nullptr) return;
|
||||
KeyEvent ev;
|
||||
ReadKeyEvent(&ev);
|
||||
memcpy(outEvent, &ev, sizeof(KeyEventV1));
|
||||
}
|
||||
|
||||
// SYS_GETKEY_EX: size-versioned. The caller's size was validated by the
|
||||
// dispatcher (>= sizeof(KeyEventV1), writable).
|
||||
static int Sys_GetKeyEx(void* outEvent, uint64_t size) {
|
||||
KeyEvent ev;
|
||||
ReadKeyEvent(&ev);
|
||||
uint64_t n = size < sizeof(KeyEvent) ? size : sizeof(KeyEvent);
|
||||
memcpy(outEvent, &ev, n);
|
||||
if (size > n) memset((uint8_t*)outEvent + n, 0, size - n);
|
||||
return Success;
|
||||
}
|
||||
|
||||
static char Sys_GetChar() {
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
char pending;
|
||||
if (KeyPendingChar(proc, &pending)) return pending;
|
||||
if (proc && proc->redirected) {
|
||||
Ipc::HandleSnapshot inputSnapshot;
|
||||
Ipc::HandleSnapshot mailboxSnapshot;
|
||||
@@ -78,11 +191,11 @@ namespace montauk::abi {
|
||||
}
|
||||
|
||||
if (mailbox != nullptr) {
|
||||
KeyEvent ev{};
|
||||
uint16_t len = sizeof(ev);
|
||||
int rc = Ipc::MailboxRecv(mailbox, nullptr, &ev, &len, true);
|
||||
KeyEvent ev;
|
||||
int rc = KeyMailboxRecv(mailbox, &ev);
|
||||
if (rc > 0) {
|
||||
if (ev.pressed && ev.ascii != 0) return ev.ascii;
|
||||
char c;
|
||||
if (KeyTakeChar(proc, ev, &c)) return c;
|
||||
continue;
|
||||
}
|
||||
if (rc < 0 && input == nullptr) return 0;
|
||||
@@ -115,6 +228,8 @@ namespace montauk::abi {
|
||||
*/
|
||||
static char Sys_GetCharNb() {
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
char pending;
|
||||
if (KeyPendingChar(proc, &pending)) return pending;
|
||||
if (proc && proc->redirected) {
|
||||
Ipc::HandleSnapshot inputSnapshot;
|
||||
Ipc::HandleSnapshot mailboxSnapshot;
|
||||
@@ -127,10 +242,10 @@ namespace montauk::abi {
|
||||
}
|
||||
|
||||
while (mailbox != nullptr) {
|
||||
KeyEvent ev{};
|
||||
uint16_t len = sizeof(ev);
|
||||
if (Ipc::MailboxRecv(mailbox, nullptr, &ev, &len, true) <= 0) break;
|
||||
if (ev.pressed && ev.ascii != 0) return ev.ascii;
|
||||
KeyEvent ev;
|
||||
if (KeyMailboxRecv(mailbox, &ev) <= 0) break;
|
||||
char c;
|
||||
if (KeyTakeChar(proc, ev, &c)) return c;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user