]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
Mention mailing list
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index 186cf1e8b757f7f4cdde13babbc82a5fd8592a1b..628f7700554d79254f080831a03869cf8c9e9111 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -6,7 +6,6 @@
 #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 +132,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,67 +166,84 @@ 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 dump_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
 {
     if (n == NULL)
         return;
 
     char line[MAXLEN];
 
-    for (int i = 0; i < depth; i++)
-        strcat(rsp, "  ");
+    for (unsigned int i = 0; i < depth; i++)
+        strncat(rsp, "  ", REMLEN(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->rectangle.x, n->client->rectangle.y, n->client->rectangle.width, n->client->rectangle.height); 
+        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" : "-"));
     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 [%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);
+        snprintf(line, sizeof(line), "%s %.2f", (n->split_type == TYPE_HORIZONTAL ? "H" : "V"), n->split_ratio);
 
-    strcat(rsp, line);
+    strncat(rsp, line, REMLEN(rsp));
 
     if (n == d->focus)
-        strcat(rsp, " *\n");
+        strncat(rsp, " *\n", REMLEN(rsp));
     else
-        strcat(rsp, "\n");
+        strncat(rsp, "\n", REMLEN(rsp));
 
     dump_tree(d, n->first_child, rsp, depth + 1);
     dump_tree(d, n->second_child, rsp, depth + 1);
 }
 
-void refresh_current(void) {
-    if (desk->focus == NULL)
-        ewmh_update_active_window();
-    else
-        focus_node(desk, desk->focus, true);
+void list_monitors(list_option_t opt, char *rsp)
+{
+    monitor_t *m = mon_head;
+
+    while (m != NULL) {
+        strncat(rsp, m->name, REMLEN(rsp));
+        if (mon == m)
+            strncat(rsp, " #\n", REMLEN(rsp));
+        else
+            strncat(rsp, "\n", REMLEN(rsp));
+        if (opt == LIST_OPTION_VERBOSE)
+            list_desktops(m, opt, 1, rsp);
+        m = m->next;
+    }
 }
 
-void list_desktops(char *rsp)
+void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
 {
-    desktop_t *d = desk_head;
+    desktop_t *d = m->desk_head;
 
     while (d != NULL) {
-        strcat(rsp, d->name);
-        if (desk == d)
-            strcat(rsp, " @\n");
+        for (unsigned int i = 0; i < depth; i++)
+            strncat(rsp, "  ", REMLEN(rsp));
+        strncat(rsp, d->name, REMLEN(rsp));
+        if (m->desk == d)
+            strncat(rsp, " @\n", REMLEN(rsp));
         else
-            strcat(rsp, "\n");
-        dump_tree(d, d->root, rsp, 1);
+            strncat(rsp, "\n", REMLEN(rsp));
+        if (opt == LIST_OPTION_VERBOSE)
+            dump_tree(d, d->root, rsp, depth + 1);
         d = d->next;
     }
 }
 
-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;
+    if (focus_follows_mouse)
+        get_pointer_position(&pointer_position);
+    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;
@@ -235,28 +251,38 @@ 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;
         } else {
-            r = n->client->rectangle;
+            r = n->client->floating_rectangle;
         }
 
         window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
-        window_border_width(n->client->window, border_width);
-        draw_triple_border(n, (n == d->focus ? active_border_color_pxl : normal_border_color_pxl));
-
-        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;
         xcb_rectangle_t second_rect;
 
@@ -276,17 +302,17 @@ 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;
 
-    PUTS("insert node");
+    PRINTF("insert node %X\n", n->client->window);
 
     node_t *focus = d->focus;
 
@@ -296,12 +322,16 @@ void insert_node(desktop_t *d, node_t *n)
         node_t *dad = make_node();
         node_t *fopar = focus->parent;
         n->parent = dad;
+        n->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 {
@@ -366,33 +396,37 @@ 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;
 
-    if (desk->focus != NULL && desk->focus->client->fullscreen)
-        return;
-
-    PRINTF("focus_node %x\n", n->client->window);
+    PRINTF("focus node %X\n", n->client->window);
 
     split_mode = MODE_AUTOMATIC;
+    n->client->urgent = false;
 
     if (is_mapped) {
-        if (d->focus != n) {
-            draw_triple_border(d->focus, normal_border_color_pxl);
-            draw_triple_border(n, active_border_color_pxl);
+        if (mon != m || d->focus != n) {
+            window_draw_border(d->focus, m != mon, m == mon);
+            window_draw_border(n, true, true);
         }
+        if (focus_follows_mouse)
+            get_pointer_position(&pointer_position);
         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 (!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;
@@ -404,10 +438,10 @@ void focus_node(desktop_t *d, node_t *n, bool is_mapped)
 
 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);
 }
 
 void unlink_node(desktop_t *d, node_t *n)
@@ -415,7 +449,7 @@ void unlink_node(desktop_t *d, node_t *n)
     if (d == NULL || n == NULL)
         return;
 
-    PUTS("unlink node");
+    PRINTF("unlink node %X\n", n->client->window);
 
     node_t *p = n->parent;
 
@@ -426,10 +460,16 @@ void unlink_node(desktop_t *d, node_t *n)
     } 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;
-        else
+            if (n->born_as == MODE_AUTOMATIC)
+                rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
+        } else {
             b = p->first_child;
+            if (n->born_as == MODE_AUTOMATIC)
+                rotate_tree(b, ROTATE_CLOCKWISE);
+        }
         b->parent = g;
         if (g != NULL) {
             if (is_first_child(p))
@@ -443,13 +483,17 @@ void unlink_node(desktop_t *d, node_t *n)
         n->parent = NULL;
         free(p);
 
-        if (n == d->focus) {
-            if (d->last_focus != NULL && d->last_focus != n)
+        if (n == d->last_focus) {
+            d->last_focus = NULL;
+        } else if (n == d->focus) {
+            if (d->last_focus != NULL)
                 d->focus = d->last_focus;
             else
-                d->focus = (is_first_child(b) ? second_extrema(b) : first_extrema(b));
+                d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
             d->last_focus = NULL;
         }
+
+        update_vacant_state(b->parent);
     }
 }
 
@@ -458,7 +502,7 @@ void remove_node(desktop_t *d, node_t *n)
     if (d == NULL || n == NULL)
         return;
 
-    PUTS("remove node");
+    PRINTF("remove node %X\n", n->client->window);
 
     unlink_node(d, n);
     free(n->client);
@@ -467,7 +511,7 @@ void remove_node(desktop_t *d, node_t *n)
     num_clients--;
     ewmh_update_client_list();
 
-    if (desk == d)
+    if (mon->desk == d)
         update_current();
 }
 
@@ -500,115 +544,193 @@ 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 fit_monitor(monitor_t *m, client_t *c)
 {
-    if (n == NULL || ds == NULL || dd == NULL || dd == ds)
+    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 transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
+{
+    if (n == NULL || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
         return;
 
-    PUTS("transfer node");
+    PRINTF("transfer node %X\n", n->client->window);
 
     unlink_node(ds, n);
-    if (ds == desk)
-        xcb_unmap_window(dpy, n->client->window);
-
-    insert_node(dd, n);
-    if (dd == desk) {
-        apply_layout(dd, dd->root, root_rect);
-        xcb_map_window(dpy, n->client->window);
-        focus_node(dd, n, true);
+    insert_node(md, dd, n);
+    ewmh_set_wm_desktop(n, dd);
+
+    if (ds == ms->desk && dd != md->desk) {
+        window_hide(n->client->window);
+    }
+
+    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 (ds != ms->desk && dd == md->desk) {
+        window_show(n->client->window);
+        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_desktop(desktop_t *d)
+void select_monitor(monitor_t *m)
 {
-    if (d == NULL || d == desk)
+    if (m == NULL || mon == m)
         return;
 
-    PUTS("select desktop");
+    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();
+}
+
+void select_desktop(desktop_t *d)
+{
+    if (d == NULL || d == mon->desk)
+        return;
 
-    if (d->focus != NULL)
-        xcb_map_window(dpy, d->focus->client->window);
+    PRINTF("select desktop %s\n", d->name);
 
     node_t *n = first_extrema(d->root);
 
     while (n != NULL) {
-        if (n != d->focus)
-            xcb_map_window(dpy, n->client->window);
+        window_show(n->client->window);
         n = next_leaf(n);
     }
 
-    n = first_extrema(desk->root);
+    n = first_extrema(mon->desk->root);
 
     while (n != NULL) {
-        if (n != desk->focus)
-            xcb_unmap_window(dpy, n->client->window);
+        window_hide(n->client->window);
         n = next_leaf(n);
     }
 
-    if (desk->focus != NULL)
-        xcb_unmap_window(dpy, desk->focus->client->window);
-
-    last_desk = desk;
-    desk = d;
+    mon->last_desk = mon->desk;
+    mon->desk = d;
 
     update_current();
     ewmh_update_current_desktop();
 }
 
-void cycle_desktop(cycle_dir_t dir)
+void cycle_monitor(cycle_dir_t dir)
 {
-    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));
+    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_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
+void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
+{
+    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(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 == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
 
-    while (f != NULL) {
+    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));
-    }
-
-    if (skip == SKIP_NONE && f == NULL) {
-        if (dir == DIR_PREV)
-            focus_node(d, second_extrema(d->root), true);
-        else
-            focus_node(d, first_extrema(d->root), true);
+        f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
+        if (f == NULL)
+            f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
     }
 }
 
-void toggle_floating(node_t *n)
+void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
 {
-    if (n == NULL || n->client->transient)
+    if (n == NULL)
         return;
 
-    PUTS("toggle floating");
+    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);
+}
 
-    client_t *c = n->client;
-    c->floating = !c->floating;
-    n->vacant = !n->vacant;
-    update_vacant_state(n->parent);
-    if (c->floating)
-        window_raise(c->window);
+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)
@@ -627,24 +749,42 @@ void update_vacant_state(node_t *n)
     }
 }
 
-desktop_t *find_desktop(char *name)
+monitor_t *find_monitor(char *name)
 {
-    desktop_t *d = desk_head;
-    while (d != NULL) {
-        if (strcmp(d->name, name) == 0)
-            return d;
-        d = d->next;
-    }
+    for (monitor_t *m = mon_head; m != NULL; m = m->next)
+        if (strcmp(m->name, name) == 0)
+            return m;
     return NULL;
 }
 
-void add_desktop(char *name)
+void add_desktop(monitor_t *m, char *name)
 {
     desktop_t *d = make_desktop(name);
-    desk_tail->next = d;
-    d->prev = desk_tail;
-    desk_tail = d;
+    if (m->desk == NULL) {
+        m->desk = d;
+        m->desk_head = d;
+        m->desk_tail = d;
+    } else {
+        m->desk_tail->next = d;
+        d->prev = m->desk_tail;
+        m->desk_tail = d;
+    }
     num_desktops++;
     ewmh_update_number_of_desktops();
     ewmh_update_desktop_names();
 }
+
+void add_monitor(xcb_rectangle_t *rect)
+{
+    monitor_t *m = make_monitor(rect);
+    if (mon == NULL) {
+        mon = m;
+        mon_head = m;
+        mon_tail = m;
+    } else {
+        mon_tail->next = m;
+        m->prev = mon_tail;
+        mon_tail = m;
+    }
+    num_monitors++;
+}