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 xcb_rectangle_t rect = m->rectangle;
51 rect.x += m->padding.left + d->padding.left;
52 rect.y += m->padding.top + d->padding.top;
53 rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
54 rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
56 if (d->layout == LAYOUT_MONOCLE) {
57 rect.x += monocle_padding.left;
58 rect.y += monocle_padding.top;
59 rect.width -= monocle_padding.left + monocle_padding.right;
60 rect.height -= monocle_padding.top + monocle_padding.bottom;
63 if (!gapless_monocle || d->layout != LAYOUT_MONOCLE) {
64 rect.x += d->window_gap;
65 rect.y += d->window_gap;
66 rect.width -= d->window_gap;
67 rect.height -= d->window_gap;
70 apply_layout(m, d, d->root, rect, rect);
73 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
81 if (n->presel != NULL) {
82 draw_presel_feedback(m, d, n);
87 if (n->client == NULL) {
92 if ((borderless_monocle && d->layout == LAYOUT_MONOCLE && IS_TILED(n->client))
93 || n->client->state == STATE_FULLSCREEN) {
96 bw = n->client->border_width;
100 xcb_rectangle_t cr = get_window_rectangle(n);
101 client_state_t s = n->client->state;
102 /* tiled and pseudo-tiled clients */
103 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
104 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
106 int bleed = wg + 2 * bw;
107 r.width = (bleed < r.width ? r.width - bleed : 1);
108 r.height = (bleed < r.height ? r.height - bleed : 1);
109 /* pseudo-tiled clients */
110 if (s == STATE_PSEUDO_TILED) {
111 xcb_rectangle_t f = n->client->floating_rectangle;
112 r.width = MIN(r.width, f.width);
113 r.height = MIN(r.height, f.height);
114 if (center_pseudo_tiled) {
115 r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
116 r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
119 n->client->tiled_rectangle = r;
120 /* floating clients */
121 } else if (s == STATE_FLOATING) {
122 r = n->client->floating_rectangle;
123 /* fullscreen clients */
126 n->client->tiled_rectangle = r;
129 apply_size_hints(n->client, &r.width, &r.height);
131 if (!rect_eq(r, cr)) {
132 window_move_resize(n->id, r.x, r.y, r.width, r.height);
134 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);
138 window_border_width(n->id, bw);
141 xcb_rectangle_t first_rect;
142 xcb_rectangle_t second_rect;
144 if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
145 first_rect = second_rect = rect;
148 if (n->split_type == TYPE_VERTICAL) {
149 fence = rect.width * n->split_ratio;
150 if ((n->first_child->constraints.min_width + n->second_child->constraints.min_width) <= rect.width) {
151 if (fence < n->first_child->constraints.min_width) {
152 fence = n->first_child->constraints.min_width;
153 n->split_ratio = (double) fence / (double) rect.width;
154 } else if (fence > (uint16_t) (rect.width - n->second_child->constraints.min_width)) {
155 fence = (rect.width - n->second_child->constraints.min_width);
156 n->split_ratio = (double) fence / (double) rect.width;
159 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
160 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
162 fence = rect.height * n->split_ratio;
163 if ((n->first_child->constraints.min_height + n->second_child->constraints.min_height) <= rect.height) {
164 if (fence < n->first_child->constraints.min_height) {
165 fence = n->first_child->constraints.min_height;
166 n->split_ratio = (double) fence / (double) rect.height;
167 } else if (fence > (uint16_t) (rect.height - n->second_child->constraints.min_height)) {
168 fence = (rect.height - n->second_child->constraints.min_height);
169 n->split_ratio = (double) fence / (double) rect.height;
172 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
173 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
177 apply_layout(m, d, n->first_child, first_rect, root_rect);
178 apply_layout(m, d, n->second_child, second_rect, root_rect);
182 presel_t *make_presel(void)
184 presel_t *p = calloc(1, sizeof(presel_t));
185 p->split_dir = DIR_EAST;
186 p->split_ratio = split_ratio;
187 p->feedback = XCB_NONE;
191 void set_ratio(node_t *n, double rat)
197 n->split_ratio = rat;
200 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
202 if (n->presel == NULL) {
203 n->presel = make_presel();
206 n->presel->split_dir = dir;
208 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));
211 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
213 if (n->presel == NULL) {
214 n->presel = make_presel();
217 n->presel->split_ratio = ratio;
219 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
222 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
224 if (n->presel == NULL) {
228 if (n->presel->feedback != XCB_NONE) {
229 xcb_destroy_window(dpy, n->presel->feedback);
235 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
238 void cancel_presel_in(monitor_t *m, desktop_t *d, node_t *n)
243 cancel_presel(m, d, n);
244 cancel_presel_in(m, d, n->first_child);
245 cancel_presel_in(m, d, n->second_child);
248 node_t *find_public(desktop_t *d)
250 unsigned int b_manual_area = 0;
251 unsigned int b_automatic_area = 0;
252 node_t *b_manual = NULL;
253 node_t *b_automatic = NULL;
254 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
258 unsigned int n_area = node_area(d, n);
259 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
261 b_manual_area = n_area;
263 if (n_area > b_automatic_area &&
264 n->presel == NULL && !n->private && private_count(n->parent) == 0) {
266 b_automatic_area = n_area;
269 if (b_automatic != NULL) {
276 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
278 if (d == NULL || n == NULL) {
282 /* n: inserted node */
283 /* c: new internal node */
284 /* f: focus or insertion anchor */
285 /* p: parent of focus */
286 /* g: grand parent of focus */
294 } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
295 node_t *p = f->parent;
297 if (is_first_child(f)) {
309 node_t *c = make_node(XCB_NONE);
310 node_t *p = f->parent;
311 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
312 node_t *k = find_public(d);
317 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
318 xcb_rectangle_t rect = get_rectangle(m, d, f);
319 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
323 if (f->presel == NULL) {
324 bool single_tiled = f->client != NULL && IS_TILED(f->client) && tiled_count(d->root, true) == 1;
325 if (p == NULL || automatic_scheme != SCHEME_SPIRAL || single_tiled) {
327 if (is_first_child(f)) {
337 if (initial_polarity == FIRST_CHILD) {
344 if (p == NULL || automatic_scheme == SCHEME_LONGEST_SIDE || single_tiled) {
345 if (f->rectangle.width > f->rectangle.height) {
346 c->split_type = TYPE_VERTICAL;
348 c->split_type = TYPE_HORIZONTAL;
352 while (q != NULL && (q->first_child->vacant || q->second_child->vacant)) {
358 if (q->split_type == TYPE_HORIZONTAL) {
359 c->split_type = TYPE_VERTICAL;
361 c->split_type = TYPE_HORIZONTAL;
365 node_t *g = p->parent;
368 if (is_first_child(p)) {
376 c->split_type = p->split_type;
377 c->split_ratio = p->split_ratio;
380 if (is_first_child(f)) {
395 if (is_first_child(f)) {
401 c->split_ratio = f->presel->split_ratio;
404 switch (f->presel->split_dir) {
406 c->split_type = TYPE_VERTICAL;
411 c->split_type = TYPE_VERTICAL;
416 c->split_type = TYPE_HORIZONTAL;
421 c->split_type = TYPE_HORIZONTAL;
429 cancel_presel(m, d, f);
430 set_marked(m, d, n, false);
434 m->sticky_count += sticky_count(n);
436 propagate_flags_upward(m, d, n);
438 if (d->focus == NULL && is_focusable(n)) {
445 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
447 node_t *r = make_node(XCB_NONE);
448 insert_node(m, d, r, n);
450 if (single_monocle && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
451 set_layout(m, d, d->user_layout, false);
455 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
457 if (n == NULL && d->root != NULL) {
460 n = history_last_node(d, NULL);
463 n = first_focusable_leaf(d->root);
467 if (d == mon->desk || (n != NULL && !is_focusable(n))) {
472 if (d->focus != NULL && n != d->focus) {
473 neutralize_occluding_windows(m, d, n);
477 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
478 if (f->client != NULL && !is_descendant(f, n)) {
479 window_draw_border(f->id, get_border_color(false, (m == mon)));
483 draw_border(n, true, (m == mon));
487 history_add(m, d, n, false);
489 put_status(SBSC_MASK_REPORT);
495 put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
500 void transfer_sticky_nodes(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
504 } else if (n->sticky) {
505 sticky_still = false;
506 transfer_node(ms, ds, n, md, dd, dd->focus, false);
509 /* we need references to the children because n might be freed after
510 * the first recursive call */
511 node_t *first_child = n->first_child;
512 node_t *second_child = n->second_child;
513 transfer_sticky_nodes(ms, ds, md, dd, first_child);
514 transfer_sticky_nodes(ms, ds, md, dd, second_child);
518 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
523 m = history_last_monitor(NULL);
537 d = history_last_desktop(m, NULL);
548 bool guess = (n == NULL);
550 if (n == NULL && d->root != NULL) {
553 n = history_last_node(d, NULL);
556 n = first_focusable_leaf(d->root);
560 if (n != NULL && !is_focusable(n)) {
564 if ((mon != NULL && mon->desk != d) || n == NULL || n->client == NULL) {
568 if (m->sticky_count > 0 && m->desk != NULL && d != m->desk) {
569 if (guess && m->desk->focus != NULL && m->desk->focus->sticky) {
573 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
575 if (n == NULL && d->focus != NULL) {
580 if (d->focus != NULL && n != d->focus) {
581 neutralize_occluding_windows(m, d, n);
584 if (n != NULL && n->client != NULL && n->client->urgent) {
585 set_urgent(m, d, n, false);
590 for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
591 draw_border(e->focus, true, false);
594 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
598 draw_border(e->focus, true, true);
603 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
604 if (f->client != NULL && !is_descendant(f, n)) {
605 window_draw_border(f->id, get_border_color(false, true));
610 draw_border(n, true, true);
615 ewmh_update_active_window();
616 history_add(m, d, n, true);
618 put_status(SBSC_MASK_REPORT);
621 if (focus_follows_pointer) {
622 update_motion_recorder();
627 put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
632 if (pointer_follows_focus) {
633 center_pointer(get_rectangle(m, d, n));
634 } else if (focus_follows_pointer) {
635 update_motion_recorder();
641 void hide_node(desktop_t *d, node_t *n)
643 if (n == NULL || (!hide_sticky && n->sticky)) {
647 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
648 window_hide(n->presel->feedback);
650 if (n->client != NULL) {
654 if (n->client != NULL) {
655 n->client->shown = false;
657 hide_node(d, n->first_child);
658 hide_node(d, n->second_child);
662 void show_node(desktop_t *d, node_t *n)
668 if (n->client != NULL) {
671 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
672 window_show(n->presel->feedback);
675 if (n->client != NULL) {
676 n->client->shown = true;
678 show_node(d, n->first_child);
679 show_node(d, n->second_child);
683 node_t *make_node(uint32_t id)
685 if (id == XCB_NONE) {
686 id = xcb_generate_id(dpy);
688 node_t *n = calloc(1, sizeof(node_t));
690 n->parent = n->first_child = n->second_child = NULL;
691 n->vacant = n->hidden = n->sticky = n->private = n->locked = n->marked = false;
692 n->split_ratio = split_ratio;
693 n->split_type = TYPE_VERTICAL;
694 n->constraints = (constraints_t) {MIN_WIDTH, MIN_HEIGHT};
700 client_t *make_client(void)
702 client_t *c = calloc(1, sizeof(client_t));
703 c->state = c->last_state = STATE_TILED;
704 c->layer = c->last_layer = LAYER_NORMAL;
705 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
706 snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
707 c->border_width = border_width;
711 c->icccm_props.input_hint = true;
712 c->icccm_props.take_focus = false;
713 c->icccm_props.delete_window = false;
714 c->size_hints.flags = 0;
718 void initialize_client(node_t *n)
720 xcb_window_t win = n->id;
721 client_t *c = n->client;
722 xcb_icccm_get_wm_protocols_reply_t protos;
723 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
724 for (uint32_t i = 0; i < protos.atoms_len; i++) {
725 if (protos.atoms[i] == WM_TAKE_FOCUS) {
726 c->icccm_props.take_focus = true;
727 } else if (protos.atoms[i] == WM_DELETE_WINDOW) {
728 c->icccm_props.delete_window = true;
731 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
733 xcb_ewmh_get_atoms_reply_t wm_state;
734 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
735 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
736 #define HANDLE_WM_STATE(s) \
737 if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
738 c->wm_flags |= WM_FLAG_##s; continue; \
740 HANDLE_WM_STATE(MODAL)
741 HANDLE_WM_STATE(STICKY)
742 HANDLE_WM_STATE(MAXIMIZED_VERT)
743 HANDLE_WM_STATE(MAXIMIZED_HORZ)
744 HANDLE_WM_STATE(SHADED)
745 HANDLE_WM_STATE(SKIP_TASKBAR)
746 HANDLE_WM_STATE(SKIP_PAGER)
747 HANDLE_WM_STATE(HIDDEN)
748 HANDLE_WM_STATE(FULLSCREEN)
749 HANDLE_WM_STATE(ABOVE)
750 HANDLE_WM_STATE(BELOW)
751 HANDLE_WM_STATE(DEMANDS_ATTENTION)
752 #undef HANDLE_WM_STATE
754 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
756 xcb_icccm_wm_hints_t hints;
757 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
758 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
759 c->icccm_props.input_hint = hints.input;
761 xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
764 bool is_focusable(node_t *n)
766 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
767 if (f->client != NULL && !f->hidden) {
774 bool is_leaf(node_t *n)
776 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
779 bool is_first_child(node_t *n)
781 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
784 bool is_second_child(node_t *n)
786 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
789 unsigned int clients_count_in(node_t *n)
794 return (n->client != NULL ? 1 : 0) +
795 clients_count_in(n->first_child) +
796 clients_count_in(n->second_child);
800 node_t *brother_tree(node_t *n)
802 if (n == NULL || n->parent == NULL) {
805 if (is_first_child(n)) {
806 return n->parent->second_child;
808 return n->parent->first_child;
812 node_t *first_extrema(node_t *n)
816 } else if (n->first_child == NULL) {
819 return first_extrema(n->first_child);
823 node_t *second_extrema(node_t *n)
827 } else if (n->second_child == NULL) {
830 return second_extrema(n->second_child);
834 node_t *first_focusable_leaf(node_t *n)
836 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
837 if (f->client != NULL && !f->hidden) {
844 node_t *next_node(node_t *n)
850 if (n->second_child != NULL) {
851 return first_extrema(n->second_child);
854 while (is_second_child(p)) {
857 if (is_first_child(p)) {
865 node_t *prev_node(node_t *n)
871 if (n->first_child != NULL) {
872 return second_extrema(n->first_child);
875 while (is_first_child(p)) {
878 if (is_second_child(p)) {
886 node_t *next_leaf(node_t *n, node_t *r)
892 while (is_second_child(p) && p != r) {
898 return first_extrema(p->parent->second_child);
901 node_t *prev_leaf(node_t *n, node_t *r)
907 while (is_first_child(p) && p != r) {
913 return second_extrema(p->parent->first_child);
916 node_t *next_tiled_leaf(node_t *n, node_t *r)
918 node_t *next = next_leaf(n, r);
919 if (next == NULL || (next->client != NULL && !next->vacant)) {
922 return next_tiled_leaf(next, r);
926 node_t *prev_tiled_leaf(node_t *n, node_t *r)
928 node_t *prev = prev_leaf(n, r);
929 if (prev == NULL || (prev->client != NULL && !prev->vacant)) {
932 return prev_tiled_leaf(prev, r);
936 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
937 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
941 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
944 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
947 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
950 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
956 node_t *find_fence(node_t *n, direction_t dir)
967 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
968 (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
969 (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
970 (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
978 /* returns *true* if *a* is a child of *b* */
979 bool is_child(node_t *a, node_t *b)
981 if (a == NULL || b == NULL) {
984 return (a->parent != NULL && a->parent == b);
987 /* returns *true* if *a* is a descendant of *b* */
988 bool is_descendant(node_t *a, node_t *b)
990 if (a == NULL || b == NULL) {
993 while (a != b && a != NULL) {
999 bool find_by_id(uint32_t id, coordinates_t *loc)
1001 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1002 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1003 node_t *n = find_by_id_in(d->root, id);
1015 node_t *find_by_id_in(node_t *r, uint32_t id)
1019 } else if (r->id == id) {
1022 node_t *f = find_by_id_in(r->first_child, id);
1026 return find_by_id_in(r->second_child, id);
1031 void find_any_node(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1033 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1034 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1035 if (find_any_node_in(m, d, d->root, ref, dst, sel)) {
1042 bool find_any_node_in(monitor_t *m, desktop_t *d, node_t *n, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1047 coordinates_t loc = {m, d, n};
1048 if (node_matches(&loc, ref, sel)) {
1052 if (find_any_node_in(m, d, n->first_child, ref, dst, sel)) {
1055 return find_any_node_in(m, d, n->second_child, ref, dst, sel);
1061 void find_first_ancestor(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1063 if (ref->node == NULL) {
1067 coordinates_t loc = {ref->monitor, ref->desktop, ref->node};
1068 while ((loc.node = loc.node->parent) != NULL) {
1069 if (node_matches(&loc, ref, sel)) {
1076 /* Based on https://github.com/ntrrgc/right-window */
1077 void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t *sel)
1079 xcb_rectangle_t rect = get_rectangle(ref->monitor, ref->desktop, ref->node);
1080 uint32_t md = UINT32_MAX, mr = UINT32_MAX;
1082 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1083 desktop_t *d = m->desk;
1084 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1085 coordinates_t loc = {m, d, f};
1086 xcb_rectangle_t r = get_rectangle(m, d, f);
1087 if (f == ref->node ||
1088 f->client == NULL ||
1090 is_descendant(f, ref->node) ||
1091 !node_matches(&loc, ref, sel) ||
1092 !on_dir_side(rect, r, dir)) {
1095 uint32_t fd = boundary_distance(rect, r, dir);
1096 uint32_t fr = history_rank(f);
1097 if (fd < md || (fd == md && fr < mr)) {
1106 unsigned int node_area(desktop_t *d, node_t *n)
1111 return area(get_rectangle(NULL, d, n));
1114 int tiled_count(node_t *n, bool include_receptacles)
1120 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1121 if (!f->hidden && ((include_receptacles && f->client == NULL) ||
1122 (f->client != NULL && IS_TILED(f->client)))) {
1129 void find_by_area(area_peak_t ap, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1131 unsigned int p_area;
1132 if (ap == AREA_BIGGEST) {
1138 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1139 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1140 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1141 coordinates_t loc = {m, d, f};
1142 if (f->vacant || !node_matches(&loc, ref, sel)) {
1145 unsigned int f_area = node_area(d, f);
1146 if ((ap == AREA_BIGGEST && f_area > p_area) || (ap == AREA_SMALLEST && f_area < p_area)) {
1155 void rotate_tree(node_t *n, int deg)
1157 rotate_tree_rec(n, deg);
1158 rebuild_constraints(n);
1161 void rotate_tree_rec(node_t *n, int deg)
1163 if (n == NULL || is_leaf(n) || deg == 0) {
1169 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1170 (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1172 tmp = n->first_child;
1173 n->first_child = n->second_child;
1174 n->second_child = tmp;
1175 n->split_ratio = 1.0 - n->split_ratio;
1179 if (n->split_type == TYPE_HORIZONTAL) {
1180 n->split_type = TYPE_VERTICAL;
1181 } else if (n->split_type == TYPE_VERTICAL) {
1182 n->split_type = TYPE_HORIZONTAL;
1186 rotate_tree_rec(n->first_child, deg);
1187 rotate_tree_rec(n->second_child, deg);
1190 void flip_tree(node_t *n, flip_t flp)
1192 if (n == NULL || is_leaf(n)) {
1198 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1199 (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1200 tmp = n->first_child;
1201 n->first_child = n->second_child;
1202 n->second_child = tmp;
1203 n->split_ratio = 1.0 - n->split_ratio;
1206 flip_tree(n->first_child, flp);
1207 flip_tree(n->second_child, flp);
1210 void equalize_tree(node_t *n)
1212 if (n == NULL || n->vacant) {
1215 n->split_ratio = split_ratio;
1216 equalize_tree(n->first_child);
1217 equalize_tree(n->second_child);
1221 int balance_tree(node_t *n)
1223 if (n == NULL || n->vacant) {
1225 } else if (is_leaf(n)) {
1228 int b1 = balance_tree(n->first_child);
1229 int b2 = balance_tree(n->second_child);
1231 if (b1 > 0 && b2 > 0) {
1232 n->split_ratio = (double) b1 / b;
1238 /* Adjust the split ratios so that they keep their position
1239 * despite the potential alteration of their rectangle. */
1240 void adjust_ratios(node_t *n, xcb_rectangle_t rect)
1248 if (n->split_type == TYPE_VERTICAL) {
1249 double position = (double) n->rectangle.x + n->split_ratio * (double) n->rectangle.width;
1250 ratio = (position - (double) rect.x) / (double) rect.width;
1252 double position = (double) n->rectangle.y + n->split_ratio * (double) n->rectangle.height;
1253 ratio = (position - (double) rect.y) / (double) rect.height;
1256 ratio = MAX(0.0, ratio);
1257 ratio = MIN(1.0, ratio);
1258 n->split_ratio = ratio;
1260 xcb_rectangle_t first_rect;
1261 xcb_rectangle_t second_rect;
1264 if (n->split_type == TYPE_VERTICAL) {
1265 fence = rect.width * n->split_ratio;
1266 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
1267 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
1269 fence = rect.height * n->split_ratio;
1270 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
1271 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
1274 adjust_ratios(n->first_child, first_rect);
1275 adjust_ratios(n->second_child, second_rect);
1278 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1280 if (d == NULL || n == NULL) {
1284 node_t *p = n->parent;
1286 if (m->sticky_count > 0) {
1287 m->sticky_count -= sticky_count(n);
1294 if (d->focus == p || is_descendant(d->focus, n)) {
1298 history_remove(d, p, false);
1299 cancel_presel(m, d, p);
1305 node_t *b = brother_tree(n);
1306 node_t *g = p->parent;
1311 if (is_first_child(p)) {
1314 g->second_child = b;
1320 if (!n->vacant && removal_adjustment) {
1321 if (automatic_scheme == SCHEME_SPIRAL) {
1322 if (is_first_child(n)) {
1323 rotate_tree(b, 270);
1327 } else if (automatic_scheme == SCHEME_LONGEST_SIDE || g == NULL) {
1329 if (p->rectangle.width > p->rectangle.height) {
1330 b->split_type = TYPE_VERTICAL;
1332 b->split_type = TYPE_HORIZONTAL;
1335 } else if (automatic_scheme == SCHEME_ALTERNATE) {
1336 if (g->split_type == TYPE_HORIZONTAL) {
1337 b->split_type = TYPE_VERTICAL;
1339 b->split_type = TYPE_HORIZONTAL;
1347 propagate_flags_upward(m, d, b);
1351 void close_node(node_t *n)
1355 } else if (n->client != NULL) {
1356 if (n->client->icccm_props.delete_window) {
1357 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1359 xcb_kill_client(dpy, n->id);
1362 close_node(n->first_child);
1363 close_node(n->second_child);
1367 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1373 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1374 if (f->client != NULL) {
1375 xcb_kill_client(dpy, f->id);
1379 remove_node(m, d, n);
1382 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1388 unlink_node(m, d, n);
1389 history_remove(d, n, true);
1390 remove_stack_node(n);
1391 cancel_presel_in(m, d, n);
1392 clients_count -= clients_count_in(n);
1393 if (is_descendant(grabbed_node, n)) {
1394 grabbed_node = NULL;
1398 if (single_monocle && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
1399 set_layout(m, d, LAYOUT_MONOCLE, false);
1402 ewmh_update_client_list(false);
1403 ewmh_update_client_list(true);
1405 if (mon != NULL && d->focus == NULL) {
1406 if (d == mon->desk) {
1407 focus_node(m, d, NULL);
1409 activate_node(m, d, NULL);
1414 void free_node(node_t *n)
1419 node_t *first_child = n->first_child;
1420 node_t *second_child = n->second_child;
1423 free_node(first_child);
1424 free_node(second_child);
1427 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2, bool follow)
1429 if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1430 (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1431 (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1435 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);
1437 node_t *pn1 = n1->parent;
1438 node_t *pn2 = n2->parent;
1439 bool n1_first_child = is_first_child(n1);
1440 bool n2_first_child = is_first_child(n2);
1441 bool n1_held_focus = is_descendant(d1->focus, n1);
1442 bool n2_held_focus = is_descendant(d2->focus, n2);
1443 node_t *last_d1_focus = d1->focus;
1444 node_t *last_d2_focus = d2->focus;
1447 if (n1_first_child) {
1448 pn1->first_child = n2;
1450 pn1->second_child = n2;
1455 if (n2_first_child) {
1456 pn2->first_child = n1;
1458 pn2->second_child = n1;
1465 propagate_flags_upward(m2, d2, n1);
1466 propagate_flags_upward(m1, d1, n2);
1469 if (d1->root == n1) {
1473 if (d2->root == n2) {
1477 if (n1_held_focus) {
1478 d1->focus = n2_held_focus ? last_d2_focus : n2;
1481 if (n2_held_focus) {
1482 d2->focus = n1_held_focus ? last_d1_focus : n1;
1486 adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1487 adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1490 ewmh_set_wm_desktop(n1, d2);
1491 ewmh_set_wm_desktop(n2, d1);
1493 history_remove(d1, n1, true);
1494 history_remove(d2, n2, true);
1496 bool d1_was_focused = (d1 == mon->desk);
1497 bool d2_was_focused = (d2 == mon->desk);
1499 if (m1->desk != d1 && m2->desk == d2) {
1501 if (!follow || !d2_was_focused || !n2_held_focus) {
1504 } else if (m1->desk == d1 && m2->desk != d2) {
1505 if (!follow || !d1_was_focused || !n1_held_focus) {
1511 if (single_monocle) {
1512 layout_t l1 = tiled_count(d1->root, true) <= 1 ? LAYOUT_MONOCLE : d1->user_layout;
1513 layout_t l2 = tiled_count(d2->root, true) <= 1 ? LAYOUT_MONOCLE : d2->user_layout;
1514 set_layout(m1, d1, l1, false);
1515 set_layout(m2, d2, l2, false);
1518 if (n1_held_focus) {
1519 if (d1_was_focused) {
1521 focus_node(m2, d2, last_d1_focus);
1523 focus_node(m1, d1, d1->focus);
1526 activate_node(m1, d1, d1->focus);
1529 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1532 if (n2_held_focus) {
1533 if (d2_was_focused) {
1535 focus_node(m1, d1, last_d2_focus);
1537 focus_node(m2, d2, d2->focus);
1540 activate_node(m2, d2, d2->focus);
1543 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1546 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1547 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1559 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd, bool follow)
1561 if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1565 if (sticky_still && ms->sticky_count > 0 && sticky_count(ns) > 0 && dd != md->desk) {
1569 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);
1571 bool held_focus = is_descendant(ds->focus, ns);
1572 /* avoid ending up with a dangling pointer (because of unlink_node) */
1573 node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1574 bool ds_was_focused = (ds == mon->desk);
1576 if (held_focus && ds_was_focused) {
1577 clear_input_focus();
1580 unlink_node(ms, ds, ns);
1581 insert_node(md, dd, ns, nd);
1584 if (ns->client == NULL || monitor_from_client(ns->client) != md) {
1585 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1590 ewmh_set_wm_desktop(ns, dd);
1592 if (ds == ms->desk && dd != md->desk) {
1594 } else if (ds != ms->desk && dd == md->desk) {
1600 history_remove(ds, ns, true);
1601 stack(dd, ns, false);
1605 if (ds_was_focused) {
1606 focus_node(ms, ds, last_ds_focus);
1608 activate_node(ms, ds, last_ds_focus);
1611 draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1614 if (single_monocle) {
1615 if (ds->layout != LAYOUT_MONOCLE && tiled_count(ds->root, true) <= 1) {
1616 set_layout(ms, ds, LAYOUT_MONOCLE, false);
1618 if (dd->layout == LAYOUT_MONOCLE && tiled_count(dd->root, true) > 1) {
1619 set_layout(md, dd, dd->user_layout, false);
1624 if (ds_was_focused) {
1625 focus_node(md, dd, last_ds_focus);
1627 activate_node(ms, ds, ds->focus);
1629 if (ds_was_focused) {
1630 focus_node(ms, ds, ds->focus);
1632 activate_node(ms, ds, ds->focus);
1636 if (!held_focus || !follow || !ds_was_focused) {
1637 if (dd->focus == ns) {
1638 if (dd == mon->desk) {
1639 focus_node(md, dd, held_focus ? last_ds_focus : ns);
1641 activate_node(md, dd, held_focus ? last_ds_focus : ns);
1644 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1658 bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t *sel)
1660 monitor_t *m = ref->monitor;
1661 desktop_t *d = ref->desktop;
1662 node_t *n = ref->node;
1663 n = (dir == CYCLE_PREV ? prev_node(n) : next_node(n));
1665 #define HANDLE_BOUNDARIES(m, d, n) \
1666 while (n == NULL) { \
1667 d = (dir == CYCLE_PREV ? d->prev : d->next); \
1669 m = (dir == CYCLE_PREV ? m->prev : m->next); \
1671 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
1673 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
1675 n = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
1676 if (ref->node == NULL && d == ref->desktop) { \
1680 HANDLE_BOUNDARIES(m, d, n);
1682 while (n != ref->node) {
1683 coordinates_t loc = {m, d, n};
1684 if (node_matches(&loc, ref, sel)) {
1688 n = (dir == CYCLE_PREV ? prev_node(n) : next_node(n));
1689 HANDLE_BOUNDARIES(m, d, n);
1690 if (ref->node == NULL && d == ref->desktop) {
1694 #undef HANDLE_BOUNDARIES
1698 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1700 if (tiled_count(n, false) < 2) {
1703 node_t *p = d->focus->parent;
1704 bool focus_first_child = is_first_child(d->focus);
1705 if (dir == CIRCULATE_FORWARD) {
1706 node_t *e = second_extrema(n);
1707 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1708 e = prev_leaf(e, n);
1710 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)) {
1711 swap_nodes(m, d, f, m, d, s, false);
1714 node_t *e = first_extrema(n);
1715 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1716 e = next_leaf(e, n);
1718 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)) {
1719 swap_nodes(m, d, f, m, d, s, false);
1723 node_t *f = focus_first_child ? p->first_child : p->second_child;
1725 if (d == mon->desk) {
1726 focus_node(m, d, f);
1728 activate_node(m, d, f);
1734 void set_vacant(monitor_t *m, desktop_t *d, node_t *n, bool value)
1736 if (n->vacant == value) {
1740 propagate_vacant_downward(m, d, n, value);
1741 propagate_vacant_upward(m, d, n);
1744 void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1746 if (n->vacant == value) {
1753 cancel_presel(m, d, n);
1757 void propagate_vacant_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1763 set_vacant_local(m, d, n, value);
1765 propagate_vacant_downward(m, d, n->first_child, value);
1766 propagate_vacant_downward(m, d, n->second_child, value);
1769 void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
1775 node_t *p = n->parent;
1778 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1781 propagate_vacant_upward(m, d, p);
1784 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1786 if (n == NULL || n->client == NULL || n->client->layer == l) {
1790 n->client->last_layer = n->client->layer;
1791 n->client->layer = l;
1793 if (l == LAYER_ABOVE) {
1794 n->client->wm_flags |= WM_FLAG_ABOVE;
1795 n->client->wm_flags &= ~WM_FLAG_BELOW;
1796 } else if (l == LAYER_BELOW) {
1797 n->client->wm_flags |= WM_FLAG_BELOW;
1798 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1800 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1803 ewmh_wm_state_update(n);
1805 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));
1807 if (d->focus == n) {
1808 neutralize_occluding_windows(m, d, n);
1811 stack(d, n, (d->focus == n));
1816 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1818 if (n == NULL || n->client == NULL || n->client->state == s) {
1822 client_t *c = n->client;
1824 bool was_tiled = IS_TILED(c);
1826 c->last_state = c->state;
1829 switch (c->last_state) {
1831 case STATE_PSEUDO_TILED:
1833 case STATE_FLOATING:
1834 set_floating(m, d, n, false);
1836 case STATE_FULLSCREEN:
1837 set_fullscreen(m, d, n, false);
1841 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));
1845 case STATE_PSEUDO_TILED:
1847 case STATE_FLOATING:
1848 set_floating(m, d, n, true);
1850 case STATE_FULLSCREEN:
1851 set_fullscreen(m, d, n, true);
1855 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));
1857 if (n == m->desk->focus) {
1858 put_status(SBSC_MASK_REPORT);
1861 if (single_monocle && was_tiled != IS_TILED(c)) {
1862 if (was_tiled && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
1863 set_layout(m, d, LAYOUT_MONOCLE, false);
1864 } else if (!was_tiled && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
1865 set_layout(m, d, d->user_layout, false);
1872 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1878 cancel_presel(m, d, n);
1880 set_vacant(m, d, n, value);
1883 if (!value && d->focus == n) {
1884 neutralize_occluding_windows(m, d, n);
1887 stack(d, n, (d->focus == n));
1890 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1896 client_t *c = n->client;
1898 cancel_presel(m, d, n);
1900 set_vacant(m, d, n, value);
1904 c->wm_flags |= WM_FLAG_FULLSCREEN;
1906 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1907 if (d->focus == n) {
1908 neutralize_occluding_windows(m, d, n);
1912 ewmh_wm_state_update(n);
1913 stack(d, n, (d->focus == n));
1916 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1918 bool changed = false;
1919 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1920 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1921 if (a != f && a->client != NULL && f->client != NULL &&
1922 IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1923 set_state(m, d, a, a->client->last_state);
1933 void rebuild_constraints(node_t *n)
1935 if (n == NULL || is_leaf(n)) {
1938 rebuild_constraints(n->first_child);
1939 rebuild_constraints(n->second_child);
1940 update_constraints(n);
1944 void update_constraints(node_t *n)
1946 if (n == NULL || is_leaf(n)) {
1949 if (n->split_type == TYPE_VERTICAL) {
1950 n->constraints.min_width = n->first_child->constraints.min_width + n->second_child->constraints.min_width;
1951 n->constraints.min_height = MAX(n->first_child->constraints.min_height, n->second_child->constraints.min_height);
1953 n->constraints.min_width = MAX(n->first_child->constraints.min_width, n->second_child->constraints.min_width);
1954 n->constraints.min_height = n->first_child->constraints.min_height + n->second_child->constraints.min_height;
1958 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
1964 node_t *p = n->parent;
1967 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1968 set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
1969 update_constraints(p);
1972 propagate_flags_upward(m, d, p);
1975 void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
1977 if (n == NULL || n->hidden == value) {
1981 bool held_focus = is_descendant(d->focus, n);
1983 propagate_hidden_downward(m, d, n, value);
1984 propagate_hidden_upward(m, d, n);
1986 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));
1988 if (held_focus || d->focus == NULL) {
1989 if (d->focus != NULL) {
1991 draw_border(n, false, (mon == m));
1993 if (d == mon->desk) {
1994 focus_node(m, d, d->focus);
1996 activate_node(m, d, d->focus);
2000 if (single_monocle) {
2001 if (value && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
2002 set_layout(m, d, LAYOUT_MONOCLE, false);
2003 } else if (!value && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
2004 set_layout(m, d, d->user_layout, false);
2009 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
2011 if (n->hidden == value) {
2017 if (n->client != NULL) {
2018 if (n->client->shown) {
2019 window_set_visibility(n->id, !value);
2022 if (IS_TILED(n->client)) {
2023 set_vacant(m, d, n, value);
2027 n->client->wm_flags |= WM_FLAG_HIDDEN;
2029 n->client->wm_flags &= ~WM_FLAG_HIDDEN;
2032 ewmh_wm_state_update(n);
2036 void propagate_hidden_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
2042 set_hidden_local(m, d, n, value);
2044 propagate_hidden_downward(m, d, n->first_child, value);
2045 propagate_hidden_downward(m, d, n->second_child, value);
2048 void propagate_hidden_upward(monitor_t *m, desktop_t *d, node_t *n)
2054 node_t *p = n->parent;
2057 set_hidden_local(m, d, p, p->first_child->hidden && p->second_child->hidden);
2060 propagate_hidden_upward(m, d, p);
2063 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
2065 if (n == NULL || n->sticky == value) {
2070 transfer_node(m, d, n, m, m->desk, m->desk->focus, false);
2081 if (n->client != NULL) {
2083 n->client->wm_flags |= WM_FLAG_STICKY;
2085 n->client->wm_flags &= ~WM_FLAG_STICKY;
2087 ewmh_wm_state_update(n);
2090 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));
2092 if (n == m->desk->focus) {
2093 put_status(SBSC_MASK_REPORT);
2097 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
2099 if (n == NULL || n->private == value) {
2105 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));
2107 if (n == m->desk->focus) {
2108 put_status(SBSC_MASK_REPORT);
2112 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2114 if (n == NULL || n->locked == value) {
2120 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));
2122 if (n == m->desk->focus) {
2123 put_status(SBSC_MASK_REPORT);
2127 void set_marked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2129 if (n == NULL || n->marked == value) {
2135 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));
2137 if (n == m->desk->focus) {
2138 put_status(SBSC_MASK_REPORT);
2142 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
2144 if (value && mon->desk->focus == n) {
2148 n->client->urgent = value;
2151 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
2153 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
2156 ewmh_wm_state_update(n);
2158 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));
2159 put_status(SBSC_MASK_REPORT);
2162 xcb_rectangle_t get_rectangle(monitor_t *m, desktop_t *d, node_t *n)
2165 return m->rectangle;
2167 client_t *c = n->client;
2169 if (IS_FLOATING(c)) {
2170 return c->floating_rectangle;
2172 return c->tiled_rectangle;
2175 int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
2176 xcb_rectangle_t rect = n->rectangle;
2183 void listen_enter_notify(node_t *n, bool enable)
2185 uint32_t mask = CLIENT_EVENT_MASK | (enable ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
2186 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
2187 if (f->client == NULL) {
2190 xcb_change_window_attributes(dpy, f->id, XCB_CW_EVENT_MASK, &mask);
2191 if (f->presel != NULL) {
2192 xcb_change_window_attributes(dpy, f->presel->feedback, XCB_CW_EVENT_MASK, &mask);
2197 void regenerate_ids_in(node_t *n)
2199 if (n == NULL || n->client != NULL) {
2202 n->id = xcb_generate_id(dpy);
2203 regenerate_ids_in(n->first_child);
2204 regenerate_ids_in(n->second_child);
2207 #define DEF_FLAG_COUNT(flag) \
2208 unsigned int flag##_count(node_t *n) \
2213 return ((n->flag ? 1 : 0) + \
2214 flag##_count(n->first_child) + \
2215 flag##_count(n->second_child)); \
2218 DEF_FLAG_COUNT(sticky)
2219 DEF_FLAG_COUNT(private)
2220 DEF_FLAG_COUNT(locked)
2221 #undef DEF_FLAG_COUNT