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 list_monitors(list_option_t opt, char *rsp)
197 monitor_t *m = mon_head;
200 strncat(rsp, m->name, REMLEN(rsp));
202 strncat(rsp, " #\n", REMLEN(rsp));
204 strncat(rsp, "\n", REMLEN(rsp));
205 if (opt == LIST_OPTION_VERBOSE)
206 list_desktops(m, opt, 1, rsp);
211 void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
213 desktop_t *d = m->desk_head;
216 for (unsigned int i = 0; i < depth; i++)
217 strncat(rsp, " ", REMLEN(rsp));
218 strncat(rsp, d->name, REMLEN(rsp));
220 strncat(rsp, " @\n", REMLEN(rsp));
222 strncat(rsp, "\n", REMLEN(rsp));
223 if (opt == LIST_OPTION_VERBOSE)
224 dump_tree(d, d->root, rsp, depth + 1);
229 void arrange(monitor_t *m, desktop_t *d)
231 PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
233 xcb_rectangle_t rect = m->rectangle;
234 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
235 rect.x += m->left_padding + wg;
236 rect.y += m->top_padding + wg;
237 rect.width -= m->left_padding + m->right_padding + wg;
238 rect.height -= m->top_padding + m->bottom_padding + wg;
239 if (focus_follows_mouse)
240 get_pointer_position(&pointer_position);
241 apply_layout(m, d, d->root, rect, rect);
244 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
252 if (n->client->fullscreen)
255 if (is_floating(n->client) && n->client->border_width != border_width) {
256 int ds = 2 * (border_width - n->client->border_width);
257 n->client->floating_rectangle.width += ds;
258 n->client->floating_rectangle.height += ds;
261 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
262 n->client->border_width = 0;
264 n->client->border_width = border_width;
267 if (is_tiled(n->client)) {
268 if (d->layout == LAYOUT_TILED)
270 else if (d->layout == LAYOUT_MONOCLE)
272 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
273 int bleed = wg + 2 * n->client->border_width;
274 r.width = (bleed < r.width ? r.width - bleed : 1);
275 r.height = (bleed < r.height ? r.height - bleed : 1);
276 n->client->tiled_rectangle = r;
278 r = n->client->floating_rectangle;
281 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
282 window_border_width(n->client->window, n->client->border_width);
283 window_draw_border(n, n == d->focus, m == mon);
286 xcb_rectangle_t first_rect;
287 xcb_rectangle_t second_rect;
289 if (n->first_child->vacant || n->second_child->vacant) {
290 first_rect = second_rect = rect;
293 if (n->split_type == TYPE_VERTICAL) {
294 fence = rect.width * n->split_ratio;
295 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
296 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
298 } else if (n->split_type == TYPE_HORIZONTAL) {
299 fence = rect.height * n->split_ratio;
300 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
301 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
305 apply_layout(m, d, n->first_child, first_rect, root_rect);
306 apply_layout(m, d, n->second_child, second_rect, root_rect);
310 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
312 if (d == NULL || n == NULL)
315 PRINTF("insert node %X\n", n->client->window);
317 node_t *focus = d->focus;
322 node_t *dad = make_node();
323 node_t *fopar = focus->parent;
325 n->born_as = split_mode;
326 switch (split_mode) {
329 dad->first_child = n;
330 dad->second_child = focus;
331 if (m->rectangle.width > m->rectangle.height)
332 dad->split_type = TYPE_VERTICAL;
334 dad->split_type = TYPE_HORIZONTAL;
338 node_t *grandpa = fopar->parent;
339 dad->parent = grandpa;
340 if (grandpa != NULL) {
341 if (is_first_child(fopar))
342 grandpa->first_child = dad;
344 grandpa->second_child = dad;
348 dad->split_type = fopar->split_type;
349 dad->split_ratio = fopar->split_ratio;
351 if (is_first_child(focus)) {
352 dad->first_child = n;
353 dad->second_child = fopar;
354 rotate_tree(fopar, ROTATE_CLOCKWISE);
356 dad->first_child = fopar;
357 dad->second_child = n;
358 rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
364 if (is_first_child(focus))
365 fopar->first_child = dad;
367 fopar->second_child = dad;
369 dad->split_ratio = focus->split_ratio;
374 dad->split_type = TYPE_VERTICAL;
375 dad->first_child = n;
376 dad->second_child = focus;
379 dad->split_type = TYPE_VERTICAL;
380 dad->first_child = focus;
381 dad->second_child = n;
384 dad->split_type = TYPE_HORIZONTAL;
385 dad->first_child = n;
386 dad->second_child = focus;
389 dad->split_type = TYPE_HORIZONTAL;
390 dad->first_child = focus;
391 dad->second_child = n;
394 if (d->root == focus)
396 split_mode = MODE_AUTOMATIC;
400 update_vacant_state(fopar);
404 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
409 PRINTF("focus node %X\n", n->client->window);
411 split_mode = MODE_AUTOMATIC;
412 n->client->urgent = false;
415 if (mon != m || d->focus != n) {
416 window_draw_border(d->focus, m != mon, m == mon);
417 window_draw_border(n, true, true);
419 if (focus_follows_mouse)
420 get_pointer_position(&pointer_position);
421 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
424 if (!is_tiled(n->client)) {
425 if (!adaptative_raise || !might_cover(d, n))
426 window_raise(n->client->window);
428 window_pseudo_raise(d, n->client->window);
432 d->last_focus = d->focus;
436 ewmh_update_active_window();
439 void update_current(void)
441 if (mon->desk->focus == NULL)
442 ewmh_update_active_window();
444 focus_node(mon, mon->desk, mon->desk->focus, true);
447 void unlink_node(desktop_t *d, node_t *n)
449 if (d == NULL || n == NULL)
452 PRINTF("unlink node %X\n", n->client->window);
454 node_t *p = n->parent;
459 d->last_focus = NULL;
462 node_t *g = p->parent;
463 bool n_first_child = is_first_child(n);
466 if (n->born_as == MODE_AUTOMATIC)
467 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
470 if (n->born_as == MODE_AUTOMATIC)
471 rotate_tree(b, ROTATE_CLOCKWISE);
475 if (is_first_child(p))
486 if (n == d->last_focus) {
487 d->last_focus = NULL;
488 } else if (n == d->focus) {
489 if (d->last_focus != NULL)
490 d->focus = d->last_focus;
492 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
493 d->last_focus = NULL;
496 update_vacant_state(b->parent);
500 void remove_node(desktop_t *d, node_t *n)
502 if (d == NULL || n == NULL)
505 PRINTF("remove node %X\n", n->client->window);
512 ewmh_update_client_list();
518 void swap_nodes(node_t *n1, node_t *n2)
520 if (n1 == NULL || n2 == NULL || n1 == n2)
525 /* (n1 and n2 are leaves) */
526 node_t *pn1 = n1->parent;
527 node_t *pn2 = n2->parent;
528 bool n1_first_child = is_first_child(n1);
529 bool n2_first_child = is_first_child(n2);
533 pn1->first_child = n2;
535 pn1->second_child = n2;
540 pn2->first_child = n1;
542 pn2->second_child = n1;
548 if (n1->vacant != n2->vacant) {
549 update_vacant_state(n1->parent);
550 update_vacant_state(n2->parent);
554 void fit_monitor(monitor_t *m, client_t *c)
556 xcb_rectangle_t crect = c->floating_rectangle;
557 xcb_rectangle_t mrect = m->rectangle;
558 while (crect.x < mrect.x)
559 crect.x += mrect.width;
560 while (crect.x > (mrect.x + mrect.width - 1))
561 crect.x -= mrect.width;
562 while (crect.y < mrect.y)
563 crect.y += mrect.height;
564 while (crect.y > (mrect.y + mrect.height - 1))
565 crect.y -= mrect.height;
566 c->floating_rectangle = crect;
569 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
571 if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
574 PRINTF("transfer node %X\n", n->client->window);
577 insert_node(md, dd, n);
578 ewmh_set_wm_desktop(n, dd);
580 if (ds == ms->desk && dd != md->desk) {
581 window_hide(n->client->window);
584 fit_monitor(md, n->client);
586 if (n->client->fullscreen)
587 window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
589 if (ds != ms->desk && dd == md->desk) {
590 window_show(n->client->window);
591 focus_node(md, dd, n, true);
593 focus_node(md, dd, n, false);
596 if (ds == ms->desk || dd == md->desk)
600 void select_monitor(monitor_t *m)
602 if (m == NULL || mon == m)
605 PRINTF("select monitor %s\n", m->name);
607 focus_node(m, m->desk, m->desk->focus, true);
612 ewmh_update_current_desktop();
615 void select_desktop(desktop_t *d)
617 if (d == NULL || d == mon->desk)
620 PRINTF("select desktop %s\n", d->name);
622 node_t *n = first_extrema(d->root);
625 window_show(n->client->window);
629 n = first_extrema(mon->desk->root);
632 window_hide(n->client->window);
636 mon->last_desk = mon->desk;
640 ewmh_update_current_desktop();
643 void cycle_monitor(cycle_dir_t dir)
645 if (dir == CYCLE_NEXT)
646 select_monitor((mon->next == NULL ? mon_head : mon->next));
647 else if (dir == CYCLE_PREV)
648 select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
651 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
653 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
655 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
658 if (skip == DESKTOP_SKIP_NONE
659 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
660 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
664 f = (dir == CYCLE_PREV ? f->prev : f->next);
666 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
670 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
677 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
679 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
682 bool tiled = is_tiled(f->client);
683 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
684 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
685 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
686 focus_node(m, d, f, true);
689 f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
691 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
695 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
700 PUTS("nearest leaf");
704 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
705 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
706 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
707 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
708 if ((dir == NEAREST_OLDER
709 && (f->client->uid < n->client->uid)
710 && (x == NULL || f->client->uid > x->client->uid))
711 || (dir == NEAREST_NEWER
712 && (f->client->uid > n->client->uid)
713 && (x == NULL || f->client->uid < x->client->uid)))
716 focus_node(m, d, x, true);
719 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
720 if (d == NULL || d->root == NULL || is_leaf(d->root))
722 node_t *par = d->focus->parent;
723 bool focus_first_child = is_first_child(d->focus);
724 if (dir == CIRCULATE_FORWARD)
725 for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
728 for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
730 if (focus_first_child)
731 focus_node(m, d, par->first_child, true);
733 focus_node(m, d, par->second_child, true);
736 void update_vacant_state(node_t *n)
741 PUTS("update vacant state");
743 /* n is not a leaf */
747 p->vacant = (p->first_child->vacant && p->second_child->vacant);
752 monitor_t *find_monitor(char *name)
754 for (monitor_t *m = mon_head; m != NULL; m = m->next)
755 if (strcmp(m->name, name) == 0)
760 void add_desktop(monitor_t *m, char *name)
762 desktop_t *d = make_desktop(name);
763 if (m->desk == NULL) {
768 m->desk_tail->next = d;
769 d->prev = m->desk_tail;
773 ewmh_update_number_of_desktops();
774 ewmh_update_desktop_names();
777 void add_monitor(xcb_rectangle_t *rect)
779 monitor_t *m = make_monitor(rect);