[Xlib and Xt] Handle SubstructureNotify events on the root window

Andrew Troschinetz ast at arlut.utexas.edu
Wed Feb 25 09:32:40 PST 2009

I'd like to setup an Xt event callback on the root window for  
SubstructureNotify events, but I can't figure out how to do it without  
causing BadWindow Xlib errors and "Error: Event with wrong window" Xt  

Here's what I have:

	// ... XtToolkitInitialize(), XtCreateApplicationContext(), etc ...

	Display* display = XtOpenDisplay (...);
	Widget toplevel = XtAppCreateShell (...);
	Window root_window = XDefaultRootWindow (display);
	// And this is where I get lost, how do I get a Widget for the root  

I've tried this:

	// Returns NULL, so this is a no-go
	Widget root_widget = XtWindowToWidget (display, root_window);

Also this:

	// Works but I get errors
	XtRegisterDrawable (display, root_window, toplevel);
	XSelectInput (display, root_window, SubstructureNotify Mask);
	XtAddRawEventHandler (toplebel, SubstructureNotify Mask, False,  
my_callback, NULL);

If I use XSetErrorHandler() and XtSetErrorHandler() to ignore the  
errors, then I get event callbacks when I want them. Somehow this  
doesn't seem like a good solution though.

Section 7.3.1 of the X Toolkit Programming Manual seems to suggest  
that this should be possible (**emphasis mine**):

"Sometimes an application must handle events for drawables that are  
not associated with widgets in its widget tree. Examples include  
handling GraphicsExpose and NoExposeevents on Pixmaps, and handling  
PropertyNotify events on the **root window**."

So is it possible to handle SubstructureNotify events on the root  

Andrew Troschinetz
Applied Research Laboratories

More information about the xorg mailing list