From a5c95fb971fe0fc6032714ec7093d29c9f28e4d2 Mon Sep 17 00:00:00 2001 From: cheat45681 Date: Mon, 23 Nov 2026 19:05:25 -0500 Subject: [PATCH 1/3] Linux: add a text entry window for the software keyboard (horse naming, #401) --- runtime/gfx_window_sdl.c | 3 + runtime/swkbd_sdl.c | 146 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 139 insertions(+), 10 deletions(-) diff --git a/runtime/gfx_window_sdl.c b/runtime/gfx_window_sdl.c index eef6efe6..083df653 100644 --- a/runtime/gfx_window_sdl.c +++ b/runtime/gfx_window_sdl.c @@ -27,6 +27,8 @@ #include "vk.h" #include "platform/platform.h" +int swkbd_sdl_event(const SDL_Event *e); // swkbd_sdl.c: the text entry window's events + static SDL_Window *g_win; static uint32_t g_ev_base; // SDL_RegisterEvents: the user events below enum { EV_RUN, EV_SETTINGS, EV_CLOSE, EV_COUNT }; @@ -158,6 +160,7 @@ static void handle(const SDL_Event *e) { } if (e->type == g_ev_base + EV_CLOSE) gfx_window_closed(); if (!g_win) return; + if (swkbd_sdl_event(e)) return; input_sdl_event(e); menu_sdl_event(e); switch (e->type) { diff --git a/runtime/swkbd_sdl.c b/runtime/swkbd_sdl.c index becace2a..060938c1 100644 --- a/runtime/swkbd_sdl.c +++ b/runtime/swkbd_sdl.c @@ -1,21 +1,147 @@ -// The software keyboard's front end for SDL windows (swkbd.h). Until the in-game text field exists (an ImGui entry in -// the menu overlay, which SDL_StartTextInput can pair with the Steam Deck's on-screen keyboard), the text entry takes -// the starting text at once, as TOTK_SWKBD_ANSWER= does: the game's initial text, else the default name from -// the catalog. TotK asks only for a horse's name. TOTK_SWKBD_TEST=cancel stands for Cancel pressed, as in the Win32 -// window. +// The software keyboard's front end for SDL windows (swkbd.h): a small text window of its own, opened on the window +// thread (gfx_window_sdl_run) and fed by that thread's events: typed text, Backspace, Enter (OK), Escape or closing the +// window (Cancel). The guest thread waits for the answer. Without a game window it takes the starting text at once. +#include #include +#include + #include "cfg.h" #include "host.h" +#include "input_sdl.h" +#include "platform/platform.h" #include "swkbd.h" +#include "ui_text.h" + +typedef struct { + const SwkbdRequest *req; + uint16_t text[KB_LIMIT + 1]; + size_t n; + int result; // 1 OK, -1 Cancel, 0 still open + int open; + SDL_Window *win; + SDL_Renderer *ren; + Uint32 id; + PlatEvent done; +} Box; + +static Box *g_box; // one text entry at a time (swkbd.c holds the lock) + +static void draw(const Box *b) { + char prompt[0x101 * 3 + 1], text[KB_LIMIT * 3 + 2], line[KB_LIMIT * 3 + 2]; + size_t pl = 0; + while (pl < 0x100 && b->req->prompt[pl]) pl++; + if (pl) swkbd_utf16_to_utf8(b->req->prompt, pl, prompt, sizeof prompt); + else snprintf(prompt, sizeof prompt, "%s", ui_text(UI_SWKBD_PROMPT)); + swkbd_utf16_to_utf8(b->text, b->n, text, sizeof text); + snprintf(line, sizeof line, "%s_", text); + SDL_SetRenderDrawColor(b->ren, 24, 24, 32, 255); + SDL_RenderClear(b->ren); + SDL_SetRenderDrawColor(b->ren, 240, 240, 240, 255); + SDL_SetRenderScale(b->ren, 3.0f, 3.0f); + SDL_RenderDebugText(b->ren, 4, 4, prompt); + SDL_RenderDebugText(b->ren, 4, 28, line); + SDL_RenderDebugText(b->ren, 4, 56, "Enter = OK Esc = Cancel"); + SDL_RenderPresent(b->ren); +} + +static void open_box(void *arg) { + Box *b = arg; + b->win = SDL_CreateWindow(ui_text(UI_SWKBD_TITLE), 720, 240, 0); + if (!b->win) { + rt_log("swkbd: cannot open the text entry window (%s)", SDL_GetError()); + return; + } + b->ren = SDL_CreateRenderer(b->win, NULL); + if (!b->ren) { + rt_log("swkbd: no renderer for the text entry (%s)", SDL_GetError()); + SDL_DestroyWindow(b->win); + b->win = NULL; + return; + } + b->id = SDL_GetWindowID(b->win); + b->open = 1; + g_box = b; + SDL_StartTextInput(b->win); + SDL_RaiseWindow(b->win); + draw(b); +} + +static void close_box(Box *b) { + SDL_StopTextInput(b->win); + SDL_DestroyRenderer(b->ren); + SDL_DestroyWindow(b->win); + b->win = NULL; + b->ren = NULL; + g_box = NULL; + plat_event_set(&b->done); +} + +// The window thread's events for the text entry window; returns 1 when the event was the text entry's. +int swkbd_sdl_event(const SDL_Event *e) { + Box *b = g_box; + if (!b) return 0; + Uint32 id; + switch (e->type) { + case SDL_EVENT_TEXT_INPUT: id = e->text.windowID; break; + case SDL_EVENT_KEY_DOWN: id = e->key.windowID; break; + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: id = e->window.windowID; break; + default: return 0; + } + if (id != b->id) return 0; + const SwkbdRequest *r = b->req; + if (e->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) { + b->result = -1; + } else if (e->type == SDL_EVENT_TEXT_INPUT) { + uint16_t u[KB_LIMIT + 1]; + size_t k = swkbd_utf8_to_utf16(e->text.text, u, KB_LIMIT + 1); + for (size_t i = 0; i < k && b->n < (size_t)r->max_len && b->n < KB_LIMIT; i++) { + int surrogate = u[i] >= 0xd800 && u[i] < 0xe000; + if (surrogate || swkbd_allowed(u[i], r->disabled, r->numeric)) b->text[b->n++] = u[i]; + } + } else { + switch (e->key.scancode) { + case SDL_SCANCODE_BACKSPACE: + if (b->n) b->n--; + break; + case SDL_SCANCODE_RETURN: + case SDL_SCANCODE_KP_ENTER: + if ((int)b->n >= r->min_len) b->result = 1; + break; + case SDL_SCANCODE_ESCAPE: + b->result = -1; + break; + default: + break; + } + } + if (b->result) close_box(b); + else draw(b); + return 1; +} int text_entry_run(const SwkbdRequest *req, uint16_t *out, size_t cap, size_t *n) { const char *test = cfg_get("TOTK_SWKBD_TEST"); if (test && !strcmp(test, "cancel")) { *n = 0; return 0; } + Box b = {.req = req}; size_t k = 0; - while (k < cap && req->initial[k]) k++; - memcpy(out, req->initial, k * 2); - *n = k; - rt_log("swkbd: no text entry window on this host yet: the %s name is taken", req->game_initial ? "game's" : "default"); - return k > 0; + while (k < KB_LIMIT && req->initial[k]) k++; + memcpy(b.text, req->initial, k * 2); + b.n = k; + if (gfx_window_sdl()) { + plat_event_init(&b.done, 1, 0); + gfx_window_sdl_run(open_box, &b); + } + if (!b.open) { + rt_log("swkbd: no text entry window on this host: the %s name is taken", req->game_initial ? "game's" : "default"); + size_t f = b.n < cap ? b.n : cap; + memcpy(out, b.text, f * 2); + *n = f; + return f > 0; + } + plat_event_wait(&b.done); + if (b.result != 1) { *n = 0; return 0; } + *n = b.n < cap ? b.n : cap; + memcpy(out, b.text, *n * 2); + return 1; } From 37551aedade1b5e4525233356096160259b12764 Mon Sep 17 00:00:00 2001 From: cheat45681 Date: Mon, 23 Nov 2026 23:30:55 -0500 Subject: [PATCH 2/3] Linux text entry: controller letter grid (D-pad, bottom add, right delete, Start OK, top cancel); keyboard unchanged --- runtime/swkbd_sdl.c | 89 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/runtime/swkbd_sdl.c b/runtime/swkbd_sdl.c index 060938c1..3f50b0b1 100644 --- a/runtime/swkbd_sdl.c +++ b/runtime/swkbd_sdl.c @@ -1,6 +1,8 @@ -// The software keyboard's front end for SDL windows (swkbd.h): a small text window of its own, opened on the window -// thread (gfx_window_sdl_run) and fed by that thread's events: typed text, Backspace, Enter (OK), Escape or closing the -// window (Cancel). The guest thread waits for the answer. Without a game window it takes the starting text at once. +// The software keyboard's front end for SDL windows (swkbd.h): a text window of its own, opened on the window thread +// (gfx_window_sdl_run) and fed by that thread's events. The keyboard types, Backspace deletes, Enter is OK and Escape +// cancels. The controller moves a letter grid: D-pad moves the cursor, the bottom button adds the letter, the right +// button deletes, Start is OK and the top button cancels. The guest thread waits for the answer. Without a game window +// it takes the starting text at once. #include #include @@ -13,10 +15,15 @@ #include "swkbd.h" #include "ui_text.h" +#define GRID_COLS 10 +static const char grid_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-."; +#define GRID_N ((int)(sizeof grid_chars - 1)) + typedef struct { const SwkbdRequest *req; uint16_t text[KB_LIMIT + 1]; size_t n; + int sel; // the grid cell the controller's cursor is on int result; // 1 OK, -1 Cancel, 0 still open int open; SDL_Window *win; @@ -37,17 +44,31 @@ static void draw(const Box *b) { snprintf(line, sizeof line, "%s_", text); SDL_SetRenderDrawColor(b->ren, 24, 24, 32, 255); SDL_RenderClear(b->ren); + SDL_SetRenderScale(b->ren, 4.0f, 4.0f); SDL_SetRenderDrawColor(b->ren, 240, 240, 240, 255); - SDL_SetRenderScale(b->ren, 3.0f, 3.0f); SDL_RenderDebugText(b->ren, 4, 4, prompt); - SDL_RenderDebugText(b->ren, 4, 28, line); - SDL_RenderDebugText(b->ren, 4, 56, "Enter = OK Esc = Cancel"); + SDL_RenderDebugText(b->ren, 4, 20, line); + for (int i = 0; i < GRID_N; i++) { + float x = 4.0f + (float)(i % GRID_COLS) * 14.0f, y = 40.0f + (float)(i / GRID_COLS) * 18.0f; + char c[2] = {grid_chars[i], 0}; + if (i == b->sel) { + SDL_FRect cell = {x - 2.0f, y - 2.0f, 12.0f, 12.0f}; + SDL_SetRenderDrawColor(b->ren, 200, 160, 40, 255); + SDL_RenderFillRect(b->ren, &cell); + SDL_SetRenderDrawColor(b->ren, 24, 24, 32, 255); + } + SDL_RenderDebugText(b->ren, x, y, c); + SDL_SetRenderDrawColor(b->ren, 240, 240, 240, 255); + } + SDL_RenderDebugText(b->ren, 4, 116, "Keys: type, Enter OK, Esc cancel"); + SDL_RenderDebugText(b->ren, 4, 130, "Pad: D-pad move, bottom add, right del"); + SDL_RenderDebugText(b->ren, 4, 144, "Start OK, top cancel"); SDL_RenderPresent(b->ren); } static void open_box(void *arg) { Box *b = arg; - b->win = SDL_CreateWindow(ui_text(UI_SWKBD_TITLE), 720, 240, 0); + b->win = SDL_CreateWindow(ui_text(UI_SWKBD_TITLE), 1280, 720, 0); if (!b->win) { rt_log("swkbd: cannot open the text entry window (%s)", SDL_GetError()); return; @@ -77,10 +98,57 @@ static void close_box(Box *b) { plat_event_set(&b->done); } -// The window thread's events for the text entry window; returns 1 when the event was the text entry's. +static void add_text(Box *b, const uint16_t *u, size_t k) { + const SwkbdRequest *r = b->req; + for (size_t i = 0; i < k && b->n < (size_t)r->max_len && b->n < KB_LIMIT; i++) { + int surrogate = u[i] >= 0xd800 && u[i] < 0xe000; + if (surrogate || swkbd_allowed(u[i], r->disabled, r->numeric)) b->text[b->n++] = u[i]; + } +} + +static void pad_button(Box *b, Uint8 button) { + switch (button) { + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: + if (b->sel % GRID_COLS) b->sel--; + break; + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: + if (b->sel + 1 < GRID_N && (b->sel + 1) % GRID_COLS) b->sel++; + break; + case SDL_GAMEPAD_BUTTON_DPAD_UP: + if (b->sel >= GRID_COLS) b->sel -= GRID_COLS; + break; + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: + if (b->sel + GRID_COLS < GRID_N) b->sel += GRID_COLS; + break; + case SDL_GAMEPAD_BUTTON_SOUTH: { + uint16_t one = (uint16_t)grid_chars[b->sel]; + add_text(b, &one, 1); + break; + } + case SDL_GAMEPAD_BUTTON_EAST: + if (b->n) b->n--; + break; + case SDL_GAMEPAD_BUTTON_START: + if ((int)b->n >= b->req->min_len) b->result = 1; + break; + case SDL_GAMEPAD_BUTTON_NORTH: + b->result = -1; + break; + default: + break; + } +} + +// The window thread's events for the text entry; returns 1 when the event was the text entry's. int swkbd_sdl_event(const SDL_Event *e) { Box *b = g_box; if (!b) return 0; + if (e->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) { + pad_button(b, e->gbutton.button); + if (b->result) close_box(b); + else draw(b); + return 1; + } Uint32 id; switch (e->type) { case SDL_EVENT_TEXT_INPUT: id = e->text.windowID; break; @@ -95,10 +163,7 @@ int swkbd_sdl_event(const SDL_Event *e) { } else if (e->type == SDL_EVENT_TEXT_INPUT) { uint16_t u[KB_LIMIT + 1]; size_t k = swkbd_utf8_to_utf16(e->text.text, u, KB_LIMIT + 1); - for (size_t i = 0; i < k && b->n < (size_t)r->max_len && b->n < KB_LIMIT; i++) { - int surrogate = u[i] >= 0xd800 && u[i] < 0xe000; - if (surrogate || swkbd_allowed(u[i], r->disabled, r->numeric)) b->text[b->n++] = u[i]; - } + add_text(b, u, k); } else { switch (e->key.scancode) { case SDL_SCANCODE_BACKSPACE: From c3cd1e31c267f3e5559c76d12b093f819d6833cb Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 24 Nov 2026 09:07:09 +0300 Subject: [PATCH 3/3] Text entry on SDL: hint lines come from the catalog, a password shows stars, a surrogate pair goes in and out whole and obeys the length limit, TOTK_SWKBD_TEST=ok presses OK, and the window redraws when exposed (#401) Co-Authored-By: Claude Opus 5.5 --- runtime/swkbd_sdl.c | 54 ++++++++++++++++++++++++++++++++++++--------- runtime/ui_text.c | 5 +++++ runtime/ui_text.h | 3 +++ 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/runtime/swkbd_sdl.c b/runtime/swkbd_sdl.c index 3f50b0b1..436c72b4 100644 --- a/runtime/swkbd_sdl.c +++ b/runtime/swkbd_sdl.c @@ -40,7 +40,14 @@ static void draw(const Box *b) { while (pl < 0x100 && b->req->prompt[pl]) pl++; if (pl) swkbd_utf16_to_utf8(b->req->prompt, pl, prompt, sizeof prompt); else snprintf(prompt, sizeof prompt, "%s", ui_text(UI_SWKBD_PROMPT)); - swkbd_utf16_to_utf8(b->text, b->n, text, sizeof text); + if (b->req->password) { // one star per character, not per UTF-16 unit + size_t k = 0; + for (size_t i = 0; i < b->n && k < KB_LIMIT; i++) + if (b->text[i] < 0xdc00 || b->text[i] >= 0xe000) text[k++] = '*'; + text[k] = 0; + } else { + swkbd_utf16_to_utf8(b->text, b->n, text, sizeof text); + } snprintf(line, sizeof line, "%s_", text); SDL_SetRenderDrawColor(b->ren, 24, 24, 32, 255); SDL_RenderClear(b->ren); @@ -60,12 +67,15 @@ static void draw(const Box *b) { SDL_RenderDebugText(b->ren, x, y, c); SDL_SetRenderDrawColor(b->ren, 240, 240, 240, 255); } - SDL_RenderDebugText(b->ren, 4, 116, "Keys: type, Enter OK, Esc cancel"); - SDL_RenderDebugText(b->ren, 4, 130, "Pad: D-pad move, bottom add, right del"); - SDL_RenderDebugText(b->ren, 4, 144, "Start OK, top cancel"); + // the debug font draws ASCII: the hints are the catalog's English text whatever the game's language + SDL_RenderDebugText(b->ren, 4, 116, ui_text_in("en", UI_SWKBD_SDL_KEYS_HINT)); + SDL_RenderDebugText(b->ren, 4, 130, ui_text_in("en", UI_SWKBD_SDL_PAD_HINT1)); + SDL_RenderDebugText(b->ren, 4, 144, ui_text_in("en", UI_SWKBD_SDL_PAD_HINT2)); SDL_RenderPresent(b->ren); } +static void close_box(Box *b); + static void open_box(void *arg) { Box *b = arg; b->win = SDL_CreateWindow(ui_text(UI_SWKBD_TITLE), 1280, 720, 0); @@ -86,6 +96,11 @@ static void open_box(void *arg) { SDL_StartTextInput(b->win); SDL_RaiseWindow(b->win); draw(b); + const char *test = cfg_get("TOTK_SWKBD_TEST"); + if (test && !strcmp(test, "ok")) { // as the Win32 window: OK pressed at once + b->result = 1; + close_box(b); + } } static void close_box(Box *b) { @@ -98,14 +113,29 @@ static void close_box(Box *b) { plat_event_set(&b->done); } +// Appends typed UTF-16 within the game's limits: a surrogate pair goes in whole or not at all (as clean in swkbd.c), +// and a number pad takes digits only. static void add_text(Box *b, const uint16_t *u, size_t k) { const SwkbdRequest *r = b->req; - for (size_t i = 0; i < k && b->n < (size_t)r->max_len && b->n < KB_LIMIT; i++) { - int surrogate = u[i] >= 0xd800 && u[i] < 0xe000; - if (surrogate || swkbd_allowed(u[i], r->disabled, r->numeric)) b->text[b->n++] = u[i]; + size_t max = (size_t)r->max_len < KB_LIMIT ? (size_t)r->max_len : KB_LIMIT; + for (size_t i = 0; i < k; i++) { + int pair = u[i] >= 0xd800 && u[i] < 0xdc00 && i + 1 < k && u[i + 1] >= 0xdc00 && u[i + 1] < 0xe000; + if (pair) { + if (!r->numeric && b->n + 2 <= max) b->text[b->n++] = u[i], b->text[b->n++] = u[i + 1]; + i++; + } else if (b->n < max && swkbd_allowed(u[i], r->disabled, r->numeric)) { + b->text[b->n++] = u[i]; + } } } +static void delete_last(Box *b) { + if (!b->n) return; + int pair = b->n >= 2 && b->text[b->n - 1] >= 0xdc00 && b->text[b->n - 1] < 0xe000 && b->text[b->n - 2] >= 0xd800 && + b->text[b->n - 2] < 0xdc00; + b->n -= pair ? 2 : 1; +} + static void pad_button(Box *b, Uint8 button) { switch (button) { case SDL_GAMEPAD_BUTTON_DPAD_LEFT: @@ -126,7 +156,7 @@ static void pad_button(Box *b, Uint8 button) { break; } case SDL_GAMEPAD_BUTTON_EAST: - if (b->n) b->n--; + delete_last(b); break; case SDL_GAMEPAD_BUTTON_START: if ((int)b->n >= b->req->min_len) b->result = 1; @@ -153,13 +183,17 @@ int swkbd_sdl_event(const SDL_Event *e) { switch (e->type) { case SDL_EVENT_TEXT_INPUT: id = e->text.windowID; break; case SDL_EVENT_KEY_DOWN: id = e->key.windowID; break; - case SDL_EVENT_WINDOW_CLOSE_REQUESTED: id = e->window.windowID; break; + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + case SDL_EVENT_WINDOW_EXPOSED: id = e->window.windowID; break; default: return 0; } if (id != b->id) return 0; const SwkbdRequest *r = b->req; if (e->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) { b->result = -1; + } else if (e->type == SDL_EVENT_WINDOW_EXPOSED) { + draw(b); // the compositor dropped the contents + return 1; } else if (e->type == SDL_EVENT_TEXT_INPUT) { uint16_t u[KB_LIMIT + 1]; size_t k = swkbd_utf8_to_utf16(e->text.text, u, KB_LIMIT + 1); @@ -167,7 +201,7 @@ int swkbd_sdl_event(const SDL_Event *e) { } else { switch (e->key.scancode) { case SDL_SCANCODE_BACKSPACE: - if (b->n) b->n--; + delete_last(b); break; case SDL_SCANCODE_RETURN: case SDL_SCANCODE_KP_ENTER: diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 401a2818..bd652507 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -40,6 +40,11 @@ typedef struct { const char *lang; const char *const *text; } Row; static const char *const text_en[UI_TEXT_COUNT] = { SWKBD("Text entry", "Enter a name", "Horse", "OK", "Cancel", "Controller: A = OK, B = Cancel"), + // The SDL text entry draws with SDL's ASCII debug font, so these three exist in English only (the other rows fall + // back to it). + [UI_SWKBD_SDL_KEYS_HINT] = "Keys: type, Enter OK, Esc cancel", + [UI_SWKBD_SDL_PAD_HINT1] = "Pad: D-pad move, bottom add, right del", + [UI_SWKBD_SDL_PAD_HINT2] = "Start OK, top cancel", [UI_MENU_TITLE] = "Settings", [UI_MENU_TAB_SCREEN] = "Screen", [UI_MENU_TAB_GAME] = "Game", [UI_MENU_TAB_MODS] = "Mods", [UI_MENU_TAB_SOUND] = "Sound", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index db8bf1d3..71b7d594 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -10,6 +10,9 @@ typedef enum { UI_SWKBD_OK, UI_SWKBD_CANCEL, UI_SWKBD_PAD_HINT, // how a controller answers + UI_SWKBD_SDL_KEYS_HINT, // the SDL window's key line; English only: its debug font draws ASCII (swkbd_sdl.c) + UI_SWKBD_SDL_PAD_HINT1, // the SDL window's controller lines, the letter grid's buttons + UI_SWKBD_SDL_PAD_HINT2, // Menu: window, tabs, bottom row. UI_MENU_FIRST..UI_MENU_LAST_ID carry English, Russian, Brazilian Portuguese, // Simplified Chinese, German, French, Spanish, Italian, Dutch, Japanese, Korean and Vietnamese texts.