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.
37 #include "subscribe.h"
43 void arrange(monitor_t *m, desktop_t *d)
45 if (d->root == NULL) {
49 layout_t last_layout = d->layout;
51 if (single_monocle && tiled_count(d->root) == 1) {
52 d->layout = LAYOUT_MONOCLE;
55 xcb_rectangle_t rect = m->rectangle;
57 if (!paddingless_monocle || d->layout != LAYOUT_MONOCLE) {
58 rect.x += m->padding.left + d->padding.left;
59 rect.y += m->padding.top + d->padding.top;
60 rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
61 rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
64 if (!gapless_monocle || d->layout != LAYOUT_MONOCLE) {
65 rect.x += d->window_gap;
66 rect.y += d->window_gap;
67 rect.width -= d->window_gap;
68 rect.height -= d->window_gap;
71 apply_layout(m, d, d->root, rect, rect);
73 d->layout = last_layout;
76 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
84 if (pointer_follows_focus && mon->desk->focus == n && frozen_pointer->action == ACTION_NONE) {
85 xcb_rectangle_t r = rect;
86 r.width -= d->window_gap;
87 r.height -= d->window_gap;
91 if (n->presel != NULL) {
92 draw_presel_feedback(m, d, n);
97 if (n->client == NULL) {
102 if ((borderless_monocle && n->client->state == STATE_TILED && d->layout == LAYOUT_MONOCLE)
103 || n->client->state == STATE_FULLSCREEN) {
106 bw = n->client->border_width;
110 xcb_rectangle_t cr = get_rectangle(d, n);
111 client_state_t s = n->client->state;
112 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
113 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
115 if (s == STATE_TILED) {
117 int bleed = wg + 2 * bw;
118 r.width = (bleed < r.width ? r.width - bleed : 1);
119 r.height = (bleed < r.height ? r.height - bleed : 1);
120 /* pseudo-tiled clients */
122 r = n->client->floating_rectangle;
123 if (center_pseudo_tiled) {
124 r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
125 r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
131 n->client->tiled_rectangle = r;
132 /* floating clients */
133 } else if (s == STATE_FLOATING) {
134 r = n->client->floating_rectangle;
135 /* fullscreen clients */
140 if (!rect_eq(r, cr)) {
141 window_move_resize(n->id, r.x, r.y, r.width, r.height);
142 if (frozen_pointer->action == ACTION_NONE) {
143 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);
147 window_border_width(n->id, bw);
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 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 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
164 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
168 apply_layout(m, d, n->first_child, first_rect, root_rect);
169 apply_layout(m, d, n->second_child, second_rect, root_rect);
173 presel_t *make_presel(void)
175 presel_t *p = malloc(sizeof(presel_t));
176 p->split_dir = DIR_EAST;
177 p->split_ratio = split_ratio;
178 p->feedback = XCB_NONE;
182 void set_ratio(node_t *n, double rat)
188 n->split_ratio = rat;
191 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
193 if (n->presel == NULL) {
194 n->presel = make_presel();
197 n->presel->split_dir = dir;
199 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));
202 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
204 if (n->presel == NULL) {
205 n->presel = make_presel();
208 n->presel->split_ratio = ratio;
210 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
213 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
215 if (n->presel == NULL) {
219 if (n->presel->feedback != XCB_NONE) {
220 xcb_destroy_window(dpy, n->presel->feedback);
226 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
229 node_t *find_public(desktop_t *d)
231 unsigned int b_manual_area = 0;
232 unsigned int b_automatic_area = 0;
233 node_t *b_manual = NULL;
234 node_t *b_automatic = NULL;
235 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
239 unsigned int n_area = node_area(d, n);
240 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
242 b_manual_area = n_area;
244 if (n_area > b_automatic_area &&
245 n->presel == NULL && !n->private && private_count(n->parent) == 0) {
247 b_automatic_area = n_area;
250 if (b_automatic != NULL) {
257 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
259 if (d == NULL || n == NULL) {
263 /* n: inserted node */
264 /* c: new internal node */
265 /* f: focus or insertion anchor */
266 /* p: parent of focus */
267 /* g: grand parent of focus */
275 } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
276 node_t *p = f->parent;
278 if (is_first_child(f)) {
289 node_t *c = make_node(XCB_NONE);
290 node_t *p = f->parent;
291 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
292 node_t *k = find_public(d);
297 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
298 xcb_rectangle_t rect = get_rectangle(d, f);
299 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
302 if (f->presel == NULL && p != NULL && p->vacant) {
307 c->birth_rotation = f->birth_rotation;
308 if (f->presel == NULL) {
310 if (initial_polarity == FIRST_CHILD) {
317 if (m->rectangle.width > m->rectangle.height) {
318 c->split_type = TYPE_VERTICAL;
320 c->split_type = TYPE_HORIZONTAL;
325 node_t *g = p->parent;
328 if (is_first_child(p)) {
336 c->split_type = p->split_type;
337 c->split_ratio = p->split_ratio;
340 if (is_first_child(f)) {
349 n->birth_rotation = rot;
356 if (is_first_child(f)) {
362 c->split_ratio = f->presel->split_ratio;
365 f->birth_rotation = 0;
366 switch (f->presel->split_dir) {
368 c->split_type = TYPE_VERTICAL;
373 c->split_type = TYPE_VERTICAL;
378 c->split_type = TYPE_HORIZONTAL;
383 c->split_type = TYPE_HORIZONTAL;
391 cancel_presel(m, d, f);
394 propagate_vacant_state(m, d, f);
398 if (d->focus == NULL && !IS_RECEPTACLE(n)) {
405 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
407 node_t *r = make_node(XCB_NONE);
408 insert_node(m, d, r, n);
411 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
413 if (IS_RECEPTACLE(n)) {
417 if (n == NULL && d->root != NULL) {
418 n = history_last_node(d, NULL);
420 n = first_focusable_leaf(d->root);
425 if (d->focus != NULL && n != d->focus) {
426 neutralize_occluding_windows(m, d, n);
430 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
431 if (f->client != NULL && !is_descendant(f, n)) {
432 window_draw_border(f->id, get_border_color(false, (m == mon)));
436 draw_border(n, true, (m == mon));
440 history_add(m, d, n);
442 put_status(SBSC_MASK_REPORT);
448 put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
453 void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n)
457 } else if (n->sticky) {
458 transfer_node(m, ds, n, m, dd, dd->focus);
460 /* we need references to the children because n might be freed after
461 * the first recursive call */
462 node_t *first_child = n->first_child;
463 node_t *second_child = n->second_child;
464 transfer_sticky_nodes(m, ds, dd, first_child);
465 transfer_sticky_nodes(m, ds, dd, second_child);
469 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
471 if (IS_RECEPTACLE(n)) {
475 if (n == NULL && d->root != NULL) {
476 n = history_last_node(d, NULL);
478 n = first_focusable_leaf(d->root);
482 if (mon->desk != d || n == NULL || n->client == NULL) {
486 if (m->sticky_count > 0 && d != m->desk) {
487 sticky_still = false;
488 transfer_sticky_nodes(m, m->desk, d, m->desk->root);
490 if (n == NULL && d->focus != NULL) {
495 if (d->focus != NULL && n != d->focus) {
496 neutralize_occluding_windows(m, d, n);
499 if (n != NULL && n->client != NULL && n->client->urgent) {
500 set_urgent(m, d, n, false);
504 for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
505 draw_border(e->focus, true, false);
507 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
511 draw_border(e->focus, true, true);
516 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
517 if (f->client != NULL && !is_descendant(f, n)) {
518 window_draw_border(f->id, get_border_color(false, true));
523 draw_border(n, true, true);
528 ewmh_update_active_window();
529 history_add(m, d, n);
531 put_status(SBSC_MASK_REPORT);
537 put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
542 if (focus_follows_pointer) {
543 xcb_window_t win = XCB_NONE;
544 query_pointer(&win, NULL);
546 enable_motion_recorder();
548 disable_motion_recorder();
552 if (pointer_follows_focus) {
553 center_pointer(get_rectangle(d, n));
559 void update_focused(void)
561 focus_node(mon, mon->desk, mon->desk->focus);
564 void hide_node(node_t *n)
569 if (n->presel != NULL) {
570 window_hide(n->presel->feedback);
572 if (n->client != NULL) {
574 n->client->visible = false;
576 hide_node(n->first_child);
577 hide_node(n->second_child);
581 void show_node(node_t *n)
586 if (n->client != NULL) {
588 n->client->visible = true;
590 if (n->presel != NULL) {
591 window_show(n->presel->feedback);
593 show_node(n->first_child);
594 show_node(n->second_child);
598 node_t *make_node(uint32_t id)
600 if (id == XCB_NONE) {
601 id = xcb_generate_id(dpy);
603 node_t *n = malloc(sizeof(node_t));
605 n->parent = n->first_child = n->second_child = NULL;
606 n->vacant = n->sticky = n->private = n->locked = false;
607 n->split_ratio = split_ratio;
608 n->split_type = TYPE_VERTICAL;
609 n->birth_rotation = 0;
615 client_t *make_client(void)
617 client_t *c = malloc(sizeof(client_t));
618 c->state = c->last_state = STATE_TILED;
619 c->layer = c->last_layer = LAYER_NORMAL;
620 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
621 snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
622 c->border_width = border_width;
623 c->urgent = c->visible = c->icccm_focus = false;
624 c->icccm_input = true;
625 c->wm_states_count = 0;
629 void initialize_client(node_t *n)
631 xcb_window_t win = n->id;
632 if (win != XCB_NONE) {
633 client_t *c = n->client;
634 xcb_icccm_get_wm_protocols_reply_t protocols;
635 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
636 if (has_proto(WM_TAKE_FOCUS, &protocols)) {
637 c->icccm_focus = true;
639 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
641 xcb_ewmh_get_atoms_reply_t wm_state;
642 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
643 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
644 ewmh_wm_state_add(n, wm_state.atoms[i]);
646 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
648 xcb_icccm_wm_hints_t hints;
649 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
650 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
651 c->icccm_input = hints.input;
656 bool is_leaf(node_t *n)
658 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
661 bool is_first_child(node_t *n)
663 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
666 bool is_second_child(node_t *n)
668 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
671 unsigned int clients_count_in(node_t *n)
676 return (n->client != NULL ? 1 : 0) +
677 clients_count_in(n->first_child) +
678 clients_count_in(n->second_child);
682 node_t *brother_tree(node_t *n)
684 if (n == NULL || n->parent == NULL) {
687 if (is_first_child(n)) {
688 return n->parent->second_child;
690 return n->parent->first_child;
694 node_t *first_extrema(node_t *n)
698 } else if (n->first_child == NULL) {
701 return first_extrema(n->first_child);
705 node_t *second_extrema(node_t *n)
709 } else if (n->second_child == NULL) {
712 return second_extrema(n->second_child);
716 node_t *first_focusable_leaf(node_t *n)
718 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
719 if (f->client != NULL) {
726 node_t *next_leaf(node_t *n, node_t *r)
732 while (is_second_child(p) && p != r) {
738 return first_extrema(p->parent->second_child);
741 node_t *prev_leaf(node_t *n, node_t *r)
747 while (is_first_child(p) && p != r) {
753 return second_extrema(p->parent->first_child);
756 node_t *next_tiled_leaf(node_t *n, node_t *r)
758 node_t *next = next_leaf(n, r);
759 if (next == NULL || (next->client != NULL && IS_TILED(next->client))) {
762 return next_tiled_leaf(next, r);
766 node_t *prev_tiled_leaf(node_t *n, node_t *r)
768 node_t *prev = prev_leaf(n, r);
769 if (prev == NULL || (prev->client != NULL && IS_TILED(prev->client))) {
772 return prev_tiled_leaf(prev, r);
776 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
777 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
781 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
784 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
787 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
790 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
796 node_t *find_fence(node_t *n, direction_t dir)
807 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
808 (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
809 (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
810 (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
818 node_t *nearest_neighbor(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
820 if (n == NULL || (n->client != NULL && IS_FULLSCREEN(n->client)) ||
821 (d->layout == LAYOUT_MONOCLE && (n->client != NULL && IS_TILED(n->client)))) {
825 node_t *nearest = NULL;
826 if (history_aware_focus) {
827 nearest = nearest_from_history(m, d, n, dir, sel);
829 if (nearest == NULL) {
830 if (focus_by_distance) {
831 nearest = nearest_from_distance(m, d, n, dir, sel);
833 nearest = nearest_from_tree(m, d, n, dir, sel);
839 /* returns *true* if *a* is a child of *b* */
840 bool is_child(node_t *a, node_t *b)
842 if (a == NULL || b == NULL) {
845 return (a->parent != NULL && a->parent == b);
848 /* returns *true* if *a* is a descendant of *b* */
849 bool is_descendant(node_t *a, node_t *b)
851 if (a == NULL || b == NULL) {
854 while (a != b && a != NULL) {
860 bool find_by_id(uint32_t id, coordinates_t *loc)
862 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
863 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
864 node_t *n = find_by_id_in(d->root, id);
876 node_t *find_by_id_in(node_t *r, uint32_t id)
880 } else if (r->id == id) {
883 node_t *f = find_by_id_in(r->first_child, id);
887 return find_by_id_in(r->second_child, id);
892 node_t *nearest_from_tree(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
898 node_t *fence = find_fence(n, dir);
904 node_t *nearest = NULL;
906 if (dir == DIR_NORTH || dir == DIR_WEST) {
907 nearest = second_extrema(fence->first_child);
908 } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
909 nearest = first_extrema(fence->second_child);
912 coordinates_t ref = {m, d, n};
913 coordinates_t loc = {m, d, nearest};
915 if (nearest != NULL && (nearest->vacant ||
916 nearest->client == NULL || !node_matches(&loc, &ref, sel))) {
923 node_t *nearest_from_history(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
925 if (n == NULL || (n->client != NULL && !IS_TILED(n->client))) {
929 node_t *target = find_fence(n, dir);
930 if (target == NULL) {
933 if (dir == DIR_NORTH || dir == DIR_WEST) {
934 target = target->first_child;
935 } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
936 target = target->second_child;
939 node_t *nearest = NULL;
940 int min_rank = INT_MAX;
941 coordinates_t ref = {m, d, n};
943 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
944 if (a->vacant || a->client == NULL || !is_adjacent(n, a, dir) || a == n) {
947 coordinates_t loc = {m, d, a};
948 if (!node_matches(&loc, &ref, sel)) {
951 int rank = history_rank(d, a);
952 if (rank >= 0 && rank < min_rank) {
961 node_t *nearest_from_distance(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
967 node_t *target = NULL;
969 if (n->client == NULL || IS_TILED(n->client)) {
970 target = find_fence(n, dir);
971 if (target == NULL) {
974 if (dir == DIR_NORTH || dir == DIR_WEST) {
975 target = target->first_child;
976 } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
977 target = target->second_child;
983 node_t *nearest = NULL;
984 direction_t dir2 = DIR_NORTH;
987 get_side_handle(d, n, dir, &pt);
988 get_opposite(dir, &dir2);
990 coordinates_t ref = {m, d, n};
992 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
993 coordinates_t loc = {m, d, a};
996 !node_matches(&loc, &ref, sel) ||
997 (n->client != NULL && (IS_TILED(a->client) != IS_TILED(n->client))) ||
998 (IS_TILED(a->client) && !is_adjacent(n, a, dir))) {
1001 get_side_handle(d, a, dir2, &pt2);
1002 double ds2 = distance(pt, pt2);
1012 void get_opposite(direction_t src, direction_t *dst)
1030 unsigned int node_area(desktop_t *d, node_t *n)
1035 return area(get_rectangle(d, n));
1038 int tiled_count(node_t *n)
1044 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1045 if (f->client != NULL && IS_TILED(f->client)) {
1052 node_t *find_biggest(monitor_t *m, desktop_t *d, node_t *n, node_select_t sel)
1059 unsigned int b_area = 0;
1060 coordinates_t ref = {m, d, n};
1062 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1063 coordinates_t loc = {m, d, f};
1064 if (f->client == NULL || f->vacant || !node_matches(&loc, &ref, sel)) {
1067 unsigned int f_area = node_area(d, f);
1068 if (f_area > b_area) {
1077 void rotate_tree(node_t *n, int deg)
1079 if (n == NULL || is_leaf(n) || deg == 0) {
1085 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1086 (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1088 tmp = n->first_child;
1089 n->first_child = n->second_child;
1090 n->second_child = tmp;
1091 n->split_ratio = 1.0 - n->split_ratio;
1095 if (n->split_type == TYPE_HORIZONTAL) {
1096 n->split_type = TYPE_VERTICAL;
1097 } else if (n->split_type == TYPE_VERTICAL) {
1098 n->split_type = TYPE_HORIZONTAL;
1102 rotate_tree(n->first_child, deg);
1103 rotate_tree(n->second_child, deg);
1106 void rotate_brother(node_t *n)
1108 rotate_tree(brother_tree(n), n->birth_rotation);
1111 void unrotate_tree(node_t *n, int rot)
1116 rotate_tree(n, 360 - rot);
1119 void unrotate_brother(node_t *n)
1121 unrotate_tree(brother_tree(n), n->birth_rotation);
1124 void flip_tree(node_t *n, flip_t flp)
1126 if (n == NULL || is_leaf(n)) {
1132 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1133 (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1134 tmp = n->first_child;
1135 n->first_child = n->second_child;
1136 n->second_child = tmp;
1137 n->split_ratio = 1.0 - n->split_ratio;
1140 flip_tree(n->first_child, flp);
1141 flip_tree(n->second_child, flp);
1144 void equalize_tree(node_t *n)
1146 if (n == NULL || n->vacant) {
1149 n->split_ratio = split_ratio;
1150 equalize_tree(n->first_child);
1151 equalize_tree(n->second_child);
1155 int balance_tree(node_t *n)
1157 if (n == NULL || n->vacant) {
1159 } else if (is_leaf(n)) {
1162 int b1 = balance_tree(n->first_child);
1163 int b2 = balance_tree(n->second_child);
1165 if (b1 > 0 && b2 > 0) {
1166 n->split_ratio = (double) b1 / b;
1172 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1174 if (d == NULL || n == NULL) {
1178 node_t *p = n->parent;
1184 if (d->focus == p || is_descendant(d->focus, n)) {
1188 node_t *b = brother_tree(n);
1189 node_t *g = p->parent;
1192 unrotate_tree(b, n->birth_rotation);
1198 if (is_first_child(p)) {
1201 g->second_child = b;
1207 b->birth_rotation = p->birth_rotation;
1210 history_remove(d, p, false);
1211 cancel_presel(m, d, p);
1217 propagate_vacant_state(m, d, b);
1221 void close_node(node_t *n)
1225 } else if (n->client != NULL) {
1226 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1228 close_node(n->first_child);
1229 close_node(n->second_child);
1233 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1239 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1240 if (f->client != NULL) {
1241 xcb_kill_client(dpy, f->id);
1245 remove_node(m, d, n);
1248 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1254 node_t *last_focus = d->focus;
1256 unlink_node(m, d, n);
1257 history_remove(d, n, true);
1258 remove_stack_node(n);
1259 cancel_presel(m, d, n);
1260 if (m->sticky_count > 0) {
1261 m->sticky_count -= sticky_count(n);
1263 clients_count -= clients_count_in(n);
1267 ewmh_update_client_list(false);
1268 ewmh_update_client_list(true);
1270 if (d->focus != last_focus) {
1271 if (d == mon->desk) {
1272 focus_node(m, d, d->focus);
1274 activate_node(m, d, d->focus);
1279 void destroy_tree(monitor_t *m, desktop_t *d, node_t *n)
1284 node_t *first_child = n->first_child;
1285 node_t *second_child = n->second_child;
1286 if (n->client != NULL) {
1287 remove_stack_node(n);
1293 cancel_presel(m, d, n);
1296 if (first_child != NULL && second_child != NULL) {
1297 first_child->parent = second_child->parent = NULL;
1298 destroy_tree(m, d, first_child);
1299 destroy_tree(m, d, second_child);
1303 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
1305 if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1306 (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1307 (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1311 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);
1313 node_t *pn1 = n1->parent;
1314 node_t *pn2 = n2->parent;
1315 bool n1_first_child = is_first_child(n1);
1316 bool n2_first_child = is_first_child(n2);
1317 int br1 = n1->birth_rotation;
1318 int br2 = n2->birth_rotation;
1319 bool n1_held_focus = is_descendant(d1->focus, n1);
1320 bool n2_held_focus = is_descendant(d2->focus, n2);
1321 node_t *last_d1_focus = d1->focus;
1322 node_t *last_d2_focus = d2->focus;
1325 if (n1_first_child) {
1326 pn1->first_child = n2;
1328 pn1->second_child = n2;
1333 if (n2_first_child) {
1334 pn2->first_child = n1;
1336 pn2->second_child = n1;
1342 n1->birth_rotation = br2;
1343 n2->birth_rotation = br1;
1345 if (n1->vacant != n2->vacant) {
1346 propagate_vacant_state(m2, d2, n1);
1347 propagate_vacant_state(m1, d1, n2);
1351 if (d1->root == n1) {
1355 if (d2->root == n2) {
1359 if (n1_held_focus) {
1360 d1->focus = n2_held_focus ? last_d2_focus : n2;
1363 if (n2_held_focus) {
1364 d2->focus = n1_held_focus ? last_d1_focus : n1;
1368 adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1369 adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1372 ewmh_set_wm_desktop(n1, d2);
1373 ewmh_set_wm_desktop(n2, d1);
1375 history_swap_nodes(m1, d1, n1, m2, d2, n2);
1377 if (m1->desk != d1 && m2->desk == d2) {
1380 } else if (m1->desk == d1 && m2->desk != d2) {
1385 if (n1_held_focus) {
1386 if (d1 == mon->desk) {
1387 focus_node(m1, d1, d1->focus);
1389 activate_node(m1, d1, d1->focus);
1392 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1395 if (n2_held_focus) {
1396 if (d2 == mon->desk) {
1397 focus_node(m2, d2, d2->focus);
1399 activate_node(m2, d2, d2->focus);
1402 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1415 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
1417 if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1421 unsigned int sc = 0;
1423 if (sticky_still && ms->sticky_count > 0 && ds != dd && (sc = sticky_count(ns)) > 0) {
1427 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);
1429 bool held_focus = is_descendant(ds->focus, ns);
1430 /* avoid ending up with a dangling pointer (because of unlink_node) */
1431 node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1433 if (held_focus && ds == mon->desk) {
1434 clear_input_focus();
1437 unlink_node(ms, ds, ns);
1438 insert_node(md, dd, ns, nd);
1440 if (md != ms && (ns->client == NULL || monitor_from_client(ns->client) != md)) {
1441 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1445 ewmh_set_wm_desktop(ns, dd);
1447 if (ds == ms->desk && dd != md->desk) {
1449 } else if (ds != ms->desk && dd == md->desk) {
1455 history_transfer_node(md, dd, ns);
1456 stack(dd, ns, false);
1460 if (ds == mon->desk) {
1461 focus_node(ms, ds, last_ds_focus);
1463 activate_node(ms, ds, last_ds_focus);
1466 draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1470 if (ds == mon->desk) {
1471 focus_node(ms, ds, ds->focus);
1473 activate_node(ms, ds, ds->focus);
1476 if (dd->focus == ns) {
1477 if (dd == mon->desk) {
1478 focus_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1480 activate_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1483 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1496 node_t *closest_node(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, node_select_t sel)
1502 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
1504 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
1507 coordinates_t ref = {m, d, n};
1509 coordinates_t loc = {m, d, f};
1510 if (f->client != NULL && node_matches(&loc, &ref, sel)) {
1513 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
1515 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
1521 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1523 if (n == NULL || d->focus == NULL || is_leaf(n)) {
1526 node_t *p = d->focus->parent;
1527 bool focus_first_child = is_first_child(d->focus);
1528 if (dir == CIRCULATE_FORWARD) {
1529 node_t *e = second_extrema(n);
1530 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1531 e = prev_leaf(e, n);
1533 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)) {
1534 swap_nodes(m, d, f, m, d, s);
1537 node_t *e = first_extrema(n);
1538 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1539 e = next_leaf(e, n);
1541 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)) {
1542 swap_nodes(m, d, f, m, d, s);
1545 if (focus_first_child) {
1546 focus_node(m, d, p->first_child);
1548 focus_node(m, d, p->second_child);
1552 void set_vacant_state(monitor_t *m, desktop_t *d, node_t *n, bool value)
1557 unrotate_brother(n);
1563 cancel_presel(m, d, n);
1566 propagate_vacant_state(m, d, n);
1569 void propagate_vacant_state(monitor_t *m, desktop_t *d, node_t *n)
1575 node_t *p = n->parent;
1578 p->vacant = (p->first_child->vacant && p->second_child->vacant);
1580 cancel_presel(m, d, p);
1586 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1588 if (n == NULL || n->client->layer == l) {
1592 n->client->last_layer = n->client->layer;
1593 n->client->layer = l;
1595 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));
1597 if (d->focus == n) {
1598 neutralize_occluding_windows(m, d, n);
1601 stack(d, n, (d->focus == n));
1606 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1608 if (n == NULL || n->client->state == s) {
1612 client_t *c = n->client;
1614 c->last_state = c->state;
1617 switch (c->last_state) {
1619 case STATE_PSEUDO_TILED:
1621 case STATE_FLOATING:
1622 set_floating(m, d, n, false);
1624 case STATE_FULLSCREEN:
1625 set_fullscreen(m, d, n, false);
1629 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));
1633 case STATE_PSEUDO_TILED:
1635 case STATE_FLOATING:
1636 set_floating(m, d, n, true);
1638 case STATE_FULLSCREEN:
1639 set_fullscreen(m, d, n, true);
1643 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));
1645 if (n == m->desk->focus) {
1646 put_status(SBSC_MASK_REPORT);
1652 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1658 cancel_presel(m, d, n);
1659 set_vacant_state(m, d, n, value);
1661 if (!value && d->focus == n) {
1662 neutralize_occluding_windows(m, d, n);
1665 stack(d, n, (d->focus == n));
1668 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1674 client_t *c = n->client;
1676 cancel_presel(m, d, n);
1677 set_vacant_state(m, d, n, value);
1680 ewmh_wm_state_add(n, ewmh->_NET_WM_STATE_FULLSCREEN);
1681 c->last_layer = c->layer;
1682 c->layer = LAYER_ABOVE;
1684 ewmh_wm_state_remove(n, ewmh->_NET_WM_STATE_FULLSCREEN);
1685 c->layer = c->last_layer;
1686 if (d->focus == n) {
1687 neutralize_occluding_windows(m, d, n);
1691 stack(d, n, (d->focus == n));
1694 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1696 bool changed = false;
1697 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1698 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1699 if (a != f && a->client != NULL && f->client != NULL &&
1700 IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1701 set_state(m, d, a, a->client->last_state);
1711 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
1713 if (n == NULL || n->locked == value) {
1719 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));
1721 if (n == m->desk->focus) {
1722 put_status(SBSC_MASK_REPORT);
1726 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
1728 if (n == NULL || n->sticky == value) {
1733 transfer_node(m, d, n, m, m->desk, m->desk->focus);
1739 ewmh_wm_state_add(n, ewmh->_NET_WM_STATE_STICKY);
1742 ewmh_wm_state_remove(n, ewmh->_NET_WM_STATE_STICKY);
1746 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));
1748 if (n == m->desk->focus) {
1749 put_status(SBSC_MASK_REPORT);
1754 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
1756 if (n == NULL || n->private == value) {
1762 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));
1764 if (n == m->desk->focus) {
1765 put_status(SBSC_MASK_REPORT);
1769 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
1771 if (value && mon->desk->focus == n) {
1775 n->client->urgent = value;
1777 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));
1778 put_status(SBSC_MASK_REPORT);
1781 /* Returns true if a contains b */
1782 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
1784 return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
1785 a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
1788 xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
1790 client_t *c = n->client;
1792 xcb_get_geometry_reply_t *g = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, n->id), NULL);
1794 xcb_rectangle_t rect = (xcb_rectangle_t) {g->x, g->y, g->width, g->height};
1798 return (xcb_rectangle_t) {0, 0, screen_width, screen_height};
1801 int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
1802 xcb_rectangle_t rect = n->rectangle;
1809 void get_side_handle(desktop_t *d, node_t *n, direction_t dir, xcb_point_t *pt)
1811 xcb_rectangle_t rect = get_rectangle(d, n);
1815 pt->x = rect.x + rect.width;
1816 pt->y = rect.y + (rect.height / 2);
1819 pt->x = rect.x + (rect.width / 2);
1820 pt->y = rect.y + rect.height;
1824 pt->y = rect.y + (rect.height / 2);
1827 pt->x = rect.x + (rect.width / 2);
1833 #define DEF_FLAG_COUNT(flag) \
1834 unsigned int flag##_count(node_t *n) \
1839 return ((n->flag ? 1 : 0) + \
1840 flag##_count(n->first_child) + \
1841 flag##_count(n->second_child)); \
1844 DEF_FLAG_COUNT(sticky)
1845 DEF_FLAG_COUNT(private)
1846 DEF_FLAG_COUNT(locked)
1847 #undef DEF_FLAG_COUNT