]> git.lizzy.rs Git - bspwm.git/blobdiff - events.c
New setting: `honor_ewmh_focus`
[bspwm.git] / events.c
index fc60e06ce959ac4d382542724ed3cec4aadc4c68..99834bbf45a7330be0d20a4787a43615d6e31b63 100644 (file)
--- a/events.c
+++ b/events.c
@@ -1,18 +1,11 @@
-#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <xcb/xcb.h>
-#include <xcb/randr.h>
-#include <xcb/xcb_event.h>
-#include <xcb/xcb_icccm.h>
-#include "types.h"
 #include "bspwm.h"
 #include "settings.h"
-#include "helpers.h"
-#include "window.h"
 #include "events.h"
+#include "monitor.h"
+#include "window.h"
+#include "query.h"
 #include "tree.h"
-#include "rules.h"
 #include "ewmh.h"
 
 void handle_event(xcb_generic_event_t *evt)
@@ -41,7 +34,10 @@ void handle_event(xcb_generic_event_t *evt)
             enter_notify(evt);
             break;
         case XCB_MOTION_NOTIFY:
-            motion_notify();
+            motion_notify(evt);
+            break;
+        case XCB_FOCUS_IN:
+            focus_in(evt);
             break;
         default:
             if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
@@ -65,7 +61,7 @@ void configure_request(xcb_generic_event_t *evt)
 
     PRINTF("configure request %X\n", e->window);
 
-    window_location_t loc;
+    coordinates_t loc;
     bool is_managed = locate_window(e->window, &loc);
 
     if (!is_managed || is_floating(loc.node->client)) {
@@ -154,7 +150,7 @@ void destroy_notify(xcb_generic_event_t *evt)
 
     PRINTF("destroy notify %X\n", e->window);
 
-    window_location_t loc;
+    coordinates_t loc;
     if (locate_window(e->window, &loc)) {
         remove_node(loc.desktop, loc.node);
         arrange(loc.monitor, loc.desktop);
@@ -167,7 +163,7 @@ void unmap_notify(xcb_generic_event_t *evt)
 
     PRINTF("unmap notify %X\n", e->window);
 
-    window_location_t loc;
+    coordinates_t loc;
     if (locate_window(e->window, &loc)) {
         remove_node(loc.desktop, loc.node);
         arrange(loc.monitor, loc.desktop);
@@ -184,7 +180,7 @@ void property_notify(xcb_generic_event_t *evt)
     if (e->atom != XCB_ATOM_WM_HINTS)
         return;
 
-    window_location_t loc;
+    coordinates_t loc;
     if (locate_window(e->window, &loc)
             && xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1)
         set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
@@ -197,13 +193,13 @@ void client_message(xcb_generic_event_t *evt)
     PRINTF("client message %X %u\n", e->window, e->type);
 
     if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
-        desktop_location_t loc;
+        coordinates_t loc;
         if (ewmh_locate_desktop(e->data.data32[0], &loc))
             focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
         return;
     }
 
-    window_location_t loc;
+    coordinates_t loc;
     if (!locate_window(e->window, &loc))
         return;
 
@@ -211,14 +207,39 @@ void client_message(xcb_generic_event_t *evt)
         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 (!honor_ewmh_focus || loc.node == mon->desk->focus)
+            return;
         if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
-            toggle_fullscreen(loc.desktop, loc.desktop->focus);
+            set_fullscreen(loc.desktop, loc.desktop->focus, false);
             arrange(loc.monitor, loc.desktop);
         }
         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, dloc.monitor, dloc.desktop, loc.node);
     }
 }
 
+void focus_in(xcb_generic_event_t *evt)
+{
+    xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
+
+    /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
+
+    if (e->mode == XCB_NOTIFY_MODE_GRAB
+            || e->mode == XCB_NOTIFY_MODE_UNGRAB)
+        return;
+    /* prevent focus stealing */
+    if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
+                e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
+                e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
+                e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
+            (mon->desk->focus == NULL
+             || mon->desk->focus->client->window != e->event))
+        update_input_focus();
+}
+
 void enter_notify(xcb_generic_event_t *evt)
 {
     xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
@@ -233,32 +254,52 @@ void enter_notify(xcb_generic_event_t *evt)
     enable_motion_recorder();
 }
 
-void motion_notify(void)
+void motion_notify(xcb_generic_event_t *evt)
 {
     PUTS("motion notify");
 
+    xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
+
+    int dtime = e->time - last_motion_time;
+    if (dtime > 1000) {
+        last_motion_time = e->time;
+        last_motion_x = e->event_x;
+        last_motion_y = e->event_y;
+        return;
+    }
+
+    int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
+    if (mdist < 10)
+        return;
+
     disable_motion_recorder();
 
     xcb_window_t win = XCB_NONE;
+    xcb_point_t pt = {e->root_x, e->root_y};
     query_pointer(&win, NULL);
-    if (win != XCB_NONE) {
-        bool backup = pointer_follows_monitor;
-        pointer_follows_monitor = false;
-        window_focus(win);
-        pointer_follows_monitor = backup;
+
+    bool backup = pointer_follows_monitor;
+    auto_raise = false;
+    pointer_follows_monitor = false;
+    if (!window_focus(win)) {
+        monitor_t *m = monitor_from_point(pt);
+        if (m != NULL && m != mon)
+            focus_node(m, m->desk, m->desk->focus);
     }
+    pointer_follows_monitor = backup;
+    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(d, n);
-            arrange(m, d);
-        }
+        if (action == XCB_EWMH_WM_STATE_ADD)
+            set_fullscreen(d, n, true);
+        else if (action == XCB_EWMH_WM_STATE_REMOVE)
+            set_fullscreen(d, n, false);
+        else if (action == XCB_EWMH_WM_STATE_TOGGLE)
+            set_fullscreen(d, n, !n->client->fullscreen);
+        arrange(m, d);
     } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
         if (action == XCB_EWMH_WM_STATE_ADD)
             set_urgency(m, d, n, true);
@@ -278,10 +319,7 @@ void grab_pointer(pointer_action_t pac)
 
     query_pointer(&win, &pos);
 
-    if (win == XCB_NONE)
-        return;
-
-    window_location_t loc;
+    coordinates_t loc;
     if (locate_window(win, &loc)) {
         client_t *c = NULL;
         frozen_pointer->position = pos;
@@ -302,6 +340,8 @@ void grab_pointer(pointer_action_t pac)
                     pointer_follows_monitor = false;
                     focus_node(loc.monitor, loc.desktop, loc.node);
                     pointer_follows_monitor = backup;
+                } else if (focus_follows_pointer && is_floating(loc.node->client)) {
+                    stack(loc.desktop, loc.node);
                 }
                 break;
             case ACTION_MOVE:
@@ -397,6 +437,11 @@ void grab_pointer(pointer_action_t pac)
                 break;
         }
     } else {
+        if (pac == ACTION_FOCUS) {
+            monitor_t *m = monitor_from_point(pos);
+            if (m != NULL && m != mon)
+                focus_node(m, m->desk, m->desk->focus);
+        }
         frozen_pointer->action = ACTION_NONE;
     }
 }
@@ -406,7 +451,7 @@ void track_pointer(int root_x, int root_y)
     if (frozen_pointer->action == ACTION_NONE)
         return;
 
-    int16_t delta_x, delta_y, x, y, w, h;
+    int16_t delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
     uint16_t width, height;
 
     pointer_action_t pac = frozen_pointer->action;
@@ -419,9 +464,6 @@ void track_pointer(int root_x, int root_y)
     node_t *vertical_fence = frozen_pointer->vertical_fence;
     node_t *horizontal_fence = frozen_pointer->horizontal_fence;
 
-    x = y = 0;
-    w = h = 1;
-
     delta_x = root_x - frozen_pointer->position.x;
     delta_y = root_y - frozen_pointer->position.y;
 
@@ -432,7 +474,7 @@ void track_pointer(int root_x, int root_y)
                 query_pointer(&pwin, NULL);
                 if (pwin == win)
                     return;
-                window_location_t loc;
+                coordinates_t loc;
                 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
                 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
                     swap_nodes(n, loc.node);
@@ -458,6 +500,15 @@ void track_pointer(int root_x, int root_y)
                 x = rect.x + delta_x;
                 y = rect.y + delta_y;
                 window_move(win, x, y);
+                c->floating_rectangle.x = x;
+                c->floating_rectangle.y = y;
+                xcb_point_t pt = (xcb_point_t) {root_x, root_y};
+                monitor_t *pmon = monitor_from_point(pt);
+                if (pmon == NULL || pmon == m)
+                    return;
+                transfer_node(m, d, pmon, pmon->desk, n);
+                frozen_pointer->monitor = pmon;
+                frozen_pointer->desktop = pmon->desk;
             }
             break;
         case ACTION_RESIZE_SIDE:
@@ -549,16 +600,3 @@ void track_pointer(int root_x, int root_y)
             break;
     }
 }
-
-void ungrab_pointer(void)
-{
-    PUTS("ungrab pointer");
-
-    if (frozen_pointer->action == ACTION_NONE || frozen_pointer->is_tiled)
-        return;
-
-    update_floating_rectangle(frozen_pointer->client);
-    monitor_t *m = underlying_monitor(frozen_pointer->client);
-    if (m != NULL && m != frozen_pointer->monitor)
-        transfer_node(frozen_pointer->monitor, frozen_pointer->desktop, m, m->desk, frozen_pointer->node);
-}