Blame SOURCES/SDL-1.2.15-SDL_EnableUNICODE_drops_keyboard_events.patch

24d066
# HG changeset patch
24d066
# User Sam Lantinga <slouken@libsdl.org>
24d066
# Date 1397799374 25200
24d066
#      Thu Apr 17 22:36:14 2014 -0700
24d066
# Branch SDL-1.2
24d066
# Node ID 0aade9c0203f717fe4b823a176c3c040f1a709f8
24d066
# Parent  22a7f096bb9d4d596f35a93e33608825693462b0
24d066
Fixed bug 2325 - SDL_EnableUNICODE sometimes drops keyboard events completely
24d066
24d066
Rafał Mużyło
24d066
24d066
The most annoying part of this bug is that though I've found it in two separate apps, I don't have a trivial testcase for it.
24d066
24d066
The problem seems to be a condition race, as it's triggered quite randomly (therefore it will be hard to tell whether it really gets fixed, if a probable fix is found).
24d066
24d066
While it's specific to SDL 1.2, it seems quite similar to the problem described and fixed in http://forums.libsdl.org/viewtopic.php?p=40503.
24d066
24d066
Now, I should start describing the problem.
24d066
24d066
A game uses Escape to open menu (the exact key might not be important). Upon opening, it calls SDL_EnableUNICODE(1). Upon closing it calls SDL_EnableUNICODE(0).
24d066
24d066
I have an IME running.
24d066
24d066
Game uses SDL_PollEvent to get the events.
24d066
24d066
If Escape is pressed repeatedly, menu is opened and closed, till it eventually freezes in open state.
24d066
"freezes" in this context means "app itself still runs, but no keyboard events are getting delivered (though - for example - mouse events still are)". "getting delivered" should mean "SDL_PollEvent is not receiving any".
24d066
If it matters, the last delivered keyboard event is a keypress, the release never arrives.
24d066
24d066
It seems (no guarantees, due to random nature of the freeze) that unsetting XMODIFIERS (which - AFAIU - will disable IME as far as SDL is concerned) prevents the freeze, therefore the reference to that SDL2 thread.
24d066
24d066
diff -r 22a7f096bb9d -r 0aade9c0203f src/video/x11/SDL_x11events.c
24d066
--- a/src/video/x11/SDL_x11events.c	Sun Dec 01 00:00:17 2013 -0500
24d066
+++ b/src/video/x11/SDL_x11events.c	Thu Apr 17 22:36:14 2014 -0700
24d066
@@ -395,6 +395,8 @@
24d066
 {
24d066
 	int posted;
24d066
 	XEvent xevent;
24d066
+	int orig_event_type;
24d066
+	KeyCode orig_keycode;
24d066
 
24d066
 	SDL_memset(&xevent, '\0', sizeof (XEvent));  /* valgrind fix. --ryan. */
24d066
 	XNextEvent(SDL_Display, &xevent);
24d066
@@ -410,9 +412,29 @@
24d066
 #ifdef X_HAVE_UTF8_STRING
24d066
 	/* If we are translating with IM, we need to pass all events
24d066
 	   to XFilterEvent, and discard those filtered events immediately.  */
24d066
+	orig_event_type = xevent.type;
24d066
+	if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
24d066
+	     orig_keycode = xevent.xkey.keycode;
24d066
+	} else {
24d066
+	     orig_keycode = 0;
24d066
+	}
24d066
 	if ( SDL_TranslateUNICODE
24d066
 	     && SDL_IM != NULL
24d066
 	     && XFilterEvent(&xevent, None) ) {
24d066
+	        if (orig_keycode) {
24d066
+	            SDL_keysym keysym;
24d066
+	            static XComposeStatus state;
24d066
+	            char keybuf[32];
24d066
+
24d066
+	            keysym.scancode = xevent.xkey.keycode;
24d066
+	            keysym.sym = X11_TranslateKeycode(SDL_Display, xevent.xkey.keycode);
24d066
+	            keysym.mod = KMOD_NONE;
24d066
+	            keysym.unicode = 0;
24d066
+	            if (orig_event_type == KeyPress && XLookupString(&xevent.xkey, keybuf, sizeof(keybuf), NULL, &state))
24d066
+	                keysym.unicode = (Uint8)keybuf[0];
24d066
+
24d066
+	            SDL_PrivateKeyboard(orig_event_type == KeyPress ? SDL_PRESSED : SDL_RELEASED, &keysym);
24d066
+	        }
24d066
 		return 0;
24d066
 	}
24d066
 #endif