1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "subscribe.h"
43 void arrange(monitor_t *m, desktop_t *d)
45 if (d->root == NULL) {
49 layout_t l = ACTUAL_LAYOUT(d);
51 xcb_rectangle_t rect = m->rectangle;
53 rect.x += m->padding.left + d->padding.left;
54 rect.y += m->padding.top + d->padding.top;
55 rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
56 rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
58 if (l == LAYOUT_MONOCLE) {
59 rect.x += monocle_padding.left;
60 rect.y += monocle_padding.top;
61 rect.width -= monocle_padding.left + monocle_padding.right;
62 rect.height -= monocle_padding.top + monocle_padding.bottom;
65 if (!gapless_monocle || l != LAYOUT_MONOCLE) {
66 rect.x += d->window_gap;
67 rect.y += d->window_gap;
68 rect.width -= d->window_gap;
69 rect.height -= d->window_gap;
72 apply_layout(m, d, d->root, l, rect, rect);
75 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
83 if (n->presel != NULL) {
84 draw_presel_feedback(m, d, n);
89 if (n->client == NULL) {
94 if ((borderless_monocle && l == LAYOUT_MONOCLE && IS_TILED(n->client))
95 || n->client->state == STATE_FULLSCREEN) {
98 bw = n->client->border_width;
102 xcb_rectangle_t cr = get_window_rectangle(n);
103 client_state_t s = n->client->state;
104 /* tiled and pseudo-tiled clients */
105 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
106 int wg = (gapless_monocle && l == LAYOUT_MONOCLE ? 0 : d->window_gap);
108 int bleed = wg + 2 * bw;
109 r.width = (bleed < r.width ? r.width - bleed : 1);
110 r.height = (bleed < r.height ? r.height - bleed : 1);
111 /* pseudo-tiled clients */
112 if (s == STATE_PSEUDO_TILED) {
113 xcb_rectangle_t f = n->client->floating_rectangle;
114 r.width = MIN(r.width, f.width);
115 r.height = MIN(r.height, f.height);
116 if (center_pseudo_tiled) {
117 r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
118 r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
121 n->client->tiled_rectangle = r;
122 /* floating clients */
123 } else if (s == STATE_FLOATING) {
124 r = n->client->floating_rectangle;
125 /* fullscreen clients */
128 n->client->tiled_rectangle = r;
131 apply_size_hints(n->client, &r.width, &r.height);
133 if (!rect_eq(r, cr)) {
134 window_move_resize(n->id, r.x, r.y, r.width, r.height);
136 put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", m->id, d->id, n->id, r.width, r.height, r.x, r.y);
140 window_border_width(n->id, bw);
143 xcb_rectangle_t first_rect;
144 xcb_rectangle_t second_rect;
146 if (l == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
147 first_rect = second_rect = rect;
150 if (n->split_type == TYPE_VERTICAL) {
151 fence = rect.width * n->split_ratio;
152 if ((n->first_child->constraints.min_width + n->second_child->constraints.min_width) <= rect.width) {
153 if (fence < n->first_child->constraints.min_width) {
154 fence = n->first_child->constraints.min_width;
155 n->split_ratio = (double) fence / (double) rect.width;
156 } else if (fence > (uint16_t) (rect.width - n->second_child->constraints.min_width)) {
157 fence = (rect.width - n->second_child->constraints.min_width);
158 n->split_ratio = (double) fence / (double) rect.width;
161 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
162 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
164 fence = rect.height * n->split_ratio;
165 if ((n->first_child->constraints.min_height + n->second_child->constraints.min_height) <= rect.height) {
166 if (fence < n->first_child->constraints.min_height) {
167 fence = n->first_child->constraints.min_height;
168 n->split_ratio = (double) fence / (double) rect.height;
169 } else if (fence > (uint16_t) (rect.height - n->second_child->constraints.min_height)) {
170 fence = (rect.height - n->second_child->constraints.min_height);
171 n->split_ratio = (double) fence / (double) rect.height;
174 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
175 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
179 apply_layout(m, d, n->first_child, l, first_rect, root_rect);
180 apply_layout(m, d, n->second_child, l, second_rect, root_rect);
184 presel_t *make_presel(void)
186 presel_t *p = calloc(1, sizeof(presel_t));
187 p->split_dir = DIR_EAST;
188 p->split_ratio = split_ratio;
189 p->feedback = XCB_NONE;
193 void set_ratio(node_t *n, double rat)
199 n->split_ratio = rat;
202 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
204 if (n->presel == NULL) {
205 n->presel = make_presel();
208 n->presel->split_dir = dir;
210 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X dir %s\n", m->id, d->id, n->id, SPLIT_DIR_STR(dir));
213 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
215 if (n->presel == NULL) {
216 n->presel = make_presel();
219 n->presel->split_ratio = ratio;
221 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
224 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
226 if (n->presel == NULL) {
230 if (n->presel->feedback != XCB_NONE) {
231 xcb_destroy_window(dpy, n->presel->feedback);
237 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
240 void cancel_presel_in(monitor_t *m, desktop_t *d, node_t *n)
245 cancel_presel(m, d, n);
246 cancel_presel_in(m, d, n->first_child);
247 cancel_presel_in(m, d, n->second_child);
250 node_t *find_public(desktop_t *d)
252 unsigned int b_manual_area = 0;
253 unsigned int b_automatic_area = 0;
254 node_t *b_manual = NULL;
255 node_t *b_automatic = NULL;
256 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
260 unsigned int n_area = node_area(d, n);
261 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
263 b_manual_area = n_area;
265 if (n_area > b_automatic_area &&
266 n->presel == NULL && !n->private && private_count(n->parent) == 0) {
268 b_automatic_area = n_area;
271 if (b_automatic != NULL) {
278 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
280 if (d == NULL || n == NULL) {
284 /* n: inserted node */
285 /* c: new internal node */
286 /* f: focus or insertion anchor */
287 /* p: parent of focus */
288 /* g: grand parent of focus */
296 } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
297 node_t *p = f->parent;
299 if (is_first_child(f)) {
311 node_t *c = make_node(XCB_NONE);
312 node_t *p = f->parent;
313 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
314 node_t *k = find_public(d);
319 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
320 xcb_rectangle_t rect = get_rectangle(m, d, f);
321 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
325 if (f->presel == NULL) {
326 bool single_tiled = f->client != NULL && IS_TILED(f->client) && tiled_count(d->root, true) == 1;
327 if (p == NULL || automatic_scheme != SCHEME_SPIRAL || single_tiled) {
329 if (is_first_child(f)) {
339 if (initial_polarity == FIRST_CHILD) {
346 if (p == NULL || automatic_scheme == SCHEME_LONGEST_SIDE || single_tiled) {
347 if (f->rectangle.width > f->rectangle.height) {
348 c->split_type = TYPE_VERTICAL;
350 c->split_type = TYPE_HORIZONTAL;
353 if (p->split_type == TYPE_HORIZONTAL) {
354 c->split_type = TYPE_VERTICAL;
356 c->split_type = TYPE_HORIZONTAL;
360 node_t *g = p->parent;
363 if (is_first_child(p)) {
371 c->split_type = p->split_type;
372 c->split_ratio = p->split_ratio;
375 if (is_first_child(f)) {
390 if (is_first_child(f)) {
396 c->split_ratio = f->presel->split_ratio;
399 switch (f->presel->split_dir) {
401 c->split_type = TYPE_VERTICAL;
406 c->split_type = TYPE_VERTICAL;
411 c->split_type = TYPE_HORIZONTAL;
416 c->split_type = TYPE_HORIZONTAL;
424 cancel_presel(m, d, f);
425 set_marked(m, d, n, false);
429 propagate_flags_upward(m, d, n);
431 if (d->focus == NULL && is_focusable(n)) {
438 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
440 node_t *r = make_node(XCB_NONE);
441 insert_node(m, d, r, n);
443 put_status(SBSC_MASK_REPORT);
446 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
448 if (n == NULL && d->root != NULL) {
451 n = history_last_node(d, NULL);
454 n = first_focusable_leaf(d->root);
458 if (d == mon->desk || (n != NULL && !is_focusable(n))) {
463 if (d->focus != NULL && n != d->focus) {
464 neutralize_occluding_windows(m, d, n);
468 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
469 if (f->client != NULL && !is_descendant(f, n)) {
470 window_draw_border(f->id, get_border_color(false, (m == mon)));
474 draw_border(n, true, (m == mon));
478 history_add(m, d, n, false);
480 put_status(SBSC_MASK_REPORT);
486 put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
491 void transfer_sticky_nodes(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
495 } else if (n->sticky) {
496 sticky_still = false;
497 transfer_node(ms, ds, n, md, dd, dd->focus, false);
500 /* we need references to the children because n might be freed after
501 * the first recursive call */
502 node_t *first_child = n->first_child;
503 node_t *second_child = n->second_child;
504 transfer_sticky_nodes(ms, ds, md, dd, first_child);
505 transfer_sticky_nodes(ms, ds, md, dd, second_child);
509 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
514 m = history_last_monitor(NULL);
528 d = history_last_desktop(m, NULL);
539 bool guess = (n == NULL);
541 if (n == NULL && d->root != NULL) {
544 n = history_last_node(d, NULL);
547 n = first_focusable_leaf(d->root);
551 if (n != NULL && !is_focusable(n)) {
555 if ((mon != NULL && mon->desk != d) || n == NULL || n->client == NULL) {
559 if (m->sticky_count > 0 && m->desk != NULL && d != m->desk) {
560 if (guess && m->desk->focus != NULL && m->desk->focus->sticky) {
564 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
566 if (n == NULL && d->focus != NULL) {
571 if (d->focus != NULL && n != d->focus) {
572 neutralize_occluding_windows(m, d, n);
575 if (n != NULL && n->client != NULL && n->client->urgent) {
576 set_urgent(m, d, n, false);
581 for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
582 draw_border(e->focus, true, false);
585 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
589 draw_border(e->focus, true, true);
594 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
595 if (f->client != NULL && !is_descendant(f, n)) {
596 window_draw_border(f->id, get_border_color(false, true));
601 draw_border(n, true, true);
606 ewmh_update_active_window();
607 history_add(m, d, n, true);
609 put_status(SBSC_MASK_REPORT);
612 if (focus_follows_pointer) {
613 update_motion_recorder();
618 put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
623 if (pointer_follows_focus) {
624 center_pointer(get_rectangle(m, d, n));
625 } else if (focus_follows_pointer) {
626 update_motion_recorder();
632 void hide_node(desktop_t *d, node_t *n)
634 if (n == NULL || (!hide_sticky && n->sticky)) {
638 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
639 window_hide(n->presel->feedback);
641 if (n->client != NULL) {
645 if (n->client != NULL) {
646 n->client->shown = false;
648 hide_node(d, n->first_child);
649 hide_node(d, n->second_child);
653 void show_node(desktop_t *d, node_t *n)
659 if (n->client != NULL) {
662 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
663 window_show(n->presel->feedback);
666 if (n->client != NULL) {
667 n->client->shown = true;
669 show_node(d, n->first_child);
670 show_node(d, n->second_child);
674 node_t *make_node(uint32_t id)
676 if (id == XCB_NONE) {
677 id = xcb_generate_id(dpy);
679 node_t *n = calloc(1, sizeof(node_t));
681 n->parent = n->first_child = n->second_child = NULL;
682 n->vacant = n->hidden = n->sticky = n->private = n->locked = n->marked = false;
683 n->split_ratio = split_ratio;
684 n->split_type = TYPE_VERTICAL;
685 n->constraints = (constraints_t) {MIN_WIDTH, MIN_HEIGHT};
691 client_t *make_client(void)
693 client_t *c = calloc(1, sizeof(client_t));
694 c->state = c->last_state = STATE_TILED;
695 c->layer = c->last_layer = LAYER_NORMAL;
696 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
697 snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
698 c->border_width = border_width;
702 c->icccm_props.input_hint = true;
703 c->icccm_props.take_focus = false;
704 c->icccm_props.delete_window = false;
705 c->size_hints.flags = 0;
709 void initialize_client(node_t *n)
711 xcb_window_t win = n->id;
712 client_t *c = n->client;
713 xcb_icccm_get_wm_protocols_reply_t protos;
714 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
715 for (uint32_t i = 0; i < protos.atoms_len; i++) {
716 if (protos.atoms[i] == WM_TAKE_FOCUS) {
717 c->icccm_props.take_focus = true;
718 } else if (protos.atoms[i] == WM_DELETE_WINDOW) {
719 c->icccm_props.delete_window = true;
722 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
724 xcb_ewmh_get_atoms_reply_t wm_state;
725 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
726 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
727 #define HANDLE_WM_STATE(s) \
728 if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
729 c->wm_flags |= WM_FLAG_##s; continue; \
731 HANDLE_WM_STATE(MODAL)
732 HANDLE_WM_STATE(STICKY)
733 HANDLE_WM_STATE(MAXIMIZED_VERT)
734 HANDLE_WM_STATE(MAXIMIZED_HORZ)
735 HANDLE_WM_STATE(SHADED)
736 HANDLE_WM_STATE(SKIP_TASKBAR)
737 HANDLE_WM_STATE(SKIP_PAGER)
738 HANDLE_WM_STATE(HIDDEN)
739 HANDLE_WM_STATE(FULLSCREEN)
740 HANDLE_WM_STATE(ABOVE)
741 HANDLE_WM_STATE(BELOW)
742 HANDLE_WM_STATE(DEMANDS_ATTENTION)
743 #undef HANDLE_WM_STATE
745 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
747 xcb_icccm_wm_hints_t hints;
748 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
749 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
750 c->icccm_props.input_hint = hints.input;
752 xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
755 bool is_focusable(node_t *n)
757 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
758 if (f->client != NULL && !f->hidden) {
765 bool is_leaf(node_t *n)
767 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
770 bool is_first_child(node_t *n)
772 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
775 bool is_second_child(node_t *n)
777 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
780 unsigned int clients_count_in(node_t *n)
785 return (n->client != NULL ? 1 : 0) +
786 clients_count_in(n->first_child) +
787 clients_count_in(n->second_child);
791 node_t *brother_tree(node_t *n)
793 if (n == NULL || n->parent == NULL) {
796 if (is_first_child(n)) {
797 return n->parent->second_child;
799 return n->parent->first_child;
803 node_t *first_extrema(node_t *n)
807 } else if (n->first_child == NULL) {
810 return first_extrema(n->first_child);
814 node_t *second_extrema(node_t *n)
818 } else if (n->second_child == NULL) {
821 return second_extrema(n->second_child);
825 node_t *first_focusable_leaf(node_t *n)
827 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
828 if (f->client != NULL && !f->hidden) {
835 node_t *next_leaf(node_t *n, node_t *r)
841 while (is_second_child(p) && p != r) {
847 return first_extrema(p->parent->second_child);
850 node_t *prev_leaf(node_t *n, node_t *r)
856 while (is_first_child(p) && p != r) {
862 return second_extrema(p->parent->first_child);
865 node_t *next_tiled_leaf(node_t *n, node_t *r)
867 node_t *next = next_leaf(n, r);
868 if (next == NULL || (next->client != NULL && !next->vacant)) {
871 return next_tiled_leaf(next, r);
875 node_t *prev_tiled_leaf(node_t *n, node_t *r)
877 node_t *prev = prev_leaf(n, r);
878 if (prev == NULL || (prev->client != NULL && !prev->vacant)) {
881 return prev_tiled_leaf(prev, r);
885 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
886 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
890 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
893 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
896 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
899 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
905 node_t *find_fence(node_t *n, direction_t dir)
916 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
917 (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
918 (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
919 (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
927 /* returns *true* if *a* is a child of *b* */
928 bool is_child(node_t *a, node_t *b)
930 if (a == NULL || b == NULL) {
933 return (a->parent != NULL && a->parent == b);
936 /* returns *true* if *a* is a descendant of *b* */
937 bool is_descendant(node_t *a, node_t *b)
939 if (a == NULL || b == NULL) {
942 while (a != b && a != NULL) {
948 bool find_by_id(uint32_t id, coordinates_t *loc)
950 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
951 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
952 node_t *n = find_by_id_in(d->root, id);
964 node_t *find_by_id_in(node_t *r, uint32_t id)
968 } else if (r->id == id) {
971 node_t *f = find_by_id_in(r->first_child, id);
975 return find_by_id_in(r->second_child, id);
980 void find_any_node(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
982 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
983 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
984 if (find_any_node_in(m, d, d->root, ref, dst, sel)) {
991 bool find_any_node_in(monitor_t *m, desktop_t *d, node_t *n, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
996 coordinates_t loc = {m, d, n};
997 if (node_matches(&loc, ref, sel)) {
1001 if (find_any_node_in(m, d, n->first_child, ref, dst, sel)) {
1004 return find_any_node_in(m, d, n->second_child, ref, dst, sel);
1010 /* Based on https://github.com/ntrrgc/right-window */
1011 void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t *sel)
1013 xcb_rectangle_t rect = get_rectangle(ref->monitor, ref->desktop, ref->node);
1014 uint32_t md = UINT32_MAX, mr = UINT32_MAX;
1016 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1017 desktop_t *d = m->desk;
1018 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1019 coordinates_t loc = {m, d, f};
1020 xcb_rectangle_t r = get_rectangle(m, d, f);
1021 if (f == ref->node ||
1022 f->client == NULL ||
1024 is_descendant(f, ref->node) ||
1025 !node_matches(&loc, ref, sel) ||
1026 !on_dir_side(rect, r, dir)) {
1029 uint32_t fd = boundary_distance(rect, r, dir);
1030 uint32_t fr = history_rank(f);
1031 if (fd < md || (fd == md && fr < mr)) {
1040 unsigned int node_area(desktop_t *d, node_t *n)
1045 return area(get_rectangle(NULL, d, n));
1048 int tiled_count(node_t *n, bool include_receptacles)
1054 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1055 if (!f->hidden && ((include_receptacles && f->client == NULL) ||
1056 (f->client != NULL && IS_TILED(f->client)))) {
1063 void find_by_area(area_peak_t ap, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1065 unsigned int p_area;
1066 if (ap == AREA_BIGGEST) {
1072 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1073 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1074 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1075 coordinates_t loc = {m, d, f};
1076 if (f->client == NULL || f->vacant || !node_matches(&loc, ref, sel)) {
1079 unsigned int f_area = node_area(d, f);
1080 if ((ap == AREA_BIGGEST && f_area > p_area) || (ap == AREA_SMALLEST && f_area < p_area)) {
1089 void rotate_tree(node_t *n, int deg)
1091 rotate_tree_rec(n, deg);
1092 rebuild_constraints(n);
1095 void rotate_tree_rec(node_t *n, int deg)
1097 if (n == NULL || is_leaf(n) || deg == 0) {
1103 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1104 (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1106 tmp = n->first_child;
1107 n->first_child = n->second_child;
1108 n->second_child = tmp;
1109 n->split_ratio = 1.0 - n->split_ratio;
1113 if (n->split_type == TYPE_HORIZONTAL) {
1114 n->split_type = TYPE_VERTICAL;
1115 } else if (n->split_type == TYPE_VERTICAL) {
1116 n->split_type = TYPE_HORIZONTAL;
1120 rotate_tree_rec(n->first_child, deg);
1121 rotate_tree_rec(n->second_child, deg);
1124 void flip_tree(node_t *n, flip_t flp)
1126 if (n == NULL || is_leaf(n)) {
1132 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1133 (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1134 tmp = n->first_child;
1135 n->first_child = n->second_child;
1136 n->second_child = tmp;
1137 n->split_ratio = 1.0 - n->split_ratio;
1140 flip_tree(n->first_child, flp);
1141 flip_tree(n->second_child, flp);
1144 void equalize_tree(node_t *n)
1146 if (n == NULL || n->vacant) {
1149 n->split_ratio = split_ratio;
1150 equalize_tree(n->first_child);
1151 equalize_tree(n->second_child);
1155 int balance_tree(node_t *n)
1157 if (n == NULL || n->vacant) {
1159 } else if (is_leaf(n)) {
1162 int b1 = balance_tree(n->first_child);
1163 int b2 = balance_tree(n->second_child);
1165 if (b1 > 0 && b2 > 0) {
1166 n->split_ratio = (double) b1 / b;
1172 /* Adjust the split ratios so that they keep their position
1173 * despite the potential alteration of their rectangle. */
1174 void adjust_ratios(node_t *n, xcb_rectangle_t rect)
1182 if (n->split_type == TYPE_VERTICAL) {
1183 double position = (double) n->rectangle.x + n->split_ratio * (double) n->rectangle.width;
1184 ratio = (position - (double) rect.x) / (double) rect.width;
1186 double position = (double) n->rectangle.y + n->split_ratio * (double) n->rectangle.height;
1187 ratio = (position - (double) rect.y) / (double) rect.height;
1190 ratio = MAX(0.0, ratio);
1191 ratio = MIN(1.0, ratio);
1192 n->split_ratio = ratio;
1194 xcb_rectangle_t first_rect;
1195 xcb_rectangle_t second_rect;
1198 if (n->split_type == TYPE_VERTICAL) {
1199 fence = rect.width * n->split_ratio;
1200 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
1201 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
1203 fence = rect.height * n->split_ratio;
1204 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
1205 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
1208 adjust_ratios(n->first_child, first_rect);
1209 adjust_ratios(n->second_child, second_rect);
1212 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1214 if (d == NULL || n == NULL) {
1218 node_t *p = n->parent;
1224 if (d->focus == p || is_descendant(d->focus, n)) {
1228 history_remove(d, p, false);
1229 cancel_presel(m, d, p);
1234 node_t *b = brother_tree(n);
1235 node_t *g = p->parent;
1240 if (is_first_child(p)) {
1243 g->second_child = b;
1249 if (!n->vacant && removal_adjustment) {
1250 if (automatic_scheme == SCHEME_LONGEST_SIDE || g == NULL) {
1252 if (p->rectangle.width > p->rectangle.height) {
1253 b->split_type = TYPE_VERTICAL;
1255 b->split_type = TYPE_HORIZONTAL;
1258 } else if (automatic_scheme == SCHEME_ALTERNATE) {
1259 if (g->split_type == TYPE_HORIZONTAL) {
1260 b->split_type = TYPE_VERTICAL;
1262 b->split_type = TYPE_HORIZONTAL;
1270 propagate_flags_upward(m, d, b);
1274 void close_node(node_t *n)
1278 } else if (n->client != NULL) {
1279 if (n->client->icccm_props.delete_window) {
1280 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1282 xcb_kill_client(dpy, n->id);
1285 close_node(n->first_child);
1286 close_node(n->second_child);
1290 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1296 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1297 if (f->client != NULL) {
1298 xcb_kill_client(dpy, f->id);
1302 remove_node(m, d, n);
1305 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1311 unlink_node(m, d, n);
1312 history_remove(d, n, true);
1313 remove_stack_node(n);
1314 cancel_presel_in(m, d, n);
1315 if (m->sticky_count > 0) {
1316 m->sticky_count -= sticky_count(n);
1318 clients_count -= clients_count_in(n);
1319 if (is_descendant(grabbed_node, n)) {
1320 grabbed_node = NULL;
1324 ewmh_update_client_list(false);
1325 ewmh_update_client_list(true);
1327 if (mon != NULL && d->focus == NULL) {
1328 if (d == mon->desk) {
1329 focus_node(m, d, NULL);
1331 activate_node(m, d, NULL);
1336 void free_node(node_t *n)
1341 node_t *first_child = n->first_child;
1342 node_t *second_child = n->second_child;
1345 free_node(first_child);
1346 free_node(second_child);
1349 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2, bool follow)
1351 if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1352 (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1353 (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1357 put_status(SBSC_MASK_NODE_SWAP, "node_swap 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n", m1->id, d1->id, n1->id, m2->id, d2->id, n2->id);
1359 node_t *pn1 = n1->parent;
1360 node_t *pn2 = n2->parent;
1361 bool n1_first_child = is_first_child(n1);
1362 bool n2_first_child = is_first_child(n2);
1363 bool n1_held_focus = is_descendant(d1->focus, n1);
1364 bool n2_held_focus = is_descendant(d2->focus, n2);
1365 node_t *last_d1_focus = d1->focus;
1366 node_t *last_d2_focus = d2->focus;
1369 if (n1_first_child) {
1370 pn1->first_child = n2;
1372 pn1->second_child = n2;
1377 if (n2_first_child) {
1378 pn2->first_child = n1;
1380 pn2->second_child = n1;
1387 propagate_flags_upward(m2, d2, n1);
1388 propagate_flags_upward(m1, d1, n2);
1391 if (d1->root == n1) {
1395 if (d2->root == n2) {
1399 if (n1_held_focus) {
1400 d1->focus = n2_held_focus ? last_d2_focus : n2;
1403 if (n2_held_focus) {
1404 d2->focus = n1_held_focus ? last_d1_focus : n1;
1408 adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1409 adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1412 ewmh_set_wm_desktop(n1, d2);
1413 ewmh_set_wm_desktop(n2, d1);
1415 history_remove(d1, n1, true);
1416 history_remove(d2, n2, true);
1418 bool d1_was_focused = (d1 == mon->desk);
1419 bool d2_was_focused = (d2 == mon->desk);
1421 if (m1->desk != d1 && m2->desk == d2) {
1423 if (!follow || !d2_was_focused || !n2_held_focus) {
1426 } else if (m1->desk == d1 && m2->desk != d2) {
1427 if (!follow || !d1_was_focused || !n1_held_focus) {
1433 if (n1_held_focus) {
1434 if (d1_was_focused) {
1436 focus_node(m2, d2, last_d1_focus);
1438 focus_node(m1, d1, d1->focus);
1441 activate_node(m1, d1, d1->focus);
1444 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1447 if (n2_held_focus) {
1448 if (d2_was_focused) {
1450 focus_node(m1, d1, last_d2_focus);
1452 focus_node(m2, d2, d2->focus);
1455 activate_node(m2, d2, d2->focus);
1458 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1461 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1462 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1474 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd, bool follow)
1476 if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1480 if (sticky_still && ms->sticky_count > 0 && sticky_count(ns) > 0 && dd != md->desk) {
1484 put_status(SBSC_MASK_NODE_TRANSFER, "node_transfer 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n", ms->id, ds->id, ns->id, md->id, dd->id, nd!=NULL?nd->id:0);
1486 bool held_focus = is_descendant(ds->focus, ns);
1487 /* avoid ending up with a dangling pointer (because of unlink_node) */
1488 node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1489 bool ds_was_focused = (ds == mon->desk);
1491 if (held_focus && ds_was_focused) {
1492 clear_input_focus();
1495 unlink_node(ms, ds, ns);
1496 insert_node(md, dd, ns, nd);
1499 if (ns->client == NULL || monitor_from_client(ns->client) != md) {
1500 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1505 ewmh_set_wm_desktop(ns, dd);
1507 if (ds == ms->desk && dd != md->desk) {
1509 } else if (ds != ms->desk && dd == md->desk) {
1515 history_remove(ds, ns, true);
1516 stack(dd, ns, false);
1520 if (ds_was_focused) {
1521 focus_node(ms, ds, last_ds_focus);
1523 activate_node(ms, ds, last_ds_focus);
1526 draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1531 if (ds_was_focused) {
1532 focus_node(md, dd, last_ds_focus);
1534 activate_node(ms, ds, ds->focus);
1536 if (ds_was_focused) {
1537 focus_node(ms, ds, ds->focus);
1539 activate_node(ms, ds, ds->focus);
1543 if (!held_focus || !follow || !ds_was_focused) {
1544 if (dd->focus == ns) {
1545 if (dd == mon->desk) {
1546 focus_node(md, dd, held_focus ? last_ds_focus : ns);
1548 activate_node(md, dd, held_focus ? last_ds_focus : ns);
1551 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1565 bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t *sel)
1567 monitor_t *m = ref->monitor;
1568 desktop_t *d = ref->desktop;
1569 node_t *n = ref->node;
1570 n = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
1572 #define HANDLE_BOUNDARIES(m, d, n) \
1573 while (n == NULL) { \
1574 d = (dir == CYCLE_PREV ? d->prev : d->next); \
1576 m = (dir == CYCLE_PREV ? m->prev : m->next); \
1578 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
1580 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
1582 n = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
1583 if (ref->node == NULL && d == ref->desktop) { \
1587 HANDLE_BOUNDARIES(m, d, n);
1589 while (n != ref->node) {
1590 coordinates_t loc = {m, d, n};
1591 if (n->client != NULL && !n->hidden && node_matches(&loc, ref, sel)) {
1595 n = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
1596 HANDLE_BOUNDARIES(m, d, n);
1598 #undef HANDLE_BOUNDARIES
1602 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1604 if (tiled_count(n, false) < 2) {
1607 node_t *p = d->focus->parent;
1608 bool focus_first_child = is_first_child(d->focus);
1609 if (dir == CIRCULATE_FORWARD) {
1610 node_t *e = second_extrema(n);
1611 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1612 e = prev_leaf(e, n);
1614 for (node_t *s = e, *f = prev_tiled_leaf(s, n); f != NULL; s = prev_tiled_leaf(f, n), f = prev_tiled_leaf(s, n)) {
1615 swap_nodes(m, d, f, m, d, s, false);
1618 node_t *e = first_extrema(n);
1619 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1620 e = next_leaf(e, n);
1622 for (node_t *f = e, *s = next_tiled_leaf(f, n); s != NULL; f = next_tiled_leaf(s, n), s = next_tiled_leaf(f, n)) {
1623 swap_nodes(m, d, f, m, d, s, false);
1627 node_t *f = focus_first_child ? p->first_child : p->second_child;
1629 if (d == mon->desk) {
1630 focus_node(m, d, f);
1632 activate_node(m, d, f);
1638 void set_vacant(monitor_t *m, desktop_t *d, node_t *n, bool value)
1640 if (n->vacant == value) {
1644 propagate_vacant_downward(m, d, n, value);
1645 propagate_vacant_upward(m, d, n);
1648 void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1650 if (n->vacant == value) {
1657 cancel_presel(m, d, n);
1661 void propagate_vacant_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1667 set_vacant_local(m, d, n, value);
1669 propagate_vacant_downward(m, d, n->first_child, value);
1670 propagate_vacant_downward(m, d, n->second_child, value);
1673 void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
1679 node_t *p = n->parent;
1682 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1685 propagate_vacant_upward(m, d, p);
1688 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1690 if (n == NULL || n->client == NULL || n->client->layer == l) {
1694 n->client->last_layer = n->client->layer;
1695 n->client->layer = l;
1697 if (l == LAYER_ABOVE) {
1698 n->client->wm_flags |= WM_FLAG_ABOVE;
1699 n->client->wm_flags &= ~WM_FLAG_BELOW;
1700 } else if (l == LAYER_BELOW) {
1701 n->client->wm_flags |= WM_FLAG_BELOW;
1702 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1704 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1707 ewmh_wm_state_update(n);
1709 put_status(SBSC_MASK_NODE_LAYER, "node_layer 0x%08X 0x%08X 0x%08X %s\n", m->id, d->id, n->id, LAYER_STR(l));
1711 if (d->focus == n) {
1712 neutralize_occluding_windows(m, d, n);
1715 stack(d, n, (d->focus == n));
1720 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1722 if (n == NULL || n->client == NULL || n->client->state == s) {
1726 client_t *c = n->client;
1728 c->last_state = c->state;
1731 switch (c->last_state) {
1733 case STATE_PSEUDO_TILED:
1735 case STATE_FLOATING:
1736 set_floating(m, d, n, false);
1738 case STATE_FULLSCREEN:
1739 set_fullscreen(m, d, n, false);
1743 put_status(SBSC_MASK_NODE_STATE, "node_state 0x%08X 0x%08X 0x%08X %s off\n", m->id, d->id, n->id, STATE_STR(c->last_state));
1747 case STATE_PSEUDO_TILED:
1749 case STATE_FLOATING:
1750 set_floating(m, d, n, true);
1752 case STATE_FULLSCREEN:
1753 set_fullscreen(m, d, n, true);
1757 put_status(SBSC_MASK_NODE_STATE, "node_state 0x%08X 0x%08X 0x%08X %s on\n", m->id, d->id, n->id, STATE_STR(c->state));
1759 if (n == m->desk->focus) {
1760 put_status(SBSC_MASK_REPORT);
1766 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1772 cancel_presel(m, d, n);
1773 set_vacant(m, d, n, value);
1775 if (!value && d->focus == n) {
1776 neutralize_occluding_windows(m, d, n);
1779 stack(d, n, (d->focus == n));
1782 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1788 client_t *c = n->client;
1790 cancel_presel(m, d, n);
1791 set_vacant(m, d, n, value);
1794 c->wm_flags |= WM_FLAG_FULLSCREEN;
1796 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1797 if (d->focus == n) {
1798 neutralize_occluding_windows(m, d, n);
1802 ewmh_wm_state_update(n);
1803 stack(d, n, (d->focus == n));
1806 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1808 bool changed = false;
1809 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1810 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1811 if (a != f && a->client != NULL && f->client != NULL &&
1812 IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1813 set_state(m, d, a, a->client->last_state);
1823 void rebuild_constraints(node_t *n)
1825 if (n == NULL || is_leaf(n)) {
1828 rebuild_constraints(n->first_child);
1829 rebuild_constraints(n->second_child);
1830 update_constraints(n);
1834 void update_constraints(node_t *n)
1836 if (n == NULL || is_leaf(n)) {
1839 if (n->split_type == TYPE_VERTICAL) {
1840 n->constraints.min_width = n->first_child->constraints.min_width + n->second_child->constraints.min_width;
1841 n->constraints.min_height = MAX(n->first_child->constraints.min_height, n->second_child->constraints.min_height);
1843 n->constraints.min_width = MAX(n->first_child->constraints.min_width, n->second_child->constraints.min_width);
1844 n->constraints.min_height = n->first_child->constraints.min_height + n->second_child->constraints.min_height;
1848 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
1854 node_t *p = n->parent;
1857 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1858 set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
1859 update_constraints(p);
1862 propagate_flags_upward(m, d, p);
1865 void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
1867 if (n == NULL || n->hidden == value) {
1871 bool held_focus = is_descendant(d->focus, n);
1873 propagate_hidden_downward(m, d, n, value);
1874 propagate_hidden_upward(m, d, n);
1876 put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X hidden %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
1878 if (held_focus || d->focus == NULL) {
1879 if (d->focus != NULL) {
1881 draw_border(n, false, (mon == m));
1883 if (d == mon->desk) {
1884 focus_node(m, d, d->focus);
1886 activate_node(m, d, d->focus);
1891 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1893 if (n->hidden == value) {
1899 if (n->client != NULL) {
1900 if (n->client->shown) {
1901 window_set_visibility(n->id, !value);
1904 if (IS_TILED(n->client)) {
1905 set_vacant(m, d, n, value);
1909 n->client->wm_flags |= WM_FLAG_HIDDEN;
1911 n->client->wm_flags &= ~WM_FLAG_HIDDEN;
1914 ewmh_wm_state_update(n);
1918 void propagate_hidden_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1924 set_hidden_local(m, d, n, value);
1926 propagate_hidden_downward(m, d, n->first_child, value);
1927 propagate_hidden_downward(m, d, n->second_child, value);
1930 void propagate_hidden_upward(monitor_t *m, desktop_t *d, node_t *n)
1936 node_t *p = n->parent;
1939 set_hidden_local(m, d, p, p->first_child->hidden && p->second_child->hidden);
1942 propagate_hidden_upward(m, d, p);
1945 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
1947 if (n == NULL || n->sticky == value) {
1952 transfer_node(m, d, n, m, m->desk, m->desk->focus, false);
1963 if (n->client != NULL) {
1965 n->client->wm_flags |= WM_FLAG_STICKY;
1967 n->client->wm_flags &= ~WM_FLAG_STICKY;
1969 ewmh_wm_state_update(n);
1972 put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X sticky %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
1974 if (n == m->desk->focus) {
1975 put_status(SBSC_MASK_REPORT);
1979 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
1981 if (n == NULL || n->private == value) {
1987 put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X private %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
1989 if (n == m->desk->focus) {
1990 put_status(SBSC_MASK_REPORT);
1994 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
1996 if (n == NULL || n->locked == value) {
2002 put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X locked %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2004 if (n == m->desk->focus) {
2005 put_status(SBSC_MASK_REPORT);
2009 void set_marked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2011 if (n == NULL || n->marked == value) {
2017 put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X marked %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2019 if (n == m->desk->focus) {
2020 put_status(SBSC_MASK_REPORT);
2024 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
2026 if (value && mon->desk->focus == n) {
2030 n->client->urgent = value;
2033 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
2035 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
2038 ewmh_wm_state_update(n);
2040 put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X urgent %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2041 put_status(SBSC_MASK_REPORT);
2044 xcb_rectangle_t get_rectangle(monitor_t *m, desktop_t *d, node_t *n)
2047 return m->rectangle;
2049 client_t *c = n->client;
2051 if (IS_FLOATING(c)) {
2052 return c->floating_rectangle;
2054 return c->tiled_rectangle;
2057 int wg = (d == NULL ? 0 : (gapless_monocle && IS_MONOCLE(d) ? 0 : d->window_gap));
2058 xcb_rectangle_t rect = n->rectangle;
2065 void listen_enter_notify(node_t *n, bool enable)
2067 uint32_t mask = CLIENT_EVENT_MASK | (enable ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
2068 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
2069 if (f->client == NULL) {
2072 xcb_change_window_attributes(dpy, f->id, XCB_CW_EVENT_MASK, &mask);
2073 if (f->presel != NULL) {
2074 xcb_change_window_attributes(dpy, f->presel->feedback, XCB_CW_EVENT_MASK, &mask);
2079 void regenerate_ids_in(node_t *n)
2081 if (n == NULL || n->client != NULL) {
2084 n->id = xcb_generate_id(dpy);
2085 regenerate_ids_in(n->first_child);
2086 regenerate_ids_in(n->second_child);
2089 #define DEF_FLAG_COUNT(flag) \
2090 unsigned int flag##_count(node_t *n) \
2095 return ((n->flag ? 1 : 0) + \
2096 flag##_count(n->first_child) + \
2097 flag##_count(n->second_child)); \
2100 DEF_FLAG_COUNT(sticky)
2101 DEF_FLAG_COUNT(private)
2102 DEF_FLAG_COUNT(locked)
2103 #undef DEF_FLAG_COUNT