]> git.sur5r.net Git - i3/i3/blobdiff - src/bindings.c
Merge pull request #2061 from Airblader/bug-2049
[i3/i3] / src / bindings.c
index eac63a59e38039b3865add28722461110c23b21c..b9d994ea3ce566db2d96ed53bb35f79bb2b94254 100644 (file)
@@ -27,7 +27,7 @@ const char *DEFAULT_BINDING_MODE = "default";
  * the list of modes.
  *
  */
-static struct Mode *mode_from_name(const char *name) {
+static struct Mode *mode_from_name(const char *name, bool pango_markup) {
     struct Mode *mode;
 
     /* Try to find the mode in the list of modes and return it */
@@ -39,6 +39,7 @@ static struct Mode *mode_from_name(const char *name) {
     /* If the mode was not found, create a new one */
     mode = scalloc(1, sizeof(struct Mode));
     mode->name = sstrdup(name);
+    mode->pango_markup = pango_markup;
     mode->bindings = scalloc(1, sizeof(struct bindings_head));
     TAILQ_INIT(mode->bindings);
     SLIST_INSERT_HEAD(&modes, mode, modes);
@@ -54,7 +55,7 @@ static struct Mode *mode_from_name(const char *name) {
  */
 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
                            const char *release, const char *border, const char *whole_window,
-                           const char *command, const char *modename) {
+                           const char *command, const char *modename, bool pango_markup) {
     Binding *new_binding = scalloc(1, sizeof(Binding));
     DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
@@ -91,7 +92,7 @@ Binding *configure_binding(const char *bindtype, const char *modifiers, const ch
     if (group_bits_set > 1)
         ELOG("Keybinding has more than one Group specified, but your X server is always in precisely one group. The keybinding can never trigger.\n");
 
-    struct Mode *mode = mode_from_name(modename);
+    struct Mode *mode = mode_from_name(modename, pango_markup);
     TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
 
     return new_binding;
@@ -145,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.
@@ -164,16 +186,34 @@ static Binding *get_binding(i3_event_state_mask_t state_filtered, bool is_releas
         }
     }
 
+    const uint32_t xkb_group_state = (state_filtered & 0xFFFF0000);
+    const uint32_t modifiers_state = (state_filtered & 0x0000FFFF);
     TAILQ_FOREACH(bind, bindings, bindings) {
-        DLOG("binding with event_state_mask 0x%x, state_filtered 0x%x, match: %s\n",
-             bind->event_state_mask, state_filtered,
-             ((state_filtered & bind->event_state_mask) == bind->event_state_mask) ? "yes" : "no");
+        const uint32_t xkb_group_mask = (bind->event_state_mask & 0xFFFF0000);
+        /* modifiers_mask is a special case: a value of 0 does not mean “match all”,
+         * but rather “match exactly when no modifiers are present”. */
+        const uint32_t modifiers_mask = (bind->event_state_mask & 0x0000FFFF);
+        const bool groups_match = ((xkb_group_state & xkb_group_mask) == xkb_group_mask);
+        bool mods_match;
+        if (modifiers_mask == 0) {
+            /* Verify no modifiers are pressed. A bitwise AND would lead to
+             * false positives, see issue #2002. */
+            mods_match = (modifiers_state == 0);
+        } else {
+            mods_match = ((modifiers_state & modifiers_mask) == modifiers_mask);
+        }
+        const bool state_matches = (groups_match && mods_match);
+
+        DLOG("binding groups_match = %s, mods_match = %s, state_matches = %s\n",
+             (groups_match ? "yes" : "no"),
+             (mods_match ? "yes" : "no"),
+             (state_matches ? "yes" : "no"));
         /* First compare the state_filtered (unless this is a
          * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
          * event) */
         if (bind->input_type != input_type)
             continue;
-        if ((state_filtered & bind->event_state_mask) != bind->event_state_mask &&
+        if (!state_matches &&
             (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
              !is_release))
             continue;
@@ -412,6 +452,7 @@ void translate_keysyms(void) {
     }
 
     xkb_state_unref(dummy_state);
+    xkb_state_unref(dummy_state_no_shift);
 
     if (has_errors) {
         start_config_error_nagbar(current_configpath, true);
@@ -437,7 +478,8 @@ void switch_mode(const char *new_mode) {
         grab_all_keys(conn);
 
         char *event_msg;
-        sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name);
+        sasprintf(&event_msg, "{\"change\":\"%s\", \"pango_markup\":%s}",
+                  mode->name, (mode->pango_markup ? "true" : "false"));
 
         ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
         FREE(event_msg);
@@ -691,19 +733,19 @@ static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names) {
         remaining -= len;
         switch (i) {
             case 0:
-                asprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
+                sasprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
                 break;
             case 1:
-                asprintf((char **)&(xkb_names->model), "%.*s", len, walk);
+                sasprintf((char **)&(xkb_names->model), "%.*s", len, walk);
                 break;
             case 2:
-                asprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
+                sasprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
                 break;
             case 3:
-                asprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
+                sasprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
                 break;
             case 4:
-                asprintf((char **)&(xkb_names->options), "%.*s", len, walk);
+                sasprintf((char **)&(xkb_names->options), "%.*s", len, walk);
                 break;
         }
         DLOG("component %d of _XKB_RULES_NAMES is \"%.*s\"\n", i, len, walk);
@@ -767,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;
+}