Skip to content

Commit

Permalink
fixup! SDL2: preliminary handling SDL_TEXTINPUT
Browse files Browse the repository at this point in the history
Make SDL2's textinput API more robust. We always send keyboard events if
textinput has not been enabled by the application. Moreover, we only
send key presses as text if the codepoint is valid and if it's not a known
non-printable. Keyboard events are omitted if key presses are sent as
text.

This has been tested with sdl_vnc.

#368
  • Loading branch information
jschlatow authored and chelmuth committed May 16, 2024
1 parent 1757cda commit 3efc500
Showing 1 changed file with 16 additions and 47 deletions.
63 changes: 16 additions & 47 deletions src/lib/sdl2/video/SDL_genode_fb_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,48 +82,20 @@ extern "C" {
#include "SDL_sysevents.h"
#include "SDL_genode_fb_events.h"

static bool skipkey(Input::Keycode const keycode)
static bool skipcode(Genode::Codepoint const code)
{
using namespace Input;
if (!code.valid())
return true;

/* just filter some keys that might wreck havoc */
switch (keycode) {
case KEY_CAPSLOCK: return true;
case KEY_LEFTALT: return true;
case KEY_LEFTCTRL: return true;
case KEY_LEFTSHIFT: return true;
case KEY_RIGHTALT: return true;
case KEY_RIGHTCTRL: return true;
case KEY_RIGHTSHIFT: return true;
default: return false;
}
}
/* non-printable ascii */
if (code.value <= 31 || code.value == 127)
return true;

/* "borrowed" from SDL_windowsevents.c */
static int ConvertUTF32toUTF8(unsigned int codepoint, char *text)
{
if (codepoint <= 0x7F) {
text[0] = (char) codepoint;
text[1] = '\0';
} else if (codepoint <= 0x7FF) {
text[0] = 0xC0 | (char) ((codepoint >> 6) & 0x1F);
text[1] = 0x80 | (char) (codepoint & 0x3F);
text[2] = '\0';
} else if (codepoint <= 0xFFFF) {
text[0] = 0xE0 | (char) ((codepoint >> 12) & 0x0F);
text[1] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
text[2] = 0x80 | (char) (codepoint & 0x3F);
text[3] = '\0';
} else if (codepoint <= 0x10FFFF) {
text[0] = 0xF0 | (char) ((codepoint >> 18) & 0x0F);
text[1] = 0x80 | (char) ((codepoint >> 12) & 0x3F);
text[2] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
text[3] = 0x80 | (char) (codepoint & 0x3F);
text[4] = '\0';
} else {
return SDL_FALSE;
}
return SDL_TRUE;
/* function-key unicodes */
if (code.value >= 0xf000 && code.value <= 0xf72d)
return true;

return false;
}

static Genode::Constructible<Input::Session_client> input;
Expand Down Expand Up @@ -196,15 +168,12 @@ extern "C" {
curr.handle_press([&] (Input::Keycode key, Genode::Codepoint codepoint) {
if (mouse_button(key))
SDL_SendMouseButton(window, mouse_id, SDL_PRESSED, buttonmap[key]);
else
else if (skipcode(codepoint) || !SDL_IsTextInputActive())
SDL_SendKeyboardKey(SDL_PRESSED, getscancode(key));

if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY) && !skipkey(key)) {
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
SDL_zeroa(text);
if (ConvertUTF32toUTF8(codepoint.value, text)) {
SDL_SendKeyboardText(text);
}
else {
Genode::String<5> text(codepoint);
if (text.valid())
SDL_SendKeyboardText(text.string());
}
});

Expand Down

0 comments on commit 3efc500

Please sign in to comment.