| 1 |
/* |
| 2 |
* Copyright 2007 Kim woelders |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 5 |
* documentation for any purpose is hereby granted without fee, provided that |
| 6 |
* the above copyright notice appear in all copies and that both that copyright |
| 7 |
* notice and this permission notice appear in supporting documentation, and |
| 8 |
* that the name of the copyright holders not be used in advertising or |
| 9 |
* publicity pertaining to distribution of the software without specific, |
| 10 |
* written prior permission. The copyright holders make no representations |
| 11 |
* about the suitability of this software for any purpose. It is provided "as |
| 12 |
* is" without express or implied warranty. |
| 13 |
* |
| 14 |
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 |
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 20 |
* OF THIS SOFTWARE. |
| 21 |
*/ |
| 22 |
#include <xcb/xcb.h> |
| 23 |
#include <xcb/xproto.h> |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include "clientwin.h" |
| 29 |
#include "dsimple.h" |
| 30 |
#define Bool int |
| 31 |
#define False 0 |
| 32 |
#define True 1 |
| 33 |
static xcb_atom_t atom_wm_state = XCB_ATOM_NONE; |
| 34 |
|
| 35 |
#ifdef __cplusplus |
| 36 |
extern "C" { |
| 37 |
#endif |
| 38 |
|
| 39 |
/* |
| 40 |
* Check if window has given property |
| 41 |
*/ |
| 42 |
static Bool |
| 43 |
Window_Has_Property(xcb_connection_t *dpy, xcb_window_t win, xcb_atom_t atom) |
| 44 |
{ |
| 45 |
xcb_get_property_cookie_t prop_cookie; |
| 46 |
xcb_get_property_reply_t *prop_reply; |
| 47 |
|
| 48 |
prop_cookie = xcb_get_property(dpy, False, win, atom, |
| 49 |
XCB_GET_PROPERTY_TYPE_ANY, 0, 0); |
| 50 |
|
| 51 |
prop_reply = xcb_get_property_reply(dpy, prop_cookie, NULL); |
| 52 |
|
| 53 |
if (prop_reply) { |
| 54 |
xcb_atom_t reply_type = prop_reply->type; |
| 55 |
free(prop_reply); |
| 56 |
if (reply_type != XCB_NONE) |
| 57 |
return True; |
| 58 |
} |
| 59 |
|
| 60 |
return False; |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* Check if window is viewable |
| 65 |
*/ |
| 66 |
static Bool |
| 67 |
Window_Is_Viewable(xcb_connection_t *dpy, xcb_window_t win) |
| 68 |
{ |
| 69 |
Bool ok = False; |
| 70 |
xcb_get_window_attributes_cookie_t attr_cookie; |
| 71 |
xcb_get_window_attributes_reply_t *xwa; |
| 72 |
|
| 73 |
attr_cookie = xcb_get_window_attributes(dpy, win); |
| 74 |
xwa = xcb_get_window_attributes_reply(dpy, attr_cookie, NULL); |
| 75 |
|
| 76 |
if (xwa) { |
| 77 |
ok = (xwa->_class == XCB_WINDOW_CLASS_INPUT_OUTPUT) && (xwa->map_state == XCB_MAP_STATE_VIEWABLE); |
| 78 |
free(xwa); |
| 79 |
} |
| 80 |
|
| 81 |
return ok; |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* Find a window that has WM_STATE set in the window tree below win. |
| 86 |
* Unmapped/unviewable windows are not considered valid matches. |
| 87 |
* Children are searched in top-down stacking order. |
| 88 |
* The first matching window is returned, None if no match is found. |
| 89 |
*/ |
| 90 |
static xcb_window_t |
| 91 |
Find_Client_In_Children(xcb_connection_t *dpy, xcb_window_t win) |
| 92 |
{ |
| 93 |
xcb_query_tree_cookie_t qt_cookie; |
| 94 |
xcb_query_tree_reply_t *tree; |
| 95 |
xcb_window_t *children; |
| 96 |
int n_children; |
| 97 |
int i; |
| 98 |
|
| 99 |
qt_cookie = xcb_query_tree(dpy, win); |
| 100 |
tree = xcb_query_tree_reply(dpy, qt_cookie, NULL); |
| 101 |
if (!tree) |
| 102 |
return XCB_WINDOW_NONE; |
| 103 |
n_children = xcb_query_tree_children_length(tree); |
| 104 |
if (!n_children) { |
| 105 |
free(tree); |
| 106 |
return XCB_WINDOW_NONE; |
| 107 |
} |
| 108 |
children = xcb_query_tree_children(tree); |
| 109 |
|
| 110 |
/* Check each child for WM_STATE and other validity */ |
| 111 |
win = XCB_WINDOW_NONE; |
| 112 |
for (i = n_children - 1; i >= 0; i--) { |
| 113 |
if (!Window_Is_Viewable(dpy, children[i])) { |
| 114 |
/* Don't bother descending into this one */ |
| 115 |
children[i] = XCB_WINDOW_NONE; |
| 116 |
continue; |
| 117 |
} |
| 118 |
if (!Window_Has_Property(dpy, children[i], atom_wm_state)) |
| 119 |
continue; |
| 120 |
|
| 121 |
/* Got one */ |
| 122 |
win = children[i]; |
| 123 |
goto done; |
| 124 |
} |
| 125 |
|
| 126 |
/* No children matched, now descend into each child */ |
| 127 |
for (i = (int)n_children - 1; i >= 0; i--) { |
| 128 |
if (children[i] == XCB_WINDOW_NONE) |
| 129 |
continue; |
| 130 |
win = Find_Client_In_Children(dpy, children[i]); |
| 131 |
if (win != XCB_WINDOW_NONE) |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
done: |
| 136 |
free(tree); /* includes children */ |
| 137 |
|
| 138 |
return win; |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* Find virtual roots (_NET_VIRTUAL_ROOTS) |
| 143 |
*/ |
| 144 |
static xcb_window_t * |
| 145 |
Find_Roots(xcb_connection_t *dpy, xcb_window_t root, unsigned int *num) |
| 146 |
{ |
| 147 |
xcb_atom_t atom_virtual_root; |
| 148 |
|
| 149 |
xcb_get_property_cookie_t prop_cookie; |
| 150 |
xcb_get_property_reply_t *prop_reply; |
| 151 |
|
| 152 |
xcb_window_t *prop_ret = NULL; |
| 153 |
|
| 154 |
*num = 0; |
| 155 |
|
| 156 |
atom_virtual_root = Get_Atom(dpy, "_NET_VIRTUAL_ROOTS"); |
| 157 |
if (atom_virtual_root == XCB_ATOM_NONE) |
| 158 |
return NULL; |
| 159 |
|
| 160 |
prop_cookie = xcb_get_property(dpy, False, root, atom_virtual_root, |
| 161 |
XCB_ATOM_WINDOW, 0, 0x7fffffff); |
| 162 |
prop_reply = xcb_get_property_reply(dpy, prop_cookie, NULL); |
| 163 |
if (!prop_reply) |
| 164 |
return NULL; |
| 165 |
|
| 166 |
if ((prop_reply->value_len > 0) && (prop_reply->type == XCB_ATOM_WINDOW) |
| 167 |
&& (prop_reply->format == 32)) { |
| 168 |
int length = xcb_get_property_value_length(prop_reply); |
| 169 |
prop_ret = malloc((size_t)length); |
| 170 |
if (prop_ret) { |
| 171 |
memcpy(prop_ret, xcb_get_property_value(prop_reply), (size_t)length); |
| 172 |
*num = prop_reply->value_len; |
| 173 |
} |
| 174 |
} |
| 175 |
free(prop_reply); |
| 176 |
|
| 177 |
return prop_ret; |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* Find child window at pointer location |
| 182 |
*/ |
| 183 |
static xcb_window_t |
| 184 |
Find_Child_At_Pointer(xcb_connection_t *dpy, xcb_window_t win) |
| 185 |
{ |
| 186 |
xcb_window_t child_return = XCB_WINDOW_NONE; |
| 187 |
|
| 188 |
xcb_query_pointer_cookie_t qp_cookie; |
| 189 |
xcb_query_pointer_reply_t *qp_reply; |
| 190 |
|
| 191 |
qp_cookie = xcb_query_pointer(dpy, win); |
| 192 |
qp_reply = xcb_query_pointer_reply(dpy, qp_cookie, NULL); |
| 193 |
|
| 194 |
if (qp_reply) { |
| 195 |
child_return = qp_reply->child; |
| 196 |
free(qp_reply); |
| 197 |
} |
| 198 |
|
| 199 |
return child_return; |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
* Find client window at pointer location |
| 204 |
* |
| 205 |
* root is the root window. |
| 206 |
* subwin is the subwindow reported by a ButtonPress event on root. |
| 207 |
* |
| 208 |
* If the WM uses virtual roots subwin may be a virtual root. |
| 209 |
* If so, we descend the window stack at the pointer location and assume the |
| 210 |
* child is the client or one of its WM frame windows. |
| 211 |
* This will of course work only if the virtual roots are children of the real |
| 212 |
* root. |
| 213 |
*/ |
| 214 |
xcb_window_t |
| 215 |
Find_Client(xcb_connection_t *dpy, xcb_window_t root, xcb_window_t subwin) |
| 216 |
{ |
| 217 |
xcb_window_t *roots; |
| 218 |
unsigned int i, n_roots; |
| 219 |
xcb_window_t win; |
| 220 |
|
| 221 |
/* Check if subwin is a virtual root */ |
| 222 |
roots = Find_Roots(dpy, root, &n_roots); |
| 223 |
for (i = 0; i < n_roots; i++) { |
| 224 |
if (subwin != roots[i]) |
| 225 |
continue; |
| 226 |
win = Find_Child_At_Pointer(dpy, subwin); |
| 227 |
if (win == XCB_WINDOW_NONE) { |
| 228 |
free(roots); |
| 229 |
return subwin; /* No child - Return virtual root. */ |
| 230 |
} |
| 231 |
subwin = win; |
| 232 |
break; |
| 233 |
} |
| 234 |
free(roots); |
| 235 |
|
| 236 |
if (atom_wm_state == XCB_ATOM_NONE) { |
| 237 |
atom_wm_state = Get_Atom(dpy, "WM_STATE"); |
| 238 |
if (atom_wm_state == XCB_ATOM_NONE) |
| 239 |
return subwin; |
| 240 |
} |
| 241 |
|
| 242 |
/* Check if subwin has WM_STATE */ |
| 243 |
if (Window_Has_Property(dpy, subwin, atom_wm_state)) |
| 244 |
return subwin; |
| 245 |
|
| 246 |
/* Attempt to find a client window in subwin's children */ |
| 247 |
win = Find_Client_In_Children(dpy, subwin); |
| 248 |
if (win != XCB_WINDOW_NONE) |
| 249 |
return win; /* Found a client */ |
| 250 |
|
| 251 |
/* Did not find a client */ |
| 252 |
return subwin; |
| 253 |
} |
| 254 |
|
| 255 |
#ifdef __cplusplus |
| 256 |
} |
| 257 |
#endif |