Skip to content
Permalink
Browse files

Fixed bug 2325 - SDL_EnableUNICODE sometimes drops keyboard events co…

…mpletely

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.
  • Loading branch information
slouken committed Apr 18, 2014
1 parent e60e9cb commit 0332e2bb18dc68d6892c3b653b2547afe323854b
Showing with 22 additions and 0 deletions.
  1. +22 −0 src/video/x11/SDL_x11events.c
@@ -395,6 +395,8 @@ static int X11_DispatchEvent(_THIS)
{
int posted;
XEvent xevent;
int orig_event_type;
KeyCode orig_keycode;

SDL_memset(&xevent, '\0', sizeof (XEvent)); /* valgrind fix. --ryan. */
XNextEvent(SDL_Display, &xevent);
@@ -410,9 +412,29 @@ static int X11_DispatchEvent(_THIS)
#ifdef X_HAVE_UTF8_STRING
/* If we are translating with IM, we need to pass all events
to XFilterEvent, and discard those filtered events immediately. */
orig_event_type = xevent.type;
if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
orig_keycode = xevent.xkey.keycode;
} else {
orig_keycode = 0;
}
if ( SDL_TranslateUNICODE
&& SDL_IM != NULL
&& XFilterEvent(&xevent, None) ) {
if (orig_keycode) {
SDL_keysym keysym;
static XComposeStatus state;
char keybuf[32];

keysym.scancode = xevent.xkey.keycode;
keysym.sym = X11_TranslateKeycode(SDL_Display, xevent.xkey.keycode);
keysym.mod = KMOD_NONE;
keysym.unicode = 0;
if (orig_event_type == KeyPress && XLookupString(&xevent.xkey, keybuf, sizeof(keybuf), NULL, &state))
keysym.unicode = (Uint8)keybuf[0];

SDL_PrivateKeyboard(orig_event_type == KeyPress ? SDL_PRESSED : SDL_RELEASED, &keysym);
}
return 0;
}
#endif

0 comments on commit 0332e2b

Please sign in to comment.