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);
88 if (n->client == NULL) {
93 bool the_only_window = !m->prev && !m->next && d->root->client;
94 if ((borderless_monocle && d->layout == LAYOUT_MONOCLE && IS_TILED(n->client))
95 || (borderless_singleton && the_only_window)
96 || n->client->state == STATE_FULLSCREEN) {
99 bw = n->client->border_width;
103 xcb_rectangle_t cr = get_window_rectangle(n);
104 client_state_t s = n->client->state;
105 /* tiled and pseudo-tiled clients */
106 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
107 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
109 int bleed = wg + 2 * bw;
110 r.width = (bleed < r.width ? r.width - bleed : 1);
111 r.height = (bleed < r.height ? r.height - bleed : 1);
112 /* pseudo-tiled clients */
113 if (s == STATE_PSEUDO_TILED) {
114 xcb_rectangle_t f = n->client->floating_rectangle;
115 r.width = MIN(r.width, f.width);
116 r.height = MIN(r.height, f.height);
117 if (center_pseudo_tiled) {
118 r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
119 r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
122 n->client->tiled_rectangle = r;
123 if (!gapless_monocle || 1 != LAYOUT_MONOCLE)
124 br = n->client->border_radius;
125 /* floating clients */
126 } else if (s == STATE_FLOATING) {
127 r = n->client->floating_rectangle;
128 br = n->client->border_radius;
129 /* fullscreen clients */
132 n->client->tiled_rectangle = r;
135 apply_size_hints(n->client, &r.width, &r.height);
137 if (!rect_eq(r, cr)) {
138 window_move_resize(n->id, r.x, r.y, r.width, r.height);
139 window_rounded_border(n);
141 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);
145 window_border_width(n->id, bw);
146 window_border_radius(n->client, br);
147 window_rounded_border(n);
150 xcb_rectangle_t first_rect;
151 xcb_rectangle_t second_rect;
153 if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
154 first_rect = second_rect = rect;
157 if (n->split_type == TYPE_VERTICAL) {
158 fence = rect.width * n->split_ratio;
159 if ((n->first_child->constraints.min_width + n->second_child->constraints.min_width) <= rect.width) {
160 if (fence < n->first_child->constraints.min_width) {
161 fence = n->first_child->constraints.min_width;
162 n->split_ratio = (double) fence / (double) rect.width;
163 } else if (fence > (uint16_t) (rect.width - n->second_child->constraints.min_width)) {
164 fence = (rect.width - n->second_child->constraints.min_width);
165 n->split_ratio = (double) fence / (double) rect.width;
168 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
169 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
171 fence = rect.height * n->split_ratio;
172 if ((n->first_child->constraints.min_height + n->second_child->constraints.min_height) <= rect.height) {
173 if (fence < n->first_child->constraints.min_height) {
174 fence = n->first_child->constraints.min_height;
175 n->split_ratio = (double) fence / (double) rect.height;
176 } else if (fence > (uint16_t) (rect.height - n->second_child->constraints.min_height)) {
177 fence = (rect.height - n->second_child->constraints.min_height);
178 n->split_ratio = (double) fence / (double) rect.height;
181 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
182 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
186 apply_layout(m, d, n->first_child, first_rect, root_rect);
187 apply_layout(m, d, n->second_child, second_rect, root_rect);
191 presel_t *make_presel(void)
193 presel_t *p = calloc(1, sizeof(presel_t));
194 p->split_dir = DIR_EAST;
195 p->split_ratio = split_ratio;
196 p->feedback = XCB_NONE;
200 void set_type(node_t *n, split_type_t typ)
207 update_constraints(n);
208 rebuild_constraints_towards_root(n);
211 void set_ratio(node_t *n, double rat)
217 n->split_ratio = rat;
220 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
222 if (n->presel == NULL) {
223 n->presel = make_presel();
226 n->presel->split_dir = dir;
228 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));
231 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
233 if (n->presel == NULL) {
234 n->presel = make_presel();
237 n->presel->split_ratio = ratio;
239 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
242 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
244 if (n->presel == NULL) {
248 if (n->presel->feedback != XCB_NONE) {
249 xcb_destroy_window(dpy, n->presel->feedback);
255 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
258 void cancel_presel_in(monitor_t *m, desktop_t *d, node_t *n)
263 cancel_presel(m, d, n);
264 cancel_presel_in(m, d, n->first_child);
265 cancel_presel_in(m, d, n->second_child);
268 node_t *find_public(desktop_t *d)
270 unsigned int b_manual_area = 0;
271 unsigned int b_automatic_area = 0;
272 node_t *b_manual = NULL;
273 node_t *b_automatic = NULL;
274 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
278 unsigned int n_area = node_area(d, n);
279 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
281 b_manual_area = n_area;
283 if (n_area > b_automatic_area &&
284 n->presel == NULL && !n->private && private_count(n->parent) == 0) {
286 b_automatic_area = n_area;
289 if (b_automatic != NULL) {
296 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
298 if (d == NULL || n == NULL) {
302 /* n: inserted node */
303 /* c: new internal node */
304 /* f: focus or insertion anchor */
305 /* p: parent of focus */
306 /* g: grand parent of focus */
314 } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
315 node_t *p = f->parent;
317 if (is_first_child(f)) {
329 node_t *c = make_node(XCB_NONE);
330 node_t *p = f->parent;
331 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
332 node_t *k = find_public(d);
337 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
338 xcb_rectangle_t rect = get_rectangle(m, d, f);
339 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
343 if (f->presel == NULL) {
344 bool single_tiled = f->client != NULL && IS_TILED(f->client) && tiled_count(d->root, true) == 1;
345 if (p == NULL || automatic_scheme != SCHEME_SPIRAL || single_tiled) {
347 if (is_first_child(f)) {
357 if (initial_polarity == FIRST_CHILD) {
364 if (p == NULL || automatic_scheme == SCHEME_LONGEST_SIDE || single_tiled) {
365 if (f->rectangle.width > f->rectangle.height) {
366 c->split_type = TYPE_VERTICAL;
368 c->split_type = TYPE_HORIZONTAL;
372 while (q != NULL && (q->first_child->vacant || q->second_child->vacant)) {
378 if (q->split_type == TYPE_HORIZONTAL) {
379 c->split_type = TYPE_VERTICAL;
381 c->split_type = TYPE_HORIZONTAL;
385 node_t *g = p->parent;
388 if (is_first_child(p)) {
396 c->split_type = p->split_type;
397 c->split_ratio = p->split_ratio;
400 if (is_first_child(f)) {
415 if (is_first_child(f)) {
421 c->split_ratio = f->presel->split_ratio;
424 switch (f->presel->split_dir) {
426 c->split_type = TYPE_VERTICAL;
431 c->split_type = TYPE_VERTICAL;
436 c->split_type = TYPE_HORIZONTAL;
441 c->split_type = TYPE_HORIZONTAL;
449 cancel_presel(m, d, f);
450 set_marked(m, d, n, false);
454 propagate_flags_upward(m, d, n);
456 if (d->focus == NULL && is_focusable(n)) {
463 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
465 node_t *r = make_node(XCB_NONE);
466 insert_node(m, d, r, n);
467 put_status(SBSC_MASK_NODE_ADD, "node_add 0x%08X 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n != NULL ? n->id : 0, r->id);
469 if (single_monocle && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
470 set_layout(m, d, d->user_layout, false);
474 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
476 if (n == NULL && d->root != NULL) {
479 n = history_last_node(d, NULL);
482 n = first_focusable_leaf(d->root);
486 if (d == mon->desk || (n != NULL && !is_focusable(n))) {
491 if (d->focus != NULL && n != d->focus) {
492 neutralize_occluding_windows(m, d, n);
496 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
497 if (f->client != NULL && !is_descendant(f, n)) {
498 window_draw_border(f->id, get_border_color(false, (m == mon)));
502 draw_border(n, true, (m == mon));
506 history_add(m, d, n, false);
508 put_status(SBSC_MASK_REPORT);
514 put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
519 void transfer_sticky_nodes(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
523 } else if (n->sticky) {
524 sticky_still = false;
525 transfer_node(ms, ds, n, md, dd, dd->focus, false);
528 /* we need references to the children because n might be freed after
529 * the first recursive call */
530 node_t *first_child = n->first_child;
531 node_t *second_child = n->second_child;
532 transfer_sticky_nodes(ms, ds, md, dd, first_child);
533 transfer_sticky_nodes(ms, ds, md, dd, second_child);
537 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
542 m = history_last_monitor(NULL);
556 d = history_last_desktop(m, NULL);
567 bool guess = (n == NULL);
569 if (n == NULL && d->root != NULL) {
572 n = history_last_node(d, NULL);
575 n = first_focusable_leaf(d->root);
579 if (n != NULL && !is_focusable(n)) {
583 if ((mon != NULL && mon->desk != d) || n == NULL || n->client == NULL) {
587 if (m->sticky_count > 0 && m->desk != NULL && d != m->desk) {
588 if (guess && m->desk->focus != NULL && m->desk->focus->sticky) {
592 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
594 if (n == NULL && d->focus != NULL) {
599 if (d->focus != NULL && n != d->focus) {
600 neutralize_occluding_windows(m, d, n);
603 if (n != NULL && n->client != NULL && n->client->urgent) {
604 set_urgent(m, d, n, false);
609 for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
610 draw_border(e->focus, true, false);
613 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
617 draw_border(e->focus, true, true);
622 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
623 if (f->client != NULL && !is_descendant(f, n)) {
624 window_draw_border(f->id, get_border_color(false, true));
629 draw_border(n, true, true);
631 bool desk_changed = (m != mon || m->desk != d);
632 bool has_input_focus = false;
637 if (pointer_follows_monitor) {
638 center_pointer(m->rectangle);
641 put_status(SBSC_MASK_MONITOR_FOCUS, "monitor_focus 0x%08X\n", m->id);
647 has_input_focus = true;
648 hide_desktop(m->desk);
653 ewmh_update_current_desktop();
654 put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus 0x%08X 0x%08X\n", m->id, d->id);
658 if (!has_input_focus) {
661 ewmh_update_active_window();
662 history_add(m, d, n, true);
664 put_status(SBSC_MASK_REPORT);
667 if (focus_follows_pointer) {
668 update_motion_recorder();
673 put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
677 if (pointer_follows_focus) {
678 center_pointer(get_rectangle(m, d, n));
679 } else if (focus_follows_pointer) {
680 update_motion_recorder();
686 void hide_node(desktop_t *d, node_t *n)
688 if (n == NULL || (!hide_sticky && n->sticky)) {
692 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
693 window_hide(n->presel->feedback);
695 if (n->client != NULL) {
699 if (n->client != NULL) {
700 n->client->shown = false;
702 hide_node(d, n->first_child);
703 hide_node(d, n->second_child);
707 void show_node(desktop_t *d, node_t *n)
713 if (n->client != NULL) {
716 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
717 window_show(n->presel->feedback);
720 if (n->client != NULL) {
721 n->client->shown = true;
723 show_node(d, n->first_child);
724 show_node(d, n->second_child);
728 node_t *make_node(uint32_t id)
730 if (id == XCB_NONE) {
731 id = xcb_generate_id(dpy);
733 node_t *n = calloc(1, sizeof(node_t));
735 n->parent = n->first_child = n->second_child = NULL;
736 n->vacant = n->hidden = n->sticky = n->private = n->locked = n->marked = false;
737 n->split_ratio = split_ratio;
738 n->split_type = TYPE_VERTICAL;
739 n->constraints = (constraints_t) {MIN_WIDTH, MIN_HEIGHT};
745 client_t *make_client(void)
747 client_t *c = calloc(1, sizeof(client_t));
748 c->state = c->last_state = STATE_TILED;
749 c->layer = c->last_layer = LAYER_NORMAL;
750 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
751 snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
752 c->border_width = border_width;
753 c->border_radius = border_radius;
756 c->sets_own_shape = false;
758 c->icccm_props.input_hint = true;
759 c->icccm_props.take_focus = false;
760 c->icccm_props.delete_window = false;
761 c->size_hints.flags = 0;
765 void initialize_client(node_t *n)
767 xcb_window_t win = n->id;
768 client_t *c = n->client;
769 xcb_icccm_get_wm_protocols_reply_t protos;
770 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
771 for (uint32_t i = 0; i < protos.atoms_len; i++) {
772 if (protos.atoms[i] == WM_TAKE_FOCUS) {
773 c->icccm_props.take_focus = true;
774 } else if (protos.atoms[i] == WM_DELETE_WINDOW) {
775 c->icccm_props.delete_window = true;
778 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
780 xcb_ewmh_get_atoms_reply_t wm_state;
781 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
782 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
783 #define HANDLE_WM_STATE(s) \
784 if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
785 c->wm_flags |= WM_FLAG_##s; continue; \
787 HANDLE_WM_STATE(MODAL)
788 HANDLE_WM_STATE(STICKY)
789 HANDLE_WM_STATE(MAXIMIZED_VERT)
790 HANDLE_WM_STATE(MAXIMIZED_HORZ)
791 HANDLE_WM_STATE(SHADED)
792 HANDLE_WM_STATE(SKIP_TASKBAR)
793 HANDLE_WM_STATE(SKIP_PAGER)
794 HANDLE_WM_STATE(HIDDEN)
795 HANDLE_WM_STATE(FULLSCREEN)
796 HANDLE_WM_STATE(ABOVE)
797 HANDLE_WM_STATE(BELOW)
798 HANDLE_WM_STATE(DEMANDS_ATTENTION)
799 #undef HANDLE_WM_STATE
801 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
803 xcb_icccm_wm_hints_t hints;
804 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
805 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
806 c->icccm_props.input_hint = hints.input;
808 xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
811 bool is_focusable(node_t *n)
813 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
814 if (f->client != NULL && !f->hidden) {
821 bool is_leaf(node_t *n)
823 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
826 bool is_first_child(node_t *n)
828 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
831 bool is_second_child(node_t *n)
833 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
836 unsigned int clients_count_in(node_t *n)
841 return (n->client != NULL ? 1 : 0) +
842 clients_count_in(n->first_child) +
843 clients_count_in(n->second_child);
847 node_t *brother_tree(node_t *n)
849 if (n == NULL || n->parent == NULL) {
852 if (is_first_child(n)) {
853 return n->parent->second_child;
855 return n->parent->first_child;
859 node_t *first_extrema(node_t *n)
863 } else if (n->first_child == NULL) {
866 return first_extrema(n->first_child);
870 node_t *second_extrema(node_t *n)
874 } else if (n->second_child == NULL) {
877 return second_extrema(n->second_child);
881 node_t *first_focusable_leaf(node_t *n)
883 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
884 if (f->client != NULL && !f->hidden) {
891 node_t *next_node(node_t *n)
897 if (n->second_child != NULL) {
898 return first_extrema(n->second_child);
901 while (is_second_child(p)) {
904 if (is_first_child(p)) {
912 node_t *prev_node(node_t *n)
918 if (n->first_child != NULL) {
919 return second_extrema(n->first_child);
922 while (is_first_child(p)) {
925 if (is_second_child(p)) {
933 node_t *next_leaf(node_t *n, node_t *r)
939 while (is_second_child(p) && p != r) {
945 return first_extrema(p->parent->second_child);
948 node_t *prev_leaf(node_t *n, node_t *r)
954 while (is_first_child(p) && p != r) {
960 return second_extrema(p->parent->first_child);
963 node_t *next_tiled_leaf(node_t *n, node_t *r)
965 node_t *next = next_leaf(n, r);
966 if (next == NULL || (next->client != NULL && !next->vacant)) {
969 return next_tiled_leaf(next, r);
973 node_t *prev_tiled_leaf(node_t *n, node_t *r)
975 node_t *prev = prev_leaf(n, r);
976 if (prev == NULL || (prev->client != NULL && !prev->vacant)) {
979 return prev_tiled_leaf(prev, r);
983 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
984 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
988 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
991 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
994 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
997 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
1003 node_t *find_fence(node_t *n, direction_t dir)
1014 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
1015 (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
1016 (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
1017 (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
1025 /* returns *true* if *a* is a child of *b* */
1026 bool is_child(node_t *a, node_t *b)
1028 if (a == NULL || b == NULL) {
1031 return (a->parent != NULL && a->parent == b);
1034 /* returns *true* if *a* is a descendant of *b* */
1035 bool is_descendant(node_t *a, node_t *b)
1037 if (a == NULL || b == NULL) {
1040 while (a != b && a != NULL) {
1046 bool find_by_id(uint32_t id, coordinates_t *loc)
1048 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1049 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1050 node_t *n = find_by_id_in(d->root, id);
1062 node_t *find_by_id_in(node_t *r, uint32_t id)
1066 } else if (r->id == id) {
1069 node_t *f = find_by_id_in(r->first_child, id);
1073 return find_by_id_in(r->second_child, id);
1078 void find_any_node(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1080 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1081 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1082 if (find_any_node_in(m, d, d->root, ref, dst, sel)) {
1089 bool find_any_node_in(monitor_t *m, desktop_t *d, node_t *n, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1094 coordinates_t loc = {m, d, n};
1095 if (node_matches(&loc, ref, sel)) {
1099 if (find_any_node_in(m, d, n->first_child, ref, dst, sel)) {
1102 return find_any_node_in(m, d, n->second_child, ref, dst, sel);
1108 void find_first_ancestor(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1110 if (ref->node == NULL) {
1114 coordinates_t loc = {ref->monitor, ref->desktop, ref->node};
1115 while ((loc.node = loc.node->parent) != NULL) {
1116 if (node_matches(&loc, ref, sel)) {
1123 /* Based on https://github.com/ntrrgc/right-window */
1124 void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t *sel)
1126 xcb_rectangle_t rect = get_rectangle(ref->monitor, ref->desktop, ref->node);
1127 uint32_t md = UINT32_MAX, mr = UINT32_MAX;
1129 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1130 desktop_t *d = m->desk;
1131 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1132 coordinates_t loc = {m, d, f};
1133 xcb_rectangle_t r = get_rectangle(m, d, f);
1134 if (f == ref->node ||
1135 f->client == NULL ||
1137 is_descendant(f, ref->node) ||
1138 !node_matches(&loc, ref, sel) ||
1139 !on_dir_side(rect, r, dir)) {
1142 uint32_t fd = boundary_distance(rect, r, dir);
1143 uint32_t fr = history_rank(f);
1144 if (fd < md || (fd == md && fr < mr)) {
1153 unsigned int node_area(desktop_t *d, node_t *n)
1158 return area(get_rectangle(NULL, d, n));
1161 int tiled_count(node_t *n, bool include_receptacles)
1167 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1168 if (!f->hidden && ((include_receptacles && f->client == NULL) ||
1169 (f->client != NULL && IS_TILED(f->client)))) {
1176 void find_by_area(area_peak_t ap, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1178 unsigned int p_area;
1179 if (ap == AREA_BIGGEST) {
1185 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1186 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1187 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1188 coordinates_t loc = {m, d, f};
1189 if (f->vacant || !node_matches(&loc, ref, sel)) {
1192 unsigned int f_area = node_area(d, f);
1193 if ((ap == AREA_BIGGEST && f_area > p_area) || (ap == AREA_SMALLEST && f_area < p_area)) {
1202 void rotate_tree(node_t *n, int deg)
1204 rotate_tree_rec(n, deg);
1205 rebuild_constraints_from_leaves(n);
1206 rebuild_constraints_towards_root(n);
1209 void rotate_tree_rec(node_t *n, int deg)
1211 if (n == NULL || is_leaf(n) || deg == 0) {
1217 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1218 (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1220 tmp = n->first_child;
1221 n->first_child = n->second_child;
1222 n->second_child = tmp;
1223 n->split_ratio = 1.0 - n->split_ratio;
1227 if (n->split_type == TYPE_HORIZONTAL) {
1228 n->split_type = TYPE_VERTICAL;
1229 } else if (n->split_type == TYPE_VERTICAL) {
1230 n->split_type = TYPE_HORIZONTAL;
1234 rotate_tree_rec(n->first_child, deg);
1235 rotate_tree_rec(n->second_child, deg);
1238 void flip_tree(node_t *n, flip_t flp)
1240 if (n == NULL || is_leaf(n)) {
1246 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1247 (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1248 tmp = n->first_child;
1249 n->first_child = n->second_child;
1250 n->second_child = tmp;
1251 n->split_ratio = 1.0 - n->split_ratio;
1254 flip_tree(n->first_child, flp);
1255 flip_tree(n->second_child, flp);
1258 void equalize_tree(node_t *n)
1260 if (n == NULL || n->vacant) {
1263 n->split_ratio = split_ratio;
1264 equalize_tree(n->first_child);
1265 equalize_tree(n->second_child);
1269 int balance_tree(node_t *n)
1271 if (n == NULL || n->vacant) {
1273 } else if (is_leaf(n)) {
1276 int b1 = balance_tree(n->first_child);
1277 int b2 = balance_tree(n->second_child);
1279 if (b1 > 0 && b2 > 0) {
1280 n->split_ratio = (double) b1 / b;
1286 /* Adjust the split ratios so that they keep their position
1287 * despite the potential alteration of their rectangle. */
1288 void adjust_ratios(node_t *n, xcb_rectangle_t rect)
1296 if (n->split_type == TYPE_VERTICAL) {
1297 double position = (double) n->rectangle.x + n->split_ratio * (double) n->rectangle.width;
1298 ratio = (position - (double) rect.x) / (double) rect.width;
1300 double position = (double) n->rectangle.y + n->split_ratio * (double) n->rectangle.height;
1301 ratio = (position - (double) rect.y) / (double) rect.height;
1304 ratio = MAX(0.0, ratio);
1305 ratio = MIN(1.0, ratio);
1306 n->split_ratio = ratio;
1308 xcb_rectangle_t first_rect;
1309 xcb_rectangle_t second_rect;
1312 if (n->split_type == TYPE_VERTICAL) {
1313 fence = rect.width * n->split_ratio;
1314 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
1315 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
1317 fence = rect.height * n->split_ratio;
1318 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
1319 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
1322 adjust_ratios(n->first_child, first_rect);
1323 adjust_ratios(n->second_child, second_rect);
1326 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1328 if (d == NULL || n == NULL) {
1332 node_t *p = n->parent;
1338 if (d->focus == p || is_descendant(d->focus, n)) {
1342 history_remove(d, p, false);
1343 cancel_presel(m, d, p);
1349 node_t *b = brother_tree(n);
1350 node_t *g = p->parent;
1355 if (is_first_child(p)) {
1358 g->second_child = b;
1364 if (!n->vacant && removal_adjustment) {
1365 if (automatic_scheme == SCHEME_SPIRAL) {
1366 if (is_first_child(n)) {
1367 rotate_tree(b, 270);
1371 } else if (automatic_scheme == SCHEME_LONGEST_SIDE || g == NULL) {
1373 if (p->rectangle.width > p->rectangle.height) {
1374 b->split_type = TYPE_VERTICAL;
1376 b->split_type = TYPE_HORIZONTAL;
1379 } else if (automatic_scheme == SCHEME_ALTERNATE) {
1380 if (g->split_type == TYPE_HORIZONTAL) {
1381 b->split_type = TYPE_VERTICAL;
1383 b->split_type = TYPE_HORIZONTAL;
1391 propagate_flags_upward(m, d, b);
1395 void close_node(node_t *n)
1399 } else if (n->client != NULL) {
1400 if (n->client->icccm_props.delete_window) {
1401 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1403 xcb_kill_client(dpy, n->id);
1406 close_node(n->first_child);
1407 close_node(n->second_child);
1411 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1417 if (IS_RECEPTACLE(n)) {
1418 put_status(SBSC_MASK_NODE_REMOVE, "node_remove 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
1419 remove_node(m, d, n);
1421 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1422 if (f->client != NULL) {
1423 xcb_kill_client(dpy, f->id);
1429 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1435 unlink_node(m, d, n);
1436 history_remove(d, n, true);
1437 remove_stack_node(n);
1438 cancel_presel_in(m, d, n);
1439 if (m->sticky_count > 0 && d == m->desk) {
1440 m->sticky_count -= sticky_count(n);
1442 clients_count -= clients_count_in(n);
1443 if (is_descendant(grabbed_node, n)) {
1444 grabbed_node = NULL;
1448 if (single_monocle && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
1449 set_layout(m, d, LAYOUT_MONOCLE, false);
1452 ewmh_update_client_list(false);
1453 ewmh_update_client_list(true);
1455 if (mon != NULL && d->focus == NULL) {
1456 if (d == mon->desk) {
1457 focus_node(m, d, NULL);
1459 activate_node(m, d, NULL);
1464 void free_node(node_t *n)
1469 node_t *first_child = n->first_child;
1470 node_t *second_child = n->second_child;
1473 free_node(first_child);
1474 free_node(second_child);
1477 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2, bool follow)
1479 if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1480 (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1481 (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1485 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);
1487 node_t *pn1 = n1->parent;
1488 node_t *pn2 = n2->parent;
1489 bool n1_first_child = is_first_child(n1);
1490 bool n2_first_child = is_first_child(n2);
1491 bool n1_held_focus = is_descendant(d1->focus, n1);
1492 bool n2_held_focus = is_descendant(d2->focus, n2);
1493 node_t *last_d1_focus = d1->focus;
1494 node_t *last_d2_focus = d2->focus;
1497 if (n1_first_child) {
1498 pn1->first_child = n2;
1500 pn1->second_child = n2;
1505 if (n2_first_child) {
1506 pn2->first_child = n1;
1508 pn2->second_child = n1;
1515 propagate_flags_upward(m2, d2, n1);
1516 propagate_flags_upward(m1, d1, n2);
1519 if (d1->root == n1) {
1523 if (d2->root == n2) {
1527 if (n1_held_focus) {
1528 d1->focus = n2_held_focus ? last_d2_focus : n2;
1531 if (n2_held_focus) {
1532 d2->focus = n1_held_focus ? last_d1_focus : n1;
1536 adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1537 adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1540 ewmh_set_wm_desktop(n1, d2);
1541 ewmh_set_wm_desktop(n2, d1);
1543 history_remove(d1, n1, true);
1544 history_remove(d2, n2, true);
1546 bool d1_was_focused = (d1 == mon->desk);
1547 bool d2_was_focused = (d2 == mon->desk);
1549 if (m1->desk != d1 && m2->desk == d2) {
1551 if (!follow || !d2_was_focused || !n2_held_focus) {
1554 } else if (m1->desk == d1 && m2->desk != d2) {
1555 if (!follow || !d1_was_focused || !n1_held_focus) {
1561 if (single_monocle) {
1562 layout_t l1 = tiled_count(d1->root, true) <= 1 ? LAYOUT_MONOCLE : d1->user_layout;
1563 layout_t l2 = tiled_count(d2->root, true) <= 1 ? LAYOUT_MONOCLE : d2->user_layout;
1564 set_layout(m1, d1, l1, false);
1565 set_layout(m2, d2, l2, false);
1568 if (n1_held_focus) {
1569 if (d1_was_focused) {
1571 focus_node(m2, d2, last_d1_focus);
1573 focus_node(m1, d1, d1->focus);
1576 activate_node(m1, d1, d1->focus);
1579 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1582 if (n2_held_focus) {
1583 if (d2_was_focused) {
1585 focus_node(m1, d1, last_d2_focus);
1587 focus_node(m2, d2, d2->focus);
1590 activate_node(m2, d2, d2->focus);
1593 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1596 if (!n1_held_focus) {
1597 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1599 if (!n2_held_focus) {
1600 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1609 if (pointer_follows_focus && (n1_held_focus || n2_held_focus)) {
1610 center_pointer(get_rectangle(m1, d1, d1->focus));
1617 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd, bool follow)
1619 if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1623 unsigned int sc = (ms->sticky_count > 0 && ds == ms->desk) ? sticky_count(ns) : 0;
1624 if (sticky_still && sc > 0 && dd != md->desk) {
1628 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);
1630 bool held_focus = is_descendant(ds->focus, ns);
1631 /* avoid ending up with a dangling pointer (because of unlink_node) */
1632 node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1633 bool ds_was_focused = (ds == mon->desk);
1635 if (held_focus && ds_was_focused) {
1636 clear_input_focus();
1639 unlink_node(ms, ds, ns);
1640 insert_node(md, dd, ns, nd);
1643 if (ns->client == NULL || monitor_from_client(ns->client) != md) {
1644 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1646 ms->sticky_count -= sc;
1647 md->sticky_count += sc;
1651 ewmh_set_wm_desktop(ns, dd);
1653 if (ds == ms->desk && dd != md->desk) {
1655 } else if (ds != ms->desk && dd == md->desk) {
1661 history_remove(ds, ns, true);
1662 stack(dd, ns, false);
1666 if (ds_was_focused) {
1667 focus_node(ms, ds, last_ds_focus);
1669 activate_node(ms, ds, last_ds_focus);
1672 draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1675 if (single_monocle) {
1676 if (ds->layout != LAYOUT_MONOCLE && tiled_count(ds->root, true) <= 1) {
1677 set_layout(ms, ds, LAYOUT_MONOCLE, false);
1679 if (dd->layout == LAYOUT_MONOCLE && tiled_count(dd->root, true) > 1) {
1680 set_layout(md, dd, dd->user_layout, false);
1685 if (ds_was_focused) {
1686 focus_node(md, dd, last_ds_focus);
1688 activate_node(ms, ds, ds->focus);
1690 if (ds_was_focused) {
1691 focus_node(ms, ds, ds->focus);
1693 activate_node(ms, ds, ds->focus);
1697 if (!held_focus || !follow || !ds_was_focused) {
1698 if (dd->focus == ns) {
1699 if (dd == mon->desk) {
1700 focus_node(md, dd, held_focus ? last_ds_focus : ns);
1702 activate_node(md, dd, held_focus ? last_ds_focus : ns);
1705 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1719 bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t *sel)
1721 monitor_t *m = ref->monitor;
1722 desktop_t *d = ref->desktop;
1723 node_t *n = ref->node;
1724 n = (dir == CYCLE_PREV ? prev_node(n) : next_node(n));
1726 #define HANDLE_BOUNDARIES(m, d, n) \
1727 while (n == NULL) { \
1728 d = (dir == CYCLE_PREV ? d->prev : d->next); \
1730 m = (dir == CYCLE_PREV ? m->prev : m->next); \
1732 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
1734 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
1736 n = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
1737 if (ref->node == NULL && d == ref->desktop) { \
1741 HANDLE_BOUNDARIES(m, d, n);
1743 while (n != ref->node) {
1744 coordinates_t loc = {m, d, n};
1745 if (node_matches(&loc, ref, sel)) {
1749 n = (dir == CYCLE_PREV ? prev_node(n) : next_node(n));
1750 HANDLE_BOUNDARIES(m, d, n);
1751 if (ref->node == NULL && d == ref->desktop) {
1755 #undef HANDLE_BOUNDARIES
1759 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1761 if (tiled_count(n, false) < 2) {
1764 node_t *p = d->focus->parent;
1765 bool focus_first_child = is_first_child(d->focus);
1766 if (dir == CIRCULATE_FORWARD) {
1767 node_t *e = second_extrema(n);
1768 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1769 e = prev_leaf(e, n);
1771 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)) {
1772 swap_nodes(m, d, f, m, d, s, false);
1775 node_t *e = first_extrema(n);
1776 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1777 e = next_leaf(e, n);
1779 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)) {
1780 swap_nodes(m, d, f, m, d, s, false);
1784 node_t *f = focus_first_child ? p->first_child : p->second_child;
1786 if (d == mon->desk) {
1787 focus_node(m, d, f);
1789 activate_node(m, d, f);
1795 void set_vacant(monitor_t *m, desktop_t *d, node_t *n, bool value)
1797 if (n->vacant == value) {
1801 propagate_vacant_downward(m, d, n, value);
1802 propagate_vacant_upward(m, d, n);
1805 void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1807 if (n->vacant == value) {
1814 cancel_presel(m, d, n);
1818 void propagate_vacant_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1824 set_vacant_local(m, d, n, value);
1826 propagate_vacant_downward(m, d, n->first_child, value);
1827 propagate_vacant_downward(m, d, n->second_child, value);
1830 void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
1836 node_t *p = n->parent;
1839 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1842 propagate_vacant_upward(m, d, p);
1845 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1847 if (n == NULL || n->client == NULL || n->client->layer == l) {
1851 n->client->last_layer = n->client->layer;
1852 n->client->layer = l;
1854 if (l == LAYER_ABOVE) {
1855 n->client->wm_flags |= WM_FLAG_ABOVE;
1856 n->client->wm_flags &= ~WM_FLAG_BELOW;
1857 } else if (l == LAYER_BELOW) {
1858 n->client->wm_flags |= WM_FLAG_BELOW;
1859 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1861 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1864 ewmh_wm_state_update(n);
1866 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));
1868 if (d->focus == n) {
1869 neutralize_occluding_windows(m, d, n);
1872 stack(d, n, (d->focus == n));
1877 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1879 if (n == NULL || n->client == NULL || n->client->state == s) {
1883 client_t *c = n->client;
1885 bool was_tiled = IS_TILED(c);
1887 c->last_state = c->state;
1890 switch (c->last_state) {
1892 case STATE_PSEUDO_TILED:
1894 case STATE_FLOATING:
1895 set_floating(m, d, n, false);
1897 case STATE_FULLSCREEN:
1898 set_fullscreen(m, d, n, false);
1902 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));
1906 case STATE_PSEUDO_TILED:
1908 case STATE_FLOATING:
1909 set_floating(m, d, n, true);
1911 case STATE_FULLSCREEN:
1912 set_fullscreen(m, d, n, true);
1916 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));
1918 if (n == m->desk->focus) {
1919 put_status(SBSC_MASK_REPORT);
1922 if (single_monocle && was_tiled != IS_TILED(c)) {
1923 if (was_tiled && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
1924 set_layout(m, d, LAYOUT_MONOCLE, false);
1925 } else if (!was_tiled && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
1926 set_layout(m, d, d->user_layout, false);
1933 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1939 cancel_presel(m, d, n);
1941 set_vacant(m, d, n, value);
1944 if (!value && d->focus == n) {
1945 neutralize_occluding_windows(m, d, n);
1948 stack(d, n, (d->focus == n));
1951 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1957 client_t *c = n->client;
1959 cancel_presel(m, d, n);
1961 set_vacant(m, d, n, value);
1965 c->wm_flags |= WM_FLAG_FULLSCREEN;
1967 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1968 if (d->focus == n) {
1969 neutralize_occluding_windows(m, d, n);
1973 ewmh_wm_state_update(n);
1974 stack(d, n, (d->focus == n));
1977 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1979 bool changed = false;
1980 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1981 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1982 if (a != f && a->client != NULL && f->client != NULL &&
1983 IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1984 set_state(m, d, a, a->client->last_state);
1994 void rebuild_constraints_from_leaves(node_t *n)
1996 if (n == NULL || is_leaf(n)) {
1999 rebuild_constraints_from_leaves(n->first_child);
2000 rebuild_constraints_from_leaves(n->second_child);
2001 update_constraints(n);
2005 void rebuild_constraints_towards_root(node_t *n)
2011 node_t *p = n->parent;
2014 update_constraints(p);
2017 rebuild_constraints_towards_root(p);
2020 void update_constraints(node_t *n)
2022 if (n == NULL || is_leaf(n)) {
2025 if (n->split_type == TYPE_VERTICAL) {
2026 n->constraints.min_width = n->first_child->constraints.min_width + n->second_child->constraints.min_width;
2027 n->constraints.min_height = MAX(n->first_child->constraints.min_height, n->second_child->constraints.min_height);
2029 n->constraints.min_width = MAX(n->first_child->constraints.min_width, n->second_child->constraints.min_width);
2030 n->constraints.min_height = n->first_child->constraints.min_height + n->second_child->constraints.min_height;
2034 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
2040 node_t *p = n->parent;
2043 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
2044 set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
2045 update_constraints(p);
2048 propagate_flags_upward(m, d, p);
2051 void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
2053 if (n == NULL || n->hidden == value) {
2057 bool held_focus = is_descendant(d->focus, n);
2059 propagate_hidden_downward(m, d, n, value);
2060 propagate_hidden_upward(m, d, n);
2062 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));
2064 if (held_focus || d->focus == NULL) {
2065 if (d->focus != NULL) {
2067 draw_border(n, false, (mon == m));
2069 if (d == mon->desk) {
2070 focus_node(m, d, d->focus);
2072 activate_node(m, d, d->focus);
2076 if (single_monocle) {
2077 if (value && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
2078 set_layout(m, d, LAYOUT_MONOCLE, false);
2079 } else if (!value && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
2080 set_layout(m, d, d->user_layout, false);
2085 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
2087 if (n->hidden == value) {
2093 if (n->client != NULL) {
2094 if (n->client->shown) {
2095 window_set_visibility(n->id, !value);
2098 if (IS_TILED(n->client)) {
2099 set_vacant(m, d, n, value);
2103 n->client->wm_flags |= WM_FLAG_HIDDEN;
2105 n->client->wm_flags &= ~WM_FLAG_HIDDEN;
2108 ewmh_wm_state_update(n);
2112 void propagate_hidden_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
2118 set_hidden_local(m, d, n, value);
2120 propagate_hidden_downward(m, d, n->first_child, value);
2121 propagate_hidden_downward(m, d, n->second_child, value);
2124 void propagate_hidden_upward(monitor_t *m, desktop_t *d, node_t *n)
2130 node_t *p = n->parent;
2133 set_hidden_local(m, d, p, p->first_child->hidden && p->second_child->hidden);
2136 propagate_hidden_upward(m, d, p);
2139 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
2141 if (n == NULL || n->sticky == value) {
2146 transfer_node(m, d, n, m, m->desk, m->desk->focus, false);
2157 if (n->client != NULL) {
2159 n->client->wm_flags |= WM_FLAG_STICKY;
2161 n->client->wm_flags &= ~WM_FLAG_STICKY;
2163 ewmh_wm_state_update(n);
2166 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));
2168 if (n == m->desk->focus) {
2169 put_status(SBSC_MASK_REPORT);
2173 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
2175 if (n == NULL || n->private == value) {
2181 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));
2183 if (n == m->desk->focus) {
2184 put_status(SBSC_MASK_REPORT);
2188 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2190 if (n == NULL || n->locked == value) {
2196 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));
2198 if (n == m->desk->focus) {
2199 put_status(SBSC_MASK_REPORT);
2203 void set_marked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2205 if (n == NULL || n->marked == value) {
2211 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));
2213 if (n == m->desk->focus) {
2214 put_status(SBSC_MASK_REPORT);
2218 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
2220 if (value && mon->desk->focus == n) {
2224 n->client->urgent = value;
2227 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
2229 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
2232 ewmh_wm_state_update(n);
2234 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));
2235 put_status(SBSC_MASK_REPORT);
2238 xcb_rectangle_t get_rectangle(monitor_t *m, desktop_t *d, node_t *n)
2241 return m->rectangle;
2243 client_t *c = n->client;
2245 if (IS_FLOATING(c)) {
2246 return c->floating_rectangle;
2248 return c->tiled_rectangle;
2251 int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
2252 xcb_rectangle_t rect = n->rectangle;
2259 void listen_enter_notify(node_t *n, bool enable)
2261 uint32_t mask = CLIENT_EVENT_MASK | (enable ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
2262 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
2263 if (f->client == NULL) {
2266 xcb_change_window_attributes(dpy, f->id, XCB_CW_EVENT_MASK, &mask);
2267 if (f->presel != NULL) {
2268 xcb_change_window_attributes(dpy, f->presel->feedback, XCB_CW_EVENT_MASK, &mask);
2273 void regenerate_ids_in(node_t *n)
2275 if (n == NULL || n->client != NULL) {
2278 n->id = xcb_generate_id(dpy);
2279 regenerate_ids_in(n->first_child);
2280 regenerate_ids_in(n->second_child);
2283 #define DEF_FLAG_COUNT(flag) \
2284 unsigned int flag##_count(node_t *n) \
2289 return ((n->flag ? 1 : 0) + \
2290 flag##_count(n->first_child) + \
2291 flag##_count(n->second_child)); \
2294 DEF_FLAG_COUNT(sticky)
2295 DEF_FLAG_COUNT(private)
2296 DEF_FLAG_COUNT(locked)
2297 #undef DEF_FLAG_COUNT