[comp.windows.x] XGetWindowAttributes: documentation/reality clash

mouse@LARRY.MCRCIM.MCGILL.EDU (der Mouse) (04/24/89)

I think XGetWindowAttributes is not behaving according to the
documentation.  (System: Sun-3s running release 3.5, X11R3 with Purdue
patches in place, and it's not any specific machine that it's failing
on.)

I'm writing my own window manager and I have occasion to want to
inquire as to whether a given window ID still refers to a window.

In the Xlib document, I find the following promising lines:

	4.1.  Obtaining Window Information

	Xlib provides functions that you can use to obtain informa-
	tion about the window tree, the window's current attributes,
	the window's current geometry, or the current pointer coor-
NOTE>	dinates.  Because they are most frequently used by window
NOTE>	managers, these functions all return a status to indicate
NOTE>	whether the window still exists.
[documentation for XQueryTree omitted]
	To obtain the current attributes of a given window, use
	XGetWindowAttributes.
[documentation for XGetWindowAttributes omitted]

Fine, think I, and write

	int window_exists(w) Window w;
	{
	 XWindowAttributes attr;
	 return(XGetWindowAttributes(disp,w,&attr)?1:0);
	}

but then I get a rude shock when my wm crashes the first time this
function is called under circumstances when I would expect it to return
false (ie, the window doesn't exist).  I see the following printed by
the wm when it dies:

	X Protocol error:  not a valid window ID
	  Major opcode of failed request:  3 (X_GetWindowAttributes)
	  Minor opcode of failed request:  0
	  Resource id in failed request:  0x1100002
	  Serial number of failed request:  1657
	  Current serial number in output stream:  1657

What am I misreading?  What *is* the proper way to tell whether a
window still exists?  (I want to avoid XSync()ing and looking for
window destruction events - which certainly ought to work!  This
function is called only with the server grabbed.)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

jim@EXPO.LCS.MIT.EDU (Jim Fulton) (04/25/89)

The Xlib document is a little misleading.  The routines in section 4.1 (titled
"Obtaining Window Information", pages 62-67 in Scheifler, Gettys, and Newman)
still require you to have an error handler (which you always need in critical
applications, especially window managers).  Since these routines normally only
return an error when there is an invalid window or drawable identifier (well,
not quite: BadAlloc could crop up but your client would probably be hosed
shortly), your handler simply needs to not crap out in order for your calling
program to get a Status of 0 or a Bool of False. 


> What *is* the proper way to tell whether a window still exists?

Select for SubstructureNotify on the root and keep track of DestroyNotify
events.  Or else, just use one of the routines mentioned above once you've
written your error handler!  :-)