* 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 */
/* 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);
*/
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);
new_binding->command = sstrdup(command);
new_binding->event_state_mask = event_state_from_str(modifiers);
int group_bits_set = 0;
- if (new_binding->event_state_mask & I3_XKB_GROUP_MASK_1)
+ if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_1)
group_bits_set++;
- if (new_binding->event_state_mask & I3_XKB_GROUP_MASK_2)
+ if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
group_bits_set++;
- if (new_binding->event_state_mask & I3_XKB_GROUP_MASK_3)
+ if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
group_bits_set++;
- if (new_binding->event_state_mask & I3_XKB_GROUP_MASK_4)
+ if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
group_bits_set++;
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;
}
}
+/*
+ * 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);
+ }
+
+ /* Also grab the root window to allow bindings to work on there as well. */
+ xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, root, XCB_BUTTON_MASK_ANY);
+ xcb_grab_buttons(conn, root, 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.
}
}
+ 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;
return;
}
+ bool has_errors = false;
Binding *bind;
TAILQ_FOREACH(bind, bindings, bindings) {
if (bind->input_type == B_MOUSE) {
sasprintf(&tmp, "%s %d", keycodes, bind->translated_to[n]);
free(keycodes);
keycodes = tmp;
+
+ /* check for duplicate bindings */
+ Binding *check;
+ TAILQ_FOREACH(check, bindings, bindings) {
+ if (check == bind)
+ continue;
+ if (check->symbol != NULL)
+ continue;
+ if (check->keycode != bind->translated_to[n] ||
+ check->event_state_mask != bind->event_state_mask ||
+ check->release != bind->release)
+ continue;
+ has_errors = true;
+ ELOG("Duplicate keybinding in config file:\n keysym = %s, keycode = %d, state_mask = 0x%x\n", bind->symbol, check->keycode, bind->event_state_mask);
+ }
}
DLOG("state=0x%x, cfg=\"%s\", sym=0x%x → keycodes%s (%d)\n",
bind->event_state_mask, bind->symbol, keysym, keycodes, bind->number_keycodes);
}
xkb_state_unref(dummy_state);
+ xkb_state_unref(dummy_state_no_shift);
+
+ if (has_errors) {
+ start_config_error_nagbar(current_configpath, true);
+ }
}
/*
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);
ELOG("ERROR: Mode not found\n");
}
+static int reorder_binding_cmp(const void *a, const void *b) {
+ Binding *first = *((Binding **)a);
+ Binding *second = *((Binding **)b);
+ if (first->event_state_mask < second->event_state_mask) {
+ return 1;
+ } else if (first->event_state_mask == second->event_state_mask) {
+ return 0;
+ } else {
+ return -1;
+ }
+}
+
static void reorder_bindings_of_mode(struct Mode *mode) {
+ /* Copy the bindings into an array, so that we can use qsort(3). */
+ int n = 0;
+ Binding *current;
+ TAILQ_FOREACH(current, mode->bindings, bindings) {
+ n++;
+ }
+ Binding **tmp = scalloc(n, sizeof(Binding *));
+ n = 0;
+ TAILQ_FOREACH(current, mode->bindings, bindings) {
+ tmp[n++] = current;
+ }
+
+ qsort(tmp, n, sizeof(Binding *), reorder_binding_cmp);
+
struct bindings_head *reordered = scalloc(1, sizeof(struct bindings_head));
TAILQ_INIT(reordered);
- /* 20 bits are in use in an i3_event_state_mask_t. */
- for (int n = 19; n >= 0; n--) {
- Binding *current;
- for (current = TAILQ_FIRST(mode->bindings); current != TAILQ_END(mode->bindings);) {
- /* Advance |current| so that we can use TAILQ_REMOVE safely. */
- Binding *bind = current;
- current = TAILQ_NEXT(current, bindings);
- if ((bind->event_state_mask & (1 << n)) == 0)
- continue;
- TAILQ_REMOVE(mode->bindings, bind, bindings);
- TAILQ_INSERT_TAIL(reordered, bind, bindings);
- }
- }
- /* Move over the bindings with event_state_mask == 0x0. */
- Binding *current;
- for (current = TAILQ_FIRST(mode->bindings); current != TAILQ_END(mode->bindings);) {
- /* Advance |current| so that we can use TAILQ_REMOVE safely. */
- Binding *bind = current;
- current = TAILQ_NEXT(current, bindings);
- assert(bind->event_state_mask == 0);
- TAILQ_REMOVE(mode->bindings, bind, bindings);
- TAILQ_INSERT_TAIL(reordered, bind, bindings);
+ for (int i = 0; i < n; i++) {
+ current = tmp[i];
+ TAILQ_REMOVE(mode->bindings, current, bindings);
+ TAILQ_INSERT_TAIL(reordered, current, bindings);
}
+ free(tmp);
assert(TAILQ_EMPTY(mode->bindings));
/* Free the old bindings_head, which is now empty. */
free(mode->bindings);
/* Check if one is using keysym while the other is using bindsym.
* If so, skip. */
- /* XXX: It should be checked at a later place (when translating the
- * keysym to keycodes) if there are any duplicates */
if ((bind->symbol == NULL && current->symbol != NULL) ||
(bind->symbol != NULL && current->symbol == NULL))
continue;
/*
* Runs the given binding and handles parse errors. If con is passed, it will
* execute the command binding with that container selected by criteria.
- * Returns a CommandResult for running the binding's command. Caller should
- * render tree if needs_tree_render is true. Free with command_result_free().
+ * Returns a CommandResult for running the binding's command. Free with
+ * command_result_free().
*
*/
CommandResult *run_binding(Binding *bind, Con *con) {
return result;
}
+static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names) {
+ xcb_intern_atom_reply_t *atom_reply;
+ size_t content_max_words = 256;
+
+ xcb_window_t root = root_screen->root;
+
+ atom_reply = xcb_intern_atom_reply(
+ conn, xcb_intern_atom(conn, 0, strlen("_XKB_RULES_NAMES"), "_XKB_RULES_NAMES"), NULL);
+ if (atom_reply == NULL)
+ return -1;
+
+ xcb_get_property_cookie_t prop_cookie;
+ xcb_get_property_reply_t *prop_reply;
+ prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
+ XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
+ prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
+ if (prop_reply == NULL) {
+ free(atom_reply);
+ return -1;
+ }
+ if (xcb_get_property_value_length(prop_reply) > 0 && prop_reply->bytes_after > 0) {
+ /* We received an incomplete value. Ask again but with a properly
+ * adjusted size. */
+ content_max_words += ceil(prop_reply->bytes_after / 4.0);
+ /* Repeat the request, with adjusted size */
+ free(prop_reply);
+ prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
+ XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
+ prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
+ if (prop_reply == NULL) {
+ free(atom_reply);
+ return -1;
+ }
+ }
+ if (xcb_get_property_value_length(prop_reply) == 0) {
+ free(atom_reply);
+ free(prop_reply);
+ return -1;
+ }
+
+ const char *walk = (const char *)xcb_get_property_value(prop_reply);
+ int remaining = xcb_get_property_value_length(prop_reply);
+ for (int i = 0; i < 5 && remaining > 0; i++) {
+ const int len = strnlen(walk, remaining);
+ remaining -= len;
+ switch (i) {
+ case 0:
+ sasprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
+ break;
+ case 1:
+ sasprintf((char **)&(xkb_names->model), "%.*s", len, walk);
+ break;
+ case 2:
+ sasprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
+ break;
+ case 3:
+ sasprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
+ break;
+ case 4:
+ sasprintf((char **)&(xkb_names->options), "%.*s", len, walk);
+ break;
+ }
+ DLOG("component %d of _XKB_RULES_NAMES is \"%.*s\"\n", i, len, walk);
+ walk += (len + 1);
+ }
+
+ free(atom_reply);
+ free(prop_reply);
+ return 0;
+}
+
/*
* Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
*
}
}
- struct xkb_keymap *new_keymap;
- const int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
- DLOG("device_id = %d\n", device_id);
- if ((new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
- ELOG("xkb_x11_keymap_new_from_device failed\n");
- return false;
+ struct xkb_keymap *new_keymap = NULL;
+ int32_t device_id;
+ if (xkb_supported && (device_id = xkb_x11_get_core_keyboard_device_id(conn)) > -1) {
+ if ((new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
+ ELOG("xkb_x11_keymap_new_from_device failed\n");
+ return false;
+ }
+ } else {
+ /* Likely there is no XKB support on this server, possibly because it
+ * is a VNC server. */
+ LOG("No XKB / core keyboard device? Assembling keymap from local RMLVO.\n");
+ struct xkb_rule_names names = {
+ .rules = NULL,
+ .model = NULL,
+ .layout = NULL,
+ .variant = NULL,
+ .options = NULL};
+ if (fill_rmlvo_from_root(&names) == -1) {
+ ELOG("Could not get _XKB_RULES_NAMES atom from root window, falling back to defaults.\n");
+ if ((new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0)) == NULL) {
+ ELOG("xkb_keymap_new_from_names(NULL) failed\n");
+ return false;
+ }
+ }
+ new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0);
+ free((char *)names.rules);
+ free((char *)names.model);
+ free((char *)names.layout);
+ free((char *)names.variant);
+ free((char *)names.options);
+ if (new_keymap == NULL) {
+ ELOG("xkb_keymap_new_from_names(RMLVO) failed\n");
+ return false;
+ }
}
xkb_keymap_unref(xkb_keymap);
xkb_keymap = new_keymap;
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;
+}