From b3b9e8c2b8068ce2566a9be2a813b2f04088f3ce Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 7 Nov 2021 06:46:42 +0100 Subject: Include utf8 string in text entered event --- include/mgl/window/event.h | 2 ++ src/window/window.c | 7 ++++++- tests/main.c | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/mgl/window/event.h b/include/mgl/window/event.h index 0836098..4a3ba71 100644 --- a/include/mgl/window/event.h +++ b/include/mgl/window/event.h @@ -14,6 +14,8 @@ typedef struct { typedef struct { uint32_t codepoint; + int size; + unsigned char str[5]; /* utf8, null terminated */ } mgl_text_event; typedef struct { diff --git a/src/window/window.c b/src/window/window.c index cec6417..b53c4a1 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -4,6 +4,7 @@ #include "../../include/mgl/system/utf8.h" #include #include +#include #include #include @@ -347,13 +348,17 @@ static void mgl_window_handle_text_event(mgl_window *self, XEvent *xev) { for(size_t i = 0; i < input_str_len;) { uint32_t codepoint = 0; const size_t clen = mgl_utf8_decode((const unsigned char*)&buf[i], &codepoint); - i += clen; mgl_event text_event; text_event.type = MGL_EVENT_TEXT_ENTERED; text_event.text.codepoint = codepoint; + text_event.text.size = clen; + memcpy(text_event.text.str, (const unsigned char*)&buf[i], clen); + text_event.text.str[clen] = '\0'; if(!x11_context_append_event(x11_context, &text_event)) break; + + i += clen; } } diff --git a/tests/main.c b/tests/main.c index 85754f2..2359cda 100644 --- a/tests/main.c +++ b/tests/main.c @@ -218,7 +218,7 @@ int main(int argc, char **argv) { while(mgl_window_poll_event(&window, &event)) { switch(event.type) { case MGL_EVENT_TEXT_ENTERED: { - fprintf(stderr, "text event, codepoint: %u\n", event.text.codepoint); + fprintf(stderr, "text event, codepoint: %u, str: %s\n", event.text.codepoint, event.text.str); break; } case MGL_EVENT_KEY_PRESSED: { -- cgit v1.2.3