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"
44 void arrange(monitor_t *m, desktop_t *d)
46 if (d->root == NULL) {
50 layout_t last_layout = d->layout;
52 if (single_monocle && tiled_count(d->root) == 1) {
53 d->layout = LAYOUT_MONOCLE;
56 xcb_rectangle_t rect = m->rectangle;
58 if (!paddingless_monocle || d->layout != LAYOUT_MONOCLE) {
59 rect.x += m->padding.left + d->padding.left;
60 rect.y += m->padding.top + d->padding.top;
61 rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
62 rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
65 if (!gapless_monocle || d->layout != LAYOUT_MONOCLE) {
66 rect.x += d->window_gap;
67 rect.y += d->window_gap;
68 rect.width -= d->window_gap;
69 rect.height -= d->window_gap;
72 apply_layout(m, d, d->root, rect, rect);
74 d->layout = last_layout;
77 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
85 if (pointer_follows_focus && mon->desk->focus == n) {
86 xcb_rectangle_t r = rect;
87 r.width -= d->window_gap;
88 r.height -= d->window_gap;
92 if (n->presel != NULL) {
93 draw_presel_feedback(m, d, n);
98 if (n->client == NULL) {
103 if ((borderless_monocle && n->client->state == STATE_TILED && d->layout == LAYOUT_MONOCLE)
104 || n->client->state == STATE_FULLSCREEN) {
107 bw = n->client->border_width;
111 xcb_rectangle_t cr = get_rectangle(d, n);
112 client_state_t s = n->client->state;
113 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
114 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
116 if (s == STATE_TILED) {
118 int bleed = wg + 2 * bw;
119 r.width = (bleed < r.width ? r.width - bleed : 1);
120 r.height = (bleed < r.height ? r.height - bleed : 1);
121 /* pseudo-tiled clients */
123 r = n->client->floating_rectangle;
124 if (center_pseudo_tiled) {
125 r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
126 r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
132 n->client->tiled_rectangle = r;
133 /* floating clients */
134 } else if (s == STATE_FLOATING) {
135 r = n->client->floating_rectangle;
136 /* fullscreen clients */
141 apply_size_hints(n->client, &r.width, &r.height);
143 if (!rect_eq(r, cr)) {
144 window_move_resize(n->id, r.x, r.y, r.width, r.height);
146 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);
150 window_border_width(n->id, bw);
153 xcb_rectangle_t first_rect;
154 xcb_rectangle_t second_rect;
156 if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
157 first_rect = second_rect = rect;
160 if (n->split_type == TYPE_VERTICAL) {
161 fence = rect.width * n->split_ratio;
162 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
163 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
165 fence = rect.height * n->split_ratio;
166 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
167 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
171 apply_layout(m, d, n->first_child, first_rect, root_rect);
172 apply_layout(m, d, n->second_child, second_rect, root_rect);
176 presel_t *make_presel(void)
178 presel_t *p = malloc(sizeof(presel_t));
179 p->split_dir = DIR_EAST;
180 p->split_ratio = split_ratio;
181 p->feedback = XCB_NONE;
185 void set_ratio(node_t *n, double rat)
191 n->split_ratio = rat;
194 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
196 if (n->presel == NULL) {
197 n->presel = make_presel();
200 n->presel->split_dir = dir;
202 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));
205 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
207 if (n->presel == NULL) {
208 n->presel = make_presel();
211 n->presel->split_ratio = ratio;
213 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
216 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
218 if (n->presel == NULL) {
222 if (n->presel->feedback != XCB_NONE) {
223 xcb_destroy_window(dpy, n->presel->feedback);
229 put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
232 node_t *find_public(desktop_t *d)
234 unsigned int b_manual_area = 0;
235 unsigned int b_automatic_area = 0;
236 node_t *b_manual = NULL;
237 node_t *b_automatic = NULL;
238 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
242 unsigned int n_area = node_area(d, n);
243 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
245 b_manual_area = n_area;
247 if (n_area > b_automatic_area &&
248 n->presel == NULL && !n->private && private_count(n->parent) == 0) {
250 b_automatic_area = n_area;
253 if (b_automatic != NULL) {
260 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
262 if (d == NULL || n == NULL) {
266 /* n: inserted node */
267 /* c: new internal node */
268 /* f: focus or insertion anchor */
269 /* p: parent of focus */
270 /* g: grand parent of focus */
278 } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
279 node_t *p = f->parent;
281 if (is_first_child(f)) {
293 node_t *c = make_node(XCB_NONE);
294 node_t *p = f->parent;
295 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
296 node_t *k = find_public(d);
301 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
302 xcb_rectangle_t rect = get_rectangle(d, f);
303 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
306 if (f->presel == NULL && p != NULL && p->vacant) {
311 c->birth_rotation = f->birth_rotation;
312 if (f->presel == NULL) {
314 if (initial_polarity == FIRST_CHILD) {
321 if (m->rectangle.width > m->rectangle.height) {
322 c->split_type = TYPE_VERTICAL;
324 c->split_type = TYPE_HORIZONTAL;
329 node_t *g = p->parent;
332 if (is_first_child(p)) {
340 c->split_type = p->split_type;
341 c->split_ratio = p->split_ratio;
344 if (is_first_child(f)) {
353 n->birth_rotation = rot;
360 if (is_first_child(f)) {
366 c->split_ratio = f->presel->split_ratio;
369 f->birth_rotation = 0;
370 switch (f->presel->split_dir) {
372 c->split_type = TYPE_VERTICAL;
377 c->split_type = TYPE_VERTICAL;
382 c->split_type = TYPE_HORIZONTAL;
387 c->split_type = TYPE_HORIZONTAL;
395 cancel_presel(m, d, f);
398 propagate_vacant_state(m, d, f);
402 if (d->focus == NULL && !IS_RECEPTACLE(n)) {
409 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
411 node_t *r = make_node(XCB_NONE);
412 insert_node(m, d, r, n);
415 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
417 if (d == mon->desk || IS_RECEPTACLE(n)) {
421 if (n == NULL && d->root != NULL) {
422 n = history_last_node(d, NULL);
424 n = first_focusable_leaf(d->root);
429 if (d->focus != NULL && n != d->focus) {
430 neutralize_occluding_windows(m, d, n);
434 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
435 if (f->client != NULL && !is_descendant(f, n)) {
436 window_draw_border(f->id, get_border_color(false, (m == mon)));
440 draw_border(n, true, (m == mon));
444 history_add(m, d, n);
446 put_status(SBSC_MASK_REPORT);
452 put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
457 void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n)
461 } else if (n->sticky) {
462 transfer_node(m, ds, n, m, dd, dd->focus);
464 /* we need references to the children because n might be freed after
465 * the first recursive call */
466 node_t *first_child = n->first_child;
467 node_t *second_child = n->second_child;
468 transfer_sticky_nodes(m, ds, dd, first_child);
469 transfer_sticky_nodes(m, ds, dd, second_child);
473 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
475 if (IS_RECEPTACLE(n)) {
479 if (n == NULL && d->root != NULL) {
480 n = history_last_node(d, NULL);
482 n = first_focusable_leaf(d->root);
486 if (mon->desk != d || n == NULL || n->client == NULL) {
490 if (m->sticky_count > 0 && d != m->desk) {
491 sticky_still = false;
492 transfer_sticky_nodes(m, m->desk, d, m->desk->root);
494 if (n == NULL && d->focus != NULL) {
499 if (d->focus != NULL && n != d->focus) {
500 neutralize_occluding_windows(m, d, n);
503 if (n != NULL && n->client != NULL && n->client->urgent) {
504 set_urgent(m, d, n, false);
508 for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
509 draw_border(e->focus, true, false);
511 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
515 draw_border(e->focus, true, true);
520 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
521 if (f->client != NULL && !is_descendant(f, n)) {
522 window_draw_border(f->id, get_border_color(false, true));
527 draw_border(n, true, true);
532 ewmh_update_active_window();
533 history_add(m, d, n);
535 put_status(SBSC_MASK_REPORT);
541 put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
546 if (focus_follows_pointer) {
547 xcb_window_t win = XCB_NONE;
548 query_pointer(&win, NULL);
550 enable_motion_recorder();
552 disable_motion_recorder();
556 if (pointer_follows_focus) {
557 center_pointer(get_rectangle(d, n));
563 void update_focused(void)
565 focus_node(mon, mon->desk, mon->desk->focus);
568 void hide_node(node_t *n)
573 if (n->presel != NULL) {
574 window_hide(n->presel->feedback);
576 if (n->client != NULL) {
578 n->client->visible = false;
580 hide_node(n->first_child);
581 hide_node(n->second_child);
585 void show_node(node_t *n)
590 if (n->client != NULL) {
592 n->client->visible = true;
594 if (n->presel != NULL) {
595 window_show(n->presel->feedback);
597 show_node(n->first_child);
598 show_node(n->second_child);
602 node_t *make_node(uint32_t id)
604 if (id == XCB_NONE) {
605 id = xcb_generate_id(dpy);
607 node_t *n = malloc(sizeof(node_t));
609 n->parent = n->first_child = n->second_child = NULL;
610 n->vacant = n->sticky = n->private = n->locked = false;
611 n->split_ratio = split_ratio;
612 n->split_type = TYPE_VERTICAL;
613 n->birth_rotation = 0;
619 client_t *make_client(void)
621 client_t *c = calloc(1, sizeof(client_t));
622 c->state = c->last_state = STATE_TILED;
623 c->layer = c->last_layer = LAYER_NORMAL;
624 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
625 snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
626 c->border_width = border_width;
627 c->icccm_props.input_hint = true;
631 void initialize_client(node_t *n)
633 xcb_window_t win = n->id;
634 client_t *c = n->client;
635 xcb_icccm_get_wm_protocols_reply_t protos;
636 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
637 if (has_proto(WM_TAKE_FOCUS, &protos)) {
638 c->icccm_props.take_focus = true;
640 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
642 xcb_ewmh_get_atoms_reply_t wm_state;
643 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
644 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
645 #define HANDLE_WM_STATE(s) \
646 if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
647 c->wm_flags |= WM_FLAG_##s; continue; \
649 HANDLE_WM_STATE(MODAL)
650 HANDLE_WM_STATE(STICKY)
651 HANDLE_WM_STATE(MAXIMIZED_VERT)
652 HANDLE_WM_STATE(MAXIMIZED_HORZ)
653 HANDLE_WM_STATE(SHADED)
654 HANDLE_WM_STATE(SKIP_TASKBAR)
655 HANDLE_WM_STATE(SKIP_PAGER)
656 HANDLE_WM_STATE(HIDDEN)
657 HANDLE_WM_STATE(FULLSCREEN)
658 HANDLE_WM_STATE(ABOVE)
659 HANDLE_WM_STATE(BELOW)
660 HANDLE_WM_STATE(DEMANDS_ATTENTION)
661 #undef HANDLE_WM_STATE
663 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
665 xcb_icccm_wm_hints_t hints;
666 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
667 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
668 c->icccm_props.input_hint = hints.input;
670 xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
673 bool is_leaf(node_t *n)
675 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
678 bool is_first_child(node_t *n)
680 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
683 bool is_second_child(node_t *n)
685 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
688 unsigned int clients_count_in(node_t *n)
693 return (n->client != NULL ? 1 : 0) +
694 clients_count_in(n->first_child) +
695 clients_count_in(n->second_child);
699 node_t *brother_tree(node_t *n)
701 if (n == NULL || n->parent == NULL) {
704 if (is_first_child(n)) {
705 return n->parent->second_child;
707 return n->parent->first_child;
711 node_t *first_extrema(node_t *n)
715 } else if (n->first_child == NULL) {
718 return first_extrema(n->first_child);
722 node_t *second_extrema(node_t *n)
726 } else if (n->second_child == NULL) {
729 return second_extrema(n->second_child);
733 node_t *first_focusable_leaf(node_t *n)
735 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
736 if (f->client != NULL) {
743 node_t *next_leaf(node_t *n, node_t *r)
749 while (is_second_child(p) && p != r) {
755 return first_extrema(p->parent->second_child);
758 node_t *prev_leaf(node_t *n, node_t *r)
764 while (is_first_child(p) && p != r) {
770 return second_extrema(p->parent->first_child);
773 node_t *next_tiled_leaf(node_t *n, node_t *r)
775 node_t *next = next_leaf(n, r);
776 if (next == NULL || (next->client != NULL && IS_TILED(next->client))) {
779 return next_tiled_leaf(next, r);
783 node_t *prev_tiled_leaf(node_t *n, node_t *r)
785 node_t *prev = prev_leaf(n, r);
786 if (prev == NULL || (prev->client != NULL && IS_TILED(prev->client))) {
789 return prev_tiled_leaf(prev, r);
793 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
794 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
798 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
801 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
804 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
807 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
813 node_t *find_fence(node_t *n, direction_t dir)
824 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
825 (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
826 (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
827 (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
835 node_t *nearest_neighbor(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
837 if (n == NULL || (n->client != NULL && IS_FULLSCREEN(n->client)) ||
838 (d->layout == LAYOUT_MONOCLE && (n->client != NULL && IS_TILED(n->client)))) {
842 node_t *nearest = NULL;
843 if (history_aware_focus) {
844 nearest = nearest_from_history(m, d, n, dir, sel);
846 if (nearest == NULL) {
847 if (focus_by_distance) {
848 nearest = nearest_from_distance(m, d, n, dir, sel);
850 nearest = nearest_from_tree(m, d, n, dir, sel);
856 /* returns *true* if *a* is a child of *b* */
857 bool is_child(node_t *a, node_t *b)
859 if (a == NULL || b == NULL) {
862 return (a->parent != NULL && a->parent == b);
865 /* returns *true* if *a* is a descendant of *b* */
866 bool is_descendant(node_t *a, node_t *b)
868 if (a == NULL || b == NULL) {
871 while (a != b && a != NULL) {
877 bool find_by_id(uint32_t id, coordinates_t *loc)
879 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
880 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
881 node_t *n = find_by_id_in(d->root, id);
893 node_t *find_by_id_in(node_t *r, uint32_t id)
897 } else if (r->id == id) {
900 node_t *f = find_by_id_in(r->first_child, id);
904 return find_by_id_in(r->second_child, id);
909 node_t *nearest_from_tree(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
915 node_t *fence = find_fence(n, dir);
921 node_t *nearest = NULL;
923 if (dir == DIR_NORTH || dir == DIR_WEST) {
924 nearest = second_extrema(fence->first_child);
925 } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
926 nearest = first_extrema(fence->second_child);
929 coordinates_t ref = {m, d, n};
930 coordinates_t loc = {m, d, nearest};
932 if (nearest != NULL && (nearest->vacant ||
933 nearest->client == NULL || !node_matches(&loc, &ref, sel))) {
940 node_t *nearest_from_history(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
942 if (n == NULL || (n->client != NULL && !IS_TILED(n->client))) {
946 node_t *target = find_fence(n, dir);
947 if (target == NULL) {
950 if (dir == DIR_NORTH || dir == DIR_WEST) {
951 target = target->first_child;
952 } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
953 target = target->second_child;
956 node_t *nearest = NULL;
957 int min_rank = INT_MAX;
958 coordinates_t ref = {m, d, n};
960 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
961 if (a->vacant || a->client == NULL || !is_adjacent(n, a, dir) || a == n) {
964 coordinates_t loc = {m, d, a};
965 if (!node_matches(&loc, &ref, sel)) {
968 int rank = history_rank(d, a);
969 if (rank >= 0 && rank < min_rank) {
978 node_t *nearest_from_distance(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
984 node_t *target = NULL;
986 if (n->client == NULL || IS_TILED(n->client)) {
987 target = find_fence(n, dir);
988 if (target == NULL) {
991 if (dir == DIR_NORTH || dir == DIR_WEST) {
992 target = target->first_child;
993 } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
994 target = target->second_child;
1000 node_t *nearest = NULL;
1001 direction_t dir2 = DIR_NORTH;
1004 get_side_handle(d, n, dir, &pt);
1005 get_opposite(dir, &dir2);
1006 double ds = DBL_MAX;
1007 coordinates_t ref = {m, d, n};
1009 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
1010 coordinates_t loc = {m, d, a};
1012 a->client == NULL ||
1013 !node_matches(&loc, &ref, sel) ||
1014 (n->client != NULL && (IS_TILED(a->client) != IS_TILED(n->client))) ||
1015 (IS_TILED(a->client) && !is_adjacent(n, a, dir))) {
1018 get_side_handle(d, a, dir2, &pt2);
1019 double ds2 = distance(pt, pt2);
1029 void get_opposite(direction_t src, direction_t *dst)
1047 unsigned int node_area(desktop_t *d, node_t *n)
1052 return area(get_rectangle(d, n));
1055 int tiled_count(node_t *n)
1061 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1062 if (f->client != NULL && IS_TILED(f->client)) {
1069 node_t *find_biggest(monitor_t *m, desktop_t *d, node_t *n, node_select_t sel)
1076 unsigned int b_area = 0;
1077 coordinates_t ref = {m, d, n};
1079 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1080 coordinates_t loc = {m, d, f};
1081 if (f->client == NULL || f->vacant || !node_matches(&loc, &ref, sel)) {
1084 unsigned int f_area = node_area(d, f);
1085 if (f_area > b_area) {
1094 void rotate_tree(node_t *n, int deg)
1096 if (n == NULL || is_leaf(n) || deg == 0) {
1102 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1103 (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1105 tmp = n->first_child;
1106 n->first_child = n->second_child;
1107 n->second_child = tmp;
1108 n->split_ratio = 1.0 - n->split_ratio;
1112 if (n->split_type == TYPE_HORIZONTAL) {
1113 n->split_type = TYPE_VERTICAL;
1114 } else if (n->split_type == TYPE_VERTICAL) {
1115 n->split_type = TYPE_HORIZONTAL;
1119 rotate_tree(n->first_child, deg);
1120 rotate_tree(n->second_child, deg);
1123 void rotate_brother(node_t *n)
1125 rotate_tree(brother_tree(n), n->birth_rotation);
1128 void unrotate_tree(node_t *n, int rot)
1133 rotate_tree(n, 360 - rot);
1136 void unrotate_brother(node_t *n)
1138 unrotate_tree(brother_tree(n), n->birth_rotation);
1141 void flip_tree(node_t *n, flip_t flp)
1143 if (n == NULL || is_leaf(n)) {
1149 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1150 (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1151 tmp = n->first_child;
1152 n->first_child = n->second_child;
1153 n->second_child = tmp;
1154 n->split_ratio = 1.0 - n->split_ratio;
1157 flip_tree(n->first_child, flp);
1158 flip_tree(n->second_child, flp);
1161 void equalize_tree(node_t *n)
1163 if (n == NULL || n->vacant) {
1166 n->split_ratio = split_ratio;
1167 equalize_tree(n->first_child);
1168 equalize_tree(n->second_child);
1172 int balance_tree(node_t *n)
1174 if (n == NULL || n->vacant) {
1176 } else if (is_leaf(n)) {
1179 int b1 = balance_tree(n->first_child);
1180 int b2 = balance_tree(n->second_child);
1182 if (b1 > 0 && b2 > 0) {
1183 n->split_ratio = (double) b1 / b;
1189 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1191 if (d == NULL || n == NULL) {
1195 node_t *p = n->parent;
1201 if (d->focus == p || is_descendant(d->focus, n)) {
1205 node_t *b = brother_tree(n);
1206 node_t *g = p->parent;
1209 unrotate_tree(b, n->birth_rotation);
1215 if (is_first_child(p)) {
1218 g->second_child = b;
1224 b->birth_rotation = p->birth_rotation;
1227 history_remove(d, p, false);
1228 cancel_presel(m, d, p);
1234 propagate_vacant_state(m, d, b);
1238 void close_node(node_t *n)
1242 } else if (n->client != NULL) {
1243 send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1245 close_node(n->first_child);
1246 close_node(n->second_child);
1250 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1256 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1257 if (f->client != NULL) {
1258 xcb_kill_client(dpy, f->id);
1262 remove_node(m, d, n);
1265 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1271 node_t *last_focus = d->focus;
1273 unlink_node(m, d, n);
1274 history_remove(d, n, true);
1275 remove_stack_node(n);
1276 cancel_presel(m, d, n);
1277 if (m->sticky_count > 0) {
1278 m->sticky_count -= sticky_count(n);
1280 clients_count -= clients_count_in(n);
1281 if (grabbed_node == n) {
1282 grabbed_node = NULL;
1287 ewmh_update_client_list(false);
1288 ewmh_update_client_list(true);
1290 if (d->focus != last_focus) {
1291 if (d == mon->desk) {
1292 focus_node(m, d, d->focus);
1294 activate_node(m, d, d->focus);
1299 void destroy_tree(monitor_t *m, desktop_t *d, node_t *n)
1304 node_t *first_child = n->first_child;
1305 node_t *second_child = n->second_child;
1306 if (n->client != NULL) {
1307 remove_stack_node(n);
1313 cancel_presel(m, d, n);
1316 if (first_child != NULL && second_child != NULL) {
1317 first_child->parent = second_child->parent = NULL;
1318 destroy_tree(m, d, first_child);
1319 destroy_tree(m, d, second_child);
1323 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
1325 if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1326 (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1327 (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1331 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);
1333 node_t *pn1 = n1->parent;
1334 node_t *pn2 = n2->parent;
1335 bool n1_first_child = is_first_child(n1);
1336 bool n2_first_child = is_first_child(n2);
1337 int br1 = n1->birth_rotation;
1338 int br2 = n2->birth_rotation;
1339 bool n1_held_focus = is_descendant(d1->focus, n1);
1340 bool n2_held_focus = is_descendant(d2->focus, n2);
1341 node_t *last_d1_focus = d1->focus;
1342 node_t *last_d2_focus = d2->focus;
1345 if (n1_first_child) {
1346 pn1->first_child = n2;
1348 pn1->second_child = n2;
1353 if (n2_first_child) {
1354 pn2->first_child = n1;
1356 pn2->second_child = n1;
1362 n1->birth_rotation = br2;
1363 n2->birth_rotation = br1;
1365 if (n1->vacant != n2->vacant) {
1366 propagate_vacant_state(m2, d2, n1);
1367 propagate_vacant_state(m1, d1, n2);
1371 if (d1->root == n1) {
1375 if (d2->root == n2) {
1379 if (n1_held_focus) {
1380 d1->focus = n2_held_focus ? last_d2_focus : n2;
1383 if (n2_held_focus) {
1384 d2->focus = n1_held_focus ? last_d1_focus : n1;
1388 adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1389 adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1392 ewmh_set_wm_desktop(n1, d2);
1393 ewmh_set_wm_desktop(n2, d1);
1395 history_swap_nodes(m1, d1, n1, m2, d2, n2);
1397 if (m1->desk != d1 && m2->desk == d2) {
1400 } else if (m1->desk == d1 && m2->desk != d2) {
1405 if (n1_held_focus) {
1406 if (d1 == mon->desk) {
1407 focus_node(m1, d1, d1->focus);
1409 activate_node(m1, d1, d1->focus);
1412 draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1415 if (n2_held_focus) {
1416 if (d2 == mon->desk) {
1417 focus_node(m2, d2, d2->focus);
1419 activate_node(m2, d2, d2->focus);
1422 draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1435 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
1437 if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1441 unsigned int sc = 0;
1443 if (sticky_still && ms->sticky_count > 0 && ds != dd && (sc = sticky_count(ns)) > 0) {
1447 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);
1449 bool held_focus = is_descendant(ds->focus, ns);
1450 /* avoid ending up with a dangling pointer (because of unlink_node) */
1451 node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1453 if (held_focus && ds == mon->desk) {
1454 clear_input_focus();
1457 unlink_node(ms, ds, ns);
1458 insert_node(md, dd, ns, nd);
1460 if (md != ms && (ns->client == NULL || monitor_from_client(ns->client) != md)) {
1461 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1465 ewmh_set_wm_desktop(ns, dd);
1467 if (ds == ms->desk && dd != md->desk) {
1469 } else if (ds != ms->desk && dd == md->desk) {
1475 history_transfer_node(md, dd, ns);
1476 stack(dd, ns, false);
1480 if (ds == mon->desk) {
1481 focus_node(ms, ds, last_ds_focus);
1483 activate_node(ms, ds, last_ds_focus);
1486 draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1490 if (ds == mon->desk) {
1491 focus_node(ms, ds, ds->focus);
1493 activate_node(ms, ds, ds->focus);
1496 if (dd->focus == ns) {
1497 if (dd == mon->desk) {
1498 focus_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1500 activate_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1503 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1516 node_t *closest_node(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, node_select_t sel)
1522 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
1524 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
1527 coordinates_t ref = {m, d, n};
1529 coordinates_t loc = {m, d, f};
1530 if (f->client != NULL && node_matches(&loc, &ref, sel)) {
1533 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
1535 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
1541 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1543 if (n == NULL || d->focus == NULL || is_leaf(n)) {
1546 node_t *p = d->focus->parent;
1547 bool focus_first_child = is_first_child(d->focus);
1548 if (dir == CIRCULATE_FORWARD) {
1549 node_t *e = second_extrema(n);
1550 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1551 e = prev_leaf(e, n);
1553 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)) {
1554 swap_nodes(m, d, f, m, d, s);
1557 node_t *e = first_extrema(n);
1558 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1559 e = next_leaf(e, n);
1561 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)) {
1562 swap_nodes(m, d, f, m, d, s);
1565 if (focus_first_child) {
1566 focus_node(m, d, p->first_child);
1568 focus_node(m, d, p->second_child);
1572 void set_vacant_state(monitor_t *m, desktop_t *d, node_t *n, bool value)
1577 unrotate_brother(n);
1583 cancel_presel(m, d, n);
1586 propagate_vacant_state(m, d, n);
1589 void propagate_vacant_state(monitor_t *m, desktop_t *d, node_t *n)
1595 node_t *p = n->parent;
1598 p->vacant = (p->first_child->vacant && p->second_child->vacant);
1600 cancel_presel(m, d, p);
1606 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1608 if (n == NULL || n->client->layer == l) {
1612 n->client->last_layer = n->client->layer;
1613 n->client->layer = l;
1615 if (l == LAYER_ABOVE) {
1616 n->client->wm_flags |= WM_FLAG_ABOVE;
1617 n->client->wm_flags &= ~WM_FLAG_BELOW;
1618 } else if (l == LAYER_BELOW) {
1619 n->client->wm_flags |= WM_FLAG_BELOW;
1620 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1622 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1625 ewmh_wm_state_update(n);
1627 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));
1629 if (d->focus == n) {
1630 neutralize_occluding_windows(m, d, n);
1633 stack(d, n, (d->focus == n));
1638 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1640 if (n == NULL || n->client->state == s) {
1644 client_t *c = n->client;
1646 c->last_state = c->state;
1649 switch (c->last_state) {
1651 case STATE_PSEUDO_TILED:
1653 case STATE_FLOATING:
1654 set_floating(m, d, n, false);
1656 case STATE_FULLSCREEN:
1657 set_fullscreen(m, d, n, false);
1661 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));
1665 case STATE_PSEUDO_TILED:
1667 case STATE_FLOATING:
1668 set_floating(m, d, n, true);
1670 case STATE_FULLSCREEN:
1671 set_fullscreen(m, d, n, true);
1675 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));
1677 if (n == m->desk->focus) {
1678 put_status(SBSC_MASK_REPORT);
1684 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1690 cancel_presel(m, d, n);
1691 set_vacant_state(m, d, n, value);
1693 if (!value && d->focus == n) {
1694 neutralize_occluding_windows(m, d, n);
1697 stack(d, n, (d->focus == n));
1700 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1706 client_t *c = n->client;
1708 cancel_presel(m, d, n);
1709 set_vacant_state(m, d, n, value);
1712 c->wm_flags |= WM_FLAG_FULLSCREEN;
1713 c->last_layer = c->layer;
1714 c->layer = LAYER_ABOVE;
1716 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1717 c->layer = c->last_layer;
1718 if (d->focus == n) {
1719 neutralize_occluding_windows(m, d, n);
1723 ewmh_wm_state_update(n);
1724 stack(d, n, (d->focus == n));
1727 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1729 bool changed = false;
1730 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1731 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1732 if (a != f && a->client != NULL && f->client != NULL &&
1733 IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1734 set_state(m, d, a, a->client->last_state);
1744 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
1746 if (n == NULL || n->locked == value) {
1752 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));
1754 if (n == m->desk->focus) {
1755 put_status(SBSC_MASK_REPORT);
1759 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
1761 if (n == NULL || n->sticky == value) {
1766 transfer_node(m, d, n, m, m->desk, m->desk->focus);
1777 if (n->client != NULL) {
1779 n->client->wm_flags |= WM_FLAG_STICKY;
1781 n->client->wm_flags &= ~WM_FLAG_STICKY;
1783 ewmh_wm_state_update(n);
1786 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));
1788 if (n == m->desk->focus) {
1789 put_status(SBSC_MASK_REPORT);
1794 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
1796 if (n == NULL || n->private == value) {
1802 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));
1804 if (n == m->desk->focus) {
1805 put_status(SBSC_MASK_REPORT);
1809 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
1811 if (value && mon->desk->focus == n) {
1815 n->client->urgent = value;
1818 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
1820 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
1823 ewmh_wm_state_update(n);
1825 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));
1826 put_status(SBSC_MASK_REPORT);
1829 /* Returns true if a contains b */
1830 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
1832 return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
1833 a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
1836 xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
1838 client_t *c = n->client;
1840 xcb_get_geometry_reply_t *g = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, n->id), NULL);
1842 xcb_rectangle_t rect = (xcb_rectangle_t) {g->x, g->y, g->width, g->height};
1846 return (xcb_rectangle_t) {0, 0, screen_width, screen_height};
1849 int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
1850 xcb_rectangle_t rect = n->rectangle;
1857 void get_side_handle(desktop_t *d, node_t *n, direction_t dir, xcb_point_t *pt)
1859 xcb_rectangle_t rect = get_rectangle(d, n);
1863 pt->x = rect.x + rect.width;
1864 pt->y = rect.y + (rect.height / 2);
1867 pt->x = rect.x + (rect.width / 2);
1868 pt->y = rect.y + rect.height;
1872 pt->y = rect.y + (rect.height / 2);
1875 pt->x = rect.x + (rect.width / 2);
1881 #define DEF_FLAG_COUNT(flag) \
1882 unsigned int flag##_count(node_t *n) \
1887 return ((n->flag ? 1 : 0) + \
1888 flag##_count(n->first_child) + \
1889 flag##_count(n->second_child)); \
1892 DEF_FLAG_COUNT(sticky)
1893 DEF_FLAG_COUNT(private)
1894 DEF_FLAG_COUNT(locked)
1895 #undef DEF_FLAG_COUNT