]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
Merge branch 'master' into history
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index 3d5327b57339519093eaa659a663291dc561a182..44772e378af9bcff0a88c86c5880584a0dc1d7b0 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -1,12 +1,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <math.h>
 #include <xcb/xcb.h>
 #include <xcb/xcb_event.h>
 #include "settings.h"
 #include "helpers.h"
-#include "utils.h"
 #include "window.h"
 #include "types.h"
 #include "bspwm.h"
@@ -133,7 +133,7 @@ void move_fence(node_t *n, direction_t dir, fence_move_t mov)
     if (fence == NULL)
         return;
 
-    if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN)) 
+    if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
             || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
         change_split_ratio(fence, CHANGE_INCREASE);
     else
@@ -167,68 +167,50 @@ void rotate_tree(node_t *n, rotate_t rot)
     rotate_tree(n->second_child, rot);
 }
 
-void dump_tree(desktop_t *d, node_t *n, char *rsp, int depth)
+void flip_tree(node_t *n, flip_t flp)
 {
-    if (n == NULL)
+    if (n == NULL || is_leaf(n))
         return;
 
-    char line[MAXLEN];
-
-    for (int i = 0; i < depth; i++)
-        strcat(rsp, "  ");
-
-    if (is_leaf(n))
-        /* sprintf(line, "0x%X [%i %i %u %u]", n->client->window, n->rectangle.x, n->rectangle.y, n->rectangle.width, n->rectangle.height); */ 
-        /* sprintf(line, "C %X [%i %i %u %u] (%s%s) [%i %i %u %u]", n->client->window, n->rectangle.x, n->rectangle.y, n->rectangle.width, n->rectangle.height, (n->client->floating ? "f" : "-"), (n->client->transient ? "t" : "-"), n->client->floating_rectangle.x, n->client->floating_rectangle.y, n->client->floating_rectangle.width, n->client->floating_rectangle.height); */ 
-        sprintf(line, "C %X %s%s%s%s%s", n->client->window, (n->client->floating ? "f" : "-"), (n->client->transient ? "t" : "-"), (n->client->fullscreen ? "F" : "-"), (n->client->urgent ? "u" : "-"), (n->client->locked ? "l" : "-")); 
-    else
-        /* sprintf(line, "%s %.2f [%i %i %u %u]", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio, n->rectangle.x, n->rectangle.y, n->rectangle.width, n->rectangle.height); */
-        sprintf(line, "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
-
-    strcat(rsp, line);
-
-    if (n == d->focus)
-        strcat(rsp, " *\n");
-    else
-        strcat(rsp, "\n");
+    node_t *tmp;
 
-    dump_tree(d, n->first_child, rsp, depth + 1);
-    dump_tree(d, n->second_child, rsp, depth + 1);
-}
+    if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
+            || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
+        tmp = n->first_child;
+        n->first_child = n->second_child;
+        n->second_child = tmp;
+        n->split_ratio = 1.0 - n->split_ratio;
+    }
 
-void refresh_current(void) {
-    if (desk->focus == NULL)
-        ewmh_update_active_window();
-    else
-        focus_node(desk, desk->focus, true);
+    flip_tree(n->first_child, flp);
+    flip_tree(n->second_child, flp);
 }
 
-void list_desktops(char *rsp)
+void list_history(desktop_t *d, char *rsp)
 {
-    desktop_t *d = desk_head;
-
-    while (d != NULL) {
-        strcat(rsp, d->name);
-        if (desk == d)
-            strcat(rsp, " @\n");
-        else
-            strcat(rsp, "\n");
-        dump_tree(d, d->root, rsp, 1);
-        d = d->next;
+    char line[MAXLEN];
+    for (node_list_t *a = d->history->head; a != NULL; a = a->next) {
+        snprintf(line, sizeof(line), "%s %X\n", a->node->client->class_name, a->node->client->window);
+        strncat(rsp, line, REMLEN(rsp));
     }
 }
 
-void update_root_dimensions(void)
+void arrange(monitor_t *m, desktop_t *d)
 {
-    root_rect.x = left_padding + window_gap;
-    root_rect.y = top_padding + window_gap;
-    root_rect.width = screen_width - (left_padding + right_padding + window_gap);
-    root_rect.height = screen_height - (top_padding + bottom_padding + window_gap);
+    PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
+
+    xcb_rectangle_t rect = m->rectangle;
+    int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
+    rect.x += m->left_padding + wg;
+    rect.y += m->top_padding + wg;
+    rect.width -= m->left_padding + m->right_padding + wg;
+    rect.height -= m->top_padding + m->bottom_padding + wg;
+    apply_layout(m, d, d->root, rect, rect);
 }
 
-void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
+void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
 {
-    if (d == NULL || n == NULL)
+    if (n == NULL)
         return;
 
     n->rectangle = rect;
@@ -236,13 +218,26 @@ void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
     if (is_leaf(n)) {
         if (n->client->fullscreen)
             return;
+
+        if (is_floating(n->client) && n->client->border_width != border_width) {
+            int ds = 2 * (border_width - n->client->border_width);
+            n->client->floating_rectangle.width += ds;
+            n->client->floating_rectangle.height += ds;
+        }
+
+        if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
+            n->client->border_width = 0;
+        else
+            n->client->border_width = border_width;
+
         xcb_rectangle_t r;
         if (is_tiled(n->client)) {
             if (d->layout == LAYOUT_TILED)
                 r = rect;
             else if (d->layout == LAYOUT_MONOCLE)
                 r = root_rect;
-            int bleed = window_gap + 2 * border_width;
+            int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
+            int bleed = wg + 2 * n->client->border_width;
             r.width = (bleed < r.width ? r.width - bleed : 1);
             r.height = (bleed < r.height ? r.height - bleed : 1);
             n->client->tiled_rectangle = r;
@@ -251,11 +246,8 @@ void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
         }
 
         window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
-        window_border_width(n->client->window, border_width);
-        window_draw_border(n, n == d->focus);
-
-        if (d->layout == LAYOUT_MONOCLE && n == d->focus)
-            window_raise(n->client->window);
+        window_border_width(n->client->window, n->client->border_width);
+        window_draw_border(n, n == d->focus, m == mon);
 
     } else {
         xcb_rectangle_t first_rect;
@@ -277,12 +269,12 @@ void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
             }
         }
 
-        apply_layout(d, n->first_child, first_rect);
-        apply_layout(d, n->second_child, second_rect);
+        apply_layout(m, d, n->first_child, first_rect, root_rect);
+        apply_layout(m, d, n->second_child, second_rect, root_rect);
     }
 }
 
-void insert_node(desktop_t *d, node_t *n)
+void insert_node(monitor_t *m, desktop_t *d, node_t *n)
 {
     if (d == NULL || n == NULL)
         return;
@@ -297,13 +289,16 @@ void insert_node(desktop_t *d, node_t *n)
         node_t *dad = make_node();
         node_t *fopar = focus->parent;
         n->parent = dad;
-        dad->born_as = split_mode;
+        n->client->born_as = split_mode;
         switch (split_mode) {
             case MODE_AUTOMATIC:
                 if (fopar == NULL) {
                     dad->first_child = n;
                     dad->second_child = focus;
-                    dad->split_type = TYPE_VERTICAL;
+                    if (m->rectangle.width > m->rectangle.height)
+                        dad->split_type = TYPE_VERTICAL;
+                    else
+                        dad->split_type = TYPE_HORIZONTAL;
                     focus->parent = dad;
                     d->root = dad;
                 } else {
@@ -368,10 +363,12 @@ void insert_node(desktop_t *d, node_t *n)
                 split_mode = MODE_AUTOMATIC;
                 break;
         }
+        if (focus->vacant)
+            update_vacant_state(fopar);
     }
 }
 
-void focus_node(desktop_t *d, node_t *n, bool is_mapped)
+void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
 {
     if (n == NULL)
         return;
@@ -382,32 +379,54 @@ void focus_node(desktop_t *d, node_t *n, bool is_mapped)
     n->client->urgent = false;
 
     if (is_mapped) {
+        if (mon != m) {
+            for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
+                window_draw_border(cd->focus, true, false);
+            for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
+                if (cd != d)
+                    window_draw_border(cd->focus, true, true);
+            if (d->focus == n)
+                window_draw_border(n, true, true);
+        }
         if (d->focus != n) {
-            window_draw_border(d->focus, false);
-            window_draw_border(n, true);
+            window_draw_border(d->focus, false, true);
+            window_draw_border(n, true, true);
         }
         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
     }
 
-    if (!is_tiled(n->client) || d->layout == LAYOUT_MONOCLE)
-        window_raise(n->client->window);
-    else if (is_tiled(n->client))
-        window_lower(n->client->window);
+    if (focus_follows_pointer) {
+        xcb_window_t win = XCB_NONE;
+        get_pointed_window(&win);
+        if (win != n->client->window)
+            enable_motion_recorder();
+        else
+            disable_motion_recorder();
+    }
+
+    if (!is_tiled(n->client)) {
+        if (!adaptative_raise || !might_cover(d, n))
+            window_raise(n->client->window);
+    } else {
+        window_pseudo_raise(d, n->client->window);
+    }
 
     if (d->focus != n) {
-        d->last_focus = d->focus;
         d->focus = n;
+        history_add(d->history, n);
     }
 
     ewmh_update_active_window();
+    put_status();
 }
 
 void update_current(void)
 {
-    if (desk->focus == NULL)
+    if (mon->desk->focus == NULL)
         ewmh_update_active_window();
     else
-        focus_node(desk, desk->focus, true);
+        focus_node(mon, mon->desk, mon->desk->focus, true);
+    put_status();
 }
 
 void unlink_node(desktop_t *d, node_t *n)
@@ -422,17 +441,17 @@ void unlink_node(desktop_t *d, node_t *n)
     if (p == NULL) {
         d->root = NULL;
         d->focus = NULL;
-        d->last_focus = NULL;
     } else {
         node_t *b;
         node_t *g = p->parent;
-        if (is_first_child(n)) {
+        bool n_first_child = is_first_child(n);
+        if (n_first_child) {
             b = p->second_child;
-            if (b->born_as == MODE_AUTOMATIC)
+            if (n->client->born_as == MODE_AUTOMATIC)
                 rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
         } else {
             b = p->first_child;
-            if (b->born_as == MODE_AUTOMATIC)
+            if (n->client->born_as == MODE_AUTOMATIC)
                 rotate_tree(b, ROTATE_CLOCKWISE);
         }
         b->parent = g;
@@ -449,13 +468,19 @@ void unlink_node(desktop_t *d, node_t *n)
         free(p);
 
         if (n == d->focus) {
-            if (d->last_focus != NULL && d->last_focus != n)
-                d->focus = d->last_focus;
-            else
-                d->focus = (is_first_child(b) ? second_extrema(b) : first_extrema(b));
-            d->last_focus = NULL;
+            node_t *last_focus = history_get(d->history, 1);
+            if (last_focus != NULL) {
+                d->focus = last_focus;
+            } else {
+                d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
+                history_add(d->history, d->focus);
+            }
         }
+
+        update_vacant_state(b->parent);
     }
+
+    history_remove(d->history, n);
 }
 
 void remove_node(desktop_t *d, node_t *n)
@@ -472,10 +497,23 @@ void remove_node(desktop_t *d, node_t *n)
     num_clients--;
     ewmh_update_client_list();
 
-    if (desk == d)
+    if (mon->desk == d)
         update_current();
 }
 
+void destroy_tree(node_t *n)
+{
+    if (n == NULL)
+        return;
+    node_t *first_tree = n->first_child;
+    node_t *second_tree = n->second_child;
+    if (n->client != NULL)
+        free(n->client);
+    free(n);
+    destroy_tree(first_tree);
+    destroy_tree(second_tree);
+}
+
 void swap_nodes(node_t *n1, node_t *n2)
 {
     if (n1 == NULL || n2 == NULL || n1 == n2)
@@ -505,94 +543,184 @@ void swap_nodes(node_t *n1, node_t *n2)
 
     n1->parent = pn2;
     n2->parent = pn1;
+
+    if (n1->vacant != n2->vacant) {
+        update_vacant_state(n1->parent);
+        update_vacant_state(n2->parent);
+    }
 }
 
-void transfer_node(desktop_t *ds, desktop_t *dd, node_t *n)
+void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
 {
-    if (n == NULL || ds == NULL || dd == NULL || dd == ds)
+    if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
         return;
 
     PRINTF("transfer node %X\n", n->client->window);
 
     unlink_node(ds, n);
+    insert_node(md, dd, n);
+    ewmh_set_wm_desktop(n, dd);
 
-    if (ds == desk) {
+    if (ds == ms->desk && dd != md->desk) {
         window_hide(n->client->window);
     }
 
-    insert_node(dd, n);
+    fit_monitor(md, n->client);
+
+    if (n->client->fullscreen)
+        window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
 
-    if (dd == desk) {
+    if (ds != ms->desk && dd == md->desk) {
         window_show(n->client->window);
-        focus_node(dd, n, true);
+        focus_node(md, dd, n, true);
     } else {
-        focus_node(dd, n, false);
+        focus_node(md, dd, n, false);
     }
 
-    if (ds == desk || dd == desk)
+    if (ds == ms->desk || dd == md->desk)
         update_current();
 }
 
+void select_monitor(monitor_t *m)
+{
+    if (m == NULL || mon == m)
+        return;
+
+    PRINTF("select monitor %s\n", m->name);
+
+    focus_node(m, m->desk, m->desk->focus, true);
+
+    last_mon = mon;
+    mon = m;
+
+    ewmh_update_current_desktop();
+    put_status();
+}
+
 void select_desktop(desktop_t *d)
 {
-    if (d == NULL || d == desk)
+    if (d == NULL || d == mon->desk)
         return;
 
     PRINTF("select desktop %s\n", d->name);
 
-    node_t *n = first_extrema(d->root);
+    if (visible) {
+        node_t *n = first_extrema(d->root);
 
-    while (n != NULL) {
-        /* xcb_map_window(dpy, n->client->window); */
-        window_show(n->client->window);
-        n = next_leaf(n);
-    }
+        while (n != NULL) {
+            window_show(n->client->window);
+            n = next_leaf(n);
+        }
 
-    n = first_extrema(desk->root);
+        n = first_extrema(mon->desk->root);
 
-    while (n != NULL) {
-        window_hide(n->client->window);
-        n = next_leaf(n);
+        while (n != NULL) {
+            window_hide(n->client->window);
+            n = next_leaf(n);
+        }
     }
 
-    last_desk = desk;
-    desk = d;
+    mon->last_desk = mon->desk;
+    mon->desk = d;
 
     update_current();
     ewmh_update_current_desktop();
+    put_status();
+}
+
+void cycle_monitor(cycle_dir_t dir)
+{
+    if (dir == CYCLE_NEXT)
+        select_monitor((mon->next == NULL ? mon_head : mon->next));
+    else if (dir == CYCLE_PREV)
+        select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
 }
 
-void cycle_desktop(cycle_dir_t dir)
+void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
 {
-    if (dir == DIR_NEXT)
-        select_desktop((desk->next == NULL ? desk_head : desk->next));
-    else if (dir == DIR_PREV)
-        select_desktop((desk->prev == NULL ? desk_tail : desk->prev));
+    desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
+    if (f == NULL)
+        f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
+
+    while (f != d) {
+        if (skip == DESKTOP_SKIP_NONE 
+                || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
+                || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
+            select_desktop(f);
+            return;
+        }
+        f = (dir == CYCLE_PREV ? f->prev : f->next);
+        if (f == NULL)
+            f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
+    }
 }
 
-void cycle_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
+void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
 {
     if (n == NULL)
         return;
 
     PUTS("cycle leaf");
 
-    node_t *f = (dir == DIR_PREV ? prev_leaf(n) : next_leaf(n));
+    node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
     if (f == NULL)
-        f = (dir == DIR_PREV ? second_extrema(d->root) : first_extrema(d->root));
+        f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
 
     while (f != n) {
         bool tiled = is_tiled(f->client);
-        if (skip == SKIP_NONE || (skip == SKIP_TILED && !tiled) || (skip == SKIP_FLOATING && tiled)) {
-            focus_node(d, f, true);
+        if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
+                || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
+                || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
+            focus_node(m, d, f, true);
             return;
         }
-        f = (dir == DIR_PREV ? prev_leaf(f) : next_leaf(f));
+        f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
         if (f == NULL)
-            f = (dir == DIR_PREV ? second_extrema(d->root) : first_extrema(d->root));
+            f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
     }
 }
 
+void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
+{
+    if (n == NULL)
+        return;
+
+    PUTS("nearest leaf");
+
+    node_t *x = NULL;
+
+    for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
+        if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
+                || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
+                || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
+            if ((dir == NEAREST_OLDER
+                        && (f->client->uid < n->client->uid)
+                        && (x == NULL || f->client->uid > x->client->uid))
+                    || (dir == NEAREST_NEWER
+                        && (f->client->uid > n->client->uid)
+                        && (x == NULL || f->client->uid < x->client->uid)))
+                x = f;
+
+    focus_node(m, d, x, true);
+}
+
+void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
+    if (d == NULL || d->root == NULL || is_leaf(d->root))
+        return;
+    node_t *par = d->focus->parent;
+    bool focus_first_child = is_first_child(d->focus);
+    if (dir == CIRCULATE_FORWARD)
+        for (node_t *s = second_extrema(d->root), *f = prev_leaf(s); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
+            swap_nodes(f, s);
+    else
+        for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
+            swap_nodes(f, s);
+    if (focus_first_child)
+        focus_node(m, d, par->first_child, true);
+    else
+        focus_node(m, d, par->second_child, true);
+}
+
 void update_vacant_state(node_t *n)
 {
     if (n == NULL)
@@ -609,24 +737,227 @@ void update_vacant_state(node_t *n)
     }
 }
 
-desktop_t *find_desktop(char *name)
+void fit_monitor(monitor_t *m, client_t *c)
+{
+    xcb_rectangle_t crect = c->floating_rectangle;
+    xcb_rectangle_t mrect = m->rectangle;
+    while (crect.x < mrect.x)
+        crect.x += mrect.width;
+    while (crect.x > (mrect.x + mrect.width - 1))
+        crect.x -= mrect.width;
+    while (crect.y < mrect.y)
+        crect.y += mrect.height;
+    while (crect.y > (mrect.y + mrect.height - 1))
+        crect.y -= mrect.height;
+    c->floating_rectangle = crect;
+}
+
+void put_status(void)
+{
+    if (status_fifo == NULL)
+        return;
+    bool urgent = false;
+    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+        fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
+        for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
+            for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
+                urgent |= n->client->urgent;
+            fprintf(status_fifo, "%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
+        }
+    }
+    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));
+    fflush(status_fifo);
+}
+
+void list_monitors(list_option_t opt, char *rsp)
 {
-    desktop_t *d = desk_head;
-    while (d != NULL) {
-        if (strcmp(d->name, name) == 0)
-            return d;
-        d = d->next;
+    char line[MAXLEN];
+    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+        snprintf(line, sizeof(line), "%s %ux%u%+i%+i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y);
+        strncat(rsp, line, REMLEN(rsp));
+        if (m == mon)
+            strncat(rsp, " #\n", REMLEN(rsp));
+        else if (m == last_mon)
+            strncat(rsp, " ~\n", REMLEN(rsp));
+        else
+            strncat(rsp, "\n", REMLEN(rsp));
+        if (opt == LIST_OPTION_VERBOSE)
+            list_desktops(m, opt, 1, rsp);
     }
-    return NULL;
 }
 
-void add_desktop(char *name)
+void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
+{
+    char line[MAXLEN];
+    for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+        for (unsigned int i = 0; i < depth; i++)
+            strncat(rsp, "  ", REMLEN(rsp));
+        snprintf(line, sizeof(line), "%s %c", d->name, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
+        strncat(rsp, line, REMLEN(rsp));
+        if (d == m->desk)
+            strncat(rsp, " @\n", REMLEN(rsp));
+        else if (d == m->last_desk)
+            strncat(rsp, " ~\n", REMLEN(rsp));
+        else
+            strncat(rsp, "\n", REMLEN(rsp));
+        if (opt == LIST_OPTION_VERBOSE)
+            list(d, d->root, rsp, depth + 1);
+    }
+}
+
+void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
+{
+    if (n == NULL)
+        return;
+
+    char line[MAXLEN];
+
+    for (unsigned int i = 0; i < depth; i++)
+        strncat(rsp, "  ", REMLEN(rsp));
+
+    if (is_leaf(n)) {
+        client_t *c = n->client;
+        snprintf(line, sizeof(line), "%c %s %X %u %u %ux%u%+i%+i %c%c%c%c%c", (c->born_as == MODE_AUTOMATIC ? 'a' : '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' : '-'));
+    } else {
+        snprintf(line, sizeof(line), "%c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), n->split_ratio);
+    }
+
+    strncat(rsp, line, REMLEN(rsp));
+
+    if (n == d->focus)
+        strncat(rsp, " *\n", REMLEN(rsp));
+    else
+        strncat(rsp, "\n", REMLEN(rsp));
+
+    list(d, n->first_child, rsp, depth + 1);
+    list(d, n->second_child, rsp, depth + 1);
+}
+
+void restore(char *file_path)
 {
-    desktop_t *d = make_desktop(name);
-    desk_tail->next = d;
-    d->prev = desk_tail;
-    desk_tail = d;
-    num_desktops++;
-    ewmh_update_number_of_desktops();
-    ewmh_update_desktop_names();
+    if (file_path == NULL)
+        return;
+
+    FILE *snapshot = fopen(file_path, "r");
+    if (snapshot == NULL) {
+        warn("restore: can't open file\n");
+        return;
+    }
+
+    char line[MAXLEN];
+    monitor_t *m = NULL;
+    desktop_t *d = NULL;
+    node_t *n = NULL;
+    num_clients = 0;
+    unsigned int level, last_level = 0, max_uid = 0;
+    bool aborted = false;
+
+    while (!aborted && fgets(line, sizeof(line), snapshot) != NULL) {
+        unsigned int len = strlen(line);
+        level = 0;
+        while (level < strlen(line) && isspace(line[level]))
+            level++;
+        if (level == 0) {
+            if (m == NULL)
+                m = mon_head;
+            else
+                m = m->next;
+            if (len >= 2)
+                switch (line[len - 2]) {
+                    case '#':
+                        mon = m;
+                        break;
+                    case '~':
+                        last_mon = m;
+                        break;
+                }
+        } else if (level == 2) {
+            if (d == NULL)
+                d = m->desk_head;
+            else
+                d = d->next;
+            int i = len - 1;
+            while (i > 0 && !isupper(line[i]))
+                i--;
+            if (line[i] == 'M')
+                d->layout = LAYOUT_MONOCLE;
+            else if (line[i] == 'T')
+                d->layout = LAYOUT_TILED;
+            if (len >= 2)
+                switch (line[len - 2]) {
+                    case '@':
+                        m->desk = d;
+                        break;
+                    case '~':
+                        m->last_desk = d;
+                        break;
+                }
+        } else {
+            node_t *birth = make_node();
+            if (level == 4) {
+                empty_desktop(d);
+                d->root = birth;
+            } else {
+                if (level > last_level) {
+                    n->first_child = birth;
+                } else {
+                    do {
+                        n = n->parent;
+                    } while (n != NULL && n->second_child != NULL);
+                    if (n == NULL) {
+                        warn("restore: file is malformed\n");
+                        aborted = true;
+                    }
+                    n->second_child = birth;
+                }
+                birth->parent = n;
+            }
+            n = birth;
+
+            if (isupper(line[level])) {
+                char st;
+                sscanf(line + level, "%c %lf", &st, &n->split_ratio);
+                if (st == 'H')
+                    n->split_type = TYPE_HORIZONTAL;
+                else if (st == 'V')
+                    n->split_type = TYPE_VERTICAL;
+            } else {
+                client_t *c = make_client(XCB_NONE);
+                num_clients++;
+                char ba, floating, transient, fullscreen, urgent, locked;
+                sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c%c%c%c%c", &ba, 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);
+                if (ba == 'a')
+                    c->born_as = MODE_AUTOMATIC;
+                else if (ba == 'm')
+                    c->born_as = MODE_MANUAL;
+                c->floating = (floating == '-' ? false : true);
+                c->transient = (transient == '-' ? false : true);
+                c->fullscreen = (fullscreen == '-' ? false : true);
+                c->urgent = (urgent == '-' ? false : true);
+                c->locked = (locked == '-' ? false : true);
+                if (c->uid > max_uid)
+                    max_uid = c->uid;
+                n->client = c;
+                if (len >= 2 && line[len -2] == '*')
+                    d->focus = n;
+            }
+        }
+        last_level = level;
+    }
+
+    if (!aborted) {
+        client_uid = max_uid + 1;
+        for (monitor_t *m = mon_head; m != NULL; m = m->next)
+            for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
+                for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
+                    uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
+                    xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
+                    if (n->client->floating) {
+                        n->vacant = true;
+                        update_vacant_state(n->parent);
+                    }
+                }
+    }
+
+    fclose(snapshot);
 }