16 void arrange(monitor_t *m, desktop_t *d)
21 PRINTF("arrange %s %s\n", m->name, d->name);
23 xcb_rectangle_t rect = m->rectangle;
24 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
25 rect.x += m->left_padding + wg;
26 rect.y += m->top_padding + wg;
27 rect.width -= m->left_padding + m->right_padding + wg;
28 rect.height -= m->top_padding + m->bottom_padding + wg;
29 apply_layout(m, d, d->root, rect, rect);
32 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
41 if (is_floating(n->client) && n->client->border_width != d->border_width) {
42 int ds = 2 * (d->border_width - n->client->border_width);
43 n->client->floating_rectangle.width += ds;
44 n->client->floating_rectangle.height += ds;
47 if ((borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE) ||
48 n->client->fullscreen)
49 n->client->border_width = 0;
51 n->client->border_width = d->border_width;
54 if (!n->client->fullscreen) {
55 if (!n->client->floating) {
57 if (d->layout == LAYOUT_TILED)
59 else if (d->layout == LAYOUT_MONOCLE)
63 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
64 int bleed = wg + 2 * n->client->border_width;
65 r.width = (bleed < r.width ? r.width - bleed : 1);
66 r.height = (bleed < r.height ? r.height - bleed : 1);
67 n->client->tiled_rectangle = r;
69 /* floating clients */
70 r = n->client->floating_rectangle;
73 /* fullscreen clients */
77 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
78 window_border_width(n->client->window, n->client->border_width);
79 window_draw_border(n, n == d->focus, m == mon);
82 xcb_rectangle_t first_rect;
83 xcb_rectangle_t second_rect;
85 if (n->first_child->vacant || n->second_child->vacant) {
86 first_rect = second_rect = rect;
89 if (n->split_type == TYPE_VERTICAL) {
90 fence = rect.width * n->split_ratio;
91 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
92 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
93 } else if (n->split_type == TYPE_HORIZONTAL) {
94 fence = rect.height * n->split_ratio;
95 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
96 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
100 apply_layout(m, d, n->first_child, first_rect, root_rect);
101 apply_layout(m, d, n->second_child, second_rect, root_rect);
105 void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
107 if (d == NULL || n == NULL)
110 PRINTF("insert node %X\n", n->client->window);
112 /* n: new leaf node */
113 /* c: new container node */
114 /* f: focus or insertion anchor */
115 /* p: parent of focus */
116 /* g: grand parent of focus */
123 if (is_visible(d, n))
126 node_t *c = make_node();
127 node_t *p = f->parent;
128 if (p != NULL && f->split_mode == MODE_AUTOMATIC
129 && (p->first_child->vacant || p->second_child->vacant)) {
134 c->birth_rotation = f->birth_rotation;
135 switch (f->split_mode) {
140 if (m->rectangle.width > m->rectangle.height)
141 c->split_type = TYPE_VERTICAL;
143 c->split_type = TYPE_HORIZONTAL;
147 node_t *g = p->parent;
150 if (is_first_child(p))
157 c->split_type = p->split_type;
158 c->split_ratio = p->split_ratio;
161 if (is_first_child(f)) {
170 if (!is_floating(n->client))
172 n->birth_rotation = rot;
177 if (is_first_child(f))
182 c->split_ratio = f->split_ratio;
185 f->birth_rotation = 0;
186 switch (f->split_dir) {
188 c->split_type = TYPE_VERTICAL;
193 c->split_type = TYPE_VERTICAL;
198 c->split_type = TYPE_HORIZONTAL;
203 c->split_type = TYPE_HORIZONTAL;
210 f->split_mode = MODE_AUTOMATIC;
214 update_vacant_state(p);
216 if (n->client->sticky)
221 void pseudo_focus(desktop_t *d, node_t *n)
228 void focus_node(monitor_t *m, desktop_t *d, node_t *n)
230 if (mon->desk != d || n == NULL)
233 if (m->num_sticky > 0 && d != m->desk) {
234 node_t *a = first_extrema(m->desk->root);
235 sticky_still = false;
237 node_t *b = next_leaf(a, m->desk->root);
238 if (a->client->sticky)
239 transfer_node(m, m->desk, a, m, d, d->focus);
243 if (n == NULL && d->focus != NULL && is_visible(d, d->focus))
247 if (n != NULL && d->focus != NULL && n != d->focus && d->focus->client->fullscreen) {
248 set_fullscreen(d->focus, false);
253 for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
254 window_draw_border(cd->focus, true, false);
255 for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
257 window_draw_border(cd->focus, true, true);
259 window_draw_border(n, true, true);
263 window_draw_border(d->focus, false, true);
264 window_draw_border(n, true, true);
271 history_add(m, d, NULL);
272 ewmh_update_active_window();
276 PRINTF("focus node %X\n", n->client->window);
278 n->client->urgent = false;
280 if (!is_visible(d, n))
281 tag_node(m, d, n, d, n->client->tags_field | d->tags_field);
282 history_add(m, d, n);
285 if (focus_follows_pointer) {
286 xcb_window_t win = XCB_NONE;
287 query_pointer(&win, NULL);
288 if (win != n->client->window)
289 enable_motion_recorder();
291 disable_motion_recorder();
294 ewmh_update_active_window();
297 void update_current(void)
299 focus_node(mon, mon->desk, mon->desk->focus);
302 node_t *make_node(void)
304 node_t *n = malloc(sizeof(node_t));
305 n->parent = n->first_child = n->second_child = NULL;
306 n->split_ratio = split_ratio;
307 n->split_mode = MODE_AUTOMATIC;
308 n->split_type = TYPE_VERTICAL;
309 n->birth_rotation = 0;
315 client_t *make_client(xcb_window_t win)
317 client_t *c = malloc(sizeof(client_t));
318 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
319 c->border_width = BORDER_WIDTH;
321 c->floating = c->transient = c->fullscreen = c->locked = c->sticky = c->urgent = false;
322 c->icccm_focus = false;
323 xcb_icccm_get_wm_protocols_reply_t protocols;
324 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
325 if (has_proto(WM_TAKE_FOCUS, &protocols))
326 c->icccm_focus = true;
327 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
330 xcb_ewmh_get_atoms_reply_t wm_state;
331 if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
332 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_STATE; i++)
333 ewmh_wm_state_add(c, wm_state.atoms[i]);
334 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
339 bool is_visible(desktop_t *d, node_t *n)
341 return (d->tags_field & n->client->tags_field) != 0;
344 bool is_leaf(node_t *n)
346 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
349 bool is_tiled(client_t *c)
353 return (!c->floating && !c->fullscreen);
356 bool is_floating(client_t *c)
360 return (c->floating && !c->fullscreen);
363 bool is_first_child(node_t *n)
365 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
368 bool is_second_child(node_t *n)
370 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
373 void change_split_ratio(node_t *n, value_change_t chg)
375 n->split_ratio = pow(n->split_ratio,
376 (chg == CHANGE_INCREASE ? (1 / growth_factor) : growth_factor));
379 void reset_mode(coordinates_t *loc)
381 if (loc->node != NULL) {
382 loc->node->split_mode = MODE_AUTOMATIC;
383 window_draw_border(loc->node, loc->desktop->focus == loc->node, mon == loc->monitor);
384 } else if (loc->desktop != NULL) {
385 for (node_t *a = first_extrema(loc->desktop->root); a != NULL; a = next_leaf(a, loc->desktop->root)) {
386 a->split_mode = MODE_AUTOMATIC;
387 window_draw_border(a, loc->desktop->focus == a, mon == loc->monitor);
392 node_t *brother_tree(node_t *n)
394 if (n == NULL || n->parent == NULL)
396 if (is_first_child(n))
397 return n->parent->second_child;
399 return n->parent->first_child;
402 node_t *closest_visible(desktop_t *d, node_t *n)
406 node_t *prev = prev_leaf(n, d->root);
407 node_t *next = next_leaf(n, d->root);
408 while (prev != NULL || next != NULL) {
410 if (is_visible(d, prev))
413 prev = prev_leaf(prev, d->root);
416 if (is_visible(d, next))
419 next = next_leaf(next, d->root);
425 node_t *first_extrema(node_t *n)
429 else if (n->first_child == NULL)
432 return first_extrema(n->first_child);
435 node_t *second_extrema(node_t *n)
439 else if (n->second_child == NULL)
442 return second_extrema(n->second_child);
445 node_t *next_leaf(node_t *n, node_t *r)
450 while (is_second_child(p) && p != r)
454 return first_extrema(p->parent->second_child);
457 node_t *prev_leaf(node_t *n, node_t *r)
462 while (is_first_child(p) && p != r)
466 return second_extrema(p->parent->first_child);
469 node_t *next_visible_leaf(desktop_t *d, node_t *n, node_t *r)
471 node_t *next = next_leaf(n, r);
472 if (next == NULL || is_visible(d, next))
475 return next_visible_leaf(d, next, r);
478 node_t *prev_visible_leaf(desktop_t *d, node_t *n, node_t *r)
480 node_t *prev = prev_leaf(n, r);
481 if (prev == NULL || is_visible(d, prev))
484 return prev_visible_leaf(d, prev, r);
487 /* bool is_adjacent(node_t *a, node_t *r) */
489 /* node_t *f = r->parent; */
491 /* bool first_child = is_first_child(r); */
492 /* while (p != r) { */
493 /* if (p->parent->split_type == f->split_type && is_first_child(p) == first_child) */
500 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
501 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
505 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
508 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
511 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
514 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
520 node_t *find_fence(node_t *n, direction_t dir)
530 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
531 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
532 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
533 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
542 node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
544 if (n == NULL || n->client->fullscreen
545 || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
548 node_t *nearest = NULL;
549 if (history_aware_focus)
550 nearest = nearest_from_history(d, n, dir, sel);
552 nearest = nearest_from_distance(d, n, dir, sel);
556 node_t *nearest_from_history(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
558 if (n == NULL || !is_tiled(n->client))
561 node_t *target = find_fence(n, dir);
564 if (dir == DIR_UP || dir == DIR_LEFT)
565 target = target->first_child;
566 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
567 target = target->second_child;
569 node_t *nearest = NULL;
570 int min_rank = INT_MAX;
572 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
573 if (a->vacant || !is_adjacent(n, a, dir) || a == n)
575 if (!node_matches(n, a, sel))
578 int rank = history_rank(d, a);
579 if (rank >= 0 && rank < min_rank) {
588 node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
593 node_t *target = NULL;
595 if (is_tiled(n->client)) {
596 target = find_fence(n, dir);
599 if (dir == DIR_UP || dir == DIR_LEFT)
600 target = target->first_child;
601 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
602 target = target->second_child;
607 node_t *nearest = NULL;
611 get_side_handle(n->client, dir, &pt);
612 get_opposite(dir, &dir2);
615 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
618 !node_matches(n, a, sel) ||
619 is_tiled(a->client) != is_tiled(n->client) ||
620 (is_tiled(a->client) && !is_adjacent(n, a, dir)))
623 get_side_handle(a->client, dir2, &pt2);
624 double ds2 = distance(pt, pt2);
634 void get_opposite(direction_t src, direction_t *dst)
652 int tiled_area(node_t *n)
656 xcb_rectangle_t rect = n->client->tiled_rectangle;
657 return rect.width * rect.height;
660 node_t *find_biggest(desktop_t *d, node_t *c, client_select_t sel)
666 int r_area = tiled_area(r);
668 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
669 if (!is_visible(d, f) || !is_tiled(f->client) || !node_matches(c, f, sel))
671 int f_area = tiled_area(f);
675 } else if (f_area > r_area) {
684 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
689 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
690 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
691 change_split_ratio(n, CHANGE_INCREASE);
693 change_split_ratio(n, CHANGE_DECREASE);
696 void rotate_tree(node_t *n, int deg)
698 if (n == NULL || is_leaf(n) || deg == 0)
703 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL)
704 || (deg == 270 && n->split_type == TYPE_VERTICAL)
706 tmp = n->first_child;
707 n->first_child = n->second_child;
708 n->second_child = tmp;
709 n->split_ratio = 1.0 - n->split_ratio;
713 if (n->split_type == TYPE_HORIZONTAL)
714 n->split_type = TYPE_VERTICAL;
715 else if (n->split_type == TYPE_VERTICAL)
716 n->split_type = TYPE_HORIZONTAL;
719 rotate_tree(n->first_child, deg);
720 rotate_tree(n->second_child, deg);
723 void rotate_brother(node_t *n)
725 rotate_tree(brother_tree(n), n->birth_rotation);
728 void unrotate_tree(node_t *n, int rot)
732 rotate_tree(n, 360 - rot);
735 void unrotate_brother(node_t *n)
737 unrotate_tree(brother_tree(n), n->birth_rotation);
740 void flip_tree(node_t *n, flip_t flp)
742 if (n == NULL || is_leaf(n))
747 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
748 || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
749 tmp = n->first_child;
750 n->first_child = n->second_child;
751 n->second_child = tmp;
752 n->split_ratio = 1.0 - n->split_ratio;
755 flip_tree(n->first_child, flp);
756 flip_tree(n->second_child, flp);
759 int balance_tree(node_t *n)
761 if (n == NULL || n->vacant) {
763 } else if (is_leaf(n)) {
766 int b1 = balance_tree(n->first_child);
767 int b2 = balance_tree(n->second_child);
769 if (b1 > 0 && b2 > 0)
770 n->split_ratio = (double) b1 / b;
775 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
777 if (d == NULL || n == NULL)
780 PRINTF("unlink node %X\n", n->client->window);
782 node_t *p = n->parent;
789 d->focus = history_get_node(d, n);
790 if (d->focus == NULL)
791 d->focus = closest_visible(d, n);
795 node_t *g = p->parent;
797 if (is_first_child(n)) {
800 unrotate_tree(b, n->birth_rotation);
804 unrotate_tree(b, n->birth_rotation);
810 if (is_first_child(p))
818 b->birth_rotation = p->birth_rotation;
821 update_vacant_state(b->parent);
823 if (n->client->sticky)
828 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
833 PRINTF("remove node %X\n", n->client->window);
835 unlink_node(m, d, n);
836 history_remove(d, n);
837 remove_stack_node(n);
842 ewmh_update_client_list();
848 void destroy_tree(node_t *n)
852 node_t *first_tree = n->first_child;
853 node_t *second_tree = n->second_child;
854 if (n->client != NULL)
857 destroy_tree(first_tree);
858 destroy_tree(second_tree);
861 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
863 if (n1 == NULL || n2 == NULL || n1 == n2 || (d1 != d2 && (n1->client->sticky || n2->client->sticky)))
866 PRINTF("swap nodes %X %X", n1->client->window, n2->client->window);
868 node_t *pn1 = n1->parent;
869 node_t *pn2 = n2->parent;
870 bool n1_first_child = is_first_child(n1);
871 bool n2_first_child = is_first_child(n2);
872 int br1 = n1->birth_rotation;
873 int br2 = n2->birth_rotation;
877 pn1->first_child = n2;
879 pn1->second_child = n2;
884 pn2->first_child = n1;
886 pn2->second_child = n1;
891 n1->birth_rotation = br2;
892 n2->birth_rotation = br1;
894 if (n1->vacant != n2->vacant) {
895 update_vacant_state(n1->parent);
896 update_vacant_state(n2->parent);
910 fit_monitor(m1, n2->client);
911 fit_monitor(m2, n1->client);
914 ewmh_set_wm_desktop(n1, d2);
915 ewmh_set_wm_desktop(n2, d1);
916 history_swap_nodes(m1, d1, n1, m2, d2, n2);
918 if (m1->desk != d1 && m2->desk == d2) {
919 window_show(n1->client->window);
920 window_hide(n2->client->window);
921 } else if (m1->desk == d1 && m2->desk != d2) {
922 window_hide(n1->client->window);
923 window_show(n2->client->window);
926 tag_node(m1, d1, n2, d2, n2->client->tags_field);
927 tag_node(m2, d2, n1, d1, n1->client->tags_field);
929 update_input_focus();
935 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
937 if (ns == NULL || ns == nd || (sticky_still && ns->client->sticky))
940 PRINTF("transfer node %X\n", ns->client->window);
942 bool focused = (ns == mon->desk->focus);
943 bool active = (ns == ds->focus);
948 unlink_node(ms, ds, ns);
949 insert_node(md, dd, ns, nd);
952 fit_monitor(md, ns->client);
955 ewmh_set_wm_desktop(ns, dd);
956 if (!ns->client->sticky) {
957 if (ds == ms->desk && dd != md->desk)
958 window_hide(ns->client->window);
959 else if (ds != ms->desk && dd == md->desk)
960 window_show(ns->client->window);
964 history_transfer_node(md, dd, ns);
969 focus_node(md, dd, ns);
971 pseudo_focus(dd, ns);
975 else if (ns == mon->desk->focus)
976 update_input_focus();
979 tag_node(md, dd, ns, ds, ns->client->tags_field);
987 node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
992 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
994 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
997 if (node_matches(n, f, sel))
999 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
1001 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
1006 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
1008 if (d == NULL || d->root == NULL || d->focus == NULL || is_leaf(d->root))
1010 node_t *p = d->focus->parent;
1011 bool focus_first_child = is_first_child(d->focus);
1012 node_t *head, *tail;
1013 for (head = first_extrema(d->root); head != NULL && !is_visible(d, head); head = next_leaf(head, d->root))
1015 for (tail = second_extrema(d->root); tail != NULL && !is_visible(d, tail); tail = prev_leaf(tail, d->root))
1019 if (dir == CIRCULATE_FORWARD)
1020 for (node_t *s = tail, *f = prev_visible_leaf(d, s, d->root); f != NULL; s = prev_visible_leaf(d, f, d->root), f = prev_visible_leaf(d, s, d->root))
1021 swap_nodes(m, d, f, m, d, s);
1023 for (node_t *f = head, *s = next_visible_leaf(d, f, d->root); s != NULL; f = next_visible_leaf(d, s, d->root), s = next_visible_leaf(d, f, d->root))
1024 swap_nodes(m, d, f, m, d, s);
1025 if (focus_first_child)
1026 focus_node(m, d, p->first_child);
1028 focus_node(m, d, p->second_child);
1031 void update_vacant_state(node_t *n)
1036 PUTS("update vacant state");
1038 /* n is not a leaf */
1042 p->vacant = (p->first_child->vacant && p->second_child->vacant);