]> git.lizzy.rs Git - bspwm.git/blobdiff - events.c
Mention the default reference
[bspwm.git] / events.c
index 9b43dfc698a9337ee6610a9638eb7f26de09d59f..b368eee9519425587dac4ec61a00d0f100488c18 100644 (file)
--- a/events.c
+++ b/events.c
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <xcb/xcb.h>
-#include <xcb/xcb_icccm.h>
-#include <xcb/xcb_event.h>
-#include "types.h"
+/* Copyright (c) 2012, Bastien Dejean
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdbool.h>
 #include "bspwm.h"
+#include "ewmh.h"
+#include "monitor.h"
+#include "query.h"
 #include "settings.h"
-#include "helpers.h"
+#include "subscribe.h"
+#include "tree.h"
 #include "window.h"
+#include "pointer.h"
 #include "events.h"
-#include "tree.h"
-#include "rules.h"
-#include "ewmh.h"
 
 void handle_event(xcb_generic_event_t *evt)
 {
-    switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
-        case XCB_MAP_REQUEST:
-            map_request(evt);
-            break;
-        case XCB_DESTROY_NOTIFY:
-            destroy_notify(evt);
-            break;
-        case XCB_UNMAP_NOTIFY:
-            unmap_notify(evt);
-            break;
-        case XCB_CLIENT_MESSAGE:
-            client_message(evt);
-            break;
-        case XCB_CONFIGURE_REQUEST:
-            configure_request(evt);
-            break;
-        case XCB_PROPERTY_NOTIFY:
-            property_notify(evt);
-            break;
-        case XCB_BUTTON_PRESS:
-            button_press(evt);
-            break;
-        case XCB_MOTION_NOTIFY:
-            motion_notify(evt);
-            break;
-        case XCB_BUTTON_RELEASE:
-            button_release();
-            break;
-        default:
-            break;
-    }
+       uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
+       switch (resp_type) {
+               case XCB_MAP_REQUEST:
+                       map_request(evt);
+                       break;
+               case XCB_DESTROY_NOTIFY:
+                       destroy_notify(evt);
+                       break;
+               case XCB_UNMAP_NOTIFY:
+                       unmap_notify(evt);
+                       break;
+               case XCB_CLIENT_MESSAGE:
+                       client_message(evt);
+                       break;
+               case XCB_CONFIGURE_REQUEST:
+                       configure_request(evt);
+                       break;
+               case XCB_PROPERTY_NOTIFY:
+                       property_notify(evt);
+                       break;
+               case XCB_ENTER_NOTIFY:
+                       enter_notify(evt);
+                       break;
+               case XCB_BUTTON_PRESS:
+                       button_press(evt);
+                       break;
+               case XCB_FOCUS_IN:
+                       focus_in(evt);
+                       break;
+               case 0:
+                       process_error(evt);
+                       break;
+               default:
+                       if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
+                               update_monitors();
+                       }
+                       break;
+       }
 }
 
 void map_request(xcb_generic_event_t *evt)
 {
-    xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
-
-    PRINTF("map request %X\n", e->window);
-
-    xcb_window_t win = e->window;
-    window_location_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;
-
-    if (wa != NULL) {
-        override_redirect = wa->override_redirect;
-        free(wa);
-    }
-
-    if (override_redirect || locate_window(win, &loc))
-        return;
-
-    bool floating = false, transient = false, fullscreen = false, takes_focus = true, manage = true;
-
-    handle_rules(win, &floating, &transient, &fullscreen, &takes_focus, &manage);
-
-    if (!manage) {
-        window_show(win);
-        return;
-    }
-
-    desktop_t *desk = mon->desk;
-    client_t *c = make_client(win);
-    update_floating_rectangle(c);
-
-    xcb_icccm_get_wm_class_reply_t reply;
-    if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
-        strncpy(c->class_name, reply.class_name, sizeof(c->class_name));
-        xcb_icccm_get_wm_class_reply_wipe(&reply);
-    }
-
-    if (c->transient)
-        floating = true;
-
-    node_t *birth = make_node();
-    birth->client = c;
-
-    if (floating)
-        split_mode = MODE_MANUAL;
-
-    insert_node(desk, birth);
+       xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
 
-    if (floating)
-        toggle_floating(birth);
-
-    if (desk->focus != NULL && desk->focus->client->fullscreen)
-        toggle_fullscreen(mon, desk->focus->client);
-
-    if (fullscreen)
-        toggle_fullscreen(mon, birth->client);
-
-    c->transient = transient;
-
-    if (takes_focus)
-        focus_node(mon, desk, birth, false);
-
-    fit_monitor(mon, birth->client);
-    arrange(mon, desk);
-    window_show(c->window);
-
-    if (takes_focus)
-        xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
-
-    uint32_t values[] = {CLIENT_EVENT_MASK};
-    xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
-
-    num_clients++;
-    ewmh_set_wm_desktop(birth, desk);
-    ewmh_update_client_list();
+       schedule_window(e->window);
 }
 
 void configure_request(xcb_generic_event_t *evt)
 {
-    xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
-
-    PRINTF("configure request %X\n", e->window);
-
-    window_location_t loc;
-    bool is_managed = locate_window(e->window, &loc);
-
-    if (!is_managed || is_floating(loc.node->client)) {
-        uint16_t mask = 0;
-        uint32_t values[7];
-        unsigned short i = 0;
-
-        if (e->value_mask & XCB_CONFIG_WINDOW_X) {
-            mask |= XCB_CONFIG_WINDOW_X;
-            values[i++] = e->x;
-            if (is_managed)
-                loc.node->client->floating_rectangle.x = e->x;
-        }
-
-        if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
-            mask |= XCB_CONFIG_WINDOW_Y;
-            values[i++] = e->y;
-            if (is_managed)
-                loc.node->client->floating_rectangle.y = e->y;
-        }
-
-        if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
-            mask |= XCB_CONFIG_WINDOW_WIDTH;
-            values[i++] = e->width;
-            if (is_managed)
-                loc.node->client->floating_rectangle.width = e->width;
-        }
-
-        if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
-            mask |= XCB_CONFIG_WINDOW_HEIGHT;
-            values[i++] = e->height;
-            if (is_managed)
-                loc.node->client->floating_rectangle.height = e->height;
-        }
-
-        if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
-            mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
-            values[i++] = e->border_width;
-        }
-
-        if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
-            mask |= XCB_CONFIG_WINDOW_SIBLING;
-            values[i++] = e->sibling;
-        }
-
-        if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
-            mask |= XCB_CONFIG_WINDOW_STACK_MODE;
-            values[i++] = e->stack_mode;
-        }
-
-        xcb_configure_window(dpy, e->window, mask, values);
-        if (is_managed)
-            window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
-    } else {
-        xcb_configure_notify_event_t evt;
-        xcb_rectangle_t rect;
-        unsigned int bw;
-        xcb_window_t win = loc.node->client->window;
-
-        if (is_tiled(loc.node->client)) {
-            rect = loc.node->client->tiled_rectangle;
-            bw = border_width;
-        } else {
-            rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
-            bw = 0;
-        }
-
-        evt.response_type = XCB_CONFIGURE_NOTIFY;
-        evt.event = win;
-        evt.window = win;
-        evt.above_sibling = XCB_NONE;
-        evt.x = rect.x;
-        evt.y = rect.y;
-        evt.width = rect.width;
-        evt.height = rect.height;
-        evt.border_width = bw;
-        evt.override_redirect = false;
-
-        xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
-    }
+       xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
+
+       coordinates_t loc;
+       bool is_managed = locate_window(e->window, &loc);
+       client_t *c = (is_managed ? loc.node->client : NULL);
+       uint16_t width, height;
+
+       if (!is_managed) {
+               uint16_t mask = 0;
+               uint32_t values[7];
+               unsigned short i = 0;
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_X) {
+                       mask |= XCB_CONFIG_WINDOW_X;
+                       values[i++] = e->x;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
+                       mask |= XCB_CONFIG_WINDOW_Y;
+                       values[i++] = e->y;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
+                       mask |= XCB_CONFIG_WINDOW_WIDTH;
+                       values[i++] = e->width;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
+                       mask |= XCB_CONFIG_WINDOW_HEIGHT;
+                       values[i++] = e->height;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
+                       mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
+                       values[i++] = e->border_width;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
+                       mask |= XCB_CONFIG_WINDOW_SIBLING;
+                       values[i++] = e->sibling;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
+                       mask |= XCB_CONFIG_WINDOW_STACK_MODE;
+                       values[i++] = e->stack_mode;
+               }
+
+               xcb_configure_window(dpy, e->window, mask, values);
+
+       } else if (IS_FLOATING(c)) {
+               width = c->floating_rectangle.width;
+               height = c->floating_rectangle.height;
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_X) {
+                       c->floating_rectangle.x = e->x;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
+                       c->floating_rectangle.y = e->y;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
+                       width = e->width;
+               }
+
+               if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
+                       height = e->height;
+               }
+
+               apply_size_hints(c, &width, &height);
+               c->floating_rectangle.width = width;
+               c->floating_rectangle.height = height;
+               xcb_rectangle_t r = c->floating_rectangle;
+
+               if (focus_follows_pointer) {
+                       listen_enter_notify(loc.desktop->root, false);
+               }
+
+               window_move_resize(e->window, r.x, r.y, r.width, r.height);
+
+               if (focus_follows_pointer) {
+                       listen_enter_notify(loc.desktop->root, true);
+               }
+
+               put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc.monitor->id, loc.desktop->id, e->window, r.width, r.height, r.x, r.y);
+
+               monitor_t *m = monitor_from_client(c);
+               if (m != loc.monitor) {
+                       transfer_node(loc.monitor, loc.desktop, loc.node, m, m->desk, m->desk->focus);
+               }
+       } else {
+               if (c->state == STATE_PSEUDO_TILED) {
+                       width = c->floating_rectangle.width;
+                       height = c->floating_rectangle.height;
+                       if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
+                               width = e->width;
+                       }
+                       if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
+                               height = e->height;
+                       }
+                       apply_size_hints(c, &width, &height);
+                       if (width != c->floating_rectangle.width || height != c->floating_rectangle.height) {
+                               c->floating_rectangle.width = width;
+                               c->floating_rectangle.height = height;
+                               arrange(loc.monitor, loc.desktop);
+                       }
+               }
+
+
+               xcb_configure_notify_event_t evt;
+               unsigned int bw = c->border_width;
+
+               xcb_rectangle_t r = IS_FULLSCREEN(c) ? loc.monitor->rectangle : c->tiled_rectangle;
+
+               evt.response_type = XCB_CONFIGURE_NOTIFY;
+               evt.event = e->window;
+               evt.window = e->window;
+               evt.above_sibling = XCB_NONE;
+               evt.x = r.x;
+               evt.y = r.y;
+               evt.width = r.width;
+               evt.height = r.height;
+               evt.border_width = bw;
+               evt.override_redirect = false;
+
+               xcb_send_event(dpy, false, e->window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
+       }
 }
 
 void destroy_notify(xcb_generic_event_t *evt)
 {
-    xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
+       xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
 
-    PRINTF("destroy notify %X\n", e->window);
-
-    window_location_t loc;
-    if (locate_window(e->window, &loc)) {
-        remove_node(loc.desktop, loc.node);
-        arrange(loc.monitor, loc.desktop);
-    }
+       unmanage_window(e->window);
 }
 
 void unmap_notify(xcb_generic_event_t *evt)
 {
-    xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
-
-    PRINTF("unmap notify %X\n", e->window);
+       xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
 
-    window_location_t loc;
-    if (locate_window(e->window, &loc)) {
-        remove_node(loc.desktop, loc.node);
-        arrange(loc.monitor, loc.desktop);
-    }
+       unmanage_window(e->window);
 }
 
 void property_notify(xcb_generic_event_t *evt)
 {
-    xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
-    xcb_icccm_wm_hints_t hints;
-
-    /* PRINTF("property notify %X\n", e->window); */
-
-    if (e->atom != XCB_ATOM_WM_HINTS)
-        return;
-
-    window_location_t loc;
-    if (locate_window(e->window, &loc)) {
-        if (loc.node == loc.desktop->focus)
-            return;
-        if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1) {
-            loc.node->client->urgent = (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY);
-            if (loc.monitor->desk == loc.desktop)
-                arrange(loc.monitor, loc.desktop);
-        }
-    }
+       xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
+
+       if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS) {
+               return;
+       }
+
+       coordinates_t loc;
+       if (!locate_window(e->window, &loc)) {
+                       return;
+       }
+
+       if (e->atom == XCB_ATOM_WM_HINTS) {
+               xcb_icccm_wm_hints_t hints;
+               if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
+                   (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
+                       set_urgent(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
+       } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
+               client_t *c = loc.node->client;
+               if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &c->size_hints, NULL) == 1) {
+                       arrange(loc.monitor, loc.desktop);
+               }
+       }
 }
 
 void client_message(xcb_generic_event_t *evt)
 {
-    xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
-
-    PRINTF("client message %X\n", e->window);
-
-    window_location_t loc;
-
-    if (!locate_window(e->window, &loc))
-        return;
-
-    if (e->type == ewmh->_NET_WM_STATE) {
-        handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
-        handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
-    } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
-        if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node)
-            toggle_fullscreen(loc.monitor, loc.desktop->focus->client);
-        if (loc.monitor->desk != loc.desktop) {
-            arrange(loc.monitor, loc.desktop);
-            select_desktop(loc.desktop);
-        }
-        focus_node(loc.monitor, loc.desktop, loc.node, true);
-    }
+       xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
+
+       if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
+               coordinates_t loc;
+               if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
+                       focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
+               }
+               return;
+       }
+
+       coordinates_t loc;
+       if (!locate_window(e->window, &loc)) {
+               return;
+       }
+
+       if (e->type == ewmh->_NET_WM_STATE) {
+               handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
+               handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
+       } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
+               if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
+                   loc.node == mon->desk->focus) {
+                       return;
+               }
+               focus_node(loc.monitor, loc.desktop, loc.node);
+       } else if (e->type == ewmh->_NET_WM_DESKTOP) {
+               coordinates_t dloc;
+               if (ewmh_locate_desktop(e->data.data32[0], &dloc)) {
+                       transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
+               }
+       } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
+               close_node(loc.node);
+       }
 }
 
-void button_press(xcb_generic_event_t *evt)
+void focus_in(xcb_generic_event_t *evt)
 {
-    xcb_button_press_event_t *e = (xcb_button_press_event_t *) evt;
-    xcb_window_t win = e->child;
-
-    PRINTF("button press %u %X\n", e->detail, win);
-
-    window_location_t loc;
-    if (locate_window(win, &loc)) {
-        client_t *c = loc.node->client;
-        switch (e->detail)  {
-            case XCB_BUTTON_INDEX_2:
-                focus_node(loc.monitor, loc.desktop, loc.node, true);
-                break;
-            case XCB_BUTTON_INDEX_1:
-            case XCB_BUTTON_INDEX_3:
-                if (!is_floating(loc.node->client))
-                    return;
-                frozen_pointer->desktop = loc.desktop;
-                frozen_pointer->node = loc.node;
-                frozen_pointer->rectangle = c->floating_rectangle;
-                frozen_pointer->position = (xcb_point_t) {e->root_x, e->root_y};
-                frozen_pointer->button = e->detail;
-                if (e->detail == XCB_BUTTON_INDEX_3) {
-                    int16_t mid_x, mid_y;
-                    mid_x = c->floating_rectangle.x + (c->floating_rectangle.width / 2);
-                    mid_y = c->floating_rectangle.y + (c->floating_rectangle.height / 2);
-                    if (e->root_x > mid_x) {
-                        if (e->root_y > mid_y)
-                            frozen_pointer->corner = BOTTOM_RIGHT;
-                        else
-                            frozen_pointer->corner = TOP_RIGHT;
-                    } else {
-                        if (e->root_y > mid_y)
-                            frozen_pointer->corner = BOTTOM_LEFT;
-                        else
-                            frozen_pointer->corner = TOP_LEFT;
-                    }
-                }
-                xcb_grab_pointer(dpy, false, screen->root, XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_CURRENT_TIME);
-                break;
-        }
-    }
+       xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
+
+       if (e->mode == XCB_NOTIFY_MODE_GRAB || e->mode == XCB_NOTIFY_MODE_UNGRAB
+           || e->detail == XCB_NOTIFY_DETAIL_POINTER || e->detail == XCB_NOTIFY_DETAIL_POINTER_ROOT
+           || e->detail == XCB_NOTIFY_DETAIL_NONE) {
+               return;
+       }
+
+       if (mon->desk->focus != NULL && e->event == mon->desk->focus->id) {
+               return;
+       }
+
+       coordinates_t loc;
+       if (locate_window(e->event, &loc)) {
+               // prevent input focus stealing
+               update_input_focus();
+       }
 }
 
-void motion_notify(xcb_generic_event_t *evt)
+void button_press(xcb_generic_event_t *evt)
 {
-    xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
-
-    int16_t delta_x, delta_y, x, y, w, h;
-    uint16_t width, height;
-
-    monitor_t *m = frozen_pointer->monitor;
-    desktop_t *d = frozen_pointer->desktop;
-    node_t *n = frozen_pointer->node;
-    client_t *c = n->client;
-    xcb_rectangle_t rect = frozen_pointer->rectangle;
-    xcb_window_t win = c->window;
-
-    /* PRINTF("motion notify %X\n", win); */
-
-    delta_x = e->root_x - frozen_pointer->position.x;
-    delta_y = e->root_y - frozen_pointer->position.y;
-
-    switch (frozen_pointer->button) {
-        case XCB_BUTTON_INDEX_1:
-            x = rect.x + delta_x;
-            y = rect.y + delta_y;
-            window_move(win, x, y);
-            break;
-        case XCB_BUTTON_INDEX_3:
-            switch (frozen_pointer->corner) {
-                case TOP_LEFT:
-                    x = rect.x + delta_x;
-                    y = rect.y + delta_y;
-                    w = rect.width - delta_x;
-                    h = rect.height - delta_y;
-                    break;
-                case TOP_RIGHT:
-                    x = rect.x;
-                    y = rect.y + delta_y;
-                    w = rect.width + delta_x;
-                    h = rect.height - delta_y;
-                    break;
-                case BOTTOM_LEFT:
-                    x = rect.x + delta_x;
-                    y = rect.y;
-                    w = rect.width - delta_x;
-                    h = rect.height + delta_y;
-                    break;
-                case BOTTOM_RIGHT:
-                default:
-                    x = rect.x;
-                    y = rect.y;
-                    w = rect.width + delta_x;
-                    h = rect.height + delta_y;
-                    break;
-            }
-            width = MAX(1, w);
-            height = MAX(1, h);
-            window_move_resize(win, x, y, width, height);
-            c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
-            window_draw_border(n, d->focus == n, mon == m);
-    }
+       xcb_button_press_event_t *e = (xcb_button_press_event_t *) evt;
+       bool replay = false;
+       switch (e->detail) {
+               case XCB_BUTTON_INDEX_1:
+                       if (click_to_focus && cleaned_mask(e->state) == XCB_NONE) {
+                               replay = true;
+                               bool pff = pointer_follows_focus;
+                               bool pfm = pointer_follows_monitor;
+                               pointer_follows_focus = false;
+                               pointer_follows_monitor = false;
+                               grab_pointer(ACTION_FOCUS);
+                               pointer_follows_focus = pff;
+                               pointer_follows_monitor = pfm;
+                       } else {
+                               grab_pointer(pointer_actions[0]);
+                       }
+                       break;
+               case XCB_BUTTON_INDEX_2:
+                       grab_pointer(pointer_actions[1]);
+                       break;
+               case XCB_BUTTON_INDEX_3:
+                       grab_pointer(pointer_actions[2]);
+                       break;
+       }
+       xcb_allow_events(dpy, replay ? XCB_ALLOW_REPLAY_POINTER : XCB_ALLOW_SYNC_POINTER, e->time);
+       xcb_flush(dpy);
 }
 
-void button_release(void)
+void enter_notify(xcb_generic_event_t *evt)
 {
-    PUTS("button release");
-
-    xcb_ungrab_pointer(dpy, XCB_CURRENT_TIME);
-    update_floating_rectangle(frozen_pointer->node->client);
+       xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
+       xcb_window_t win = e->event;
+
+       if (e->mode != XCB_NOTIFY_MODE_NORMAL || e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
+               return;
+       }
+
+       xcb_point_t pt = {e->root_x, e->root_y};
+       monitor_t *m = monitor_from_point(pt);
+
+       if (m == NULL) {
+               return;
+       }
+
+       bool pff = pointer_follows_focus;
+       bool pfm = pointer_follows_monitor;
+       pointer_follows_focus = false;
+       pointer_follows_monitor = false;
+       auto_raise = false;
+
+       desktop_t *d = m->desk;
+       node_t *n = NULL;
+
+       for (n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
+               if (n->id == win || (n->presel != NULL && n->presel->feedback == win)) {
+                       break;
+               }
+       }
+
+       if (n != mon->desk->focus) {
+               if (n != NULL) {
+                       focus_node(m, d, n);
+               } else if (m != mon) {
+                       focus_node(m, m->desk, m->desk->focus);
+               }
+       }
+
+       pointer_follows_focus = pff;
+       pointer_follows_monitor = pfm;
+       auto_raise = true;
 }
 
 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
 {
-    if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
-        bool fs = n->client->fullscreen;
-        if (action == XCB_EWMH_WM_STATE_TOGGLE
-                || (fs && action == XCB_EWMH_WM_STATE_REMOVE)
-                || (!fs && action == XCB_EWMH_WM_STATE_ADD)) {
-            toggle_fullscreen(m, n->client);
-            arrange(m, d);
-        }
-    }
+       if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
+               if (action == XCB_EWMH_WM_STATE_ADD) {
+                       set_state(m, d, n, STATE_FULLSCREEN);
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
+                       if (n->client->state == STATE_FULLSCREEN) {
+                               set_state(m, d, n, n->client->last_state);
+                       }
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
+                       set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
+               }
+               arrange(m, d);
+       } else if (state == ewmh->_NET_WM_STATE_BELOW) {
+               if (action == XCB_EWMH_WM_STATE_ADD) {
+                       set_layer(m, d, n, LAYER_BELOW);
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
+                       if (n->client->layer == LAYER_BELOW) {
+                               set_layer(m, d, n, n->client->last_layer);
+                       }
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
+                       set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
+               }
+       } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
+               if (action == XCB_EWMH_WM_STATE_ADD) {
+                       set_layer(m, d, n, LAYER_ABOVE);
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
+                       if (n->client->layer == LAYER_ABOVE) {
+                               set_layer(m, d, n, n->client->last_layer);
+                       }
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
+                       set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
+               }
+       } else if (state == ewmh->_NET_WM_STATE_HIDDEN) {
+               if (action == XCB_EWMH_WM_STATE_ADD) {
+                       set_hidden(m, d, n, true);
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
+                       set_hidden(m, d, n, false);
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
+                       set_hidden(m, d, n, !n->hidden);
+               }
+       } else if (state == ewmh->_NET_WM_STATE_STICKY) {
+               if (action == XCB_EWMH_WM_STATE_ADD) {
+                       set_sticky(m, d, n, true);
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
+                       set_sticky(m, d, n, false);
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
+                       set_sticky(m, d, n, !n->sticky);
+               }
+       } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
+               if (action == XCB_EWMH_WM_STATE_ADD) {
+                       set_urgent(m, d, n, true);
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
+                       set_urgent(m, d, n, false);
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
+                       set_urgent(m, d, n, !n->client->urgent);
+               }
+#define HANDLE_WM_STATE(s)  \
+       } else if (state == ewmh->_NET_WM_STATE_##s) { \
+               if (action == XCB_EWMH_WM_STATE_ADD) { \
+                       n->client->wm_flags |= WM_FLAG_##s; \
+               } else if (action == XCB_EWMH_WM_STATE_REMOVE) { \
+                       n->client->wm_flags &= ~WM_FLAG_##s; \
+               } else if (action == XCB_EWMH_WM_STATE_TOGGLE) { \
+                       n->client->wm_flags ^= WM_FLAG_##s; \
+               } \
+               ewmh_wm_state_update(n);
+       HANDLE_WM_STATE(MODAL)
+       HANDLE_WM_STATE(MAXIMIZED_VERT)
+       HANDLE_WM_STATE(MAXIMIZED_HORZ)
+       HANDLE_WM_STATE(SHADED)
+       HANDLE_WM_STATE(SKIP_TASKBAR)
+       HANDLE_WM_STATE(SKIP_PAGER)
+       }
+#undef HANDLE_WM_STATE
+}
+
+void process_error(xcb_generic_event_t *evt)
+{
+       xcb_request_error_t *e = (xcb_request_error_t *) evt;
+       warn("Failed request: %s, %s: 0x%08X.\n", xcb_event_get_request_label(e->major_opcode), xcb_event_get_error_label(e->error_code), e->bad_value);
 }