14 void arrange(monitor_t *m, desktop_t *d)
19 PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
21 xcb_rectangle_t rect = m->rectangle;
22 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
23 rect.x += m->left_padding + wg;
24 rect.y += m->top_padding + wg;
25 rect.width -= m->left_padding + m->right_padding + wg;
26 rect.height -= m->top_padding + m->bottom_padding + wg;
27 apply_layout(m, d, d->root, rect, rect);
30 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
39 if (is_floating(n->client) && n->client->border_width != border_width) {
40 int ds = 2 * (border_width - n->client->border_width);
41 n->client->floating_rectangle.width += ds;
42 n->client->floating_rectangle.height += ds;
45 if ((borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE) ||
46 n->client->fullscreen)
47 n->client->border_width = 0;
49 n->client->border_width = border_width;
52 if (!n->client->fullscreen) {
53 if (!n->client->floating) {
55 if (d->layout == LAYOUT_TILED)
57 else if (d->layout == LAYOUT_MONOCLE)
61 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
62 int bleed = wg + 2 * n->client->border_width;
63 r.width = (bleed < r.width ? r.width - bleed : 1);
64 r.height = (bleed < r.height ? r.height - bleed : 1);
65 n->client->tiled_rectangle = r;
67 /* floating clients */
68 r = n->client->floating_rectangle;
71 /* fullscreen clients */
75 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
76 window_border_width(n->client->window, n->client->border_width);
77 window_draw_border(n, n == d->focus, m == mon);
80 xcb_rectangle_t first_rect;
81 xcb_rectangle_t second_rect;
83 if (n->first_child->vacant || n->second_child->vacant) {
84 first_rect = second_rect = rect;
87 if (n->split_type == TYPE_VERTICAL) {
88 fence = rect.width * n->split_ratio;
89 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
90 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
91 } else if (n->split_type == TYPE_HORIZONTAL) {
92 fence = rect.height * n->split_ratio;
93 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
94 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
98 apply_layout(m, d, n->first_child, first_rect, root_rect);
99 apply_layout(m, d, n->second_child, second_rect, root_rect);
103 void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
105 if (d == NULL || n == NULL)
108 PRINTF("insert node %X\n", n->client->window);
110 /* n: new leaf node */
111 /* c: new container node */
112 /* f: focus or insertion anchor */
113 /* p: parent of focus */
114 /* g: grand parent of focus */
119 node_t *c = make_node();
120 node_t *p = f->parent;
122 c->birth_rotation = f->birth_rotation;
123 switch (f->split_mode) {
128 if (m->rectangle.width > m->rectangle.height)
129 c->split_type = TYPE_VERTICAL;
131 c->split_type = TYPE_HORIZONTAL;
135 node_t *g = p->parent;
138 if (is_first_child(p))
145 c->split_type = p->split_type;
146 c->split_ratio = p->split_ratio;
149 if (is_first_child(f)) {
158 if (!is_floating(n->client))
160 n->birth_rotation = rot;
165 if (is_first_child(f))
170 c->split_ratio = f->split_ratio;
173 f->birth_rotation = 0;
174 switch (f->split_dir) {
176 c->split_type = TYPE_VERTICAL;
181 c->split_type = TYPE_VERTICAL;
186 c->split_type = TYPE_HORIZONTAL;
191 c->split_type = TYPE_HORIZONTAL;
198 f->split_mode = MODE_AUTOMATIC;
202 update_vacant_state(p);
207 void pseudo_focus(desktop_t *d, node_t *n)
209 if (n == NULL || d->focus == n)
212 history_add(d->history, n);
216 void focus_node(monitor_t *m, desktop_t *d, node_t *n)
218 if (n == NULL && d->root != NULL)
221 if (mon->desk != d || n == NULL)
225 for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
226 window_draw_border(cd->focus, true, false);
227 for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
229 window_draw_border(cd->focus, true, true);
231 window_draw_border(n, true, true);
235 window_draw_border(d->focus, false, true);
236 window_draw_border(n, true, true);
239 select_desktop(m, d);
242 ewmh_update_active_window();
246 PRINTF("focus node %X\n", n->client->window);
248 n->client->urgent = false;
253 if (focus_follows_pointer) {
254 xcb_window_t win = XCB_NONE;
255 query_pointer(&win, NULL);
256 if (win != n->client->window)
257 enable_motion_recorder();
259 disable_motion_recorder();
262 ewmh_update_active_window();
265 void update_current(void)
267 focus_node(mon, mon->desk, mon->desk->focus);
270 node_t *make_node(void)
272 node_t *n = malloc(sizeof(node_t));
273 n->parent = n->first_child = n->second_child = NULL;
274 n->split_ratio = split_ratio;
275 n->split_mode = MODE_AUTOMATIC;
276 n->split_type = TYPE_VERTICAL;
277 n->birth_rotation = 0;
283 client_t *make_client(xcb_window_t win)
285 client_t *c = malloc(sizeof(client_t));
286 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
287 c->border_width = border_width;
289 c->floating = c->transient = c->fullscreen = c->locked = c->urgent = false;
290 c->icccm_focus = false;
291 xcb_icccm_get_wm_protocols_reply_t protocols;
292 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
293 if (has_proto(WM_TAKE_FOCUS, &protocols))
294 c->icccm_focus = true;
295 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
300 bool is_leaf(node_t *n)
302 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
305 bool is_tiled(client_t *c)
309 return (!c->floating && !c->fullscreen);
312 bool is_floating(client_t *c)
316 return (c->floating && !c->fullscreen);
319 bool is_first_child(node_t *n)
321 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
324 bool is_second_child(node_t *n)
326 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
329 void change_split_ratio(node_t *n, value_change_t chg)
331 n->split_ratio = pow(n->split_ratio,
332 (chg == CHANGE_INCREASE ? (1 / growth_factor) : growth_factor));
335 void reset_mode(coordinates_t *loc)
337 if (loc->node != NULL) {
338 loc->node->split_mode = MODE_AUTOMATIC;
339 window_draw_border(loc->node, loc->desktop->focus == loc->node, mon == loc->monitor);
340 } else if (loc->desktop != NULL) {
341 for (node_t *a = first_extrema(loc->desktop->root); a != NULL; a = next_leaf(a, loc->desktop->root)) {
342 a->split_mode = MODE_AUTOMATIC;
343 window_draw_border(a, loc->desktop->focus == a, mon == loc->monitor);
348 node_t *brother_tree(node_t *n)
350 if (n == NULL || n->parent == NULL)
352 if (is_first_child(n))
353 return n->parent->second_child;
355 return n->parent->first_child;
358 node_t *first_extrema(node_t *n)
362 else if (n->first_child == NULL)
365 return first_extrema(n->first_child);
368 node_t *second_extrema(node_t *n)
372 else if (n->second_child == NULL)
375 return second_extrema(n->second_child);
378 node_t *next_leaf(node_t *n, node_t *r)
383 while (is_second_child(p) && p != r)
387 return first_extrema(p->parent->second_child);
390 node_t *prev_leaf(node_t *n, node_t *r)
395 while (is_first_child(p) && p != r)
399 return second_extrema(p->parent->first_child);
402 /* bool is_adjacent(node_t *a, node_t *r) */
404 /* node_t *f = r->parent; */
406 /* bool first_child = is_first_child(r); */
407 /* while (p != r) { */
408 /* if (p->parent->split_type == f->split_type && is_first_child(p) == first_child) */
415 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
416 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
420 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
423 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
426 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
429 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
435 node_t *find_fence(node_t *n, direction_t dir)
445 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
446 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
447 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
448 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
457 node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
459 if (n == NULL || n->client->fullscreen
460 || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
463 node_t *nearest = NULL;
464 if (history_aware_focus)
465 nearest = nearest_from_history(d->history, n, dir, sel);
467 nearest = nearest_from_distance(d, n, dir, sel);
471 node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir, client_select_t sel)
473 if (n == NULL || !is_tiled(n->client))
476 node_t *target = find_fence(n, dir);
479 if (dir == DIR_UP || dir == DIR_LEFT)
480 target = target->first_child;
481 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
482 target = target->second_child;
484 node_t *nearest = NULL;
485 int min_rank = INT_MAX;
487 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
488 if (a->vacant || !is_adjacent(n, a, dir) || a == n)
490 if (!node_matches(n, a, sel))
493 int rank = history_rank(f, a);
494 if (rank >= 0 && rank < min_rank) {
503 node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
508 node_t *target = NULL;
510 if (is_tiled(n->client)) {
511 target = find_fence(n, dir);
514 if (dir == DIR_UP || dir == DIR_LEFT)
515 target = target->first_child;
516 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
517 target = target->second_child;
522 node_t *nearest = NULL;
526 get_side_handle(n->client, dir, &pt);
527 get_opposite(dir, &dir2);
530 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
531 if (a == n) continue;
532 if (!node_matches(n, a, sel)) continue;
533 if (is_tiled(a->client) != is_tiled(n->client)) continue;
534 if (is_tiled(a->client) && !is_adjacent(n, a, dir)) continue;
536 get_side_handle(a->client, dir2, &pt2);
537 double ds2 = distance(pt, pt2);
547 void get_opposite(direction_t src, direction_t *dst)
565 int tiled_area(node_t *n)
569 xcb_rectangle_t rect = n->client->tiled_rectangle;
570 return rect.width * rect.height;
573 node_t *find_biggest(desktop_t *d, node_t *c, client_select_t sel)
579 int r_area = tiled_area(r);
581 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
582 if (!is_tiled(f->client) || !node_matches(c, f, sel))
584 int f_area = tiled_area(f);
588 } else if (f_area > r_area) {
597 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
602 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
603 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
604 change_split_ratio(n, CHANGE_INCREASE);
606 change_split_ratio(n, CHANGE_DECREASE);
609 void rotate_tree(node_t *n, int deg)
611 if (n == NULL || is_leaf(n) || deg == 0)
616 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL)
617 || (deg == 270 && n->split_type == TYPE_VERTICAL)
619 tmp = n->first_child;
620 n->first_child = n->second_child;
621 n->second_child = tmp;
622 n->split_ratio = 1.0 - n->split_ratio;
626 if (n->split_type == TYPE_HORIZONTAL)
627 n->split_type = TYPE_VERTICAL;
628 else if (n->split_type == TYPE_VERTICAL)
629 n->split_type = TYPE_HORIZONTAL;
632 rotate_tree(n->first_child, deg);
633 rotate_tree(n->second_child, deg);
636 void rotate_brother(node_t *n)
638 rotate_tree(brother_tree(n), n->birth_rotation);
641 void unrotate_tree(node_t *n, int rot)
645 rotate_tree(n, 360 - rot);
648 void unrotate_brother(node_t *n)
650 unrotate_tree(brother_tree(n), n->birth_rotation);
653 void flip_tree(node_t *n, flip_t flp)
655 if (n == NULL || is_leaf(n))
660 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
661 || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
662 tmp = n->first_child;
663 n->first_child = n->second_child;
664 n->second_child = tmp;
665 n->split_ratio = 1.0 - n->split_ratio;
668 flip_tree(n->first_child, flp);
669 flip_tree(n->second_child, flp);
672 int balance_tree(node_t *n)
674 if (n == NULL || n->vacant) {
676 } else if (is_leaf(n)) {
679 int b1 = balance_tree(n->first_child);
680 int b2 = balance_tree(n->second_child);
682 if (b1 > 0 && b2 > 0)
683 n->split_ratio = (double) b1 / b;
688 void unlink_node(desktop_t *d, node_t *n)
690 if (d == NULL || n == NULL)
693 PRINTF("unlink node %X\n", n->client->window);
695 node_t *p = n->parent;
702 node_t *g = p->parent;
703 if (is_first_child(n)) {
706 unrotate_tree(b, n->birth_rotation);
710 unrotate_tree(b, n->birth_rotation);
714 if (is_first_child(p))
722 b->birth_rotation = p->birth_rotation;
727 d->focus = history_get(d->history, 1);
729 update_vacant_state(b->parent);
734 void remove_node(desktop_t *d, node_t *n)
739 PRINTF("remove node %X\n", n->client->window);
742 history_remove(d->history, n);
747 ewmh_update_client_list();
753 void destroy_tree(node_t *n)
757 node_t *first_tree = n->first_child;
758 node_t *second_tree = n->second_child;
759 if (n->client != NULL)
762 destroy_tree(first_tree);
763 destroy_tree(second_tree);
766 void swap_nodes(node_t *n1, node_t *n2)
768 if (n1 == NULL || n2 == NULL || n1 == n2)
773 /* (n1 and n2 are leaves) */
774 node_t *pn1 = n1->parent;
775 node_t *pn2 = n2->parent;
776 bool n1_first_child = is_first_child(n1);
777 bool n2_first_child = is_first_child(n2);
778 int br1 = n1->birth_rotation;
779 int br2 = n2->birth_rotation;
783 pn1->first_child = n2;
785 pn1->second_child = n2;
790 pn2->first_child = n1;
792 pn2->second_child = n1;
797 n1->birth_rotation = br2;
798 n2->birth_rotation = br1;
800 if (n1->vacant != n2->vacant) {
801 update_vacant_state(n1->parent);
802 update_vacant_state(n2->parent);
805 /* If we ever need to generalize: */
806 /* if (d1 != d2) { */
807 /* if (d1->root == n1) */
809 /* if (d1->focus == n1) */
810 /* d1->focus = n2; */
811 /* if (d1->last_focus == n1) */
812 /* d1->last_focus = n2; */
813 /* if (d2->root == n2) */
815 /* if (d2->focus == n2) */
816 /* d2->focus = n1; */
817 /* if (d2->last_focus == n2) */
818 /* d2->last_focus = n1; */
822 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
824 if (n == NULL || dd == ds)
827 PRINTF("transfer node %X\n", n->client->window);
830 history_remove(ds->history, n);
831 insert_node(md, dd, n, dd->focus);
832 ewmh_set_wm_desktop(n, dd);
834 if (ds == ms->desk && dd != md->desk) {
837 window_hide(n->client->window);
840 fit_monitor(md, n->client);
842 if (ds != ms->desk && dd == md->desk)
843 window_show(n->client->window);
850 if (ds == ms->desk || dd == md->desk)
854 void transplant_node(monitor_t *m, desktop_t *d, node_t *n1, node_t *n2)
858 bool was_focused = (d->focus == n1);
860 insert_node(m, d, n1, n2);
865 node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
870 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
872 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
875 if (node_matches(n, f, sel))
877 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
879 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
884 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
886 if (d == NULL || d->root == NULL || is_leaf(d->root))
888 node_t *p = d->focus->parent;
889 bool focus_first_child = is_first_child(d->focus);
890 if (dir == CIRCULATE_FORWARD)
891 for (node_t *s = second_extrema(d->root), *f = prev_leaf(s, d->root); f != NULL; s = prev_leaf(f, d->root), f = prev_leaf(s, d->root))
894 for (node_t *f = first_extrema(d->root), *s = next_leaf(f, d->root); s != NULL; f = next_leaf(s, d->root), s = next_leaf(f, d->root))
896 if (focus_first_child)
897 focus_node(m, d, p->first_child);
899 focus_node(m, d, p->second_child);
902 void update_vacant_state(node_t *n)
907 PUTS("update vacant state");
909 /* n is not a leaf */
913 p->vacant = (p->first_child->vacant && p->second_child->vacant);