Start SDL text input: the AKGL build's keyboard was dead

SDL3 has text input off by default and per-window, so without
SDL_StartTextInput() it emits no SDL_EVENT_TEXT_INPUT at all and every
keystroke reaches libakgl's ring with an empty text field. The editor had just
been changed to prefer that composed text over the keycode, so it read every
key as not-a-character: no echo in the window, and nothing on stdout either,
because RUN could never be typed. One cause, both symptoms.

The editor now falls back to the keycode when there is no composed text, so a
host that forgets to start text input gets a worse keyboard rather than none.

The suite missed this because every keyboard test pushes the text-input event
into SDL's queue by hand -- which is what a real keyboard produces, but only
once text input has been started. Synthesising the end of a chain cannot test
the beginning of it. The new test asserts SDL_TextInputActive() directly, and
both halves were checked by reverting each and watching it fail.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 14:23:07 -04:00
parent fca9ad4a89
commit 024a33dbba
4 changed files with 115 additions and 2 deletions

View File

@@ -77,6 +77,21 @@ akerr_ErrorContext *akbasic_frontend_akgl_init(akbasic_AkglFrontend *obj, const
&obj->window, &obj->renderer->sdl_renderer),
AKGL_ERR_SDL, "Couldn't create the window: %s", SDL_GetError());
window = obj->window;
/*
* Without this SDL sends no SDL_EVENT_TEXT_INPUT at all, every keystroke
* reaches the ring with an empty `text`, and the line editor has nothing to
* type -- akgl/controller.h says so directly. It is off by default in SDL3
* and is per-window, so it is the host's job and this is the host.
*
* Not fatal if it fails: the editor falls back to the keycode, which is a
* worse keyboard rather than no keyboard. Failing to start a window over an
* input method that is not there would be the wrong trade.
*/
if ( !SDL_StartTextInput(obj->window) ) {
SDL_Log("Could not start text input (%s); typing falls back to keycodes",
SDL_GetError());
}
/*
* Bind the 2D methods onto the renderer we just made. akgl_render_init2d()
* would do this too, but it creates its own window from the game properties
@@ -260,6 +275,7 @@ void akbasic_frontend_akgl_shutdown(akbasic_AkglFrontend *obj)
obj->renderer->sdl_renderer = NULL;
}
if ( obj->window != NULL ) {
SDL_StopTextInput(obj->window);
SDL_DestroyWindow(obj->window);
obj->window = NULL;
window = NULL;

View File

@@ -13,6 +13,7 @@
* equivalent until then.
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
@@ -218,8 +219,28 @@ static void edit_key(akbasic_AkglSink *state, const akgl_Keystroke *key, bool *s
}
/*
* No composed text: a cursor key, a function key or a bare modifier. Not an
* editing command here -- a script's own GET loop is what wants those.
* No composed text, but a printable keycode: type it anyway.
*
* **This fallback is why the editor still works on a host that never called
* SDL_StartTextInput().** SDL emits no SDL_EVENT_TEXT_INPUT until text input
* is started, so without it every keystroke arrives here with an empty
* `text` -- and treating that as "not a character" makes the entire keyboard
* dead, silently, which is exactly what happened once. A worse keyboard is a
* great deal better than no keyboard.
*
* Upper case, because that is all a bare keycode can offer and it is what a
* C128 does. When text input *is* running a printable key always carries
* text, so this never fires and lower case survives.
*/
if ( key->key >= 0x20 && key->key < 0x7f ) {
edit_append(state, (char)toupper((unsigned char)key->key));
echo_line(state);
return;
}
/*
* Neither: a cursor key, a function key or a bare modifier. Not an editing
* command here -- a script's own GET loop is what wants those.
*/
}