Friday, November 14, 2008

Chasing a bug

This afternoon I was a bit tired with my 5-pt computation at 1-loop, because I have to check if something very boring is true and I know from consistency that it has to be true.

So I tried to occupy my mind with something interesting instead, and get back to my long problem later on.

A post [1] to the Window Maker list was enough to make me curious and I decided to think about it. The problem was an apparent bug with Window Maker, which was not recognizing the release of a shortcut key called Mod4, with keycode == 115.

My first reaction was to ask for more information, because it looked like a X bug to me due to the fact that the reporter had a non-standard Sun keyboard, and it had already uncovered a X bug last month, IIRC.

After a few private emails I learned how to reproduce the issue with my keyboard too, using the "Windows" key (maybe this name or even the logo has some special ability to make bugs happen). Having something which I could test myself I went to the wmaker source code to learn where the problem was happening.

A few 'git grep'(s) later I found the source responsable for this anomaly in src/cycling.c
            case KeyRelease:
#ifdef DEBUG
printf("Got key release\n");
#endif
for (i = 0; i <>max_keypermod; i++) {
if (keymap->modifiermap[i] == ev.xkey.keycode &&
wKeyBindings[WKBD_FOCUSNEXT].modifier
& 1<<(i/keymap->max_keypermod)) {
done = True;
break;
}
}
break;
and at first I thought that the problem was about keymap->max_keypermod being 3 instead of 4. But that hypothesis did not withstand my tests, and the scenario which emerged after some debug printf's was

  1. The modifier (Mod4) keycode was equal to 115 (I looked at 'xev' output)
  2. There was no keymap->modifiermap[i] for any 1<= i <= 24 which was equal to 115, so the condition inside the if () was not being satisfied and the key would never be recognized as being released.
  3. For this particular Mod4 key, whe have wKeyBindings[WKBD_FOCUSNEXT].modifier== 64
Therefore, as 1<<( i / keymap->max_keypermod) == 64 for i == 18 (remember that keymap->max_keypermod == 3) I simply set keymap->modifiermap[18] = 115 before the 'for' loop, and it fixed the issue for me!

I wrote the workaround patch and asked it to be tested, and it worked for the reporter too. The origin of the problem was found, but the proper fix was yet to be written.

I went out for a cappuccino I had the idea of searching about it with google. After some time I found out [2] that this is really a X bug, which can be "fixed" by adding to the .Xmodmap file the following
    remove mod4 = Super_L
keycode 127 = NoSymbol
add mod4 = Super_L
I wrote about it back to the list and the original reporter confirmed that doing this modification fixed the problem for him too.

And that is how my quest for some fun ended, and Window Maker remains bug-free as far as this issue is concerned (I don't know of any other wmaker bug, though).

The lesson I guess is, ask the almighty google first and hack the code later...

...but if I had done that I wouldn't have some fun learning new things about the inner workings of Window Maker.


[1] http://lists.windowmaker.info/dev/msg00123.html
[2] http://modeemi.fi/~tuomov/ion/faq/entries/Modifier_releases.html

No comments: