8 #include <xcb/xcb_event.h>
17 bool is_leaf(node_t *n)
19 return (n != NULL && n->first_child == NULL && n->second_child == NULL);
22 bool is_tiled(client_t *c)
26 return (!c->floating && !c->transient && !c->fullscreen);
29 bool is_floating(client_t *c)
33 return (c->floating && !c->fullscreen);
36 bool is_first_child(node_t *n)
38 return (n != NULL && n->parent != NULL && n->parent->first_child == n);
41 bool is_second_child(node_t *n)
43 return (n != NULL && n->parent != NULL && n->parent->second_child == n);
46 void change_split_ratio(node_t *n, value_change_t chg)
48 n->split_ratio = pow(n->split_ratio, (chg == CHANGE_INCREASE ? INC_EXP : DEC_EXP));
51 node_t *first_extrema(node_t *n)
55 else if (n->first_child == NULL)
58 return first_extrema(n->first_child);
61 node_t *second_extrema(node_t *n)
65 else if (n->second_child == NULL)
68 return second_extrema(n->second_child);
71 node_t *next_leaf(node_t *n, node_t *r)
76 while (is_second_child(p) && p != r)
80 return first_extrema(p->parent->second_child);
83 node_t *prev_leaf(node_t *n, node_t *r)
88 while (is_first_child(p) && p != r)
92 return second_extrema(p->parent->first_child);
95 node_t *find_fence(node_t *n, direction_t dir)
105 if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
106 || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
107 || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
108 || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
116 node_t *find_neighbor(node_t *n, direction_t dir)
118 node_t *fence = find_fence(n, dir);
123 if (dir == DIR_UP || dir == DIR_LEFT)
124 return second_extrema(fence->first_child);
125 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
126 return first_extrema(fence->second_child);
131 void get_opposite(direction_t src, direction_t* dst)
149 node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir)
154 node_t *target = NULL;
155 if (is_tiled(n->client)) {
156 target = find_fence(n, dir);
159 if (dir == DIR_UP || dir == DIR_LEFT)
160 target = target->first_child;
161 else if (dir == DIR_DOWN || dir == DIR_RIGHT)
162 target = target->second_child;
166 node_t *nearest = NULL;
170 get_side_handle(n->client, dir, &pt);
171 get_opposite(dir, &dir2);
173 for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
174 if (is_tiled(a->client) != is_tiled(n->client) || a == n)
176 get_side_handle(a->client, dir2, &pt2);
177 double ds2 = distance(pt, pt2);
186 int tiled_area(node_t *n)
190 xcb_rectangle_t rect = n->client->tiled_rectangle;
191 return rect.width * rect.height;
194 node_t *find_biggest(desktop_t *d)
200 int r_area = tiled_area(r);
202 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
203 int f_area = tiled_area(f);
207 } else if (f_area > r_area) {
216 void move_fence(node_t *n, direction_t dir, fence_move_t mov)
218 node_t *fence = find_fence(n, dir);
223 if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
224 || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
225 change_split_ratio(fence, CHANGE_INCREASE);
227 change_split_ratio(fence, CHANGE_DECREASE);
230 void rotate_tree(node_t *n, rotate_t rot)
232 if (n == NULL || is_leaf(n) || rot == ROTATE_IDENTITY)
237 if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
238 || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
239 || rot == ROTATE_FULL_CYCLE) {
240 tmp = n->first_child;
241 n->first_child = n->second_child;
242 n->second_child = tmp;
243 n->split_ratio = 1.0 - n->split_ratio;
246 if (rot != ROTATE_FULL_CYCLE) {
247 if (n->split_type == TYPE_HORIZONTAL)
248 n->split_type = TYPE_VERTICAL;
249 else if (n->split_type == TYPE_VERTICAL)
250 n->split_type = TYPE_HORIZONTAL;
253 rotate_tree(n->first_child, rot);
254 rotate_tree(n->second_child, rot);
257 void rotate_brother(node_t *n)
259 if (n == NULL || n->parent == NULL)
261 if (is_first_child(n))
262 rotate_tree(n->parent->second_child, n->birth_rotation);
264 rotate_tree(n->parent->first_child, n->birth_rotation);
267 void unrotate_tree(node_t *n, rotate_t rot)
270 case ROTATE_CLOCKWISE:
271 rotate_tree(n, ROTATE_COUNTER_CLOCKWISE);
273 case ROTATE_COUNTER_CLOCKWISE:
274 rotate_tree(n, ROTATE_CLOCKWISE);
276 case ROTATE_IDENTITY:
277 case ROTATE_FULL_CYCLE:
282 void unrotate_brother(node_t *n)
284 if (n == NULL || n->parent == NULL)
286 if (is_first_child(n))
287 unrotate_tree(n->parent->second_child, n->birth_rotation);
289 unrotate_tree(n->parent->first_child, n->birth_rotation);
292 void flip_tree(node_t *n, flip_t flp)
294 if (n == NULL || is_leaf(n))
299 if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
300 || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
301 tmp = n->first_child;
302 n->first_child = n->second_child;
303 n->second_child = tmp;
304 n->split_ratio = 1.0 - n->split_ratio;
307 flip_tree(n->first_child, flp);
308 flip_tree(n->second_child, flp);
311 void list_history(char *rsp)
314 for (monitor_t *m = mon_head; m != NULL; m = m->next)
315 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
316 snprintf(line, sizeof(line), "%s\n", d->name);
317 strncat(rsp, line, REMLEN(rsp));
318 for (node_list_t *a = d->history->tail; a != NULL; a = a->prev) {
319 snprintf(line, sizeof(line), " %X\n", a->node->client->window);
320 strncat(rsp, line, REMLEN(rsp));
325 int balance_tree(node_t *n)
327 if (n == NULL || n->vacant) {
329 } else if (is_leaf(n)) {
332 int b1 = balance_tree(n->first_child);
333 int b2 = balance_tree(n->second_child);
335 if (b1 > 0 && b2 > 0)
336 n->split_ratio = (double) b1 / b;
341 void arrange(monitor_t *m, desktop_t *d)
346 PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
348 xcb_rectangle_t rect = m->rectangle;
349 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
350 rect.x += m->left_padding + wg;
351 rect.y += m->top_padding + wg;
352 rect.width -= m->left_padding + m->right_padding + wg;
353 rect.height -= m->top_padding + m->bottom_padding + wg;
354 apply_layout(m, d, d->root, rect, rect);
357 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
365 if (n->client->fullscreen)
368 if (is_floating(n->client) && n->client->border_width != border_width) {
369 int ds = 2 * (border_width - n->client->border_width);
370 n->client->floating_rectangle.width += ds;
371 n->client->floating_rectangle.height += ds;
374 if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
375 n->client->border_width = 0;
377 n->client->border_width = border_width;
380 if (is_tiled(n->client)) {
381 if (d->layout == LAYOUT_TILED)
383 else if (d->layout == LAYOUT_MONOCLE)
385 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
386 int bleed = wg + 2 * n->client->border_width;
387 r.width = (bleed < r.width ? r.width - bleed : 1);
388 r.height = (bleed < r.height ? r.height - bleed : 1);
389 n->client->tiled_rectangle = r;
391 r = n->client->floating_rectangle;
394 window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
395 window_border_width(n->client->window, n->client->border_width);
396 window_draw_border(n, n == d->focus, m == mon);
399 xcb_rectangle_t first_rect;
400 xcb_rectangle_t second_rect;
402 if (n->first_child->vacant || n->second_child->vacant) {
403 first_rect = second_rect = rect;
406 if (n->split_type == TYPE_VERTICAL) {
407 fence = rect.width * n->split_ratio;
408 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
409 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
411 } else if (n->split_type == TYPE_HORIZONTAL) {
412 fence = rect.height * n->split_ratio;
413 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
414 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
418 apply_layout(m, d, n->first_child, first_rect, root_rect);
419 apply_layout(m, d, n->second_child, second_rect, root_rect);
423 void insert_node(monitor_t *m, desktop_t *d, node_t *n)
425 if (d == NULL || n == NULL)
428 PRINTF("insert node %X\n", n->client->window);
430 node_t *focus = d->focus;
435 node_t *dad = make_node();
436 node_t *fopar = focus->parent;
438 dad->birth_rotation = focus->birth_rotation;
439 switch (split_mode) {
442 dad->first_child = n;
443 dad->second_child = focus;
444 if (m->rectangle.width > m->rectangle.height)
445 dad->split_type = TYPE_VERTICAL;
447 dad->split_type = TYPE_HORIZONTAL;
451 node_t *grandpa = fopar->parent;
452 dad->parent = grandpa;
453 if (grandpa != NULL) {
454 if (is_first_child(fopar))
455 grandpa->first_child = dad;
457 grandpa->second_child = dad;
461 dad->split_type = fopar->split_type;
462 dad->split_ratio = fopar->split_ratio;
465 if (is_first_child(focus)) {
466 dad->first_child = n;
467 dad->second_child = fopar;
468 rot = ROTATE_CLOCKWISE;
470 dad->first_child = fopar;
471 dad->second_child = n;
472 rot = ROTATE_COUNTER_CLOCKWISE;
474 rotate_tree(fopar, rot);
475 n->birth_rotation = rot;
480 if (is_first_child(focus))
481 fopar->first_child = dad;
483 fopar->second_child = dad;
485 dad->split_ratio = focus->split_ratio;
488 focus->birth_rotation = ROTATE_IDENTITY;
491 dad->split_type = TYPE_VERTICAL;
492 dad->first_child = n;
493 dad->second_child = focus;
496 dad->split_type = TYPE_VERTICAL;
497 dad->first_child = focus;
498 dad->second_child = n;
501 dad->split_type = TYPE_HORIZONTAL;
502 dad->first_child = n;
503 dad->second_child = focus;
506 dad->split_type = TYPE_HORIZONTAL;
507 dad->first_child = focus;
508 dad->second_child = n;
511 if (d->root == focus)
513 split_mode = MODE_AUTOMATIC;
517 update_vacant_state(fopar);
522 void pseudo_focus(desktop_t *d, node_t *n)
527 history_add(d->history, n);
530 void focus_node(monitor_t *m, desktop_t *d, node_t *n)
532 if (n == NULL && d->root != NULL)
535 split_mode = MODE_AUTOMATIC;
541 for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
542 window_draw_border(cd->focus, true, false);
543 for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
545 window_draw_border(cd->focus, true, true);
547 window_draw_border(n, true, true);
551 window_draw_border(d->focus, false, true);
552 window_draw_border(n, true, true);
555 select_desktop(m, d);
558 ewmh_update_active_window();
562 PRINTF("focus node %X\n", n->client->window);
564 n->client->urgent = false;
569 xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
571 if (focus_follows_pointer) {
572 xcb_window_t win = XCB_NONE;
573 query_pointer(&win, NULL);
574 if (win != n->client->window)
575 enable_motion_recorder();
577 disable_motion_recorder();
580 ewmh_update_active_window();
583 void update_current(void)
585 focus_node(mon, mon->desk, mon->desk->focus);
588 void unlink_node(desktop_t *d, node_t *n)
590 if (d == NULL || n == NULL)
593 PRINTF("unlink node %X\n", n->client->window);
595 node_t *p = n->parent;
602 node_t *g = p->parent;
603 if (is_first_child(n)) {
606 unrotate_tree(b, n->birth_rotation);
610 unrotate_tree(b, n->birth_rotation);
614 if (is_first_child(p))
622 b->birth_rotation = p->birth_rotation;
627 d->focus = history_get(d->history, 1);
629 update_vacant_state(b->parent);
631 history_remove(d->history, n);
635 void remove_node(desktop_t *d, node_t *n)
640 PRINTF("remove node %X\n", n->client->window);
647 ewmh_update_client_list();
653 void destroy_tree(node_t *n)
657 node_t *first_tree = n->first_child;
658 node_t *second_tree = n->second_child;
659 if (n->client != NULL)
662 destroy_tree(first_tree);
663 destroy_tree(second_tree);
666 void swap_nodes(node_t *n1, node_t *n2)
668 if (n1 == NULL || n2 == NULL || n1 == n2)
673 /* (n1 and n2 are leaves) */
674 node_t *pn1 = n1->parent;
675 node_t *pn2 = n2->parent;
676 bool n1_first_child = is_first_child(n1);
677 bool n2_first_child = is_first_child(n2);
678 rotate_t br1 = n1->birth_rotation;
679 rotate_t br2 = n2->birth_rotation;
683 pn1->first_child = n2;
685 pn1->second_child = n2;
690 pn2->first_child = n1;
692 pn2->second_child = n1;
697 n1->birth_rotation = br2;
698 n2->birth_rotation = br1;
700 if (n1->vacant != n2->vacant) {
701 update_vacant_state(n1->parent);
702 update_vacant_state(n2->parent);
705 /* If we ever need to generalize: */
706 /* if (d1 != d2) { */
707 /* if (d1->root == n1) */
709 /* if (d1->focus == n1) */
710 /* d1->focus = n2; */
711 /* if (d1->last_focus == n1) */
712 /* d1->last_focus = n2; */
713 /* if (d2->root == n2) */
715 /* if (d2->focus == n2) */
716 /* d2->focus = n1; */
717 /* if (d2->last_focus == n2) */
718 /* d2->last_focus = n1; */
722 void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
724 if (n == NULL || dd == ds)
727 PRINTF("transfer node %X\n", n->client->window);
730 insert_node(md, dd, n);
731 ewmh_set_wm_desktop(n, dd);
733 if (ds == ms->desk && dd != md->desk) {
736 window_hide(n->client->window);
739 fit_monitor(md, n->client);
741 if (n->client->fullscreen)
742 window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
744 if (ds != ms->desk && dd == md->desk)
745 window_show(n->client->window);
755 if (ds == ms->desk || dd == md->desk)
759 void select_monitor(monitor_t *m)
764 PRINTF("select monitor %s\n", m->name);
769 if (pointer_follows_monitor)
772 ewmh_update_current_desktop();
776 void select_desktop(monitor_t *m, desktop_t *d)
783 PRINTF("select desktop %s\n", d->name);
787 desktop_hide(mon->desk);
790 mon->last_desk = mon->desk;
793 ewmh_update_current_desktop();
797 void cycle_monitor(cycle_dir_t dir)
800 if (dir == CYCLE_NEXT)
801 m = (mon->next == NULL ? mon_head : mon->next);
802 else if (dir == CYCLE_PREV)
803 m = (mon->prev == NULL ? mon_tail : mon->prev);
804 focus_node(m, m->desk, m->desk->focus);
807 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
809 desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
811 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
814 if (skip == DESKTOP_SKIP_NONE
815 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
816 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
817 focus_node(m, f, f->focus);
820 f = (dir == CYCLE_PREV ? f->prev : f->next);
822 f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
826 void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
833 node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
835 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
838 bool tiled = is_tiled(f->client);
839 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
840 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
841 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
845 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
847 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
851 void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
856 PUTS("nearest leaf");
860 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root))
861 if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
862 || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
863 || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
864 if ((dir == NEAREST_OLDER
865 && (f->client->uid < n->client->uid)
866 && (x == NULL || f->client->uid > x->client->uid))
867 || (dir == NEAREST_NEWER
868 && (f->client->uid > n->client->uid)
869 && (x == NULL || f->client->uid < x->client->uid)))
875 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
877 if (d == NULL || d->root == NULL || is_leaf(d->root))
879 node_t *par = d->focus->parent;
880 bool focus_first_child = is_first_child(d->focus);
881 if (dir == CIRCULATE_FORWARD)
882 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))
885 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))
887 if (focus_first_child)
888 focus_node(m, d, par->first_child);
890 focus_node(m, d, par->second_child);
893 void update_vacant_state(node_t *n)
898 PUTS("update vacant state");
900 /* n is not a leaf */
904 p->vacant = (p->first_child->vacant && p->second_child->vacant);
909 void fit_monitor(monitor_t *m, client_t *c)
911 xcb_rectangle_t crect = c->floating_rectangle;
912 xcb_rectangle_t mrect = m->rectangle;
913 while (crect.x < mrect.x)
914 crect.x += mrect.width;
915 while (crect.x > (mrect.x + mrect.width - 1))
916 crect.x -= mrect.width;
917 while (crect.y < mrect.y)
918 crect.y += mrect.height;
919 while (crect.y > (mrect.y + mrect.height - 1))
920 crect.y -= mrect.height;
921 c->floating_rectangle = crect;
924 void put_status(void)
926 if (status_fifo == NULL)
928 if (status_prefix != NULL)
929 fprintf(status_fifo, "%s", status_prefix);
931 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
932 fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
933 for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
934 for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
935 urgent |= n->client->urgent;
936 fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
939 if (mon != NULL && mon->desk != NULL)
940 fprintf(status_fifo, "L%s\n", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
944 void list_monitors(list_option_t opt, char *rsp)
947 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
948 snprintf(line, sizeof(line), "%s %ux%u%+i%+i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y);
949 strncat(rsp, line, REMLEN(rsp));
951 strncat(rsp, " #\n", REMLEN(rsp));
952 else if (m == last_mon)
953 strncat(rsp, " ~\n", REMLEN(rsp));
955 strncat(rsp, "\n", REMLEN(rsp));
956 if (opt == LIST_OPTION_VERBOSE)
957 list_desktops(m, opt, 1, rsp);
961 void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
964 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
965 for (unsigned int i = 0; i < depth; i++)
966 strncat(rsp, " ", REMLEN(rsp));
967 snprintf(line, sizeof(line), "%s %c", d->name, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
968 strncat(rsp, line, REMLEN(rsp));
970 strncat(rsp, " @\n", REMLEN(rsp));
971 else if (d == m->last_desk)
972 strncat(rsp, " ~\n", REMLEN(rsp));
974 strncat(rsp, "\n", REMLEN(rsp));
975 if (opt == LIST_OPTION_VERBOSE)
976 list(d, d->root, rsp, depth + 1);
980 void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
987 for (unsigned int i = 0; i < depth; i++)
988 strncat(rsp, " ", REMLEN(rsp));
991 client_t *c = n->client;
992 snprintf(line, sizeof(line), "%c %s %X %u %u %ux%u%+i%+i %c%c%c%c%c", (n->birth_rotation == ROTATE_CLOCKWISE ? 'a' : (n->birth_rotation == ROTATE_COUNTER_CLOCKWISE ? 'c' : 'm')), c->class_name, c->window, c->uid, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'));
994 snprintf(line, sizeof(line), "%c %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == ROTATE_CLOCKWISE ? 'a' : (n->birth_rotation == ROTATE_COUNTER_CLOCKWISE ? 'c' : 'm')), n->split_ratio);
997 strncat(rsp, line, REMLEN(rsp));
1000 strncat(rsp, " *\n", REMLEN(rsp));
1002 strncat(rsp, "\n", REMLEN(rsp));
1004 list(d, n->first_child, rsp, depth + 1);
1005 list(d, n->second_child, rsp, depth + 1);
1008 void restore_layout(char *file_path)
1010 if (file_path == NULL)
1013 FILE *snapshot = fopen(file_path, "r");
1014 if (snapshot == NULL) {
1015 warn("restore: can't open file\n");
1019 PUTS("restore layout");
1022 monitor_t *m = NULL;
1023 desktop_t *d = NULL;
1026 unsigned int level, last_level = 0, max_uid = 0;
1027 bool aborted = false;
1029 while (!aborted && fgets(line, sizeof(line), snapshot) != NULL) {
1030 unsigned int len = strlen(line);
1032 while (level < strlen(line) && isspace(line[level]))
1040 switch (line[len - 2]) {
1048 } else if (level == 2) {
1054 while (i > 0 && !isupper(line[i]))
1057 d->layout = LAYOUT_MONOCLE;
1058 else if (line[i] == 'T')
1059 d->layout = LAYOUT_TILED;
1061 switch (line[len - 2]) {
1070 node_t *birth = make_node();
1075 if (level > last_level) {
1076 n->first_child = birth;
1080 } while (n != NULL && n->second_child != NULL);
1082 warn("restore: file is malformed\n");
1085 n->second_child = birth;
1092 if (isupper(line[level])) {
1094 sscanf(line + level, "%c %c %lf", &st, &br, &n->split_ratio);
1096 n->split_type = TYPE_HORIZONTAL;
1098 n->split_type = TYPE_VERTICAL;
1100 client_t *c = make_client(XCB_NONE);
1102 char floating, transient, fullscreen, urgent, locked;
1103 sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c%c%c%c%c", &br, c->class_name, &c->window, &c->uid, &c->border_width, &c->floating_rectangle.width, &c->floating_rectangle.height, &c->floating_rectangle.x, &c->floating_rectangle.y, &floating, &transient, &fullscreen, &urgent, &locked);
1104 c->floating = (floating == '-' ? false : true);
1105 c->transient = (transient == '-' ? false : true);
1106 c->fullscreen = (fullscreen == '-' ? false : true);
1107 c->urgent = (urgent == '-' ? false : true);
1108 c->locked = (locked == '-' ? false : true);
1109 if (c->uid > max_uid)
1112 if (len >= 2 && line[len - 2] == '*')
1116 n->birth_rotation = ROTATE_CLOCKWISE;
1118 n->birth_rotation = ROTATE_COUNTER_CLOCKWISE;
1120 n->birth_rotation = ROTATE_IDENTITY;
1128 client_uid = max_uid + 1;
1129 for (monitor_t *m = mon_head; m != NULL; m = m->next)
1130 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
1131 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
1132 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
1133 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
1134 if (n->client->floating) {
1136 update_vacant_state(n->parent);
1139 ewmh_update_current_desktop();
1143 void restore_history(char *file_path)
1145 if (file_path == NULL)
1148 FILE *snapshot = fopen(file_path, "r");
1149 if (snapshot == NULL) {
1150 warn("restore history: can't open file\n");
1154 PUTS("restore history");
1157 desktop_t *d = NULL;
1160 while (fgets(line, sizeof(line), snapshot) != NULL) {
1161 unsigned int i = strlen(line) - 1;
1162 while (i > 0 && isspace(line[i]))
1165 while (level < strlen(line) && isspace(line[level]))
1168 desktop_location_t loc;
1169 if (locate_desktop(line + level, &loc))
1171 } else if (d != NULL) {
1173 if (sscanf(line + level, "%X", &win) == 1) {
1174 window_location_t loc;
1175 if (locate_window(win, &loc))
1176 history_add(d->history, loc.node);