ime: wayland: Make use of `SDL_TEXTEDITING_EXT` Because we were sending multiple chunks of preedit strings, `SDL_SendEditingText` was using the old `SDL_TEXTEDITING` event only. Now if `SDL_HINT_IME_SUPPORT_EXTENDED_TEXT` is enabled, we send the full string and correctly set the cursor position and selection size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index b08da5b..0d3c19b 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -25,6 +25,7 @@
#include "SDL_stdinc.h"
#include "SDL_timer.h"
+#include "SDL_hints.h"
#include "../../core/unix/SDL_poll.h"
#include "../../events/SDL_sysevents.h"
@@ -1554,18 +1555,33 @@ text_input_preedit_string(void *data,
char buf[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
text_input->has_preedit = SDL_TRUE;
if (text) {
- size_t text_bytes = SDL_strlen(text), i = 0;
- size_t cursor = 0;
-
- do {
- const size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
- const size_t chars = SDL_utf8strlen(buf);
-
- SDL_SendEditingText(buf, cursor, chars);
-
- i += sz;
- cursor += chars;
- } while (i < text_bytes);
+ if (SDL_GetHintBoolean(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, SDL_FALSE)) {
+ size_t cursor_begin_utf8 = cursor_begin >= 0 ? SDL_utf8strnlen(text, cursor_begin) : -1;
+ size_t cursor_end_utf8 = cursor_end >= 0 ? SDL_utf8strnlen(text, cursor_end) : -1;
+ size_t cursor_size_utf8;
+ if (cursor_end_utf8 >= 0) {
+ if (cursor_begin_utf8 >= 0) {
+ cursor_size_utf8 = cursor_end_utf8 - cursor_begin_utf8;
+ } else {
+ cursor_size_utf8 = cursor_end_utf8;
+ }
+ } else {
+ cursor_size_utf8 = -1;
+ }
+ SDL_SendEditingText(text, cursor_begin_utf8, cursor_size_utf8);
+ } else {
+ size_t text_bytes = SDL_strlen(text), i = 0;
+ size_t cursor = 0;
+ do {
+ const size_t sz = SDL_utf8strlcpy(buf, text+i, sizeof(buf));
+ const size_t chars = SDL_utf8strlen(buf);
+
+ SDL_SendEditingText(buf, cursor, chars);
+
+ i += sz;
+ cursor += chars;
+ } while (i < text_bytes);
+ }
} else {
buf[0] = '\0';
SDL_SendEditingText(buf, 0, 0);