aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-07 06:46:42 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-07 06:46:42 +0100
commitb3b9e8c2b8068ce2566a9be2a813b2f04088f3ce (patch)
tree82b8c03d3e9fa25c0b50fe5ef2f59b713432ac21
parent214336492da0d184d5ad4ac64c31920954c5f7e7 (diff)
Include utf8 string in text entered event
-rw-r--r--include/mgl/window/event.h2
-rw-r--r--src/window/window.c7
-rw-r--r--tests/main.c2
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 <X11/Xutil.h>
#include <stdlib.h>
+#include <string.h>
#include <errno.h>
#include <stdio.h>
@@ -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: {