Fixed bug 2325 - SDL_EnableUNICODE sometimes drops keyboard events completely SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Thu, 17 Apr 2014 22:36:14 -0700
branchSDL-1.2
changeset 87160aade9c0203f
parent 8040 22a7f096bb9d
child 8911 606600f0f6d7
Fixed bug 2325 - SDL_EnableUNICODE sometimes drops keyboard events completely

Rafał Mużyło

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.

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).

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.

Now, I should start describing the problem.

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).

I have an IME running.

Game uses SDL_PollEvent to get the events.

If Escape is pressed repeatedly, menu is opened and closed, till it eventually freezes in open state.
"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".
If it matters, the last delivered keyboard event is a keypress, the release never arrives.

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.
src/video/x11/SDL_x11events.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Sun Dec 01 00:00:17 2013 -0500
     1.2 +++ b/src/video/x11/SDL_x11events.c	Thu Apr 17 22:36:14 2014 -0700
     1.3 @@ -395,6 +395,8 @@
     1.4  {
     1.5  	int posted;
     1.6  	XEvent xevent;
     1.7 +	int orig_event_type;
     1.8 +	KeyCode orig_keycode;
     1.9  
    1.10  	SDL_memset(&xevent, '\0', sizeof (XEvent));  /* valgrind fix. --ryan. */
    1.11  	XNextEvent(SDL_Display, &xevent);
    1.12 @@ -410,9 +412,29 @@
    1.13  #ifdef X_HAVE_UTF8_STRING
    1.14  	/* If we are translating with IM, we need to pass all events
    1.15  	   to XFilterEvent, and discard those filtered events immediately.  */
    1.16 +	orig_event_type = xevent.type;
    1.17 +	if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
    1.18 +	     orig_keycode = xevent.xkey.keycode;
    1.19 +	} else {
    1.20 +	     orig_keycode = 0;
    1.21 +	}
    1.22  	if ( SDL_TranslateUNICODE
    1.23  	     && SDL_IM != NULL
    1.24  	     && XFilterEvent(&xevent, None) ) {
    1.25 +	        if (orig_keycode) {
    1.26 +	            SDL_keysym keysym;
    1.27 +	            static XComposeStatus state;
    1.28 +	            char keybuf[32];
    1.29 +
    1.30 +	            keysym.scancode = xevent.xkey.keycode;
    1.31 +	            keysym.sym = X11_TranslateKeycode(SDL_Display, xevent.xkey.keycode);
    1.32 +	            keysym.mod = KMOD_NONE;
    1.33 +	            keysym.unicode = 0;
    1.34 +	            if (orig_event_type == KeyPress && XLookupString(&xevent.xkey, keybuf, sizeof(keybuf), NULL, &state))
    1.35 +	                keysym.unicode = (Uint8)keybuf[0];
    1.36 +
    1.37 +	            SDL_PrivateKeyboard(orig_event_type == KeyPress ? SDL_PRESSED : SDL_RELEASED, &keysym);
    1.38 +	        }
    1.39  		return 0;
    1.40  	}
    1.41  #endif