SDL: Fix keymap updating for X11 backend

From de6d290266d1def0eef9df81bf9be41c12a98c61 Mon Sep 17 00:00:00 2001
From: ReNoM <[EMAIL REDACTED]>
Date: Mon, 19 Apr 2021 21:48:11 +0300
Subject: [PATCH] Fix keymap updating for X11 backend

---
 src/video/x11/SDL_x11events.c | 4 +++-
 src/video/x11/SDL_x11sym.h    | 1 +
 src/video/x11/SDL_x11window.c | 2 ++
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c
index e93fd7945..37c76c5e9 100644
--- a/src/video/x11/SDL_x11events.c
+++ b/src/video/x11/SDL_x11events.c
@@ -676,6 +676,7 @@ X11_DispatchEvent(_THIS)
     Display *display;
     SDL_WindowData *data;
     XEvent xevent;
+    XkbEvent* xkbEvent;
     int orig_event_type;
     KeyCode orig_keycode;
     XClientMessageEvent m;
@@ -688,6 +689,7 @@ X11_DispatchEvent(_THIS)
 
     SDL_zero(xevent);           /* valgrind fix. --ryan. */
     X11_XNextEvent(display, &xevent);
+    xkbEvent = (XkbEvent*) &xevent;
 
     /* Save the original keycode for dead keys, which are filtered out by
        the XFilterEvent() call below.
@@ -768,7 +770,7 @@ X11_DispatchEvent(_THIS)
             if (SDL_GetKeyboardFocus() != NULL) {
                 X11_ReconcileKeyboardState(_this);
             }
-        } else if (xevent.type == MappingNotify) {
+        } else if (xevent.type == MappingNotify || xkbEvent->any.xkb_type == XkbStateNotify) {
             /* Has the keyboard layout changed? */
             const int request = xevent.xmapping.request;
 
diff --git a/src/video/x11/SDL_x11sym.h b/src/video/x11/SDL_x11sym.h
index f226733ce..a550dc2e9 100644
--- a/src/video/x11/SDL_x11sym.h
+++ b/src/video/x11/SDL_x11sym.h
@@ -182,6 +182,7 @@ SDL_X11_SYM(XkbDescPtr,XkbGetMap,(Display* a,unsigned int b,unsigned int c),(a,b
 SDL_X11_SYM(void,XkbFreeClientMap,(XkbDescPtr a,unsigned int b, Bool c),(a,b,c),)
 SDL_X11_SYM(void,XkbFreeKeyboard,(XkbDescPtr a,unsigned int b, Bool c),(a,b,c),)
 SDL_X11_SYM(Bool,XkbSetDetectableAutoRepeat,(Display* a, Bool b, Bool* c),(a,b,c),return)
+SDL_X11_SYM(Bool,XkbSelectEvents,(Display* a, unsigned int b, unsigned int c, unsigned int d),(a,b,c,d),return)
 #endif
 
 #if NeedWidePrototypes
diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c
index 3b090f7d5..71e51076b 100644
--- a/src/video/x11/SDL_x11window.c
+++ b/src/video/x11/SDL_x11window.c
@@ -668,6 +668,8 @@ X11_CreateWindow(_THIS, SDL_Window * window)
                  PropertyChangeMask | StructureNotifyMask |
                  KeymapStateMask | fevent));
 
+    X11_XkbSelectEvents(display, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask);
+
     X11_XFlush(display);
 
     return 0;