| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stddef.h> |
| 5 |
#include <uthash.h> |
| 6 |
#include <xcb/xproto.h> |
| 7 |
|
| 8 |
#include "common.h" |
| 9 |
#include "log.h" |
| 10 |
#include "utils/list.h" |
| 11 |
#include "x.h" |
| 12 |
|
| 13 |
#include "win.h" |
| 14 |
#include "wm.h" |
| 15 |
#include "wm_internal.h" |
| 16 |
|
| 17 |
struct wm_query_tree_request { |
| 18 |
struct x_async_request_base base; |
| 19 |
struct wm *wm; |
| 20 |
struct atom *atoms; |
| 21 |
xcb_window_t wid; |
| 22 |
}; |
| 23 |
|
| 24 |
struct wm_get_property_request { |
| 25 |
struct x_async_request_base base; |
| 26 |
struct wm *wm; |
| 27 |
xcb_window_t wid; |
| 28 |
}; |
| 29 |
|
| 30 |
struct wm { |
| 31 |
|
| 32 |
|
| 33 |
struct wm_tree_node *focused_win; |
| 34 |
struct wm_tree tree; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
struct wm_tree_node orphan_root; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
unsigned n_pending_imports; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
bool needs_leader_refresh; |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
struct wm_ref { |
| 70 |
struct list_node inner; |
| 71 |
}; |
| 72 |
static_assert(offsetof(struct wm_ref, inner) == 0, "wm_cursor should be usable as a " |
| 73 |
"wm_tree_node"); |
| 74 |
static_assert(alignof(struct wm_ref) == alignof(struct list_node), |
| 75 |
"wm_cursor should have the same alignment as wm_tree_node"); |
| 76 |
|
| 77 |
static inline const struct wm_tree_node *to_tree_node(const struct wm_ref *cursor) { |
| 78 |
return cursor != NULL ? list_entry(&cursor->inner, struct wm_tree_node, siblings) |
| 79 |
: NULL; |
| 80 |
} |
| 81 |
|
| 82 |
static inline struct wm_tree_node *to_tree_node_mut(struct wm_ref *cursor) { |
| 83 |
return cursor != NULL ? list_entry(&cursor->inner, struct wm_tree_node, siblings) |
| 84 |
: NULL; |
| 85 |
} |
| 86 |
|
| 87 |
xcb_window_t wm_ref_win_id(const struct wm_ref *cursor) { |
| 88 |
return to_tree_node(cursor)->id.x; |
| 89 |
} |
| 90 |
|
| 91 |
wm_treeid wm_ref_treeid(const struct wm_ref *cursor) { |
| 92 |
return to_tree_node(cursor)->id; |
| 93 |
} |
| 94 |
|
| 95 |
struct win *wm_ref_deref(const struct wm_ref *cursor) { |
| 96 |
auto node = to_tree_node(cursor); |
| 97 |
if (node->parent == NULL) { |
| 98 |
log_error("Trying to dereference a root node. Expect malfunction."); |
| 99 |
return NULL; |
| 100 |
} |
| 101 |
if (node->parent->parent != NULL) { |
| 102 |
|
| 103 |
|
| 104 |
return NULL; |
| 105 |
} |
| 106 |
return node->win; |
| 107 |
} |
| 108 |
|
| 109 |
void wm_ref_set(struct wm_ref *cursor, struct win *w) { |
| 110 |
to_tree_node_mut(cursor)->win = w; |
| 111 |
} |
| 112 |
|
| 113 |
void wm_ref_set_focused(struct wm *wm, struct wm_ref *cursor) { |
| 114 |
auto node = to_tree_node_mut(cursor); |
| 115 |
if (wm->focused_win == node) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
log_debug("Focused window changed from %#010x to %#010x", |
| 120 |
wm->focused_win ? wm->focused_win->id.x : 0, node ? node->id.x : 0); |
| 121 |
wm->focused_win = node; |
| 122 |
} |
| 123 |
|
| 124 |
void wm_ref_set_leader(struct wm *wm, struct wm_ref *cursor, xcb_window_t leader) { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
struct wm_tree_node *node = |
| 129 |
wm_tree_find_toplevel_for(&wm->tree, to_tree_node_mut(cursor)); |
| 130 |
if (node->leader == leader) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
wm->needs_leader_refresh = true; |
| 135 |
node->leader = leader; |
| 136 |
} |
| 137 |
|
| 138 |
struct wm_ref *wm_focused_win(struct wm *wm) { |
| 139 |
return wm->focused_win ? (struct wm_ref *)&wm->focused_win->siblings : NULL; |
| 140 |
} |
| 141 |
|
| 142 |
const struct wm_ref *wm_focused_leader(struct wm *wm) { |
| 143 |
return wm->focused_win != NULL |
| 144 |
? (struct wm_ref *)&wm->focused_win->leader_final->siblings |
| 145 |
: NULL; |
| 146 |
} |
| 147 |
|
| 148 |
const struct wm_ref *wm_ref_leader(const struct wm_ref *cursor) { |
| 149 |
auto node = to_tree_node(cursor); |
| 150 |
return (struct wm_ref *)&node->leader_final->siblings; |
| 151 |
} |
| 152 |
|
| 153 |
bool wm_ref_is_zombie(const struct wm_ref *cursor) { |
| 154 |
return to_tree_node(cursor)->is_zombie; |
| 155 |
} |
| 156 |
|
| 157 |
struct wm_ref *wm_ref_below(const struct wm_ref *cursor) { |
| 158 |
return &to_tree_node(cursor)->parent->children != cursor->inner.next |
| 159 |
? (struct wm_ref *)cursor->inner.next |
| 160 |
: NULL; |
| 161 |
} |
| 162 |
|
| 163 |
struct wm_ref *wm_ref_above(const struct wm_ref *cursor) { |
| 164 |
return &to_tree_node(cursor)->parent->children != cursor->inner.prev |
| 165 |
? (struct wm_ref *)cursor->inner.prev |
| 166 |
: NULL; |
| 167 |
} |
| 168 |
|
| 169 |
struct wm_ref *wm_root_ref(const struct wm *wm) { |
| 170 |
return (struct wm_ref *)&wm->tree.root->siblings; |
| 171 |
} |
| 172 |
|
| 173 |
struct wm_ref *wm_ref_topmost_child(const struct wm_ref *cursor) { |
| 174 |
auto node = to_tree_node(cursor); |
| 175 |
return !list_is_empty(&node->children) ? (struct wm_ref *)node->children.next : NULL; |
| 176 |
} |
| 177 |
|
| 178 |
struct wm_ref *wm_ref_bottommost_child(const struct wm_ref *cursor) { |
| 179 |
auto node = to_tree_node(cursor); |
| 180 |
return !list_is_empty(&node->children) ? (struct wm_ref *)node->children.prev : NULL; |
| 181 |
} |
| 182 |
|
| 183 |
struct wm_ref *wm_find(const struct wm *wm, xcb_window_t id) { |
| 184 |
auto node = wm_tree_find(&wm->tree, id); |
| 185 |
return node != NULL ? (struct wm_ref *)&node->siblings : NULL; |
| 186 |
} |
| 187 |
|
| 188 |
struct wm_ref *wm_find_by_client(const struct wm *wm, xcb_window_t client) { |
| 189 |
auto node = wm_tree_find(&wm->tree, client); |
| 190 |
if (node == NULL || node->parent == NULL) { |
| 191 |
|
| 192 |
|
| 193 |
return NULL; |
| 194 |
} |
| 195 |
auto toplevel = wm_tree_find_toplevel_for(&wm->tree, node); |
| 196 |
return toplevel != NULL ? (struct wm_ref *)&toplevel->siblings : NULL; |
| 197 |
} |
| 198 |
|
| 199 |
struct wm_ref *wm_ref_toplevel_of(const struct wm *wm, struct wm_ref *cursor) { |
| 200 |
auto toplevel = wm_tree_find_toplevel_for(&wm->tree, to_tree_node_mut(cursor)); |
| 201 |
return toplevel != NULL ? (struct wm_ref *)&toplevel->siblings : NULL; |
| 202 |
} |
| 203 |
|
| 204 |
struct wm_ref *wm_ref_client_of(struct wm_ref *cursor) { |
| 205 |
auto client = to_tree_node(cursor)->client_window; |
| 206 |
return client != NULL ? (struct wm_ref *)&client->siblings : NULL; |
| 207 |
} |
| 208 |
|
| 209 |
struct wm_ref *wm_stack_end(struct wm *wm) { |
| 210 |
return (struct wm_ref *)&wm->tree.root->children; |
| 211 |
} |
| 212 |
|
| 213 |
static struct wm_tree_node *wm_find_leader(struct wm *wm, struct wm_tree_node *node) { |
| 214 |
if (node->leader_final != NULL) { |
| 215 |
if (node->visited) { |
| 216 |
log_warn("Window %#010x is part of a cycle in the leadership " |
| 217 |
"tree", |
| 218 |
node->id.x); |
| 219 |
} |
| 220 |
return node->leader_final; |
| 221 |
} |
| 222 |
|
| 223 |
node->leader_final = node; |
| 224 |
if (node->leader != node->id.x) { |
| 225 |
auto leader_node = wm_tree_find(&wm->tree, node->leader); |
| 226 |
if (leader_node == NULL) { |
| 227 |
return node->leader_final; |
| 228 |
} |
| 229 |
leader_node = wm_tree_find_toplevel_for(&wm->tree, leader_node); |
| 230 |
if (leader_node == NULL) { |
| 231 |
log_debug("Cannot find toplevel for leader %#010x of window " |
| 232 |
"%#010x. tree consistency: %d", |
| 233 |
node->leader, node->id.x, wm_is_consistent(wm)); |
| 234 |
wm->needs_leader_refresh = true; |
| 235 |
return node->leader_final; |
| 236 |
} |
| 237 |
node->visited = true; |
| 238 |
node->leader_final = wm_find_leader(wm, leader_node); |
| 239 |
node->visited = false; |
| 240 |
} |
| 241 |
return node->leader_final; |
| 242 |
} |
| 243 |
|
| 244 |
void wm_refresh_leaders(struct wm *wm) { |
| 245 |
if (!wm->needs_leader_refresh) { |
| 246 |
return; |
| 247 |
} |
| 248 |
log_debug("Refreshing window leaders, tree consistency: %d", wm_is_consistent(wm)); |
| 249 |
wm->needs_leader_refresh = false; |
| 250 |
list_foreach(struct wm_tree_node, i, &wm->tree.root->children, siblings) { |
| 251 |
if (i->is_zombie) { |
| 252 |
|
| 253 |
continue; |
| 254 |
} |
| 255 |
i->leader_final = NULL; |
| 256 |
} |
| 257 |
list_foreach(struct wm_tree_node, i, &wm->tree.root->children, siblings) { |
| 258 |
if (i->is_zombie) { |
| 259 |
continue; |
| 260 |
} |
| 261 |
wm_find_leader(wm, i); |
| 262 |
BUG_ON_NULL(i->leader_final); |
| 263 |
log_verbose("Window %#010x has leader %#010x", i->id.x, |
| 264 |
i->leader_final->id.x); |
| 265 |
} |
| 266 |
if (wm->needs_leader_refresh) { |
| 267 |
log_debug("Leaders not fully resolved, will try again later."); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
void wm_stack_move_to_above(struct wm *wm, struct wm_ref *cursor, xcb_window_t below) { |
| 274 |
BUG_ON_NULL(cursor); |
| 275 |
auto node = to_tree_node_mut(cursor); |
| 276 |
if (node->parent == &wm->orphan_root) { |
| 277 |
|
| 278 |
|
| 279 |
log_debug("Ignoring restack request for orphaned window %#010x", node->id.x); |
| 280 |
return; |
| 281 |
} |
| 282 |
if (below == XCB_NONE) { |
| 283 |
|
| 284 |
wm_tree_move_to_end(&wm->tree, node, true); |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
auto below_node = wm_tree_find(&wm->tree, below); |
| 289 |
if (below_node == NULL) { |
| 290 |
log_error("Trying to restack window %#010x, but its sibling window " |
| 291 |
"%#010x is not in our tree. Expect malfunction.", |
| 292 |
node->id.x, below); |
| 293 |
assert(false); |
| 294 |
return; |
| 295 |
} |
| 296 |
wm_tree_move_to_above(&wm->tree, node, below_node); |
| 297 |
} |
| 298 |
|
| 299 |
void wm_stack_move_to_end(struct wm *wm, struct wm_ref *cursor, bool to_bottom) { |
| 300 |
auto node = to_tree_node_mut(cursor); |
| 301 |
if (node->parent == &wm->orphan_root) { |
| 302 |
return; |
| 303 |
} |
| 304 |
wm_tree_move_to_end(&wm->tree, node, to_bottom); |
| 305 |
} |
| 306 |
|
| 307 |
struct wm *wm_new(void) { |
| 308 |
auto wm = ccalloc(1, struct wm); |
| 309 |
wm_tree_init(&wm->tree); |
| 310 |
list_init_head(&wm->orphan_root.children); |
| 311 |
wm->n_pending_imports = 0; |
| 312 |
return wm; |
| 313 |
} |
| 314 |
|
| 315 |
void wm_free(struct wm *wm) { |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if (wm->tree.root != NULL) { |
| 320 |
wm_stack_foreach_safe(wm, i, next) { |
| 321 |
auto w = wm_ref_deref(i); |
| 322 |
auto tree_node = to_tree_node_mut(i); |
| 323 |
free(w); |
| 324 |
|
| 325 |
if (tree_node->is_zombie) { |
| 326 |
|
| 327 |
|
| 328 |
log_debug("Leftover zombie node for window %#010x", |
| 329 |
tree_node->id.x); |
| 330 |
wm_tree_reap_zombie(tree_node); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
wm_tree_clear(&wm->tree); |
| 335 |
assert(wm_is_consistent(wm)); |
| 336 |
assert(list_is_empty(&wm->orphan_root.children)); |
| 337 |
|
| 338 |
free(wm); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
static void wm_move_win(struct wm_tree_node *from, struct wm_tree_node *to) { |
| 343 |
if (from->win != NULL) { |
| 344 |
from->win->tree_ref = (struct wm_ref *)&to->siblings; |
| 345 |
} |
| 346 |
to->win = from->win; |
| 347 |
from->win = NULL; |
| 348 |
} |
| 349 |
|
| 350 |
static void wm_detach_inner(struct wm *wm, struct wm_tree_node *child) { |
| 351 |
auto zombie = wm_tree_detach(&wm->tree, child); |
| 352 |
assert(zombie != NULL || child->win == NULL); |
| 353 |
if (zombie != NULL) { |
| 354 |
wm_move_win(child, zombie); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
void wm_disconnect(struct wm *wm, xcb_window_t child, xcb_window_t parent, |
| 362 |
xcb_window_t new_parent) { |
| 363 |
auto parent_node = wm_tree_find(&wm->tree, parent); |
| 364 |
auto new_parent_node = wm_tree_find(&wm->tree, new_parent); |
| 365 |
BUG_ON_NULL(parent_node); |
| 366 |
|
| 367 |
auto child_node = wm_tree_find(&wm->tree, child); |
| 368 |
if (child_node == NULL) { |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
log_debug("Child window %#010x of %#010x is not in our tree, ignoring", |
| 378 |
child, parent); |
| 379 |
return; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
BUG_ON(child_node->parent != parent_node && |
| 385 |
(parent_node->tree_queried || child_node->parent != &wm->orphan_root)); |
| 386 |
|
| 387 |
wm_detach_inner(wm, child_node); |
| 388 |
if ((new_parent_node != NULL && new_parent_node->receiving_events) || |
| 389 |
child_node->receiving_events) { |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
log_debug("Disconnecting window %#010x from window %#010x", child, parent); |
| 397 |
wm_tree_attach(&wm->tree, child_node, &wm->orphan_root); |
| 398 |
} else { |
| 399 |
|
| 400 |
|
| 401 |
log_debug("Destroy window %#010x because we can't keep track of it", child); |
| 402 |
HASH_DEL(wm->tree.nodes, child_node); |
| 403 |
free(child_node); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
void wm_destroy(struct wm *wm, xcb_window_t wid) { |
| 408 |
struct wm_tree_node *node = wm_tree_find(&wm->tree, wid); |
| 409 |
BUG_ON_NULL(node); |
| 410 |
|
| 411 |
log_debug("Destroying window %#010x", wid); |
| 412 |
|
| 413 |
if (!list_is_empty(&node->children)) { |
| 414 |
log_error("Window %#010x is destroyed but it still has children. " |
| 415 |
"Orphaning them.", |
| 416 |
wid); |
| 417 |
list_foreach(struct wm_tree_node, i, &node->children, siblings) { |
| 418 |
log_error(" Child: %#010x", i->id.x); |
| 419 |
} |
| 420 |
list_splice(&node->children, &wm->orphan_root.children); |
| 421 |
} |
| 422 |
|
| 423 |
if (node == wm->focused_win) { |
| 424 |
wm->focused_win = NULL; |
| 425 |
} |
| 426 |
|
| 427 |
wm_detach_inner(wm, node); |
| 428 |
|
| 429 |
HASH_DEL(wm->tree.nodes, node); |
| 430 |
free(node); |
| 431 |
} |
| 432 |
|
| 433 |
void wm_reap_zombie(struct wm_ref *zombie) { |
| 434 |
wm_tree_reap_zombie(to_tree_node_mut(zombie)); |
| 435 |
} |
| 436 |
|
| 437 |
struct wm_wid_or_node { |
| 438 |
union { |
| 439 |
xcb_window_t wid; |
| 440 |
struct wm_tree_node *node; |
| 441 |
}; |
| 442 |
bool is_wid; |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
static void wm_import_start_inner(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 450 |
xcb_window_t wid, struct wm_tree_node *parent); |
| 451 |
|
| 452 |
static void wm_reparent_inner(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 453 |
xcb_window_t wid, struct wm_tree_node *new_parent) { |
| 454 |
BUG_ON_NULL(new_parent); |
| 455 |
auto window = wm_tree_find(&wm->tree, wid); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (window == NULL) { |
| 462 |
if (new_parent->tree_queried) { |
| 463 |
|
| 464 |
wm_import_start_inner(wm, c, atoms, wid, new_parent); |
| 465 |
} |
| 466 |
return; |
| 467 |
} |
| 468 |
|
| 469 |
if (window->parent == new_parent) { |
| 470 |
|
| 471 |
|
| 472 |
BUG_ON(!new_parent->tree_queried); |
| 473 |
wm_tree_move_to_end(&wm->tree, window, false); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
BUG_ON(window->parent != &wm->orphan_root); |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
if (!new_parent->tree_queried) { |
| 493 |
log_debug("Window %#010x is attached to window %#010x that is currently " |
| 494 |
"been queried, orphaning.", |
| 495 |
window->id.x, new_parent->id.x); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
log_debug("Reparented window %#010x to window %#010x", window->id.x, |
| 500 |
new_parent->id.x); |
| 501 |
BUG_ON(wm_tree_detach(&wm->tree, window) != NULL); |
| 502 |
wm_tree_attach(&wm->tree, window, new_parent); |
| 503 |
} |
| 504 |
|
| 505 |
void wm_reparent(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 506 |
xcb_window_t wid, xcb_window_t parent) { |
| 507 |
return wm_reparent_inner(wm, c, atoms, wid, wm_tree_find(&wm->tree, parent)); |
| 508 |
} |
| 509 |
|
| 510 |
void wm_set_has_wm_state(struct wm *wm, struct wm_ref *cursor, bool has_wm_state) { |
| 511 |
wm_tree_set_wm_state(&wm->tree, to_tree_node_mut(cursor), has_wm_state); |
| 512 |
} |
| 513 |
|
| 514 |
static const xcb_event_mask_t WM_IMPORT_EV_MASK = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | |
| 515 |
XCB_EVENT_MASK_STRUCTURE_NOTIFY | |
| 516 |
XCB_EVENT_MASK_PROPERTY_CHANGE; |
| 517 |
|
| 518 |
static void |
| 519 |
wm_handle_query_tree_reply(struct x_connection *c, struct x_async_request_base *base, |
| 520 |
const xcb_raw_generic_event_t *reply_or_error) { |
| 521 |
auto req = (struct wm_query_tree_request *)base; |
| 522 |
auto atoms = req->atoms; |
| 523 |
auto wm = req->wm; |
| 524 |
auto node = wm_tree_find(&wm->tree, req->wid); |
| 525 |
free(req); |
| 526 |
|
| 527 |
wm->n_pending_imports--; |
| 528 |
|
| 529 |
if (reply_or_error == NULL) { |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
if (node == NULL) { |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
if (reply_or_error->response_type != 0) { |
| 544 |
log_debug("Ignoring query tree reply for window not in our " |
| 545 |
"tree."); |
| 546 |
BUG_ON(wm_is_consistent(wm)); |
| 547 |
} |
| 548 |
return; |
| 549 |
} |
| 550 |
|
| 551 |
if (!node->receiving_events) { |
| 552 |
log_debug("Window ID %#010x is destroyed then reused, and hasn't had " |
| 553 |
"event mask set yet.", |
| 554 |
node->id.x); |
| 555 |
return; |
| 556 |
} |
| 557 |
|
| 558 |
if (reply_or_error->response_type == 0) { |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
xcb_generic_error_t *err = (xcb_generic_error_t *)reply_or_error; |
| 564 |
log_error("Query tree request for window %#010x failed with error %s.", |
| 565 |
node == NULL ? 0 : node->id.x, x_strerror(c, err)); |
| 566 |
BUG_ON(false); |
| 567 |
} |
| 568 |
|
| 569 |
if (node->tree_queried) { |
| 570 |
log_debug("Window %#010x has already been queried.", node->id.x); |
| 571 |
return; |
| 572 |
} |
| 573 |
|
| 574 |
node->tree_queried = true; |
| 575 |
|
| 576 |
auto reply = (const xcb_query_tree_reply_t *)reply_or_error; |
| 577 |
log_debug("Finished querying tree for window %#010x", node->id.x); |
| 578 |
|
| 579 |
auto children = xcb_query_tree_children(reply); |
| 580 |
log_debug("Window %#010x has %d children", node->id.x, |
| 581 |
xcb_query_tree_children_length(reply)); |
| 582 |
for (int i = 0; i < xcb_query_tree_children_length(reply); i++) { |
| 583 |
auto child = children[i]; |
| 584 |
|
| 585 |
|
| 586 |
wm_reparent_inner(wm, c, atoms, child, node); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
static void |
| 591 |
wm_handle_get_wm_state_reply(struct x_connection *c, struct x_async_request_base *base, |
| 592 |
const xcb_raw_generic_event_t *reply_or_error) { |
| 593 |
auto req = (struct wm_get_property_request *)base; |
| 594 |
if (reply_or_error == NULL) { |
| 595 |
free(req); |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if (reply_or_error->response_type == 0) { |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
xcb_generic_error_t *err = (xcb_generic_error_t *)reply_or_error; |
| 608 |
log_debug("Get WM_STATE request for window %#010x failed with " |
| 609 |
"error %s", |
| 610 |
req->wid, x_strerror(c, err)); |
| 611 |
free(req); |
| 612 |
return; |
| 613 |
} |
| 614 |
|
| 615 |
auto node = wm_tree_find(&req->wm->tree, req->wid); |
| 616 |
if (node == NULL) { |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
free(req); |
| 622 |
return; |
| 623 |
} |
| 624 |
auto reply = (const xcb_get_property_reply_t *)reply_or_error; |
| 625 |
wm_tree_set_wm_state(&req->wm->tree, node, reply->type != XCB_NONE); |
| 626 |
free(req); |
| 627 |
} |
| 628 |
|
| 629 |
struct wm_set_event_mask_request { |
| 630 |
struct x_async_request_base base; |
| 631 |
struct wm *wm; |
| 632 |
struct atom *atoms; |
| 633 |
xcb_window_t wid; |
| 634 |
}; |
| 635 |
|
| 636 |
static void |
| 637 |
wm_handle_set_event_mask_reply(struct x_connection *c, struct x_async_request_base *base, |
| 638 |
const xcb_raw_generic_event_t *reply_or_error) { |
| 639 |
auto req = (struct wm_set_event_mask_request *)base; |
| 640 |
auto wm = req->wm; |
| 641 |
auto atoms = req->atoms; |
| 642 |
auto wid = req->wid; |
| 643 |
free(req); |
| 644 |
|
| 645 |
if (reply_or_error == NULL) { |
| 646 |
goto end_import; |
| 647 |
} |
| 648 |
if (reply_or_error->response_type == 0) { |
| 649 |
log_debug("Failed to set event mask for window %#010x: %s, ignoring this " |
| 650 |
"window.", |
| 651 |
wid, x_strerror(c, (const xcb_generic_error_t *)reply_or_error)); |
| 652 |
goto end_import; |
| 653 |
} |
| 654 |
|
| 655 |
auto node = wm_tree_find(&wm->tree, wid); |
| 656 |
if (node == NULL) { |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
node = wm_tree_new_window(&wm->tree, wid); |
| 661 |
wm_tree_add_window(&wm->tree, node); |
| 662 |
wm_tree_attach(&wm->tree, node, &wm->orphan_root); |
| 663 |
} |
| 664 |
|
| 665 |
if (node->receiving_events) { |
| 666 |
|
| 667 |
|
| 668 |
log_debug("Event mask already set for window %#010x", wid); |
| 669 |
goto end_import; |
| 670 |
} |
| 671 |
|
| 672 |
log_debug("Event mask set for window %#010x, sending query tree.", wid); |
| 673 |
node->receiving_events = true; |
| 674 |
|
| 675 |
{ |
| 676 |
auto req2 = ccalloc(1, struct wm_query_tree_request); |
| 677 |
req2->base.callback = wm_handle_query_tree_reply; |
| 678 |
req2->wid = node->id.x; |
| 679 |
req2->wm = wm; |
| 680 |
req2->atoms = atoms; |
| 681 |
x_async_query_tree(c, node->id.x, &req2->base); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
{ |
| 687 |
auto req2 = ccalloc(1, struct wm_get_property_request); |
| 688 |
req2->base.callback = wm_handle_get_wm_state_reply; |
| 689 |
req2->wm = wm; |
| 690 |
req2->wid = node->id.x; |
| 691 |
x_async_get_property(c, node->id.x, atoms->aWM_STATE, XCB_ATOM_ANY, 0, 2, |
| 692 |
&req2->base); |
| 693 |
} |
| 694 |
return; |
| 695 |
|
| 696 |
end_import: |
| 697 |
wm->n_pending_imports--; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
static void wm_import_start_inner(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 705 |
xcb_window_t wid, struct wm_tree_node *parent) { |
| 706 |
|
| 707 |
|
| 708 |
auto new = wm_tree_new_window(&wm->tree, wid); |
| 709 |
wm_tree_add_window(&wm->tree, new); |
| 710 |
wm_tree_attach(&wm->tree, new, parent); |
| 711 |
|
| 712 |
log_debug("Starting import process for window %#010x", new->id.x); |
| 713 |
|
| 714 |
auto req = ccalloc(1, struct wm_set_event_mask_request); |
| 715 |
req->base.callback = wm_handle_set_event_mask_reply; |
| 716 |
req->wid = wid; |
| 717 |
req->atoms = atoms; |
| 718 |
req->wm = wm; |
| 719 |
x_async_change_window_attributes( |
| 720 |
c, wid, XCB_CW_EVENT_MASK, (const uint32_t[]){WM_IMPORT_EV_MASK}, &req->base); |
| 721 |
|
| 722 |
wm->n_pending_imports++; |
| 723 |
} |
| 724 |
|
| 725 |
void wm_import_start(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 726 |
xcb_window_t wid, struct wm_ref *parent) { |
| 727 |
struct wm_tree_node *parent_node = parent != NULL ? to_tree_node_mut(parent) : NULL; |
| 728 |
if (parent_node != NULL && !parent_node->tree_queried) { |
| 729 |
|
| 730 |
|
| 731 |
return; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
wm_import_start_inner(wm, c, atoms, wid, parent_node); |
| 744 |
} |
| 745 |
|
| 746 |
bool wm_is_consistent(const struct wm *wm) { |
| 747 |
return wm->n_pending_imports == 0 && list_is_empty(&wm->orphan_root.children); |
| 748 |
} |
| 749 |
|
| 750 |
bool wm_has_tree_changes(const struct wm *wm) { |
| 751 |
return !list_is_empty(&wm->tree.changes); |
| 752 |
} |
| 753 |
|
| 754 |
struct wm_change wm_dequeue_change(struct wm *wm) { |
| 755 |
auto tree_change = wm_tree_dequeue_change(&wm->tree); |
| 756 |
struct wm_change ret = { |
| 757 |
.type = tree_change.type, |
| 758 |
.toplevel = NULL, |
| 759 |
}; |
| 760 |
switch (tree_change.type) { |
| 761 |
case WM_TREE_CHANGE_CLIENT: |
| 762 |
ret.client.old = tree_change.client.old; |
| 763 |
ret.client.new_ = tree_change.client.new_; |
| 764 |
ret.toplevel = (struct wm_ref *)&tree_change.client.toplevel->siblings; |
| 765 |
break; |
| 766 |
case WM_TREE_CHANGE_TOPLEVEL_KILLED: |
| 767 |
ret.toplevel = (struct wm_ref *)&tree_change.killed->siblings; |
| 768 |
wm->needs_leader_refresh = true; |
| 769 |
break; |
| 770 |
case WM_TREE_CHANGE_TOPLEVEL_NEW: |
| 771 |
ret.toplevel = (struct wm_ref *)&tree_change.new_->siblings; |
| 772 |
wm->needs_leader_refresh = true; |
| 773 |
break; |
| 774 |
default: break; |
| 775 |
} |
| 776 |
return ret; |
| 777 |
} |
| 778 |
|
| 779 |
struct wm_ref *wm_new_mock_window(struct wm *wm, xcb_window_t wid) { |
| 780 |
auto node = wm_tree_new_window(&wm->tree, wid); |
| 781 |
return (struct wm_ref *)&node->siblings; |
| 782 |
} |
| 783 |
void wm_free_mock_window(struct wm * , struct wm_ref *cursor) { |
| 784 |
free(to_tree_node_mut(cursor)); |
| 785 |
} |