]> 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 8ba7df0132751aed88851217d2dbf0222f63b665..628f7700554d79254f080831a03869cf8c9e9111 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -166,42 +166,14 @@ void rotate_tree(node_t *n, rotate_t rot)
     rotate_tree(n->second_child, rot);
 }
 
-void magnetise_tree(node_t *n, corner_t corner)
-{
-    if (n == NULL || is_leaf(n))
-        return;
-
-    PUTS("magnetise tree");
-
-    switch (n->split_type) {
-        case TYPE_HORIZONTAL:
-            if (corner == TOP_LEFT || corner == TOP_RIGHT)
-                change_split_ratio(n, CHANGE_DECREASE);
-            else
-                change_split_ratio(n, CHANGE_INCREASE);
-            break;
-        case TYPE_VERTICAL:
-            if (corner == TOP_LEFT || corner == BOTTOM_LEFT)
-                change_split_ratio(n, CHANGE_DECREASE);
-            else
-                change_split_ratio(n, CHANGE_INCREASE);
-            break;
-        default:
-            break;
-    }
-
-    magnetise_tree(n->first_child, corner);
-    magnetise_tree(n->second_child, corner);
-}
-
-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++)
+    for (unsigned int i = 0; i < depth; i++)
         strncat(rsp, "  ", REMLEN(rsp));
 
     if (is_leaf(n))
@@ -220,39 +192,58 @@ void dump_tree(desktop_t *d, node_t *n, char *rsp, int depth)
     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) {
+        for (unsigned int i = 0; i < depth; i++)
+            strncat(rsp, "  ", REMLEN(rsp));
         strncat(rsp, d->name, REMLEN(rsp));
-        if (desk == d)
+        if (m->desk == d)
             strncat(rsp, " @\n", REMLEN(rsp));
         else
             strncat(rsp, "\n", REMLEN(rsp));
-        dump_tree(d, d->root, rsp, 1);
+        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;
@@ -278,7 +269,8 @@ void apply_layout(desktop_t *d, node_t *n, xcb_rectangle_t rect)
                 r = rect;
             else if (d->layout == LAYOUT_MONOCLE)
                 r = root_rect;
-            int bleed = window_gap + 2 * n->client->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;
@@ -288,10 +280,7 @@ 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, n->client->border_width);
-        window_draw_border(n, n == d->focus);
-
-        if (d->layout == LAYOUT_MONOCLE && n == d->focus)
-            window_raise(n->client->window);
+        window_draw_border(n, n == d->focus, m == mon);
 
     } else {
         xcb_rectangle_t first_rect;
@@ -313,12 +302,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;
@@ -339,7 +328,7 @@ void insert_node(desktop_t *d, node_t *n)
                 if (fopar == NULL) {
                     dad->first_child = n;
                     dad->second_child = focus;
-                    if (focus->rectangle.width > focus->rectangle.height)
+                    if (m->rectangle.width > m->rectangle.height)
                         dad->split_type = TYPE_VERTICAL;
                     else
                         dad->split_type = TYPE_HORIZONTAL;
@@ -412,7 +401,7 @@ void insert_node(desktop_t *d, node_t *n)
     }
 }
 
-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;
@@ -423,33 +412,36 @@ void focus_node(desktop_t *d, node_t *n, bool is_mapped)
     n->client->urgent = false;
 
     if (is_mapped) {
-        if (d->focus != n) {
-            window_draw_border(d->focus, false);
-            window_draw_border(n, true);
+        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;
         d->focus = n;
     }
 
-    if (d == desk)
-        ewmh_update_active_window();
+    ewmh_update_active_window();
 }
 
 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)
@@ -519,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();
 }
 
@@ -559,35 +551,70 @@ void swap_nodes(node_t *n1, node_t *n2)
     }
 }
 
-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;
 
     PRINTF("transfer node %X\n", n->client->window);
 
     unlink_node(ds, n);
-    insert_node(dd, 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);
     }
 
-    if (dd == desk) {
+    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(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();
+}
+
 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);
@@ -599,29 +626,48 @@ void select_desktop(desktop_t *d)
         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);
     }
 
-    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 == CYCLE_NEXT)
-        select_desktop((desk->next == NULL ? desk_head : desk->next));
+        select_monitor((mon->next == NULL ? mon_head : mon->next));
     else if (dir == CYCLE_PREV)
-        select_desktop((desk->prev == NULL ? desk_tail : desk->prev));
+        select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
+}
+
+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(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;
@@ -634,10 +680,10 @@ void cycle_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
 
     while (f != n) {
         bool tiled = is_tiled(f->client);
-        if (skip == SKIP_NONE || (skip == SKIP_TILED && !tiled) || (skip == SKIP_FLOATING && tiled)
-                || (skip == SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
-                || (skip == SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
-            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 == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
@@ -646,6 +692,47 @@ void cycle_leaf(desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
     }
 }
 
+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)
@@ -662,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++;
+}