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.
35 #include "subscribe.h"
42 void arrange(monitor_t *m, desktop_t *d)
44 if (d->root == NULL) {
48 layout_t l = d->layout;
50 if (single_monocle && tiled_count(d->root) <= 1) {
54 xcb_rectangle_t rect = m->rectangle;
56 if (!paddingless_monocle || l != LAYOUT_MONOCLE) {
57 rect.x += m->padding.left + d->padding.left;
58 rect.y += m->padding.top + d->padding.top;
59 rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
60 rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
63 if (!gapless_monocle || l != 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 if (focus_follows_pointer) {
71 listen_enter_notify(d->root, false);
74 apply_layout(m, d, d->root, l, rect, rect);
76 if (focus_follows_pointer) {
77 listen_enter_notify(d->root, true);
81 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
89 if (pointer_follows_focus && mon->desk->focus == n) {
90 xcb_rectangle_t r = rect;
91 r.width -= d->window_gap;
92 r.height -= d->window_gap;
96 if (n->presel != NULL) {
97 draw_presel_feedback(m, d, n);
102 if (n->client == NULL) {
107 if ((borderless_monocle && n->client->state == STATE_TILED && l == LAYOUT_MONOCLE)
108 || n->client->state == STATE_FULLSCREEN) {
111 bw = n->client->border_width;
115 xcb_rectangle_t cr = get_window_rectangle(n);
116 client_state_t s = n->client->state;
117 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
118 int wg = (gapless_monocle && l == LAYOUT_MONOCLE ? 0 : d->window_gap);
120 if (s == STATE_TILED) {
122 int bleed = wg + 2 * bw;
123 r.width = (bleed < r.width ? r.width - bleed : 1);
124 r.height = (bleed < r.height ? r.height - bleed : 1);
125 /* pseudo-tiled clients */
127 r = n->client->floating_rectangle;
128 if (center_pseudo_tiled) {
129 r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
130 r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
136 n->client->tiled_rectangle = r;
137 /* floating clients */
138 } else if (s == STATE_FLOATING) {
139 r = n->client->floating_rectangle;
140 /* fullscreen clients */
143 n->client->tiled_rectangle = r;
146 apply_size_hints(n->client, &r.width, &r.height);
148 if (!rect_eq(r, cr)) {
149 window_move_resize(n->id, r.x, r.y, r.width, r.height);
151 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);
155 window_border_width(n->id, bw);
158 xcb_rectangle_t first_rect;
159 xcb_rectangle_t second_rect;
161 if (l == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
162 first_rect = second_rect = rect;
165 if (n->split_type == TYPE_VERTICAL) {
166 fence = rect.width * n->split_ratio;
167 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
168 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
170 fence = rect.height * n->split_ratio;
171 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
172 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
176 apply_layout(m, d, n->first_child, l, first_rect, root_rect);
177 apply_layout(m, d, n->second_child, l, second_rect, root_rect);
181 presel_t *make_presel(void)
183 presel_t *p = calloc(1, sizeof(presel_t));
184 p->split_dir = DIR_EAST;
185 p->split_ratio = split_ratio;
186 p->feedback = XCB_NONE;
190 void set_ratio(node_t *n, double rat)
196 n->split_ratio = rat;
199 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
201 if (n->presel == NULL) {
202 n->presel = make_presel();
205 n->presel->split_dir = dir;
207 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));
210 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
212 if (n->presel == NULL) {
213 n->presel = make_presel();
216 n->presel->split_ratio = ratio;
218 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
221 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
223 if (n->presel == NULL) {
227 if (focus_follows_pointer) {
228 listen_enter_notify(n, false);
231 if (n->presel->feedback != XCB_NONE) {
232 xcb_destroy_window(dpy, n->presel->feedback);
235 if (focus_follows_pointer) {
236 listen_enter_notify(n, true);
242 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
245 void cancel_presel_in(monitor_t *m, desktop_t *d, node_t *n)
250 cancel_presel(m, d, n);
251 cancel_presel_in(m, d, n->first_child);
252 cancel_presel_in(m, d, n->second_child);
255 node_t *find_public(desktop_t *d)
257 unsigned int b_manual_area = 0;
258 unsigned int b_automatic_area = 0;
259 node_t *b_manual = NULL;
260 node_t *b_automatic = NULL;
261 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
265 unsigned int n_area = node_area(d, n);
266 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
268 b_manual_area = n_area;
270 if (n_area > b_automatic_area &&
271 n->presel == NULL && !n->private && private_count(n->parent) == 0) {
273 b_automatic_area = n_area;
276 if (b_automatic != NULL) {
283 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
285 if (d == NULL || n == NULL) {
289 /* n: inserted node */
290 /* c: new internal node */
291 /* f: focus or insertion anchor */
292 /* p: parent of focus */
293 /* g: grand parent of focus */
301 } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
302 node_t *p = f->parent;
304 if (is_first_child(f)) {
316 node_t *c = make_node(XCB_NONE);
317 node_t *p = f->parent;
318 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
319 node_t *k = find_public(d);
324 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
325 xcb_rectangle_t rect = get_rectangle(d, f);
326 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
329 if (f->presel == NULL && p != NULL && p->vacant) {
334 c->birth_rotation = f->birth_rotation;
335 if (f->presel == NULL) {
337 if (initial_polarity == FIRST_CHILD) {
344 if (m->rectangle.width > m->rectangle.height) {
345 c->split_type = TYPE_VERTICAL;
347 c->split_type = TYPE_HORIZONTAL;
352 node_t *g = p->parent;
355 if (is_first_child(p)) {
363 c->split_type = p->split_type;
364 c->split_ratio = p->split_ratio;
367 if (is_first_child(f)) {
376 n->birth_rotation = rot;
383 if (is_first_child(f)) {
389 c->split_ratio = f->presel->split_ratio;
392 f->birth_rotation = 0;
393 switch (f->presel->split_dir) {
395 c->split_type = TYPE_VERTICAL;
400 c->split_type = TYPE_VERTICAL;
405 c->split_type = TYPE_HORIZONTAL;
410 c->split_type = TYPE_HORIZONTAL;
418 cancel_presel(m, d, f);
422 propagate_flags_upward(m, d, n);
424 if (d->focus == NULL && is_focusable(n)) {
431 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
433 node_t *r = make_node(XCB_NONE);
434 insert_node(m, d, r, n);
437 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
440 d = history_last_desktop(m, NULL);
450 if (n == NULL && d->root != NULL) {
451 n = history_last_node(d, NULL);
453 n = first_focusable_leaf(d->root);
457 if (d == mon->desk || (n != NULL && !is_focusable(n))) {
462 if (d->focus != NULL && n != d->focus) {
463 neutralize_occluding_windows(m, d, n);
467 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
468 if (f->client != NULL && !is_descendant(f, n)) {
469 window_draw_border(f->id, get_border_color(false, (m == mon)));
473 draw_border(n, true, (m == mon));
476 activate_desktop(m, d);
479 history_add(m, d, n);
481 put_status(SBSC_MASK_REPORT);
487 put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
492 void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n)
496 } else if (n->sticky) {
497 transfer_node(m, ds, n, m, dd, dd->focus);
499 /* we need references to the children because n might be freed after
500 * the first recursive call */
501 node_t *first_child = n->first_child;
502 node_t *second_child = n->second_child;
503 transfer_sticky_nodes(m, ds, dd, first_child);
504 transfer_sticky_nodes(m, ds, dd, second_child);
508 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
511 m = history_last_monitor(NULL);
522 d = history_last_desktop(m, NULL);
532 if (n == NULL && d->root != NULL) {
533 n = history_last_node(d, NULL);
535 n = first_focusable_leaf(d->root);
539 if (n != NULL && !is_focusable(n)) {
543 if ((mon != NULL && mon->desk != d) || n == NULL || n->client == NULL) {
547 if (m->sticky_count > 0 && d != m->desk) {
548 sticky_still = false;
549 transfer_sticky_nodes(m, m->desk, d, m->desk->root);
551 if (n == NULL && d->focus != NULL) {
556 if (d->focus != NULL && n != d->focus) {
557 neutralize_occluding_windows(m, d, n);
560 if (n != NULL && n->client != NULL && n->client->urgent) {
561 set_urgent(m, d, n, false);
566 for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
567 draw_border(e->focus, true, false);
570 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
574 draw_border(e->focus, true, true);
579 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
580 if (f->client != NULL && !is_descendant(f, n)) {
581 window_draw_border(f->id, get_border_color(false, true));
586 draw_border(n, true, true);
591 ewmh_update_active_window();
592 history_add(m, d, n);
594 put_status(SBSC_MASK_REPORT);
600 put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
605 if (pointer_follows_focus) {
606 center_pointer(get_rectangle(d, n));
612 void hide_node(desktop_t *d, node_t *n)
618 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
619 window_hide(n->presel->feedback);
621 if (n->client != NULL) {
625 if (n->client != NULL) {
626 n->client->shown = false;
628 hide_node(d, n->first_child);
629 hide_node(d, n->second_child);
633 void show_node(desktop_t *d, node_t *n)
639 if (n->client != NULL) {
642 if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
643 window_show(n->presel->feedback);
646 if (n->client != NULL) {
647 n->client->shown = true;
649 show_node(d, n->first_child);
650 show_node(d, n->second_child);
654 node_t *make_node(uint32_t id)
656 if (id == XCB_NONE) {
657 id = xcb_generate_id(dpy);
659 node_t *n = calloc(1, sizeof(node_t));
661 n->parent = n->first_child = n->second_child = NULL;
662 n->vacant = n->hidden = n->sticky = n->private = n->locked = false;
663 n->split_ratio = split_ratio;
664 n->split_type = TYPE_VERTICAL;
665 n->birth_rotation = 0;
671 client_t *make_client(void)
673 client_t *c = calloc(1, sizeof(client_t));
674 c->state = c->last_state = STATE_TILED;
675 c->layer = c->last_layer = LAYER_NORMAL;
676 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
677 snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
678 c->border_width = border_width;
682 c->icccm_props.input_hint = true;
683 c->icccm_props.take_focus = false;
684 c->icccm_props.delete_window = false;
685 c->size_hints.flags = 0;
689 void initialize_client(node_t *n)
691 xcb_window_t win = n->id;
692 client_t *c = n->client;
693 xcb_icccm_get_wm_protocols_reply_t protos;
694 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
695 for (uint32_t i = 0; i < protos.atoms_len; i++) {
696 if (protos.atoms[i] == WM_TAKE_FOCUS) {
697 c->icccm_props.take_focus = true;
698 } else if (protos.atoms[i] == WM_DELETE_WINDOW) {
699 c->icccm_props.delete_window = true;
702 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
704 xcb_ewmh_get_atoms_reply_t wm_state;
705 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
706 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
707 #define HANDLE_WM_STATE(s) \
708 if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
709 c->wm_flags |= WM_FLAG_##s; continue; \
711 HANDLE_WM_STATE(MODAL)
712 HANDLE_WM_STATE(STICKY)
713 HANDLE_WM_STATE(MAXIMIZED_VERT)
714 HANDLE_WM_STATE(MAXIMIZED_HORZ)
715 HANDLE_WM_STATE(SHADED)
716 HANDLE_WM_STATE(SKIP_TASKBAR)
717 HANDLE_WM_STATE(SKIP_PAGER)
718 HANDLE_WM_STATE(HIDDEN)
719 HANDLE_WM_STATE(FULLSCREEN)
720 HANDLE_WM_STATE(ABOVE)
721 HANDLE_WM_STATE(BELOW)
722 HANDLE_WM_STATE(DEMANDS_ATTENTION)
723 #undef HANDLE_WM_STATE
725 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
727 xcb_icccm_wm_hints_t hints;
728 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
729 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
730 c->icccm_props.input_hint = hints.input;
732 xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
735 bool is_focusable(node_t *n)
737 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
738 if (f->client != NULL && !f->hidden) {
745 bool is_leaf(node_t *n)
747 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
750 bool is_first_child(node_t *n)
752 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
755 bool is_second_child(node_t *n)
757 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
760 unsigned int clients_count_in(node_t *n)
765 return (n->client != NULL ? 1 : 0) +
766 clients_count_in(n->first_child) +
767 clients_count_in(n->second_child);
771 node_t *brother_tree(node_t *n)
773 if (n == NULL || n->parent == NULL) {
776 if (is_first_child(n)) {
777 return n->parent->second_child;
779 return n->parent->first_child;
783 node_t *first_extrema(node_t *n)
787 } else if (n->first_child == NULL) {
790 return first_extrema(n->first_child);
794 node_t *second_extrema(node_t *n)
798 } else if (n->second_child == NULL) {
801 return second_extrema(n->second_child);
805 node_t *first_focusable_leaf(node_t *n)
807 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
808 if (f->client != NULL && !f->hidden) {
815 node_t *next_leaf(node_t *n, node_t *r)
821 while (is_second_child(p) && p != r) {
827 return first_extrema(p->parent->second_child);
830 node_t *prev_leaf(node_t *n, node_t *r)
836 while (is_first_child(p) && p != r) {
842 return second_extrema(p->parent->first_child);
845 node_t *next_tiled_leaf(node_t *n, node_t *r)
847 node_t *next = next_leaf(n, r);
848 if (next == NULL || (next->client != NULL && !next->vacant)) {
851 return next_tiled_leaf(next, r);
855 node_t *prev_tiled_leaf(node_t *n, node_t *r)
857 node_t *prev = prev_leaf(n, r);
858 if (prev == NULL || (prev->client != NULL && !prev->vacant)) {
861 return prev_tiled_leaf(prev, r);
865 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
866 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
870 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
873 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
876 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
879 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
885 node_t *find_fence(node_t *n, direction_t dir)
896 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
897 (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
898 (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
899 (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
907 /* returns *true* if *a* is a child of *b* */
908 bool is_child(node_t *a, node_t *b)
910 if (a == NULL || b == NULL) {
913 return (a->parent != NULL && a->parent == b);
916 /* returns *true* if *a* is a descendant of *b* */
917 bool is_descendant(node_t *a, node_t *b)
919 if (a == NULL || b == NULL) {
922 while (a != b && a != NULL) {
928 bool find_by_id(uint32_t id, coordinates_t *loc)
930 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
931 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
932 node_t *n = find_by_id_in(d->root, id);
944 node_t *find_by_id_in(node_t *r, uint32_t id)
948 } else if (r->id == id) {
951 node_t *f = find_by_id_in(r->first_child, id);
955 return find_by_id_in(r->second_child, id);
960 void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t sel)
962 if (ref->node == NULL) {
966 xcb_rectangle_t rect = get_rectangle(ref->desktop, ref->node);
967 double md = DBL_MAX, mr = UINT32_MAX;
969 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
970 desktop_t *d = m->desk;
971 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
972 coordinates_t loc = {m, d, f};
973 xcb_rectangle_t r = get_rectangle(d, f);
974 if (f == ref->node ||
977 !node_matches(&loc, ref, sel) ||
978 !on_dir_side(rect, r, dir)) {
981 double fd = distance_center(rect, r);
982 uint32_t fr = history_rank(f);
983 if (fd < md || (fd == md && fr < mr)) {
992 unsigned int node_area(desktop_t *d, node_t *n)
997 return area(get_rectangle(d, n));
1000 int tiled_count(node_t *n)
1006 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1007 if (!f->hidden && f->client != NULL && IS_TILED(f->client)) {
1014 void find_biggest(coordinates_t *ref, coordinates_t *dst, node_select_t sel)
1016 unsigned int b_area = 0;
1018 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1019 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1020 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1021 coordinates_t loc = {m, d, f};
1022 if (f->client == NULL || f->vacant || !node_matches(&loc, ref, sel)) {
1025 unsigned int f_area = node_area(d, f);
1026 if (f_area > b_area) {
1035 void rotate_tree(node_t *n, int deg)
1037 if (n == NULL || is_leaf(n) || deg == 0) {
1043 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1044 (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1046 tmp = n->first_child;
1047 n->first_child = n->second_child;
1048 n->second_child = tmp;
1049 n->split_ratio = 1.0 - n->split_ratio;
1053 if (n->split_type == TYPE_HORIZONTAL) {
1054 n->split_type = TYPE_VERTICAL;
1055 } else if (n->split_type == TYPE_VERTICAL) {
1056 n->split_type = TYPE_HORIZONTAL;
1060 rotate_tree(n->first_child, deg);
1061 rotate_tree(n->second_child, deg);
1064 void rotate_brother(node_t *n)
1066 rotate_tree(brother_tree(n), n->birth_rotation);
1069 void unrotate_tree(node_t *n, int rot)
1074 rotate_tree(n, 360 - rot);
1077 void unrotate_brother(node_t *n)
1079 unrotate_tree(brother_tree(n), n->birth_rotation);
1082 void flip_tree(node_t *n, flip_t flp)
1084 if (n == NULL || is_leaf(n)) {
1090 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1091 (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1092 tmp = n->first_child;
1093 n->first_child = n->second_child;
1094 n->second_child = tmp;
1095 n->split_ratio = 1.0 - n->split_ratio;
1098 flip_tree(n->first_child, flp);
1099 flip_tree(n->second_child, flp);
1102 void equalize_tree(node_t *n)
1104 if (n == NULL || n->vacant) {
1107 n->split_ratio = split_ratio;
1108 equalize_tree(n->first_child);
1109 equalize_tree(n->second_child);
1113 int balance_tree(node_t *n)
1115 if (n == NULL || n->vacant) {
1117 } else if (is_leaf(n)) {
1120 int b1 = balance_tree(n->first_child);
1121 int b2 = balance_tree(n->second_child);
1123 if (b1 > 0 && b2 > 0) {
1124 n->split_ratio = (double) b1 / b;
1130 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1132 if (d == NULL || n == NULL) {
1136 node_t *p = n->parent;
1142 if (d->focus == p || is_descendant(d->focus, n)) {
1146 history_remove(d, p, false);
1147 cancel_presel(m, d, p);
1152 node_t *b = brother_tree(n);
1153 node_t *g = p->parent;
1156 unrotate_tree(b, n->birth_rotation);
1162 if (is_first_child(p)) {
1165 g->second_child = b;
1171 b->birth_rotation = p->birth_rotation;
1176 propagate_flags_upward(m, d, b);
1180 void close_node(node_t *n)
1184 } else if (n->client != NULL) {
1185 if (n->client->icccm_props.delete_window) {
1186 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1188 xcb_kill_client(dpy, n->id);
1191 close_node(n->first_child);
1192 close_node(n->second_child);
1196 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1202 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1203 if (f->client != NULL) {
1204 xcb_kill_client(dpy, f->id);
1208 remove_node(m, d, n);
1211 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1217 unlink_node(m, d, n);
1218 history_remove(d, n, true);
1219 remove_stack_node(n);
1220 cancel_presel_in(m, d, n);
1221 if (m->sticky_count > 0) {
1222 m->sticky_count -= sticky_count(n);
1224 clients_count -= clients_count_in(n);
1225 if (is_descendant(grabbed_node, n)) {
1226 grabbed_node = NULL;
1230 ewmh_update_client_list(false);
1231 ewmh_update_client_list(true);
1233 if (mon != NULL && d->focus == NULL) {
1234 if (d == mon->desk) {
1235 focus_node(m, d, NULL);
1237 activate_node(m, d, NULL);
1242 void free_node(node_t *n)
1247 node_t *first_child = n->first_child;
1248 node_t *second_child = n->second_child;
1251 free_node(first_child);
1252 free_node(second_child);
1255 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
1257 if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1258 (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1259 (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1263 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);
1265 node_t *pn1 = n1->parent;
1266 node_t *pn2 = n2->parent;
1267 bool n1_first_child = is_first_child(n1);
1268 bool n2_first_child = is_first_child(n2);
1269 int br1 = n1->birth_rotation;
1270 int br2 = n2->birth_rotation;
1271 bool n1_held_focus = is_descendant(d1->focus, n1);
1272 bool n2_held_focus = is_descendant(d2->focus, n2);
1273 node_t *last_d1_focus = d1->focus;
1274 node_t *last_d2_focus = d2->focus;
1277 if (n1_first_child) {
1278 pn1->first_child = n2;
1280 pn1->second_child = n2;
1285 if (n2_first_child) {
1286 pn2->first_child = n1;
1288 pn2->second_child = n1;
1294 n1->birth_rotation = br2;
1295 n2->birth_rotation = br1;
1297 propagate_flags_upward(m2, d2, n1);
1298 propagate_flags_upward(m1, d1, n2);
1301 if (d1->root == n1) {
1305 if (d2->root == n2) {
1309 if (n1_held_focus) {
1310 d1->focus = n2_held_focus ? last_d2_focus : n2;
1313 if (n2_held_focus) {
1314 d2->focus = n1_held_focus ? last_d1_focus : n1;
1318 adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1319 adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1322 ewmh_set_wm_desktop(n1, d2);
1323 ewmh_set_wm_desktop(n2, d1);
1325 history_swap_nodes(m1, d1, n1, m2, d2, n2);
1327 if (m1->desk != d1 && m2->desk == d2) {
1330 } else if (m1->desk == d1 && m2->desk != d2) {
1335 if (n1_held_focus) {
1336 if (d1 == mon->desk) {
1337 focus_node(m1, d1, d1->focus);
1339 activate_node(m1, d1, d1->focus);
1342 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1345 if (n2_held_focus) {
1346 if (d2 == mon->desk) {
1347 focus_node(m2, d2, d2->focus);
1349 activate_node(m2, d2, d2->focus);
1352 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1365 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
1367 if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1371 if (sticky_still && ds != dd && ms->sticky_count > 0 && sticky_count(ns) > 0) {
1375 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);
1377 bool held_focus = is_descendant(ds->focus, ns);
1378 /* avoid ending up with a dangling pointer (because of unlink_node) */
1379 node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1381 if (held_focus && ds == mon->desk) {
1382 clear_input_focus();
1385 unlink_node(ms, ds, ns);
1386 insert_node(md, dd, ns, nd);
1388 if (md != ms && (ns->client == NULL || monitor_from_client(ns->client) != md)) {
1389 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1393 ewmh_set_wm_desktop(ns, dd);
1395 if (ds == ms->desk && dd != md->desk) {
1397 } else if (ds != ms->desk && dd == md->desk) {
1403 history_transfer_node(md, dd, ns);
1404 stack(dd, ns, false);
1408 if (ds == mon->desk) {
1409 focus_node(ms, ds, last_ds_focus);
1411 activate_node(ms, ds, last_ds_focus);
1414 draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1418 if (ds == mon->desk) {
1419 focus_node(ms, ds, ds->focus);
1421 activate_node(ms, ds, ds->focus);
1424 if (dd->focus == ns) {
1425 if (dd == mon->desk) {
1426 focus_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1428 activate_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1431 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1444 bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t sel)
1446 if (ref->node == NULL) {
1450 monitor_t *m = ref->monitor;
1451 desktop_t *d = ref->desktop;
1452 node_t *n = ref->node;
1454 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
1456 #define HANDLE_BOUNDARIES(f) \
1457 while (f == NULL) { \
1458 d = (dir == CYCLE_PREV ? d->prev : d->next); \
1460 m = (dir == CYCLE_PREV ? m->prev : m->next); \
1462 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
1464 d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
1466 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
1468 HANDLE_BOUNDARIES(f);
1471 coordinates_t loc = {m, d, f};
1472 if (f->client != NULL && !f->hidden && node_matches(&loc, ref, sel)) {
1476 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
1477 HANDLE_BOUNDARIES(f);
1479 #undef HANDLE_EXTREMUM
1483 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1485 if (n == NULL || d->focus == NULL || is_leaf(n)) {
1488 node_t *p = d->focus->parent;
1489 bool focus_first_child = is_first_child(d->focus);
1490 if (dir == CIRCULATE_FORWARD) {
1491 node_t *e = second_extrema(n);
1492 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1493 e = prev_leaf(e, n);
1495 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)) {
1496 swap_nodes(m, d, f, m, d, s);
1499 node_t *e = first_extrema(n);
1500 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1501 e = next_leaf(e, n);
1503 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)) {
1504 swap_nodes(m, d, f, m, d, s);
1507 if (focus_first_child) {
1508 focus_node(m, d, p->first_child);
1510 focus_node(m, d, p->second_child);
1514 void set_vacant(monitor_t *m, desktop_t *d, node_t *n, bool value)
1516 if (n->vacant == value) {
1520 propagate_vacant_downward(m, d, n, value);
1521 propagate_vacant_upward(m, d, n);
1524 void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1526 if (n->vacant == value) {
1533 unrotate_brother(n);
1534 cancel_presel(m, d, n);
1540 void propagate_vacant_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1546 set_vacant_local(m, d, n, value);
1548 propagate_vacant_downward(m, d, n->first_child, value);
1549 propagate_vacant_downward(m, d, n->second_child, value);
1552 void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
1558 node_t *p = n->parent;
1561 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1564 propagate_vacant_upward(m, d, p);
1567 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1569 if (n == NULL || n->client->layer == l) {
1573 n->client->last_layer = n->client->layer;
1574 n->client->layer = l;
1576 if (l == LAYER_ABOVE) {
1577 n->client->wm_flags |= WM_FLAG_ABOVE;
1578 n->client->wm_flags &= ~WM_FLAG_BELOW;
1579 } else if (l == LAYER_BELOW) {
1580 n->client->wm_flags |= WM_FLAG_BELOW;
1581 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1583 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1586 ewmh_wm_state_update(n);
1588 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));
1590 if (d->focus == n) {
1591 neutralize_occluding_windows(m, d, n);
1594 stack(d, n, (d->focus == n));
1599 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1601 if (n == NULL || n->client == NULL || n->client->state == s) {
1605 client_t *c = n->client;
1607 c->last_state = c->state;
1610 switch (c->last_state) {
1612 case STATE_PSEUDO_TILED:
1614 case STATE_FLOATING:
1615 set_floating(m, d, n, false);
1617 case STATE_FULLSCREEN:
1618 set_fullscreen(m, d, n, false);
1622 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));
1626 case STATE_PSEUDO_TILED:
1628 case STATE_FLOATING:
1629 set_floating(m, d, n, true);
1631 case STATE_FULLSCREEN:
1632 set_fullscreen(m, d, n, true);
1636 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));
1638 if (n == m->desk->focus) {
1639 put_status(SBSC_MASK_REPORT);
1645 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1651 cancel_presel(m, d, n);
1652 set_vacant(m, d, n, value);
1654 if (!value && d->focus == n) {
1655 neutralize_occluding_windows(m, d, n);
1658 stack(d, n, (d->focus == n));
1661 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1667 client_t *c = n->client;
1669 cancel_presel(m, d, n);
1670 set_vacant(m, d, n, value);
1673 c->wm_flags |= WM_FLAG_FULLSCREEN;
1674 c->last_layer = c->layer;
1675 c->layer = LAYER_ABOVE;
1677 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1678 c->layer = c->last_layer;
1679 if (d->focus == n) {
1680 neutralize_occluding_windows(m, d, n);
1684 ewmh_wm_state_update(n);
1685 stack(d, n, (d->focus == n));
1688 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1690 bool changed = false;
1691 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1692 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1693 if (a != f && a->client != NULL && f->client != NULL &&
1694 IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1695 set_state(m, d, a, a->client->last_state);
1705 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
1711 node_t *p = n->parent;
1714 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1715 set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
1718 propagate_flags_upward(m, d, p);
1721 void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
1723 if (n == NULL || n->hidden == value) {
1728 if (focus_follows_pointer) {
1729 listen_enter_notify(d->root, false);
1732 bool held_focus = is_descendant(d->focus, n);
1734 propagate_hidden_downward(m, d, n, value);
1735 propagate_hidden_upward(m, d, n);
1737 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));
1739 if (held_focus || d->focus == NULL) {
1740 if (d->focus != NULL) {
1742 draw_border(n, false, (mon == m));
1744 if (d == mon->desk) {
1745 focus_node(m, d, d->focus);
1747 activate_node(m, d, d->focus);
1751 if (focus_follows_pointer) {
1752 listen_enter_notify(d->root, true);
1756 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1758 if (n->hidden == value) {
1764 if (n->client != NULL) {
1765 if (n->client->shown) {
1766 window_set_visibility(n->id, !value);
1769 if (IS_TILED(n->client)) {
1770 set_vacant(m, d, n, value);
1774 n->client->wm_flags |= WM_FLAG_HIDDEN;
1776 n->client->wm_flags &= ~WM_FLAG_HIDDEN;
1779 ewmh_wm_state_update(n);
1783 void propagate_hidden_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1789 set_hidden_local(m, d, n, value);
1791 propagate_hidden_downward(m, d, n->first_child, value);
1792 propagate_hidden_downward(m, d, n->second_child, value);
1795 void propagate_hidden_upward(monitor_t *m, desktop_t *d, node_t *n)
1801 node_t *p = n->parent;
1804 set_hidden_local(m, d, p, p->first_child->hidden && p->second_child->hidden);
1807 propagate_hidden_upward(m, d, p);
1810 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
1812 if (n == NULL || n->sticky == value) {
1817 transfer_node(m, d, n, m, m->desk, m->desk->focus);
1828 if (n->client != NULL) {
1830 n->client->wm_flags |= WM_FLAG_STICKY;
1832 n->client->wm_flags &= ~WM_FLAG_STICKY;
1834 ewmh_wm_state_update(n);
1837 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));
1839 if (n == m->desk->focus) {
1840 put_status(SBSC_MASK_REPORT);
1844 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
1846 if (n == NULL || n->private == value) {
1852 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));
1854 if (n == m->desk->focus) {
1855 put_status(SBSC_MASK_REPORT);
1859 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
1861 if (n == NULL || n->locked == value) {
1867 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));
1869 if (n == m->desk->focus) {
1870 put_status(SBSC_MASK_REPORT);
1874 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
1876 if (value && mon->desk->focus == n) {
1880 n->client->urgent = value;
1883 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
1885 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
1888 ewmh_wm_state_update(n);
1890 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));
1891 put_status(SBSC_MASK_REPORT);
1894 /* Returns true if a contains b */
1895 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
1897 return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
1898 a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
1901 xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
1903 client_t *c = n->client;
1905 if (IS_FLOATING(c)) {
1906 return c->floating_rectangle;
1908 return c->tiled_rectangle;
1911 int wg = (d == NULL ? 0 : (gapless_monocle && IS_MONOCLE(d) ? 0 : d->window_gap));
1912 xcb_rectangle_t rect = n->rectangle;
1919 void listen_enter_notify(node_t *n, bool enable)
1921 uint32_t mask = CLIENT_EVENT_MASK | (enable ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
1922 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1923 if (f->client == NULL) {
1926 xcb_change_window_attributes(dpy, f->id, XCB_CW_EVENT_MASK, &mask);
1927 if (f->presel != NULL) {
1928 xcb_change_window_attributes(dpy, f->presel->feedback, XCB_CW_EVENT_MASK, &mask);
1933 #define DEF_FLAG_COUNT(flag) \
1934 unsigned int flag##_count(node_t *n) \
1939 return ((n->flag ? 1 : 0) + \
1940 flag##_count(n->first_child) + \
1941 flag##_count(n->second_child)); \
1944 DEF_FLAG_COUNT(sticky)
1945 DEF_FLAG_COUNT(private)
1946 DEF_FLAG_COUNT(locked)
1947 #undef DEF_FLAG_COUNT