6 #include <xcb/xcb_event.h>
15 bool is_leaf(node_t *n)
17 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
20 bool is_tiled(client_t *c)
24 return (!c->floating && !c->transient && !c->fullscreen);
27 bool is_floating(client_t *c)
31 return (c->floating && !c->fullscreen);
34 bool is_first_child(node_t *n)
36 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
39 bool is_second_child(node_t *n)
41 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
44 void change_split_ratio(node_t *n, value_change_t chg) {
45 n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
48 node_t *first_extrema(node_t *n)
52 else if (n->first_child == NULL)
55 return first_extrema(n->first_child);
58 node_t *second_extrema(node_t *n)
62 else if (n->second_child == NULL)
65 return second_extrema(n->second_child);
68 node_t *next_leaf(node_t *n)
73 while (is_second_child(p))
75 if (p->parent == NULL)
77 return first_extrema(p->parent->second_child);
80 node_t *prev_leaf(node_t *n)
85 while (is_first_child(p))
87 if (p->parent == NULL)
89 return second_extrema(p->parent->first_child);
92 node_t *find_fence(node_t *n, direction_t dir)
102 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
103 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
104 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
105 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
113 node_t *find_neighbor(node_t *n, direction_t dir)
115 node_t *fence = find_fence(n, dir);
120 if (dir == DIR_UP || dir == DIR_LEFT)
121 return second_extrema(fence->first_child);
122 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
123 return first_extrema(fence->second_child);
128 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
130 node_t *fence = find_fence(n, dir);
135 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
136 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
137 change_split_ratio(fence, CHANGE_INCREASE);
139 change_split_ratio(fence, CHANGE_DECREASE);
142 void rotate_tree(node_t *n, rotate_t rot)
144 if (n == NULL || is_leaf(n))
149 if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
150 || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
151 || rot == ROTATE_FULL_CYCLE) {
152 tmp = n->first_child;
153 n->first_child = n->second_child;
154 n->second_child = tmp;
155 n->split_ratio = 1.0 - n->split_ratio;
158 if (rot != ROTATE_FULL_CYCLE) {
159 if (n->split_type == TYPE_HORIZONTAL)
160 n->split_type = TYPE_VERTICAL;
161 else if (n->split_type == TYPE_VERTICAL)
162 n->split_type = TYPE_HORIZONTAL;
165 rotate_tree(n->first_child, rot);
166 rotate_tree(n->second_child, rot);
169 void dump_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
176 for (unsigned int i = 0; i < depth; i++)
177 strncat(rsp, " ", REMLEN(rsp));
180 snprintf(line, sizeof(line), "%s %X %s%s%s%s%s", n->client->class_name, n->client->window, (n->client->floating ? "f" : "-"), (n->client->transient ? "t" : "-"), (n->client->fullscreen ? "F" : "-"), (n->client->urgent ? "u" : "-"), (n->client->locked ? "l" : "-"));
182 snprintf(line, sizeof(line), "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
184 strncat(rsp, line, REMLEN(rsp));
187 strncat(rsp, " *\n", REMLEN(rsp));
189 strncat(rsp, "\n", REMLEN(rsp));
191 dump_tree(d, n->first_child, rsp, depth + 1);
192 dump_tree(d, n->second_child, rsp, depth + 1);
195 void refresh_current(void) {
196 if (mon->desk->focus == NULL)
197 ewmh_update_active_window();
199 focus_node(mon, mon->desk, mon->desk->focus, true);
202 void list_monitors(list_option_t opt, char *rsp)
204 monitor_t *m = mon_head;
207 strncat(rsp, m->name, REMLEN(rsp));
209 strncat(rsp, " #\n", REMLEN(rsp));
211 strncat(rsp, "\n", REMLEN(rsp));
212 if (opt == LIST_OPTION_VERBOSE)
213 list_desktops(m, opt, 1, rsp);
218 void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
220 desktop_t *d = m->desk_head;
223 for (unsigned int i = 0; i < depth; i++)
224 strncat(rsp, " ", REMLEN(rsp));
225 strncat(rsp, d->name, REMLEN(rsp));
227 strncat(rsp, " @\n", REMLEN(rsp));
229 strncat(rsp, "\n", REMLEN(rsp));
230 if (opt == LIST_OPTION_VERBOSE)
231 dump_tree(d, d->root, rsp, depth + 1);
236 void put_status(void)
241 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
242 printf("%c%s:", (mon == m ? 'M' : 'm'), m->name);
243 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
244 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
245 urgent |= n->client->urgent;
246 printf("%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
249 printf("L%c:W%X\n", (mon->desk->layout == LAYOUT_TILED ? 'T' : 'M'), (mon->desk->focus == NULL ? 0 : mon->desk->focus->client->window));
253 void arrange(monitor_t *m, desktop_t *d)
255 PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
257 xcb_rectangle_t rect = m->rectangle;
258 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
259 rect.x += m->left_padding + wg;
260 rect.y += m->top_padding + wg;
261 rect.width -= m->left_padding + m->right_padding + wg;
262 rect.height -= m->top_padding + m->bottom_padding + wg;
263 if (focus_follows_mouse)
264 get_pointer_position(&pointer_position);
265 apply_layout(m, d, d->root, rect, rect);
268 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
276 if (n->client->fullscreen)
279 if (is_floating(n->client) && n->client->border_width != border_width) {
280 int ds = 2 * (border_width - n->client->border_width);
281 n->client->floating_rectangle.width += ds;
282 n->client->floating_rectangle.height += ds;
285 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
286 n->client->border_width = 0;
288 n->client->border_width = border_width;
291 if (is_tiled(n->client)) {
292 if (d->layout == LAYOUT_TILED)
294 else if (d->layout == LAYOUT_MONOCLE)
296 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
297 int bleed = wg + 2 * n->client->border_width;
298 r.width = (bleed < r.width ? r.width - bleed : 1);
299 r.height = (bleed < r.height ? r.height - bleed : 1);
300 n->client->tiled_rectangle = r;
302 r = n->client->floating_rectangle;
305 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
306 window_border_width(n->client->window, n->client->border_width);
307 window_draw_border(n, n == d->focus, m == mon);
310 xcb_rectangle_t first_rect;
311 xcb_rectangle_t second_rect;
313 if (n->first_child->vacant || n->second_child->vacant) {
314 first_rect = second_rect = rect;
317 if (n->split_type == TYPE_VERTICAL) {
318 fence = rect.width * n->split_ratio;
319 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
320 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
322 } else if (n->split_type == TYPE_HORIZONTAL) {
323 fence = rect.height * n->split_ratio;
324 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
325 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
329 apply_layout(m, d, n->first_child, first_rect, root_rect);
330 apply_layout(m, d, n->second_child, second_rect, root_rect);
334 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
336 if (d == NULL || n == NULL)
339 PRINTF("insert node %X\n", n->client->window);
341 node_t *focus = d->focus;
346 node_t *dad = make_node();
347 node_t *fopar = focus->parent;
349 n->born_as = split_mode;
350 switch (split_mode) {
353 dad->first_child = n;
354 dad->second_child = focus;
355 if (m->rectangle.width > m->rectangle.height)
356 dad->split_type = TYPE_VERTICAL;
358 dad->split_type = TYPE_HORIZONTAL;
362 node_t *grandpa = fopar->parent;
363 dad->parent = grandpa;
364 if (grandpa != NULL) {
365 if (is_first_child(fopar))
366 grandpa->first_child = dad;
368 grandpa->second_child = dad;
372 dad->split_type = fopar->split_type;
373 dad->split_ratio = fopar->split_ratio;
375 if (is_first_child(focus)) {
376 dad->first_child = n;
377 dad->second_child = fopar;
378 rotate_tree(fopar, ROTATE_CLOCKWISE);
380 dad->first_child = fopar;
381 dad->second_child = n;
382 rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
388 if (is_first_child(focus))
389 fopar->first_child = dad;
391 fopar->second_child = dad;
393 dad->split_ratio = focus->split_ratio;
398 dad->split_type = TYPE_VERTICAL;
399 dad->first_child = n;
400 dad->second_child = focus;
403 dad->split_type = TYPE_VERTICAL;
404 dad->first_child = focus;
405 dad->second_child = n;
408 dad->split_type = TYPE_HORIZONTAL;
409 dad->first_child = n;
410 dad->second_child = focus;
413 dad->split_type = TYPE_HORIZONTAL;
414 dad->first_child = focus;
415 dad->second_child = n;
418 if (d->root == focus)
420 split_mode = MODE_AUTOMATIC;
424 update_vacant_state(fopar);
428 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
433 PRINTF("focus node %X\n", n->client->window);
435 split_mode = MODE_AUTOMATIC;
436 n->client->urgent = false;
439 if (mon != m || d->focus != n) {
440 window_draw_border(d->focus, m != mon, m == mon);
441 window_draw_border(n, true, true);
443 if (focus_follows_mouse)
444 get_pointer_position(&pointer_position);
445 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
448 if (!is_tiled(n->client)) {
449 if (!adaptative_raise || !might_cover(d, n))
450 window_raise(n->client->window);
452 window_pseudo_raise(d, n->client->window);
456 d->last_focus = d->focus;
460 ewmh_update_active_window();
464 void update_current(void)
466 if (mon->desk->focus == NULL)
467 ewmh_update_active_window();
469 focus_node(mon, mon->desk, mon->desk->focus, true);
473 void unlink_node(desktop_t *d, node_t *n)
475 if (d == NULL || n == NULL)
478 PRINTF("unlink node %X\n", n->client->window);
480 node_t *p = n->parent;
485 d->last_focus = NULL;
488 node_t *g = p->parent;
489 bool n_first_child = is_first_child(n);
492 if (n->born_as == MODE_AUTOMATIC)
493 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
496 if (n->born_as == MODE_AUTOMATIC)
497 rotate_tree(b, ROTATE_CLOCKWISE);
501 if (is_first_child(p))
512 if (n == d->last_focus) {
513 d->last_focus = NULL;
514 } else if (n == d->focus) {
515 if (d->last_focus != NULL)
516 d->focus = d->last_focus;
518 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
519 d->last_focus = NULL;
522 update_vacant_state(b->parent);
526 void remove_node(desktop_t *d, node_t *n)
528 if (d == NULL || n == NULL)
531 PRINTF("remove node %X\n", n->client->window);
538 ewmh_update_client_list();
544 void swap_nodes(node_t *n1, node_t *n2)
546 if (n1 == NULL || n2 == NULL || n1 == n2)
551 /* (n1 and n2 are leaves) */
552 node_t *pn1 = n1->parent;
553 node_t *pn2 = n2->parent;
554 bool n1_first_child = is_first_child(n1);
555 bool n2_first_child = is_first_child(n2);
559 pn1->first_child = n2;
561 pn1->second_child = n2;
566 pn2->first_child = n1;
568 pn2->second_child = n1;
574 if (n1->vacant != n2->vacant) {
575 update_vacant_state(n1->parent);
576 update_vacant_state(n2->parent);
580 void fit_monitor(monitor_t *m, client_t *c)
582 xcb_rectangle_t crect = c->floating_rectangle;
583 xcb_rectangle_t mrect = m->rectangle;
584 while (crect.x < mrect.x)
585 crect.x += mrect.width;
586 while (crect.x > (mrect.x + mrect.width - 1))
587 crect.x -= mrect.width;
588 while (crect.y < mrect.y)
589 crect.y += mrect.height;
590 while (crect.y > (mrect.y + mrect.height - 1))
591 crect.y -= mrect.height;
592 c->floating_rectangle = crect;
595 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
597 if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
600 PRINTF("transfer node %X\n", n->client->window);
603 insert_node(md, dd, n);
604 ewmh_set_wm_desktop(n, dd);
606 if (ds == ms->desk && dd != md->desk) {
607 window_hide(n->client->window);
610 fit_monitor(md, n->client);
612 if (n->client->fullscreen)
613 window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
615 if (ds != ms->desk && dd == md->desk) {
616 window_show(n->client->window);
617 focus_node(md, dd, n, true);
619 focus_node(md, dd, n, false);
622 if (ds == ms->desk || dd == md->desk)
626 void select_monitor(monitor_t *m)
628 if (m == NULL || mon == m)
631 PRINTF("select monitor %s\n", m->name);
633 focus_node(m, m->desk, m->desk->focus, true);
638 ewmh_update_current_desktop();
642 void select_desktop(desktop_t *d)
644 if (d == NULL || d == mon->desk)
647 PRINTF("select desktop %s\n", d->name);
649 node_t *n = first_extrema(d->root);
652 window_show(n->client->window);
656 n = first_extrema(mon->desk->root);
659 window_hide(n->client->window);
663 mon->last_desk = mon->desk;
667 ewmh_update_current_desktop();
671 void cycle_monitor(cycle_dir_t dir)
673 if (dir == CYCLE_NEXT)
674 select_monitor((mon->next == NULL ? mon_head : mon->next));
675 else if (dir == CYCLE_PREV)
676 select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
679 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
681 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
683 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
686 if (skip == DESKTOP_SKIP_NONE
687 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
688 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
692 f = (dir == CYCLE_PREV ? f->prev : f->next);
694 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
698 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
705 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
707 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
710 bool tiled = is_tiled(f->client);
711 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
712 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
713 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
714 focus_node(m, d, f, true);
717 f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
719 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
723 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
728 PUTS("nearest leaf");
732 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
733 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
734 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
735 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
736 if ((dir == NEAREST_OLDER
737 && (f->client->uid < n->client->uid)
738 && (x == NULL || f->client->uid > x->client->uid))
739 || (dir == NEAREST_NEWER
740 && (f->client->uid > n->client->uid)
741 && (x == NULL || f->client->uid < x->client->uid)))
744 focus_node(m, d, x, true);
747 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
748 if (d == NULL || d->root == NULL || is_leaf(d->root))
750 node_t *par = d->focus->parent;
751 bool focus_first_child = is_first_child(d->focus);
752 if (dir == CIRCULATE_FORWARD)
753 for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
756 for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
758 if (focus_first_child)
759 focus_node(m, d, par->first_child, true);
761 focus_node(m, d, par->second_child, true);
764 void update_vacant_state(node_t *n)
769 PUTS("update vacant state");
771 /* n is not a leaf */
775 p->vacant = (p->first_child->vacant && p->second_child->vacant);
780 monitor_t *find_monitor(char *name)
782 for (monitor_t *m = mon_head; m != NULL; m = m->next)
783 if (strcmp(m->name, name) == 0)
788 void add_desktop(monitor_t *m, char *name)
790 desktop_t *d = make_desktop(name);
791 if (m->desk == NULL) {
796 m->desk_tail->next = d;
797 d->prev = m->desk_tail;
801 ewmh_update_number_of_desktops();
802 ewmh_update_desktop_names();
806 void add_monitor(xcb_rectangle_t *rect)
808 monitor_t *m = make_monitor(rect);