otaylor / rpms / SDL

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