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 arrange(monitor_t *m, desktop_t *d)
238 PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
240 xcb_rectangle_t rect = m->rectangle;
241 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
242 rect.x += left_padding + wg;
243 rect.y += top_padding + wg;
244 rect.width -= left_padding + right_padding + wg;
245 rect.height -= top_padding + bottom_padding + wg;
246 if (focus_follows_mouse)
247 get_pointer_position(&pointer_position);
248 apply_layout(m, d, d->root, rect, rect);
251 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
259 if (n->client->fullscreen)
262 if (is_floating(n->client) && n->client->border_width != border_width) {
263 int ds = 2 * (border_width - n->client->border_width);
264 n->client->floating_rectangle.width += ds;
265 n->client->floating_rectangle.height += ds;
268 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
269 n->client->border_width = 0;
271 n->client->border_width = border_width;
274 if (is_tiled(n->client)) {
275 if (d->layout == LAYOUT_TILED)
277 else if (d->layout == LAYOUT_MONOCLE)
279 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
280 int bleed = wg + 2 * n->client->border_width;
281 r.width = (bleed < r.width ? r.width - bleed : 1);
282 r.height = (bleed < r.height ? r.height - bleed : 1);
283 n->client->tiled_rectangle = r;
285 r = n->client->floating_rectangle;
288 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
289 window_border_width(n->client->window, n->client->border_width);
290 window_draw_border(n, n == d->focus, m == mon);
293 xcb_rectangle_t first_rect;
294 xcb_rectangle_t second_rect;
296 if (n->first_child->vacant || n->second_child->vacant) {
297 first_rect = second_rect = rect;
300 if (n->split_type == TYPE_VERTICAL) {
301 fence = rect.width * n->split_ratio;
302 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
303 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
305 } else if (n->split_type == TYPE_HORIZONTAL) {
306 fence = rect.height * n->split_ratio;
307 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
308 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
312 apply_layout(m, d, n->first_child, first_rect, root_rect);
313 apply_layout(m, d, n->second_child, second_rect, root_rect);
317 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
319 if (d == NULL || n == NULL)
322 PRINTF("insert node %X\n", n->client->window);
324 node_t *focus = d->focus;
329 node_t *dad = make_node();
330 node_t *fopar = focus->parent;
332 n->born_as = split_mode;
333 switch (split_mode) {
336 dad->first_child = n;
337 dad->second_child = focus;
338 if (m->rectangle.width > m->rectangle.height)
339 dad->split_type = TYPE_VERTICAL;
341 dad->split_type = TYPE_HORIZONTAL;
345 node_t *grandpa = fopar->parent;
346 dad->parent = grandpa;
347 if (grandpa != NULL) {
348 if (is_first_child(fopar))
349 grandpa->first_child = dad;
351 grandpa->second_child = dad;
355 dad->split_type = fopar->split_type;
356 dad->split_ratio = fopar->split_ratio;
358 if (is_first_child(focus)) {
359 dad->first_child = n;
360 dad->second_child = fopar;
361 rotate_tree(fopar, ROTATE_CLOCKWISE);
363 dad->first_child = fopar;
364 dad->second_child = n;
365 rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
371 if (is_first_child(focus))
372 fopar->first_child = dad;
374 fopar->second_child = dad;
376 dad->split_ratio = focus->split_ratio;
381 dad->split_type = TYPE_VERTICAL;
382 dad->first_child = n;
383 dad->second_child = focus;
386 dad->split_type = TYPE_VERTICAL;
387 dad->first_child = focus;
388 dad->second_child = n;
391 dad->split_type = TYPE_HORIZONTAL;
392 dad->first_child = n;
393 dad->second_child = focus;
396 dad->split_type = TYPE_HORIZONTAL;
397 dad->first_child = focus;
398 dad->second_child = n;
401 if (d->root == focus)
403 split_mode = MODE_AUTOMATIC;
407 update_vacant_state(fopar);
411 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
416 PRINTF("focus node %X\n", n->client->window);
418 split_mode = MODE_AUTOMATIC;
419 n->client->urgent = false;
422 if (mon != m || d->focus != n) {
423 window_draw_border(d->focus, m != mon, m == mon);
424 window_draw_border(n, true, true);
426 if (focus_follows_mouse)
427 get_pointer_position(&pointer_position);
428 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
431 if (!is_tiled(n->client))
432 window_raise(n->client->window);
434 window_pseudo_raise(d, n->client->window);
437 d->last_focus = d->focus;
441 ewmh_update_active_window();
444 void update_current(void)
446 if (mon->desk->focus == NULL)
447 ewmh_update_active_window();
449 focus_node(mon, mon->desk, mon->desk->focus, true);
452 void unlink_node(desktop_t *d, node_t *n)
454 if (d == NULL || n == NULL)
457 PRINTF("unlink node %X\n", n->client->window);
459 node_t *p = n->parent;
464 d->last_focus = NULL;
467 node_t *g = p->parent;
468 bool n_first_child = is_first_child(n);
471 if (n->born_as == MODE_AUTOMATIC)
472 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
475 if (n->born_as == MODE_AUTOMATIC)
476 rotate_tree(b, ROTATE_CLOCKWISE);
480 if (is_first_child(p))
491 if (n == d->last_focus) {
492 d->last_focus = NULL;
493 } else if (n == d->focus) {
494 if (d->last_focus != NULL)
495 d->focus = d->last_focus;
497 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
498 d->last_focus = NULL;
501 update_vacant_state(b->parent);
505 void remove_node(desktop_t *d, node_t *n)
507 if (d == NULL || n == NULL)
510 PRINTF("remove node %X\n", n->client->window);
517 ewmh_update_client_list();
523 void swap_nodes(node_t *n1, node_t *n2)
525 if (n1 == NULL || n2 == NULL || n1 == n2)
530 /* (n1 and n2 are leaves) */
531 node_t *pn1 = n1->parent;
532 node_t *pn2 = n2->parent;
533 bool n1_first_child = is_first_child(n1);
534 bool n2_first_child = is_first_child(n2);
538 pn1->first_child = n2;
540 pn1->second_child = n2;
545 pn2->first_child = n1;
547 pn2->second_child = n1;
553 if (n1->vacant != n2->vacant) {
554 update_vacant_state(n1->parent);
555 update_vacant_state(n2->parent);
559 void fit_monitor(monitor_t *m, client_t *c)
561 xcb_rectangle_t cr = c->floating_rectangle;
562 xcb_rectangle_t mr = m->rectangle;
569 void translate_coordinates(monitor_t *ms, monitor_t *md, client_t *c)
574 uint32_t vx = md->rectangle.x - ms->rectangle.x;
575 uint32_t vy = md->rectangle.y - ms->rectangle.y;
576 c->floating_rectangle.x += vx;
577 c->floating_rectangle.y += vy;
580 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
582 if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
585 PRINTF("transfer node %X\n", n->client->window);
588 insert_node(md, dd, n);
589 ewmh_set_wm_desktop(n, dd);
591 if (ds == ms->desk && dd != md->desk) {
592 window_hide(n->client->window);
595 translate_coordinates(ms, md, n->client);
596 if (n->client->fullscreen)
597 window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
599 if (ds != ms->desk && dd == md->desk) {
600 window_show(n->client->window);
601 focus_node(md, dd, n, true);
603 focus_node(md, dd, n, false);
606 if (ds == ms->desk || dd == md->desk)
610 void select_monitor(monitor_t *m)
612 if (m == NULL || mon == m)
615 PRINTF("select monitor %s\n", m->name);
617 focus_node(m, m->desk, m->desk->focus, true);
622 ewmh_update_current_desktop();
625 void select_desktop(desktop_t *d)
627 if (d == NULL || d == mon->desk)
630 PRINTF("select desktop %s\n", d->name);
632 node_t *n = first_extrema(d->root);
635 window_show(n->client->window);
639 n = first_extrema(mon->desk->root);
642 window_hide(n->client->window);
646 mon->last_desk = mon->desk;
650 ewmh_update_current_desktop();
653 void cycle_monitor(cycle_dir_t dir)
655 if (dir == CYCLE_NEXT)
656 select_monitor((mon->next == NULL ? mon_head : mon->next));
657 else if (dir == CYCLE_PREV)
658 select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
661 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
663 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
665 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
668 if (skip == DESKTOP_SKIP_NONE
669 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
670 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
674 f = (dir == CYCLE_PREV ? f->prev : f->next);
676 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
680 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
687 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
689 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
692 bool tiled = is_tiled(f->client);
693 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
694 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
695 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
696 focus_node(m, d, f, true);
699 f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
701 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
705 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
710 PUTS("nearest leaf");
714 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
715 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
716 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
717 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
718 if ((dir == NEAREST_OLDER
719 && (f->client->uid < n->client->uid)
720 && (x == NULL || f->client->uid > x->client->uid))
721 || (dir == NEAREST_NEWER
722 && (f->client->uid > n->client->uid)
723 && (x == NULL || f->client->uid < x->client->uid)))
726 focus_node(m, d, x, true);
729 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
730 if (d == NULL || d->root == NULL || is_leaf(d->root))
732 node_t *par = d->focus->parent;
733 bool focus_first_child = is_first_child(d->focus);
734 if (dir == CIRCULATE_FORWARD)
735 for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
738 for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
740 if (focus_first_child)
741 focus_node(m, d, par->first_child, true);
743 focus_node(m, d, par->second_child, true);
746 void update_vacant_state(node_t *n)
751 PUTS("update vacant state");
753 /* n is not a leaf */
757 p->vacant = (p->first_child->vacant && p->second_child->vacant);
762 monitor_t *find_monitor(char *name)
764 for (monitor_t *m = mon_head; m != NULL; m = m->next)
765 if (strcmp(m->name, name) == 0)
770 void add_desktop(monitor_t *m, char *name)
772 desktop_t *d = make_desktop(name);
773 if (m->desk == NULL) {
778 m->desk_tail->next = d;
779 d->prev = m->desk_tail;
783 ewmh_update_number_of_desktops();
784 ewmh_update_desktop_names();
787 void add_monitor(xcb_rectangle_t *rect)
789 monitor_t *m = make_monitor(rect);