Window Managers and XGrabKey*()

Bedő Sándor Bedo.Sandor at
Fri May 29 01:49:28 PDT 2009


I have an academic question about X Window Managers.
I've written my own - don't ask why, I know there are lots
of them -, and I realized that I don't really understand
something basic about the concepts of X, Xlib and Window
Managers, or the relations between them, or so.

I would like to do a simple thing:  switching between the
windows with special keys.  Yes, I know, it's not a nice
thing to do it with a Window Manager, but now I would
like to know the best way, the nicest solution it can be solved,
even if it's a wrong desire.  I figured out two different ways
to do this (but I think a third one will be the right):

1)  It seemed to be the easiest to grab the whole keyboard
with XGrabKeyboard() via the root window.  In this way I
can catch all the keyboard events, and if it describes an action
in the Window Manager, then I can do that action easily.
OK, but how can I send all the other events (except my
own action-triggerer ones) to the window having the keyboard
focus?  No way, because it's the root window that has the
focus at the moment.  Am I right?  Or is there a way to throw a
keyboard event to a specified window in case the root
window does have the whole keyboard grabbed?  The other
problem is that if I throw a keyboard event to the window
in front of the user (I could do it once whit the XTest extension,
and maybe it's possible with the XSendEvent function too),
then I ( == the Window Manager) may get it back again if it falls
through that window, because there is the root window behind
that one, and I can't decide if the event is a new one from the user,
or an old (falling back) one I've sent to one of the windows I manage.
OK, suppose that I can solve this!  Then what about the other clients,
that want to grab the keyboard?  It will result errors in those
clients, that check the return value of the XGrabKeyboard()
function to know if the grab was succesful or it failed.  So I think
the idea of grabbing the keyboard is wrong...

2)  My next idea was to grab only the desired keys with the
Xlib function XGrabKey().  This is the way I implemented
my Window Manager, and it works fine with a couple of
other X clients, like xterm, or firefox.  But there are a
few other clients, that use XGrabKeyboard, and in case these
applications get the focus, and appropriate the keyboard,
my Window Manager does not get the desired keystrokes.
Hmmm, maybe grabbing the whole keyboard in the Window
Manager is not such a wrong idea?  Clients usually don't care
about the return value of the XGrabKeyboard() call (as I
can see in the source of a few apps), so they always would
think that they have the keyboard grabbed, even if they
did not.

How it is possible to implement an always working window
switching key in my Window Manager, even if a client grabs
the keyboard?  Maybe there is an Xorg extension, or a tricky
Xlib function, that makes it possible...  Or the problem is
that I've read an ancient documentation about the Xlib?


More information about the xorg mailing list