]> git.lizzy.rs Git - bspwm.git/blobdiff - window.c
Stack tiling windows via focus history
[bspwm.git] / window.c
index d9242da6fd76615f5f700ef16eca4fcbafdf80d2..2b005af801e7d62cd4c993196b6a3c60002092a8 100644 (file)
--- a/window.c
+++ b/window.c
@@ -61,22 +61,27 @@ bool locate_desktop(char *name, desktop_location_t *loc)
     return false;
 }
 
-bool is_inside(monitor_t *m, xcb_point_t p)
+bool is_inside(monitor_t *m, xcb_point_t pt)
 {
     xcb_rectangle_t r = m->rectangle;
-    return (r.x <= p.x && p.x < (r.x + r.width)
-            && r.y <= p.y && p.y < (r.y + r.height));
+    return (r.x <= pt.x && pt.x < (r.x + r.width)
+            && r.y <= pt.y && pt.y < (r.y + r.height));
 }
 
-monitor_t *underlying_monitor(client_t *c)
+monitor_t *monitor_from_point(xcb_point_t pt)
 {
-    xcb_point_t p = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
     for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        if (is_inside(m, p))
+        if (is_inside(m, pt))
             return m;
     return NULL;
 }
 
+monitor_t *underlying_monitor(client_t *c)
+{
+    xcb_point_t pt = (xcb_point_t) {c->floating_rectangle.x, c->floating_rectangle.y};
+    return monitor_from_point(pt);
+}
+
 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
 {
     window_location_t loc;
@@ -91,11 +96,12 @@ void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
     if (override_redirect || locate_window(win, &loc))
         return;
 
-    bool floating = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
+    bool floating = false, follow = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
 
-    handle_rules(win, &m, &d, &floating, &transient, &fullscreen, &takes_focus, &manage);
+    handle_rules(win, &m, &d, &floating, &follow, &transient, &fullscreen, &takes_focus, &manage);
 
     if (!manage) {
+        disable_shadow(win);
         window_show(win);
         return;
     }
@@ -120,6 +126,8 @@ void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
 
     insert_node(m, d, birth);
 
+    disable_shadow(c->window);
+
     if (floating)
         toggle_floating(birth);
 
@@ -151,9 +159,14 @@ void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
     if (takes_focus)
         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
 
-    uint32_t values[] = {CLIENT_EVENT_MASK};
+    uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
     xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
 
+    if (follow) {
+        select_monitor(m);
+        select_desktop(d);
+    }
+
     num_clients++;
     ewmh_set_wm_desktop(birth, d);
     ewmh_update_client_list();
@@ -161,7 +174,7 @@ void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
 
 void adopt_orphans(void)
 {
-    xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, screen->root), NULL);
+    xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
     if (qtr == NULL)
         return;
     int len = xcb_query_tree_children_length(qtr);
@@ -183,69 +196,46 @@ void adopt_orphans(void)
 
 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
 {
-    if (n == NULL)
-        return;
-
-    if (border_width < 1 || n->client->border_width < 1)
+    if (n == NULL || border_width < 1 || n->client->border_width < 1)
         return;
 
     xcb_window_t win = n->client->window;
+    uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
 
-    xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
-
-    uint16_t width = actual_rectangle.width;
-    uint16_t height = actual_rectangle.height;
-
-    uint16_t full_width = width + 2 * border_width;
-    uint16_t full_height = height + 2 * border_width;
-
-    xcb_rectangle_t inner_rectangles[] =
-    {
-        { width, 0, 2 * border_width, height + 2 * border_width },
-        { 0, height, width + 2 * border_width, 2 * border_width }
-    };
+    if (split_mode == MODE_AUTOMATIC || !focused_monitor || !focused_window) {
+        xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
+    } else {
+        xcb_rectangle_t actual_rectangle = (is_tiled(n->client) ? n->client->tiled_rectangle : n->client->floating_rectangle);
 
-    xcb_rectangle_t main_rectangles[] =
-    {
-        { width + inner_border_width, 0, 2 * (main_border_width + outer_border_width), height + 2 * border_width },
-        { 0, height + inner_border_width, width + 2 * border_width, 2 * (main_border_width + outer_border_width) }
-    };
+        uint16_t width = actual_rectangle.width;
+        uint16_t height = actual_rectangle.height;
 
-    xcb_rectangle_t outer_rectangles[] =
-    {
-        { width + inner_border_width + main_border_width, 0, 2 * outer_border_width, height + 2 * border_width },
-        { 0, height + inner_border_width + main_border_width, width + 2 * border_width, 2 * outer_border_width }
-    };
+        uint16_t full_width = width + 2 * border_width;
+        uint16_t full_height = height + 2 * border_width;
 
-    xcb_rectangle_t *presel_rectangles;
+        xcb_rectangle_t border_rectangles[] =
+        {
+            { width, 0, 2 * border_width, height + 2 * border_width },
+            { 0, height, width + 2 * border_width, 2 * border_width }
+        };
 
-    xcb_pixmap_t pix = xcb_generate_id(dpy);
-    xcb_create_pixmap(dpy, root_depth, pix, win, full_width, full_height);
+        xcb_rectangle_t *presel_rectangles;
 
-    xcb_gcontext_t gc = xcb_generate_id(dpy);
-    xcb_create_gc(dpy, gc, pix, 0, NULL);
+        uint8_t win_depth = root_depth;
+        xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
+        if (geo != NULL)
+            win_depth = geo->depth;
+        free(geo);
 
-    uint32_t main_border_color_pxl = get_main_border_color(n->client, focused_window, focused_monitor);
+        xcb_pixmap_t pix = xcb_generate_id(dpy);
+        xcb_create_pixmap(dpy, win_depth, pix, win, full_width, full_height);
 
-    /* inner border */
-    if (inner_border_width > 0) {
-        xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &inner_border_color_pxl);
-        xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(inner_rectangles), inner_rectangles);
-    }
+        xcb_gcontext_t gc = xcb_generate_id(dpy);
+        xcb_create_gc(dpy, gc, pix, 0, NULL);
 
-    /* main border */
-    if (main_border_width > 0) {
-        xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &main_border_color_pxl);
-        xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(main_rectangles), main_rectangles);
-    }
+        xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
+        xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(border_rectangles), border_rectangles);
 
-    /* outer border */
-    if (outer_border_width > 0) {
-        xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &outer_border_color_pxl);
-        xcb_poly_fill_rectangle(dpy, pix, gc, LENGTH(outer_rectangles), outer_rectangles);
-    }
-
-    if (split_mode == MODE_MANUAL && focused_monitor && focused_window) {
         uint16_t fence = (int16_t) (n->split_ratio * ((split_dir == DIR_UP || split_dir == DIR_DOWN) ? height : width));
         presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
         switch (split_dir) {
@@ -266,16 +256,14 @@ void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
                 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
                 break;
         }
+
         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
         xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
+        xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
         free(presel_rectangles);
+        xcb_free_gc(dpy, gc);
+        xcb_free_pixmap(dpy, pix);
     }
-
-    /* apply border pixmap */
-    xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pix);
-
-    xcb_free_gc(dpy, gc);
-    xcb_free_pixmap(dpy, pix);
 }
 
 void window_close(node_t *n)
@@ -344,7 +332,7 @@ void toggle_fullscreen(monitor_t *m, client_t *c)
 
 void toggle_floating(node_t *n)
 {
-    if (n == NULL || n->client->transient)
+    if (n == NULL || n->client->transient || n->client->fullscreen)
         return;
 
     PRINTF("toggle floating %X\n", n->client->window);
@@ -357,6 +345,10 @@ void toggle_floating(node_t *n)
         window_raise(c->window);
     else if (is_tiled(c))
         window_lower(c->window);
+    if (c->floating)
+        enable_shadow(c->window);
+    else
+        disable_shadow(c->window);
     update_current();
 }
 
@@ -367,6 +359,33 @@ void toggle_locked(client_t *c)
     c->locked = !c->locked;
 }
 
+void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+    if (value && mon->desk->focus == n)
+        return;
+    n->client->urgent = value;
+    put_status();
+    if (m->desk == d)
+        arrange(m, d);
+}
+
+void set_shadow(xcb_window_t win, uint32_t value)
+{
+    if (!apply_shadow_property)
+        return;
+    xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, compton_shadow, XCB_ATOM_CARDINAL, 32, 1, &value);
+}
+
+void enable_shadow(xcb_window_t win)
+{
+    set_shadow(win, 1);
+}
+
+void disable_shadow(xcb_window_t win)
+{
+    set_shadow(win, 0);
+}
+
 void list_windows(char *rsp)
 {
     char line[MAXLEN];
@@ -379,7 +398,7 @@ void list_windows(char *rsp)
             }
 }
 
-uint32_t get_main_border_color(client_t *c, bool focused_window, bool focused_monitor)
+uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
 {
     if (c == NULL)
         return 0;
@@ -390,10 +409,10 @@ uint32_t get_main_border_color(client_t *c, bool focused_window, bool focused_mo
         else
             return focused_border_color_pxl;
     } else if (focused_window) {
-        if (c->locked)
-            return active_locked_border_color_pxl;
-        else if (c->urgent)
+        if (c->urgent)
             return urgent_border_color_pxl;
+        else if (c->locked)
+            return active_locked_border_color_pxl;
         else
             return active_border_color_pxl;
     } else {
@@ -408,13 +427,40 @@ uint32_t get_main_border_color(client_t *c, bool focused_window, bool focused_mo
 
 void update_floating_rectangle(client_t *c)
 {
-    xcb_get_geometry_reply_t *geom = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
+    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
 
-    if (geom) {
-        c->floating_rectangle = (xcb_rectangle_t) {geom->x, geom->y, geom->width, geom->height};
-        free(geom);
-    } else {
+    if (geo != NULL)
+        c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
+    else
         c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
+
+    free(geo);
+}
+
+
+void query_pointer(xcb_window_t *win, xcb_point_t *pt)
+{
+    window_lower(motion_recorder);
+    xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
+    if (qpr != NULL) {
+        if (win != NULL)
+            *win = qpr->child;
+        if (pt != NULL)
+            *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
+        free(qpr);
+    }
+    window_raise(motion_recorder);
+}
+
+void window_focus(xcb_window_t win)
+{
+    window_location_t loc;
+    if (locate_window(win, &loc)) {
+        if (loc.node == mon->desk->focus)
+            return;
+        select_monitor(loc.monitor);
+        select_desktop(loc.desktop);
+        focus_node(loc.monitor, loc.desktop, loc.node, true);
     }
 }
 
@@ -442,11 +488,11 @@ void window_raise(xcb_window_t win)
     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
 }
 
-void window_pseudo_raise(desktop_t *d, xcb_window_t win)
+void stack_tiled(desktop_t *d)
 {
-    for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n))
-        if (is_tiled(n->client) && n->client->window != win)
-            window_lower(n->client->window);
+    for (node_list_t *x = d->history->tail; x != NULL; x = x->prev)
+        if (x->latest && is_tiled(x->node->client))
+            window_lower(x->node->client->window);
 }
 
 void window_lower(xcb_window_t win)
@@ -458,12 +504,12 @@ void window_lower(xcb_window_t win)
 void window_set_visibility(xcb_window_t win, bool visible) {
     uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
     uint32_t values_on[] = {ROOT_EVENT_MASK};
-    xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_off);
+    xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
     if (visible)
         xcb_map_window(dpy, win);
     else
         xcb_unmap_window(dpy, win);
-    xcb_change_window_attributes(dpy, screen->root, XCB_CW_EVENT_MASK, values_on);
+    xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
 }
 
 void window_hide(xcb_window_t win)
@@ -478,16 +524,28 @@ void window_show(xcb_window_t win)
 
 void toggle_visibility(void)
 {
-    uint32_t values_off[] = {CLIENT_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW};
-    uint32_t values_on[] = {CLIENT_EVENT_MASK};
+    if (visible)
+        clear_input_focus();
     visible = !visible;
     for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n)) {
-            xcb_window_t win = n->client->window;
-            xcb_change_window_attributes(dpy, win, XCB_CW_EVENT_MASK, values_off);
-            window_set_visibility(win, visible);
-            xcb_change_window_attributes(dpy, win, XCB_CW_EVENT_MASK, values_on);
-        }
+        for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n))
+            window_set_visibility(n->client->window, visible);
     if (visible)
         update_current();
 }
+
+void enable_motion_recorder(void)
+{
+    window_raise(motion_recorder);
+    window_show(motion_recorder);
+}
+
+void disable_motion_recorder(void)
+{
+    window_hide(motion_recorder);
+}
+
+void clear_input_focus(void)
+{
+    xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
+}