Race condition in keyboard code?

Peter Åstrand astrand at cendio.se
Thu Mar 23 12:08:21 CST 2006

Hi, I've been debugging a very strange keyboard problem this afternoon: In 
some cases the key combination AltGr+2 produces "2" (wrong) rather than 
"@" (correct), when using a Swedish layout. The problem seems to be a race 
condition: If a KeyEvent occurs almost directly after the "2" key is 
pressed, the AltGr state is incorrect. The problem has been verified with 
this simple test program:

/* gcc -Wall xfake.c -o xfake -L/usr/X11R6/lib -lX11 -lXtst */

#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

int main( int argc, char* argv[] )
     Display* dpy;
     dpy = XOpenDisplay( NULL );
     XTestFakeKeyEvent(dpy, 0x71, True, CurrentTime);
     XTestFakeKeyEvent(dpy, 0xb, True, CurrentTime);
     XSync(dpy, False);
     XTestFakeKeyEvent(dpy, 0x71, False, CurrentTime);
     XTestFakeKeyEvent(dpy, 0xb, False, CurrentTime);
     XCloseDisplay( dpy );
     return 0;

(0x71 is Mode_Switch, 0xb is the "2" key). I'm executing the program while 
having Wines Notepad active. If the sleep() is commented out, a "2" is 
inserted into Notepad. If the sleep() is active, "@" is inserted.

More evidence: When debugging with WINEDEBUG=keyboard,key, the traces for 
X11DRV_ToUnicodeEx is printed after the Mode_Switch KeyRelease trace.

Now I wonder, is this a problem with:

1) Wine
2) The Windows API
3) The way I'm using X11/the keyboard (generating events that quickly).

? I do understand why this happens: X11DRV_ToUnicodeEx uses the global, 
static variable AltGrMask. Is this a bug?

Peter Åstrand		ThinLinc Chief Developer
Cendio			http://www.cendio.se
Teknikringen 3
583 30 Linköping        Phone: +46-13-21 46 00

More information about the wine-devel mailing list