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 put_status(void)
231 if (status_fifo == NULL)
234 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
235 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
236 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
237 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
238 urgent |= n->client->urgent;
239 fprintf(status_fifo, "%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
242 fprintf(status_fifo, "L%s:W%X\n", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"), (mon->desk->focus == NULL ? 0 : mon->desk->focus->client->window));
246 void arrange(monitor_t *m, desktop_t *d)
248 PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
250 xcb_rectangle_t rect = m->rectangle;
251 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
252 rect.x += m->left_padding + wg;
253 rect.y += m->top_padding + wg;
254 rect.width -= m->left_padding + m->right_padding + wg;
255 rect.height -= m->top_padding + m->bottom_padding + wg;
256 if (focus_follows_mouse)
257 get_pointer_position(&pointer_position);
258 apply_layout(m, d, d->root, rect, rect);
261 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
269 if (n->client->fullscreen)
272 if (is_floating(n->client) && n->client->border_width != border_width) {
273 int ds = 2 * (border_width - n->client->border_width);
274 n->client->floating_rectangle.width += ds;
275 n->client->floating_rectangle.height += ds;
278 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
279 n->client->border_width = 0;
281 n->client->border_width = border_width;
284 if (is_tiled(n->client)) {
285 if (d->layout == LAYOUT_TILED)
287 else if (d->layout == LAYOUT_MONOCLE)
289 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
290 int bleed = wg + 2 * n->client->border_width;
291 r.width = (bleed < r.width ? r.width - bleed : 1);
292 r.height = (bleed < r.height ? r.height - bleed : 1);
293 n->client->tiled_rectangle = r;
295 r = n->client->floating_rectangle;
298 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
299 window_border_width(n->client->window, n->client->border_width);
300 window_draw_border(n, n == d->focus, m == mon);
303 xcb_rectangle_t first_rect;
304 xcb_rectangle_t second_rect;
306 if (n->first_child->vacant || n->second_child->vacant) {
307 first_rect = second_rect = rect;
310 if (n->split_type == TYPE_VERTICAL) {
311 fence = rect.width * n->split_ratio;
312 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
313 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
315 } else if (n->split_type == TYPE_HORIZONTAL) {
316 fence = rect.height * n->split_ratio;
317 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
318 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
322 apply_layout(m, d, n->first_child, first_rect, root_rect);
323 apply_layout(m, d, n->second_child, second_rect, root_rect);
327 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
329 if (d == NULL || n == NULL)
332 PRINTF("insert node %X\n", n->client->window);
334 node_t *focus = d->focus;
339 node_t *dad = make_node();
340 node_t *fopar = focus->parent;
342 n->born_as = split_mode;
343 switch (split_mode) {
346 dad->first_child = n;
347 dad->second_child = focus;
348 if (m->rectangle.width > m->rectangle.height)
349 dad->split_type = TYPE_VERTICAL;
351 dad->split_type = TYPE_HORIZONTAL;
355 node_t *grandpa = fopar->parent;
356 dad->parent = grandpa;
357 if (grandpa != NULL) {
358 if (is_first_child(fopar))
359 grandpa->first_child = dad;
361 grandpa->second_child = dad;
365 dad->split_type = fopar->split_type;
366 dad->split_ratio = fopar->split_ratio;
368 if (is_first_child(focus)) {
369 dad->first_child = n;
370 dad->second_child = fopar;
371 rotate_tree(fopar, ROTATE_CLOCKWISE);
373 dad->first_child = fopar;
374 dad->second_child = n;
375 rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
381 if (is_first_child(focus))
382 fopar->first_child = dad;
384 fopar->second_child = dad;
386 dad->split_ratio = focus->split_ratio;
391 dad->split_type = TYPE_VERTICAL;
392 dad->first_child = n;
393 dad->second_child = focus;
396 dad->split_type = TYPE_VERTICAL;
397 dad->first_child = focus;
398 dad->second_child = n;
401 dad->split_type = TYPE_HORIZONTAL;
402 dad->first_child = n;
403 dad->second_child = focus;
406 dad->split_type = TYPE_HORIZONTAL;
407 dad->first_child = focus;
408 dad->second_child = n;
411 if (d->root == focus)
413 split_mode = MODE_AUTOMATIC;
417 update_vacant_state(fopar);
421 void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
426 PRINTF("focus node %X\n", n->client->window);
428 split_mode = MODE_AUTOMATIC;
429 n->client->urgent = false;
432 if (mon != m || d->focus != n) {
433 window_draw_border(d->focus, m != mon, m == mon);
434 window_draw_border(n, true, true);
436 if (focus_follows_mouse)
437 get_pointer_position(&pointer_position);
438 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
441 if (!is_tiled(n->client)) {
442 if (!adaptative_raise || !might_cover(d, n))
443 window_raise(n->client->window);
445 window_pseudo_raise(d, n->client->window);
449 d->last_focus = d->focus;
453 ewmh_update_active_window();
457 void update_current(void)
459 if (mon->desk->focus == NULL)
460 ewmh_update_active_window();
462 focus_node(mon, mon->desk, mon->desk->focus, true);
466 void unlink_node(desktop_t *d, node_t *n)
468 if (d == NULL || n == NULL)
471 PRINTF("unlink node %X\n", n->client->window);
473 node_t *p = n->parent;
478 d->last_focus = NULL;
481 node_t *g = p->parent;
482 bool n_first_child = is_first_child(n);
485 if (n->born_as == MODE_AUTOMATIC)
486 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
489 if (n->born_as == MODE_AUTOMATIC)
490 rotate_tree(b, ROTATE_CLOCKWISE);
494 if (is_first_child(p))
505 if (n == d->last_focus) {
506 d->last_focus = NULL;
507 } else if (n == d->focus) {
508 if (d->last_focus != NULL)
509 d->focus = d->last_focus;
511 d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
512 d->last_focus = NULL;
515 update_vacant_state(b->parent);
519 void remove_node(desktop_t *d, node_t *n)
521 if (d == NULL || n == NULL)
524 PRINTF("remove node %X\n", n->client->window);
531 ewmh_update_client_list();
537 void swap_nodes(node_t *n1, node_t *n2)
539 if (n1 == NULL || n2 == NULL || n1 == n2)
544 /* (n1 and n2 are leaves) */
545 node_t *pn1 = n1->parent;
546 node_t *pn2 = n2->parent;
547 bool n1_first_child = is_first_child(n1);
548 bool n2_first_child = is_first_child(n2);
552 pn1->first_child = n2;
554 pn1->second_child = n2;
559 pn2->first_child = n1;
561 pn2->second_child = n1;
567 if (n1->vacant != n2->vacant) {
568 update_vacant_state(n1->parent);
569 update_vacant_state(n2->parent);
573 void fit_monitor(monitor_t *m, client_t *c)
575 xcb_rectangle_t crect = c->floating_rectangle;
576 xcb_rectangle_t mrect = m->rectangle;
577 while (crect.x < mrect.x)
578 crect.x += mrect.width;
579 while (crect.x > (mrect.x + mrect.width - 1))
580 crect.x -= mrect.width;
581 while (crect.y < mrect.y)
582 crect.y += mrect.height;
583 while (crect.y > (mrect.y + mrect.height - 1))
584 crect.y -= mrect.height;
585 c->floating_rectangle = crect;
588 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
590 if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
593 PRINTF("transfer node %X\n", n->client->window);
596 insert_node(md, dd, n);
597 ewmh_set_wm_desktop(n, dd);
599 if (ds == ms->desk && dd != md->desk) {
600 window_hide(n->client->window);
603 fit_monitor(md, n->client);
605 if (n->client->fullscreen)
606 window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
608 if (ds != ms->desk && dd == md->desk) {
609 window_show(n->client->window);
610 focus_node(md, dd, n, true);
612 focus_node(md, dd, n, false);
615 if (ds == ms->desk || dd == md->desk)
619 void select_monitor(monitor_t *m)
621 if (m == NULL || mon == m)
624 PRINTF("select monitor %s\n", m->name);
626 focus_node(m, m->desk, m->desk->focus, true);
631 ewmh_update_current_desktop();
635 void select_desktop(desktop_t *d)
637 if (d == NULL || d == mon->desk)
640 PRINTF("select desktop %s\n", d->name);
642 node_t *n = first_extrema(d->root);
645 window_show(n->client->window);
649 n = first_extrema(mon->desk->root);
652 window_hide(n->client->window);
656 mon->last_desk = mon->desk;
660 ewmh_update_current_desktop();
664 void cycle_monitor(cycle_dir_t dir)
666 if (dir == CYCLE_NEXT)
667 select_monitor((mon->next == NULL ? mon_head : mon->next));
668 else if (dir == CYCLE_PREV)
669 select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
672 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
674 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
676 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
679 if (skip == DESKTOP_SKIP_NONE
680 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
681 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
685 f = (dir == CYCLE_PREV ? f->prev : f->next);
687 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
691 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
698 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
700 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
703 bool tiled = is_tiled(f->client);
704 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
705 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
706 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
707 focus_node(m, d, f, true);
710 f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
712 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
716 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
721 PUTS("nearest leaf");
725 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
726 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
727 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
728 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
729 if ((dir == NEAREST_OLDER
730 && (f->client->uid < n->client->uid)
731 && (x == NULL || f->client->uid > x->client->uid))
732 || (dir == NEAREST_NEWER
733 && (f->client->uid > n->client->uid)
734 && (x == NULL || f->client->uid < x->client->uid)))
737 focus_node(m, d, x, true);
740 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
741 if (d == NULL || d->root == NULL || is_leaf(d->root))
743 node_t *par = d->focus->parent;
744 bool focus_first_child = is_first_child(d->focus);
745 if (dir == CIRCULATE_FORWARD)
746 for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
749 for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
751 if (focus_first_child)
752 focus_node(m, d, par->first_child, true);
754 focus_node(m, d, par->second_child, true);
757 void update_vacant_state(node_t *n)
762 PUTS("update vacant state");
764 /* n is not a leaf */
768 p->vacant = (p->first_child->vacant && p->second_child->vacant);
773 monitor_t *find_monitor(char *name)
775 for (monitor_t *m = mon_head; m != NULL; m = m->next)
776 if (strcmp(m->name, name) == 0)
781 void add_desktop(monitor_t *m, char *name)
783 desktop_t *d = make_desktop(name);
784 if (m->desk == NULL) {
789 m->desk_tail->next = d;
790 d->prev = m->desk_tail;
794 ewmh_update_number_of_desktops();
795 ewmh_update_desktop_names();
799 void add_monitor(xcb_rectangle_t *rect)
801 monitor_t *m = make_monitor(rect);