]> git.sur5r.net Git - i3/i3/commitdiff
Merge pull request #2061 from Airblader/bug-2049
authorMichael Stapelberg <stapelberg@users.noreply.github.com>
Mon, 16 Nov 2015 08:49:27 +0000 (00:49 -0800)
committerMichael Stapelberg <stapelberg@users.noreply.github.com>
Mon, 16 Nov 2015 08:49:27 +0000 (00:49 -0800)
Only grab scrollwheel buttons if necessary

include/bindings.h
include/xcb.h
src/bindings.c
src/config.c
src/manage.c
src/xcb.c

index e9e5dac922f40e5d80602d6cab756d83efc547ad..da81de248df085ba63509b9bb5eb978e64d8fef3 100644 (file)
@@ -33,6 +33,13 @@ Binding *configure_binding(const char *bindtype, const char *modifiers, const ch
  */
 void grab_all_keys(xcb_connection_t *conn);
 
+/**
+ * Release the button grabs on all managed windows and regrab them,
+ * reevaluating which buttons need to be grabbed.
+ *
+ */
+void regrab_all_buttons(xcb_connection_t *conn);
+
 /**
  * Returns a pointer to the Binding that matches the given xcb event or NULL if
  * no such binding exists.
@@ -95,3 +102,12 @@ CommandResult *run_binding(Binding *bind, Con *con);
  *
  */
 bool load_keymap(void);
+
+/**
+ * Returns true if the current config has any binding to a scroll wheel button
+ * (4 or 5) which is a whole-window binding.
+ * We need this to figure out whether we should grab all buttons or just 1-3
+ * when managing a window. See #2049.
+ *
+ */
+bool bindings_should_grab_scrollwheel_buttons(void);
index 2c87a19c688499aaa977eadbb41efe3e3c210b7f..c1f989bd8c455b58fccfd9a877785cde302aba70 100644 (file)
@@ -160,3 +160,9 @@ void xcb_add_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom
  *
  */
 void xcb_remove_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom);
+
+/**
+ * Grab the specified buttons on a window when managing it.
+ *
+ */
+void xcb_grab_buttons(xcb_connection_t *conn, xcb_window_t window, bool bind_scrollwheel);
index 49f6159022f88253fe119821a53c9ef41022d61d..b9d994ea3ce566db2d96ed53bb35f79bb2b94254 100644 (file)
@@ -146,6 +146,27 @@ void grab_all_keys(xcb_connection_t *conn) {
     }
 }
 
+/*
+ * Release the button grabs on all managed windows and regrab them,
+ * reevaluating which buttons need to be grabbed.
+ *
+ */
+void regrab_all_buttons(xcb_connection_t *conn) {
+    bool grab_scrollwheel = bindings_should_grab_scrollwheel_buttons();
+    xcb_grab_server(conn);
+
+    Con *con;
+    TAILQ_FOREACH(con, &all_cons, all_cons) {
+        if (con->window == NULL)
+            continue;
+
+        xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, con->window->id, XCB_BUTTON_MASK_ANY);
+        xcb_grab_buttons(conn, con->window->id, grab_scrollwheel);
+    }
+
+    xcb_ungrab_server(conn);
+}
+
 /*
  * Returns a pointer to the Binding with the specified modifiers and
  * keycode or NULL if no such binding exists.
@@ -788,3 +809,33 @@ bool load_keymap(void) {
 
     return true;
 }
+
+/*
+ * Returns true if the current config has any binding to a scroll wheel button
+ * (4 or 5) which is a whole-window binding.
+ * We need this to figure out whether we should grab all buttons or just 1-3
+ * when managing a window. See #2049.
+ *
+ */
+bool bindings_should_grab_scrollwheel_buttons(void) {
+    Binding *bind;
+    TAILQ_FOREACH(bind, bindings, bindings) {
+        /* We are only interested in whole window mouse bindings. */
+        if (bind->input_type != B_MOUSE || !bind->whole_window)
+            continue;
+
+        char *endptr;
+        long button = strtol(bind->symbol + (sizeof("button") - 1), &endptr, 10);
+        if (button == LONG_MAX || button == LONG_MIN || button < 0 || *endptr != '\0' || endptr == bind->symbol) {
+            ELOG("Could not parse button number, skipping this binding. Please report this bug in i3.\n");
+            continue;
+        }
+
+        /* If the binding is for either scrollwheel button, we need to grab everything. */
+        if (button == 4 || button == 5) {
+            return true;
+        }
+    }
+
+    return false;
+}
index f146b906a4c9e5a042128d0601f5864a8df481d3..fac4e265cf9e70130398b4c1b1a817e46e8896e3 100644 (file)
@@ -229,6 +229,7 @@ void load_configuration(xcb_connection_t *conn, const char *override_configpath,
     if (reload) {
         translate_keysyms();
         grab_all_keys(conn);
+        regrab_all_buttons(conn);
     }
 
     if (config.font.type == FONT_TYPE_NONE) {
index 0dec28443906c3a209342d6e0a3cb25d8479e8ee..98051ec369ab95780b15dcf6a26ff32ce184f1d8 100644 (file)
@@ -168,12 +168,7 @@ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cooki
     cwindow->id = window;
     cwindow->depth = get_visual_depth(attr->visual);
 
-    /* We need to grab buttons 1-3 for click-to-focus and buttons 1-5
-     * to allow for mouse bindings using --whole-window to work correctly. */
-    xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS,
-                    XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
-                    XCB_BUTTON_INDEX_ANY,
-                    XCB_BUTTON_MASK_ANY /* don’t filter for any modifiers */);
+    xcb_grab_buttons(conn, window, bindings_should_grab_scrollwheel_buttons());
 
     /* update as much information as possible so far (some replies may be NULL) */
     window_update_class(cwindow, xcb_get_property_reply(conn, class_cookie, NULL), true);
index f98115f5c80ce25049e081d9ea303636acc180bd..90d591c746303efa43d3be56cec2c51867b6449d 100644 (file)
--- a/src/xcb.c
+++ b/src/xcb.c
@@ -319,3 +319,25 @@ release_grab:
     FREE(reply);
     xcb_ungrab_server(conn);
 }
+
+/*
+ * Grab the specified buttons on a window when managing it.
+ *
+ */
+void xcb_grab_buttons(xcb_connection_t *conn, xcb_window_t window, bool bind_scrollwheel) {
+    uint8_t buttons[3];
+    int num = 0;
+
+    if (bind_scrollwheel) {
+        buttons[num++] = XCB_BUTTON_INDEX_ANY;
+    } else {
+        buttons[num++] = XCB_BUTTON_INDEX_1;
+        buttons[num++] = XCB_BUTTON_INDEX_2;
+        buttons[num++] = XCB_BUTTON_INDEX_3;
+    }
+
+    for (int i = 0; i < num; i++) {
+        xcb_grab_button(conn, false, window, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_SYNC,
+                        XCB_GRAB_MODE_ASYNC, root, XCB_NONE, buttons[i], XCB_BUTTON_MASK_ANY);
+    }
+}