]> git.lizzy.rs Git - bspwm.git/blobdiff - window.c
Move a few functions from tree to desktop/monitor
[bspwm.git] / window.c
index 5c4f359ed2a40fc602426be5cb0010ca2c145e98..6aa184f7e751a40429e4e8787c257682536183ff 100644 (file)
--- a/window.c
+++ b/window.c
@@ -1,18 +1,26 @@
-#include <stdio.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <string.h>
-#include <xcb/xcb.h>
-#include <xcb/xcb_event.h>
-#include <xcb/xcb_icccm.h>
 #include "types.h"
+#include "monitor.h"
 #include "tree.h"
 #include "bspwm.h"
 #include "settings.h"
 #include "ewmh.h"
-#include "rules.h"
+#include "query.h"
+#include "rule.h"
 #include "window.h"
 
+pointer_state_t *make_pointer_state(void)
+{
+    pointer_state_t *p = malloc(sizeof(pointer_state_t));
+    p->monitor = NULL;
+    p->desktop = NULL;
+    p->node = p->vertical_fence = p->horizontal_fence = NULL;
+    p->client = NULL;
+    p->window = XCB_NONE;
+    return p;
+}
+
 void center(xcb_rectangle_t a, xcb_rectangle_t *b)
 {
     if (b->width < a.width)
@@ -27,59 +35,61 @@ bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
             && a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
 }
 
-bool might_cover(desktop_t *d, node_t *n)
+bool is_inside(monitor_t *m, xcb_point_t pt)
 {
-    for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
-        if (f != n && is_floating(f->client) && contains(n->client->floating_rectangle, f->client->floating_rectangle))
-            return true;
-    return false;
+    xcb_rectangle_t r = m->rectangle;
+    return (r.x <= pt.x && pt.x < (r.x + r.width)
+            && r.y <= pt.y && pt.y < (r.y + r.height));
 }
 
-bool locate_window(xcb_window_t win, window_location_t *loc)
+xcb_rectangle_t get_rectangle(client_t *c)
 {
-    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))
-                if (n->client->window == win) {
-                    loc->monitor = m;
-                    loc->desktop = d;
-                    loc->node = n;
-                    return true;
-                }
-    return false;
+    if (is_tiled(c))
+        return c->tiled_rectangle;
+    else
+        return c->floating_rectangle;
 }
 
-bool locate_desktop(char *name, desktop_location_t *loc)
+void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
 {
-    for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
-            if (strcmp(d->name, name) == 0) {
-                loc->monitor = m;
-                loc->desktop = d;
-                return true;
-            }
-    return false;
+    xcb_rectangle_t rect = get_rectangle(c);
+    switch (dir) {
+        case DIR_RIGHT:
+            pt->x = rect.x + rect.width;
+            pt->y = rect.y + (rect.height / 2);
+            break;
+        case DIR_DOWN:
+            pt->x = rect.x + (rect.width / 2);
+            pt->y = rect.y + rect.height;
+            break;
+        case DIR_LEFT:
+            pt->x = rect.x;
+            pt->y = rect.y + (rect.height / 2);
+            break;
+        case DIR_UP:
+            pt->x = rect.x + (rect.width / 2);
+            pt->y = rect.y;
+            break;
+    }
 }
 
-bool is_inside(monitor_t *m, xcb_point_t p)
+monitor_t *monitor_from_point(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));
+    for (monitor_t *m = mon_head; m != NULL; m = m->next)
+        if (is_inside(m, pt))
+            return m;
+    return NULL;
 }
 
 monitor_t *underlying_monitor(client_t *c)
 {
-    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))
-            return m;
-    return NULL;
+    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;
+    coordinates_t loc;
     xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
     uint8_t override_redirect = 0;
 
@@ -91,11 +101,11 @@ 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;
-
-    handle_rules(win, &m, &d, &floating, &transient, &fullscreen, &takes_focus, &manage);
+    bool floating = false, fullscreen = false, locked = false, follow = false, transient = false, takes_focus = true, manage = true;
+    handle_rules(win, &m, &d, &floating, &fullscreen, &locked, &follow, &transient, &takes_focus, &manage);
 
     if (!manage) {
+        disable_floating_atom(win);
         window_show(win);
         return;
     }
@@ -112,65 +122,73 @@ void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
     if (c->transient)
         floating = true;
 
-    node_t *birth = make_node();
-    birth->client = c;
+    node_t *n = make_node();
+    n->client = c;
 
-    if (floating)
-        split_mode = MODE_MANUAL;
+    insert_node(m, d, n, d->focus);
 
-    insert_node(m, d, birth);
-
-    if (floating)
-        toggle_floating(birth);
+    disable_floating_atom(c->window);
+    set_floating(d, n, floating);
+    set_locked(m, d, n, locked);
 
     if (d->focus != NULL && d->focus->client->fullscreen)
-        toggle_fullscreen(m, d->focus->client);
-
-    if (fullscreen)
-        toggle_fullscreen(m, birth->client);
+        set_fullscreen(d, d->focus, false);
 
-    if (is_tiled(c))
-        window_lower(c->window);
+    set_fullscreen(d, n, fullscreen);
 
     c->transient = transient;
 
-    if (takes_focus)
-        focus_node(m, d, birth, false);
+    bool give_focus = (takes_focus && (d == mon->desk || follow));
+    if (give_focus) {
+        focus_node(m, d, n);
+    } else if (takes_focus) {
+        pseudo_focus(d, n);
+    } else {
+        node_t *f = d->focus;
+        pseudo_focus(d, n);
+        if (f != NULL)
+            pseudo_focus(d, f);
+    }
 
-    xcb_rectangle_t *frect = &birth->client->floating_rectangle;
+    xcb_rectangle_t *frect = &n->client->floating_rectangle;
     if (frect->x == 0 && frect->y == 0)
         center(m->rectangle, frect);
 
-    fit_monitor(m, birth->client);
+    fit_monitor(m, n->client);
 
     arrange(m, d);
 
     if (d == m->desk && visible)
         window_show(c->window);
 
-    if (takes_focus)
+    /* the same function is already called in `focus_node` but has no effects on unmapped windows */
+    if (give_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);
 
     num_clients++;
-    ewmh_set_wm_desktop(birth, d);
+    ewmh_set_wm_desktop(n, d);
     ewmh_update_client_list();
 }
 
 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;
+
+    PUTS("adopt orphans");
+
     int len = xcb_query_tree_children_length(qtr);
     xcb_window_t *wins = xcb_query_tree_children(qtr);
     for (int i = 0; i < len; i++) {
         uint32_t idx;
         xcb_window_t win = wins[i];
+        window_hide(win);
         if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
-            desktop_location_t loc;
+            coordinates_t loc;
             if (ewmh_locate_desktop(idx, &loc))
                 manage_window(loc.monitor, loc.desktop, win);
             else
@@ -182,72 +200,52 @@ 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 (n->split_mode == MODE_AUTOMATIC) {
+        xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
+    } else {
+        uint32_t presel_border_color_pxl;
+        get_color(presel_border_color, win, &presel_border_color_pxl);
 
-    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) }
-    };
+        xcb_rectangle_t actual_rectangle = get_rectangle(n->client);
 
-    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 width = actual_rectangle.width;
+        uint16_t height = actual_rectangle.height;
 
-    xcb_rectangle_t *presel_rectangles;
+        uint16_t full_width = width + 2 * border_width;
+        uint16_t full_height = height + 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 border_rectangles[] =
+        {
+            { width, 0, 2 * border_width, height + 2 * border_width },
+            { 0, height, width + 2 * border_width, 2 * border_width }
+        };
 
-    xcb_gcontext_t gc = xcb_generate_id(dpy);
-    xcb_create_gc(dpy, gc, pix, 0, NULL);
+        xcb_rectangle_t *presel_rectangles;
 
-    uint32_t main_border_color_pxl = get_main_border_color(n->client, focused_window, focused_monitor);
+        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);
 
-    /* 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_pixmap_t pixmap = xcb_generate_id(dpy);
+        xcb_create_pixmap(dpy, win_depth, pixmap, win, full_width, full_height);
 
-    /* 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_gcontext_t gc = xcb_generate_id(dpy);
+        xcb_create_gc(dpy, gc, pixmap, 0, NULL);
 
-    /* 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);
-    }
+        xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
+        xcb_poly_fill_rectangle(dpy, pixmap, gc, LENGTH(border_rectangles), border_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));
+        uint16_t fence = (int16_t) (n->split_ratio * ((n->split_dir == DIR_UP || n->split_dir == DIR_DOWN) ? height : width));
         presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
-        switch (split_dir) {
+        switch (n->split_dir) {
             case DIR_UP:
                 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
                 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
@@ -266,15 +264,12 @@ void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
                 break;
         }
         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
-        xcb_poly_fill_rectangle(dpy, pix, gc, 2, presel_rectangles);
+        xcb_poly_fill_rectangle(dpy, pixmap, gc, 2, presel_rectangles);
+        xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pixmap);
         free(presel_rectangles);
+        xcb_free_gc(dpy, gc);
+        xcb_free_pixmap(dpy, pixmap);
     }
-
-    /* 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)
@@ -284,28 +279,7 @@ void window_close(node_t *n)
 
     PRINTF("close window %X\n", n->client->window);
 
-    xcb_atom_t WM_DELETE_WINDOW;
-    xcb_window_t win = n->client->window;
-    xcb_client_message_event_t e;
-
-    xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("WM_DELETE_WINDOW"), "WM_DELETE_WINDOW"), NULL);
-    if (reply) {
-        WM_DELETE_WINDOW = reply->atom;
-        free(reply);
-    } else {
-        warn("close_window %X: could not acquire WM_DELETE_WINDOW atom\n", win);
-        return;
-    }
-
-    e.response_type = XCB_CLIENT_MESSAGE;
-    e.window = win;
-    e.format = 32;
-    e.sequence = 0;
-    e.type = ewmh->WM_PROTOCOLS;
-    e.data.data32[0] = WM_DELETE_WINDOW;
-    e.data.data32[1] = XCB_CURRENT_TIME;
-
-    xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
+    send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
 }
 
 void window_kill(desktop_t *d, node_t *n)
@@ -313,105 +287,165 @@ void window_kill(desktop_t *d, node_t *n)
     if (n == NULL)
         return;
 
-    PRINTF("kill window %X\n", n->client->window);
+    xcb_window_t win = n->client->window;
+    PRINTF("kill window %X\n", win);
 
-    xcb_kill_client(dpy, n->client->window);
+    xcb_kill_client(dpy, win);
     remove_node(d, n);
 }
 
-void toggle_fullscreen(monitor_t *m, client_t *c)
+void set_fullscreen(desktop_t *d, node_t *n, bool value)
 {
-    PRINTF("toggle fullscreen %X\n", c->window);
+    if (n == NULL || n->client->fullscreen == value)
+        return;
 
-    if (c->fullscreen) {
-        c->fullscreen = false;
-        xcb_atom_t values[] = {XCB_NONE};
-        xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
-        if (is_tiled(c))
-            window_lower(c->window);
-    } else {
+    client_t *c = n->client;
+
+    PRINTF("fullscreen %X: %s\n", c->window, BOOLSTR(value));
+
+    if (value) {
         c->fullscreen = true;
         xcb_atom_t values[] = {ewmh->_NET_WM_STATE_FULLSCREEN};
         xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
-        window_raise(c->window);
-        window_border_width(c->window, 0);
-        xcb_rectangle_t r = m->rectangle;
-        window_move_resize(c->window, r.x, r.y, r.width, r.height);
+    } else {
+        c->fullscreen = false;
+        xcb_atom_t values[] = {XCB_NONE};
+        xcb_ewmh_set_wm_state(ewmh, c->window, LENGTH(values), values);
     }
-    update_current();
+
+    stack(d, n);
 }
 
-void toggle_floating(node_t *n)
+void set_floating(desktop_t *d, node_t *n, bool value)
 {
-    if (n == NULL || n->client->transient)
+    if (n == NULL || n->client->transient || n->client->fullscreen || n->client->floating == value)
         return;
 
-    PRINTF("toggle floating %X\n", n->client->window);
+    PRINTF("floating %X: %s\n", n->client->window, BOOLSTR(value));
 
+    n->split_mode = MODE_AUTOMATIC;
     client_t *c = n->client;
-    c->floating = !c->floating;
-    n->vacant = !n->vacant;
+    c->floating = n->vacant = value;
     update_vacant_state(n->parent);
-    if (c->floating)
-        window_raise(c->window);
-    else if (is_tiled(c))
-        window_lower(c->window);
-    update_current();
+
+    if (value) {
+        enable_floating_atom(c->window);
+        unrotate_brother(n);
+    } else {
+        disable_floating_atom(c->window);
+        rotate_brother(n);
+    }
+
+    stack(d, n);
+}
+
+void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+    if (n == NULL || n->client->locked == value)
+        return;
+
+    client_t *c = n->client;
+
+    PRINTF("set locked %X: %s\n", c->window, BOOLSTR(value));
+
+    c->locked = value;
+    window_draw_border(n, d->focus == n, m == mon);
 }
 
-void toggle_locked(client_t *c)
+void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
 {
-    PRINTF("toggle locked %X\n", c->window);
+    if (value && mon->desk->focus == n)
+        return;
+    n->client->urgent = value;
+    window_draw_border(n, d->focus == n, m == mon);
+    put_status();
+}
 
-    c->locked = !c->locked;
+void set_floating_atom(xcb_window_t win, uint32_t value)
+{
+    if (!apply_floating_atom)
+        return;
+    set_atom(win, _BSPWM_FLOATING_WINDOW, value);
 }
 
-void list_windows(char *rsp)
+void enable_floating_atom(xcb_window_t win)
 {
-    char line[MAXLEN];
+    set_floating_atom(win, 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)) {
-                snprintf(line, sizeof(line), "0x%X\n", n->client->window);
-                strncat(rsp, line, REMLEN(rsp));
-            }
+void disable_floating_atom(xcb_window_t win)
+{
+    set_floating_atom(win, 0);
 }
 
-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;
 
+    uint32_t pxl = 0;
+
     if (focused_monitor && focused_window) {
         if (c->locked)
-            return focused_locked_border_color_pxl;
+            get_color(focused_locked_border_color, c->window, &pxl);
         else
-            return focused_border_color_pxl;
+            get_color(focused_border_color, c->window, &pxl);
     } else if (focused_window) {
-        if (c->locked)
-            return active_locked_border_color_pxl;
+        if (c->urgent)
+            get_color(urgent_border_color, c->window, &pxl);
+        else if (c->locked)
+            get_color(active_locked_border_color, c->window, &pxl);
         else
-            return active_border_color_pxl;
+            get_color(active_border_color, c->window, &pxl);
     } else {
         if (c->urgent)
-            return urgent_border_color_pxl;
+            get_color(urgent_border_color, c->window, &pxl);
         else if (c->locked)
-            return normal_locked_border_color_pxl;
+            get_color(normal_locked_border_color, c->window, &pxl);
         else
-            return normal_border_color_pxl;
+            get_color(normal_border_color, c->window, &pxl);
     }
+
+    return pxl;
 }
 
 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)
+{
+    coordinates_t loc;
+    if (locate_window(win, &loc)) {
+        if (loc.node == mon->desk->focus)
+            return;
+        focus_node(loc.monitor, loc.desktop, loc.node);
     }
 }
 
@@ -427,6 +461,12 @@ void window_move(xcb_window_t win, int16_t x, int16_t y)
     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
 }
 
+void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
+{
+    uint32_t values[] = {w, h};
+    xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
+}
+
 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
 {
     uint32_t values[] = {x, y, w, h};
@@ -439,11 +479,53 @@ 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 window_stack(xcb_window_t w1, xcb_window_t w2, uint32_t mode)
 {
-    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);
+    if (w2 == XCB_NONE)
+        return;
+    uint16_t mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
+    uint32_t values[] = {w2, mode};
+    xcb_configure_window(dpy, w1, mask, values);
+}
+
+void window_above(xcb_window_t w1, xcb_window_t w2)
+{
+    window_stack(w1, w2, XCB_STACK_MODE_ABOVE);
+}
+
+void window_below(xcb_window_t w1, xcb_window_t w2)
+{
+    window_stack(w1, w2, XCB_STACK_MODE_BELOW);
+}
+
+void stack(desktop_t *d, node_t *n)
+{
+    if (is_leaf(d->root))
+        return;
+    if (n->client->fullscreen) {
+        window_raise(n->client->window);
+    } else {
+        if (n->client->floating && !auto_raise)
+            return;
+        xcb_window_t latest_tiled = XCB_NONE;
+        xcb_window_t oldest_floating = XCB_NONE;
+        for (node_list_t *a = d->history->head; a != NULL; a = a->next) {
+            if (a->latest && a->node != n) {
+                if (a->node->client->floating == n->client->floating) {
+                    window_above(n->client->window, a->node->client->window);
+                    return;
+                } else if (latest_tiled == XCB_NONE && !a->node->client->floating) {
+                    latest_tiled = a->node->client->window;
+                } else if (a->node->client->floating) {
+                    oldest_floating = a->node->client->window;
+                }
+            }
+        }
+        if (n->client->floating)
+            window_above(n->client->window, latest_tiled);
+        else
+            window_below(n->client->window, oldest_floating);
+    }
 }
 
 void window_lower(xcb_window_t win)
@@ -452,15 +534,16 @@ void window_lower(xcb_window_t win)
     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
 }
 
-void window_set_visibility(xcb_window_t win, bool visible) {
+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)
@@ -475,13 +558,105 @@ 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};
     visible = !visible;
-    for (node_t *n = first_extrema(mon->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);
+    if (!visible)
+        clear_input_focus();
+    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, m->desk->root))
+            window_set_visibility(n->client->window, visible);
+    if (visible)
+        update_input_focus();
+}
+
+void enable_motion_recorder(void)
+{
+    PUTS("enable motion recorder");
+    window_raise(motion_recorder);
+    window_show(motion_recorder);
+}
+
+void disable_motion_recorder(void)
+{
+    PUTS("disable motion recorder");
+    window_hide(motion_recorder);
+}
+
+void update_motion_recorder(void)
+{
+    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
+
+    if (geo != NULL) {
+        window_resize(motion_recorder, geo->width, geo->height);
+        PRINTF("update motion recorder size: %ux%u\n", geo->width, geo->height);
     }
+
+    free(geo);
+}
+
+void update_input_focus(void)
+{
+    set_input_focus(mon->desk->focus);
+}
+
+void set_input_focus(node_t *n)
+{
+    if (n == NULL) {
+        clear_input_focus();
+    } else {
+        if (n->client->icccm_focus)
+            send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_TAKE_FOCUS);
+        xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
+    }
+}
+
+void clear_input_focus(void)
+{
+    xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
+}
+
+void center_pointer(monitor_t *m)
+{
+    int16_t cx = m->rectangle.x + m->rectangle.width / 2;
+    int16_t cy = m->rectangle.y + m->rectangle.height / 2;
+    window_lower(motion_recorder);
+    xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
+    window_raise(motion_recorder);
+}
+
+void get_atom(char *name, xcb_atom_t *atom)
+{
+    xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
+    if (reply != NULL)
+        *atom = reply->atom;
+    else
+        *atom = XCB_NONE;
+    free(reply);
+}
+
+void set_atom(xcb_window_t win, xcb_atom_t atom, uint32_t value)
+{
+    xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, atom, XCB_ATOM_CARDINAL, 32, 1, &value);
+}
+
+bool has_proto(xcb_atom_t atom, xcb_icccm_get_wm_protocols_reply_t *protocols)
+{
+    for (uint32_t i = 0; i < protocols->atoms_len; i++)
+        if (protocols->atoms[i] == atom)
+            return true;
+    return false;
+}
+
+void send_client_message(xcb_window_t win, xcb_atom_t property, xcb_atom_t value)
+{
+    xcb_client_message_event_t e;
+
+    e.response_type = XCB_CLIENT_MESSAGE;
+    e.window = win;
+    e.format = 32;
+    e.sequence = 0;
+    e.type = property;
+    e.data.data32[0] = value;
+    e.data.data32[1] = XCB_CURRENT_TIME;
+
+    xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
 }