e2eb78
# HG changeset patch
e2eb78
# User Sam Lantinga <slouken@libsdl.org>
e2eb78
# Date 1397799374 25200
e2eb78
#      Thu Apr 17 22:36:14 2014 -0700
e2eb78
# Branch SDL-1.2
e2eb78
# Node ID 0aade9c0203f717fe4b823a176c3c040f1a709f8
e2eb78
# Parent  22a7f096bb9d4d596f35a93e33608825693462b0
e2eb78
Fixed bug 2325 - SDL_EnableUNICODE sometimes drops keyboard events completely
e2eb78
e2eb78
Rafał Mużyło
e2eb78
e2eb78
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.
e2eb78
e2eb78
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).
e2eb78
e2eb78
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.
e2eb78
e2eb78
Now, I should start describing the problem.
e2eb78
e2eb78
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).
e2eb78
e2eb78
I have an IME running.
e2eb78
e2eb78
Game uses SDL_PollEvent to get the events.
e2eb78
e2eb78
If Escape is pressed repeatedly, menu is opened and closed, till it eventually freezes in open state.
e2eb78
"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".
e2eb78
If it matters, the last delivered keyboard event is a keypress, the release never arrives.
e2eb78
e2eb78
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.
e2eb78
e2eb78
diff -r 22a7f096bb9d -r 0aade9c0203f src/video/x11/SDL_x11events.c
e2eb78
--- a/src/video/x11/SDL_x11events.c	Sun Dec 01 00:00:17 2013 -0500
e2eb78
+++ b/src/video/x11/SDL_x11events.c	Thu Apr 17 22:36:14 2014 -0700
e2eb78
@@ -395,6 +395,8 @@
e2eb78
 {
e2eb78
 	int posted;
e2eb78
 	XEvent xevent;
e2eb78
+	int orig_event_type;
e2eb78
+	KeyCode orig_keycode;
e2eb78
 
e2eb78
 	SDL_memset(&xevent, '\0', sizeof (XEvent));  /* valgrind fix. --ryan. */
e2eb78
 	XNextEvent(SDL_Display, &xevent);
e2eb78
@@ -410,9 +412,29 @@
e2eb78
 #ifdef X_HAVE_UTF8_STRING
e2eb78
 	/* If we are translating with IM, we need to pass all events
e2eb78
 	   to XFilterEvent, and discard those filtered events immediately.  */
e2eb78
+	orig_event_type = xevent.type;
e2eb78
+	if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
e2eb78
+	     orig_keycode = xevent.xkey.keycode;
e2eb78
+	} else {
e2eb78
+	     orig_keycode = 0;
e2eb78
+	}
e2eb78
 	if ( SDL_TranslateUNICODE
e2eb78
 	     && SDL_IM != NULL
e2eb78
 	     && XFilterEvent(&xevent, None) ) {
e2eb78
+	        if (orig_keycode) {
e2eb78
+	            SDL_keysym keysym;
e2eb78
+	            static XComposeStatus state;
e2eb78
+	            char keybuf[32];
e2eb78
+
e2eb78
+	            keysym.scancode = xevent.xkey.keycode;
e2eb78
+	            keysym.sym = X11_TranslateKeycode(SDL_Display, xevent.xkey.keycode);
e2eb78
+	            keysym.mod = KMOD_NONE;
e2eb78
+	            keysym.unicode = 0;
e2eb78
+	            if (orig_event_type == KeyPress && XLookupString(&xevent.xkey, keybuf, sizeof(keybuf), NULL, &state))
e2eb78
+	                keysym.unicode = (Uint8)keybuf[0];
e2eb78
+
e2eb78
+	            SDL_PrivateKeyboard(orig_event_type == KeyPress ? SDL_PRESSED : SDL_RELEASED, &keysym);
e2eb78
+	        }
e2eb78
 		return 0;
e2eb78
 	}
e2eb78
 #endif