15 void arrange(monitor_t *m, desktop_t *d)
20 PRINTF("arrange %s %s\n", m->name, d->name);
22 xcb_rectangle_t rect = m->rectangle;
23 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
24 rect.x += m->left_padding + wg;
25 rect.y += m->top_padding + wg;
26 rect.width -= m->left_padding + m->right_padding + wg;
27 rect.height -= m->top_padding + m->bottom_padding + wg;
28 apply_layout(m, d, d->root, rect, rect);
31 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
40 if (is_floating(n->client) && n->client->border_width != d->border_width) {
41 int ds = 2 * (d->border_width - n->client->border_width);
42 n->client->floating_rectangle.width += ds;
43 n->client->floating_rectangle.height += ds;
46 if ((borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE) ||
47 n->client->fullscreen)
48 n->client->border_width = 0;
50 n->client->border_width = d->border_width;
53 if (!n->client->fullscreen) {
54 if (!n->client->floating) {
56 if (d->layout == LAYOUT_TILED)
58 else if (d->layout == LAYOUT_MONOCLE)
62 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
63 int bleed = wg + 2 * n->client->border_width;
64 r.width = (bleed < r.width ? r.width - bleed : 1);
65 r.height = (bleed < r.height ? r.height - bleed : 1);
66 n->client->tiled_rectangle = r;
68 /* floating clients */
69 r = n->client->floating_rectangle;
72 /* fullscreen clients */
76 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
77 window_border_width(n->client->window, n->client->border_width);
78 window_draw_border(n, n == d->focus, m == mon);
81 xcb_rectangle_t first_rect;
82 xcb_rectangle_t second_rect;
84 if (n->first_child->vacant || n->second_child->vacant) {
85 first_rect = second_rect = rect;
88 if (n->split_type == TYPE_VERTICAL) {
89 fence = rect.width * n->split_ratio;
90 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
91 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
92 } else if (n->split_type == TYPE_HORIZONTAL) {
93 fence = rect.height * n->split_ratio;
94 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
95 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
99 apply_layout(m, d, n->first_child, first_rect, root_rect);
100 apply_layout(m, d, n->second_child, second_rect, root_rect);
104 void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
106 if (d == NULL || n == NULL)
109 PRINTF("insert node %X\n", n->client->window);
111 /* n: new leaf node */
112 /* c: new container node */
113 /* f: focus or insertion anchor */
114 /* p: parent of focus */
115 /* g: grand parent of focus */
121 node_t *c = make_node();
122 node_t *p = f->parent;
124 c->birth_rotation = f->birth_rotation;
125 switch (f->split_mode) {
130 if (m->rectangle.width > m->rectangle.height)
131 c->split_type = TYPE_VERTICAL;
133 c->split_type = TYPE_HORIZONTAL;
137 node_t *g = p->parent;
140 if (is_first_child(p))
147 c->split_type = p->split_type;
148 c->split_ratio = p->split_ratio;
151 if (is_first_child(f)) {
160 if (!is_floating(n->client))
162 n->birth_rotation = rot;
167 if (is_first_child(f))
172 c->split_ratio = f->split_ratio;
175 f->birth_rotation = 0;
176 switch (f->split_dir) {
178 c->split_type = TYPE_VERTICAL;
183 c->split_type = TYPE_VERTICAL;
188 c->split_type = TYPE_HORIZONTAL;
193 c->split_type = TYPE_HORIZONTAL;
200 f->split_mode = MODE_AUTOMATIC;
204 update_vacant_state(p);
209 void pseudo_focus(desktop_t *d, node_t *n)
217 void focus_node(monitor_t *m, desktop_t *d, node_t *n)
219 if (n == NULL && d->root != NULL)
222 if (mon->desk != d || n == NULL)
225 if (n != NULL && d->focus != NULL && n != d->focus && d->focus->client->fullscreen) {
226 set_fullscreen(d->focus, false);
231 for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
232 window_draw_border(cd->focus, true, false);
233 for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
235 window_draw_border(cd->focus, true, true);
237 window_draw_border(n, true, true);
241 window_draw_border(d->focus, false, true);
242 window_draw_border(n, true, true);
245 select_desktop(m, d);
248 history_add(m, d, NULL);
249 ewmh_update_active_window();
253 PRINTF("focus node %X\n", n->client->window);
255 n->client->urgent = false;
258 history_add(m, d, n);
261 if (focus_follows_pointer) {
262 xcb_window_t win = XCB_NONE;
263 query_pointer(&win, NULL);
264 if (win != n->client->window)
265 enable_motion_recorder();
267 disable_motion_recorder();
270 ewmh_update_active_window();
273 void update_current(void)
275 focus_node(mon, mon->desk, mon->desk->focus);
278 node_t *make_node(void)
280 node_t *n = malloc(sizeof(node_t));
281 n->parent = n->first_child = n->second_child = NULL;
282 n->split_ratio = split_ratio;
283 n->split_mode = MODE_AUTOMATIC;
284 n->split_type = TYPE_VERTICAL;
285 n->birth_rotation = 0;
291 client_t *make_client(xcb_window_t win)
293 client_t *c = malloc(sizeof(client_t));
294 snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
295 c->border_width = BORDER_WIDTH;
297 c->floating = c->transient = c->fullscreen = c->locked = c->urgent = false;
298 c->icccm_focus = false;
299 xcb_icccm_get_wm_protocols_reply_t protocols;
300 if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
301 if (has_proto(WM_TAKE_FOCUS, &protocols))
302 c->icccm_focus = true;
303 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
308 bool is_leaf(node_t *n)
310 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
313 bool is_tiled(client_t *c)
317 return (!c->floating && !c->fullscreen);
320 bool is_floating(client_t *c)
324 return (c->floating && !c->fullscreen);
327 bool is_first_child(node_t *n)
329 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
332 bool is_second_child(node_t *n)
334 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
337 void change_split_ratio(node_t *n, value_change_t chg)
339 n->split_ratio = pow(n->split_ratio,
340 (chg == CHANGE_INCREASE ? (1 / growth_factor) : growth_factor));
343 void reset_mode(coordinates_t *loc)
345 if (loc->node != NULL) {
346 loc->node->split_mode = MODE_AUTOMATIC;
347 window_draw_border(loc->node, loc->desktop->focus == loc->node, mon == loc->monitor);
348 } else if (loc->desktop != NULL) {
349 for (node_t *a = first_extrema(loc->desktop->root); a != NULL; a = next_leaf(a, loc->desktop->root)) {
350 a->split_mode = MODE_AUTOMATIC;
351 window_draw_border(a, loc->desktop->focus == a, mon == loc->monitor);
356 node_t *brother_tree(node_t *n)
358 if (n == NULL || n->parent == NULL)
360 if (is_first_child(n))
361 return n->parent->second_child;
363 return n->parent->first_child;
366 node_t *first_extrema(node_t *n)
370 else if (n->first_child == NULL)
373 return first_extrema(n->first_child);
376 node_t *second_extrema(node_t *n)
380 else if (n->second_child == NULL)
383 return second_extrema(n->second_child);
386 node_t *next_leaf(node_t *n, node_t *r)
391 while (is_second_child(p) && p != r)
395 return first_extrema(p->parent->second_child);
398 node_t *prev_leaf(node_t *n, node_t *r)
403 while (is_first_child(p) && p != r)
407 return second_extrema(p->parent->first_child);
410 /* bool is_adjacent(node_t *a, node_t *r) */
412 /* node_t *f = r->parent; */
414 /* bool first_child = is_first_child(r); */
415 /* while (p != r) { */
416 /* if (p->parent->split_type == f->split_type && is_first_child(p) == first_child) */
423 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
424 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
428 return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
431 return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
434 return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
437 return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
443 node_t *find_fence(node_t *n, direction_t dir)
453 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
454 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
455 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
456 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
465 node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
467 if (n == NULL || n->client->fullscreen
468 || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
471 node_t *nearest = NULL;
472 if (history_aware_focus)
473 nearest = nearest_from_history(d, n, dir, sel);
475 nearest = nearest_from_distance(d, n, dir, sel);
479 node_t *nearest_from_history(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
481 if (n == NULL || !is_tiled(n->client))
484 node_t *target = find_fence(n, dir);
487 if (dir == DIR_UP || dir == DIR_LEFT)
488 target = target->first_child;
489 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
490 target = target->second_child;
492 node_t *nearest = NULL;
493 int min_rank = INT_MAX;
495 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
496 if (a->vacant || !is_adjacent(n, a, dir) || a == n)
498 if (!node_matches(n, a, sel))
501 int rank = history_rank(d, a);
502 if (rank >= 0 && rank < min_rank) {
511 node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
516 node_t *target = NULL;
518 if (is_tiled(n->client)) {
519 target = find_fence(n, dir);
522 if (dir == DIR_UP || dir == DIR_LEFT)
523 target = target->first_child;
524 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
525 target = target->second_child;
530 node_t *nearest = NULL;
534 get_side_handle(n->client, dir, &pt);
535 get_opposite(dir, &dir2);
538 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
539 if (a == n) continue;
540 if (!node_matches(n, a, sel)) continue;
541 if (is_tiled(a->client) != is_tiled(n->client)) continue;
542 if (is_tiled(a->client) && !is_adjacent(n, a, dir)) continue;
544 get_side_handle(a->client, dir2, &pt2);
545 double ds2 = distance(pt, pt2);
555 void get_opposite(direction_t src, direction_t *dst)
573 int tiled_area(node_t *n)
577 xcb_rectangle_t rect = n->client->tiled_rectangle;
578 return rect.width * rect.height;
581 node_t *find_biggest(desktop_t *d, node_t *c, client_select_t sel)
587 int r_area = tiled_area(r);
589 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
590 if (!is_tiled(f->client) || !node_matches(c, f, sel))
592 int f_area = tiled_area(f);
596 } else if (f_area > r_area) {
605 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
610 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
611 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
612 change_split_ratio(n, CHANGE_INCREASE);
614 change_split_ratio(n, CHANGE_DECREASE);
617 void rotate_tree(node_t *n, int deg)
619 if (n == NULL || is_leaf(n) || deg == 0)
624 if ((deg == 90 && n->split_type == TYPE_HORIZONTAL)
625 || (deg == 270 && n->split_type == TYPE_VERTICAL)
627 tmp = n->first_child;
628 n->first_child = n->second_child;
629 n->second_child = tmp;
630 n->split_ratio = 1.0 - n->split_ratio;
634 if (n->split_type == TYPE_HORIZONTAL)
635 n->split_type = TYPE_VERTICAL;
636 else if (n->split_type == TYPE_VERTICAL)
637 n->split_type = TYPE_HORIZONTAL;
640 rotate_tree(n->first_child, deg);
641 rotate_tree(n->second_child, deg);
644 void rotate_brother(node_t *n)
646 rotate_tree(brother_tree(n), n->birth_rotation);
649 void unrotate_tree(node_t *n, int rot)
653 rotate_tree(n, 360 - rot);
656 void unrotate_brother(node_t *n)
658 unrotate_tree(brother_tree(n), n->birth_rotation);
661 void flip_tree(node_t *n, flip_t flp)
663 if (n == NULL || is_leaf(n))
668 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
669 || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
670 tmp = n->first_child;
671 n->first_child = n->second_child;
672 n->second_child = tmp;
673 n->split_ratio = 1.0 - n->split_ratio;
676 flip_tree(n->first_child, flp);
677 flip_tree(n->second_child, flp);
680 int balance_tree(node_t *n)
682 if (n == NULL || n->vacant) {
684 } else if (is_leaf(n)) {
687 int b1 = balance_tree(n->first_child);
688 int b2 = balance_tree(n->second_child);
690 if (b1 > 0 && b2 > 0)
691 n->split_ratio = (double) b1 / b;
696 void unlink_node(desktop_t *d, node_t *n)
698 if (d == NULL || n == NULL)
701 PRINTF("unlink node %X\n", n->client->window);
703 node_t *p = n->parent;
710 node_t *g = p->parent;
711 if (is_first_child(n)) {
714 unrotate_tree(b, n->birth_rotation);
718 unrotate_tree(b, n->birth_rotation);
722 if (is_first_child(p))
730 b->birth_rotation = p->birth_rotation;
735 d->focus = history_get_node(d, n);
736 if (d->focus == NULL && d->root != NULL)
737 d->focus = first_extrema(d->root);
740 update_vacant_state(b->parent);
745 void remove_node(desktop_t *d, node_t *n)
750 PRINTF("remove node %X\n", n->client->window);
753 history_remove(d, n);
754 remove_stack_node(n);
759 ewmh_update_client_list();
765 void destroy_tree(node_t *n)
769 node_t *first_tree = n->first_child;
770 node_t *second_tree = n->second_child;
771 if (n->client != NULL)
774 destroy_tree(first_tree);
775 destroy_tree(second_tree);
778 void swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
780 if (n1 == NULL || n2 == NULL || n1 == n2)
783 PRINTF("swap nodes %X %X", n1->client->window, n2->client->window);
785 node_t *pn1 = n1->parent;
786 node_t *pn2 = n2->parent;
787 bool n1_first_child = is_first_child(n1);
788 bool n2_first_child = is_first_child(n2);
789 int br1 = n1->birth_rotation;
790 int br2 = n2->birth_rotation;
794 pn1->first_child = n2;
796 pn1->second_child = n2;
801 pn2->first_child = n1;
803 pn2->second_child = n1;
808 n1->birth_rotation = br2;
809 n2->birth_rotation = br1;
811 if (n1->vacant != n2->vacant) {
812 update_vacant_state(n1->parent);
813 update_vacant_state(n2->parent);
827 fit_monitor(m1, n2->client);
828 fit_monitor(m2, n1->client);
831 ewmh_set_wm_desktop(n1, d2);
832 ewmh_set_wm_desktop(n2, d1);
833 history_swap_nodes(m1, d1, n1, m2, d2, n2);
835 if (m1->desk != d1 && m2->desk == d2) {
836 window_show(n1->client->window);
837 window_hide(n2->client->window);
838 } else if (m1->desk == d1 && m2->desk != d2) {
839 window_hide(n1->client->window);
840 window_show(n2->client->window);
843 update_input_focus();
847 void transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
849 if (ns == NULL || ns == nd)
852 PRINTF("transfer node %X\n", ns->client->window);
854 bool focused = (ns == mon->desk->focus);
855 bool active = (ns == ds->focus);
861 insert_node(md, dd, ns, nd);
864 fit_monitor(md, ns->client);
867 ewmh_set_wm_desktop(ns, dd);
868 if (ds == ms->desk && dd != md->desk)
869 window_hide(ns->client->window);
870 else if (ds != ms->desk && dd == md->desk)
871 window_show(ns->client->window);
874 history_transfer_node(md, dd, ns);
879 focus_node(md, dd, ns);
881 pseudo_focus(dd, ns);
891 node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
896 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
898 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
901 if (node_matches(n, f, sel))
903 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
905 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
910 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
912 if (d == NULL || d->root == NULL || is_leaf(d->root))
914 node_t *p = d->focus->parent;
915 bool focus_first_child = is_first_child(d->focus);
916 if (dir == CIRCULATE_FORWARD)
917 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))
918 swap_nodes(m, d, f, m, d, s);
920 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))
921 swap_nodes(m, d, f, m, d, s);
922 if (focus_first_child)
923 focus_node(m, d, p->first_child);
925 focus_node(m, d, p->second_child);
928 void update_vacant_state(node_t *n)
933 PUTS("update vacant state");
935 /* n is not a leaf */
939 p->vacant = (p->first_child->vacant && p->second_child->vacant);