1 /* $XConsortium: ClientWin.c,v 1.3 89/12/10 10:26:35 rws Exp $ */
2 
3 /*
4  * Copyright 1989 by the Massachusetts Institute of Technology
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose and without fee is hereby granted, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of M.I.T. not be used in advertising
11  * or publicity pertaining to distribution of the software without specific,
12  * written prior permission. M.I.T. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  */
17 
18 #include <X11/Xlib.h>
19 #include <X11/Xatom.h>
20 #include "ClientWin.h"
21 
22 static Window TryChildren(Display *,Window,Atom);
23 
24 /* Find a window with WM_STATE, else return win itself, as per ICCCM */
25 
XmuClientWindow(Display * dpy,Window win)26 Window XmuClientWindow (Display *dpy,Window win)
27 {
28     Atom WM_STATE;
29     Atom type = None;
30     int format;
31     unsigned long nitems, after;
32     unsigned char *data;
33     Window inf;
34 
35     WM_STATE = XInternAtom(dpy, "WM_STATE", True);
36     if (!WM_STATE)
37 	return win;
38     XGetWindowProperty(dpy, win, WM_STATE, 0, 0, False, AnyPropertyType,
39 		       &type, &format, &nitems, &after, &data);
40     if (type)
41 	return win;
42     inf = TryChildren(dpy, win, WM_STATE);
43     if (!inf)
44 	inf = win;
45     return inf;
46 }
47 
48 static
TryChildren(Display * dpy,Window win,Atom WM_STATE)49 Window TryChildren (Display *dpy, Window win, Atom WM_STATE)
50 {
51     Window root, parent;
52     Window *children;
53     unsigned int nchildren;
54     unsigned int i;
55     Atom type = None;
56     int format;
57     unsigned long nitems, after;
58     unsigned char *data;
59     Window inf = 0;
60 
61     if (!XQueryTree(dpy, win, &root, &parent, &children, &nchildren))
62 	return 0;
63     for (i = 0; !inf && (i < nchildren); i++) {
64 	XGetWindowProperty(dpy, children[i], WM_STATE, 0, 0, False,
65 			   AnyPropertyType, &type, &format, &nitems,
66 			   &after, &data);
67 	if (type)
68 	    inf = children[i];
69     }
70     for (i = 0; !inf && (i < nchildren); i++)
71 	inf = TryChildren(dpy, children[i], WM_STATE);
72     if (children) XFree((char *)children);
73     return inf;
74 }
75