SDL: x11: Send key events for dead keys consumed by the IME (e4207)

From e42071a47cca3d4e5aa2e6344faa6ac16556395e Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Sun, 20 Apr 2025 15:18:47 -0500
Subject: [PATCH] x11: Send key events for dead keys consumed by the IME

This matches the Wayland backend and what apps originally written for SDL2 are expecting.

(cherry picked from commit 47162a4168c4607e8771bab020ea8f2acd16121a)
---
 src/video/x11/SDL_x11events.c | 35 ++++++++++++++++-------------------
 1 file changed, 16 insertions(+), 19 deletions(-)

diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index 96bf8c7a1b68d..8f0ecc9b1dbf6 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -987,29 +987,26 @@ void X11_HandleKeyEvent(SDL_VideoDevice *_this, SDL_WindowData *windowdata, SDL_
         }
     }
 
-    if (!handled_by_ime) {
-        if (pressed) {
-            X11_HandleModifierKeys(videodata, scancode, true, true);
-            SDL_SendKeyboardKeyIgnoreModifiers(timestamp, keyboardID, keycode, scancode, true);
-
-            if (*text && !(SDL_GetModState() & (SDL_KMOD_CTRL | SDL_KMOD_ALT))) {
-                text[text_length] = '\0';
-                X11_ClearComposition(windowdata);
-                SDL_SendKeyboardText(text);
-            }
-        } else {
-            if (X11_KeyRepeat(display, xevent)) {
-                // We're about to get a repeated key down, ignore the key up
-                return;
-            }
+    if (pressed) {
+        X11_HandleModifierKeys(videodata, scancode, true, true);
+        SDL_SendKeyboardKeyIgnoreModifiers(timestamp, keyboardID, keycode, scancode, true);
 
-            X11_HandleModifierKeys(videodata, scancode, false, true);
-            SDL_SendKeyboardKeyIgnoreModifiers(timestamp, keyboardID, keycode, scancode, false);
+        // Synthesize a text event if the IME didn't consume a printable character
+        if (*text && !(SDL_GetModState() & (SDL_KMOD_CTRL | SDL_KMOD_ALT))) {
+            text[text_length] = '\0';
+            X11_ClearComposition(windowdata);
+            SDL_SendKeyboardText(text);
         }
-    }
 
-    if (pressed) {
         X11_UpdateUserTime(windowdata, xevent->xkey.time);
+    } else {
+        if (X11_KeyRepeat(display, xevent)) {
+            // We're about to get a repeated key down, ignore the key up
+            return;
+        }
+
+        X11_HandleModifierKeys(videodata, scancode, false, true);
+        SDL_SendKeyboardKeyIgnoreModifiers(timestamp, keyboardID, keycode, scancode, false);
     }
 }