2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * bindings.c: Functions for configuring, finding and, running bindings.
11 #include <xkbcommon/xkbcommon.h>
12 #include <xkbcommon/xkbcommon-x11.h>
14 static struct xkb_context *xkb_context;
15 static struct xkb_keymap *xkb_keymap;
17 pid_t command_error_nagbar_pid = -1;
20 * The name of the default mode.
23 const char *DEFAULT_BINDING_MODE = "default";
26 * Returns the mode specified by `name` or creates a new mode and adds it to
30 static struct Mode *mode_from_name(const char *name, bool pango_markup) {
33 /* Try to find the mode in the list of modes and return it */
34 SLIST_FOREACH(mode, &modes, modes) {
35 if (strcmp(mode->name, name) == 0)
39 /* If the mode was not found, create a new one */
40 mode = scalloc(1, sizeof(struct Mode));
41 mode->name = sstrdup(name);
42 mode->pango_markup = pango_markup;
43 mode->bindings = scalloc(1, sizeof(struct bindings_head));
44 TAILQ_INIT(mode->bindings);
45 SLIST_INSERT_HEAD(&modes, mode, modes);
51 * Adds a binding from config parameters given as strings and returns a
52 * pointer to the binding structure. Returns NULL if the input code could not
56 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
57 const char *release, const char *border, const char *whole_window,
58 const char *command, const char *modename, bool pango_markup) {
59 Binding *new_binding = scalloc(1, sizeof(Binding));
60 DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
61 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
62 new_binding->border = (border != NULL);
63 new_binding->whole_window = (whole_window != NULL);
64 if (strcmp(bindtype, "bindsym") == 0) {
65 new_binding->input_type = (strncasecmp(input_code, "button", (sizeof("button") - 1)) == 0
69 new_binding->symbol = sstrdup(input_code);
72 long keycode = strtol(input_code, &endptr, 10);
73 new_binding->keycode = keycode;
74 new_binding->input_type = B_KEYBOARD;
75 if (keycode == LONG_MAX || keycode == LONG_MIN || keycode < 0 || *endptr != '\0' || endptr == input_code) {
76 ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
81 new_binding->command = sstrdup(command);
82 new_binding->event_state_mask = event_state_from_str(modifiers);
83 int group_bits_set = 0;
84 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_1)
86 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
88 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
90 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
92 if (group_bits_set > 1)
93 ELOG("Keybinding has more than one Group specified, but your X server is always in precisely one group. The keybinding can never trigger.\n");
95 struct Mode *mode = mode_from_name(modename, pango_markup);
96 TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
101 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
102 if (bind->input_type != B_KEYBOARD)
105 /* Grab the key in all combinations */
106 #define GRAB_KEY(modifier) \
108 xcb_grab_key(conn, 0, root, modifier, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
110 int mods = bind->event_state_mask;
111 if (((mods >> 16) & I3_XKB_GROUP_MASK_1) && xkb_current_group != XCB_XKB_GROUP_1)
113 if (((mods >> 16) & I3_XKB_GROUP_MASK_2) && xkb_current_group != XCB_XKB_GROUP_2)
115 if (((mods >> 16) & I3_XKB_GROUP_MASK_3) && xkb_current_group != XCB_XKB_GROUP_3)
117 if (((mods >> 16) & I3_XKB_GROUP_MASK_4) && xkb_current_group != XCB_XKB_GROUP_4)
120 DLOG("Grabbing keycode %d with event state mask 0x%x (mods 0x%x)\n",
121 keycode, bind->event_state_mask, mods);
123 GRAB_KEY(mods | xcb_numlock_mask);
124 GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
125 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
129 * Grab the bound keys (tell X to send us keypress events for those keycodes)
132 void grab_all_keys(xcb_connection_t *conn) {
134 TAILQ_FOREACH(bind, bindings, bindings) {
135 if (bind->input_type != B_KEYBOARD)
138 /* The easy case: the user specified a keycode directly. */
139 if (bind->keycode > 0) {
140 grab_keycode_for_binding(conn, bind, bind->keycode);
144 for (uint32_t i = 0; i < bind->number_keycodes; i++)
145 grab_keycode_for_binding(conn, bind, bind->translated_to[i]);
150 * Release the button grabs on all managed windows and regrab them,
151 * reevaluating which buttons need to be grabbed.
154 void regrab_all_buttons(xcb_connection_t *conn) {
155 int *buttons = bindings_get_buttons_to_grab();
156 xcb_grab_server(conn);
159 TAILQ_FOREACH(con, &all_cons, all_cons) {
160 if (con->window == NULL)
163 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, con->window->id, XCB_BUTTON_MASK_ANY);
164 xcb_grab_buttons(conn, con->window->id, buttons);
168 xcb_ungrab_server(conn);
172 * Returns a pointer to the Binding with the specified modifiers and
173 * keycode or NULL if no such binding exists.
176 static Binding *get_binding(i3_event_state_mask_t state_filtered, bool is_release, uint16_t input_code, input_type_t input_type) {
180 /* On a press event, we first reset all B_UPON_KEYRELEASE_IGNORE_MODS
181 * bindings back to B_UPON_KEYRELEASE */
182 TAILQ_FOREACH(bind, bindings, bindings) {
183 if (bind->input_type != input_type)
185 if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
186 bind->release = B_UPON_KEYRELEASE;
190 const uint32_t xkb_group_state = (state_filtered & 0xFFFF0000);
191 const uint32_t modifiers_state = (state_filtered & 0x0000FFFF);
192 TAILQ_FOREACH(bind, bindings, bindings) {
193 const uint32_t xkb_group_mask = (bind->event_state_mask & 0xFFFF0000);
194 /* modifiers_mask is a special case: a value of 0 does not mean “match all”,
195 * but rather “match exactly when no modifiers are present”. */
196 const uint32_t modifiers_mask = (bind->event_state_mask & 0x0000FFFF);
197 const bool groups_match = ((xkb_group_state & xkb_group_mask) == xkb_group_mask);
199 if (modifiers_mask == 0) {
200 /* Verify no modifiers are pressed. A bitwise AND would lead to
201 * false positives, see issue #2002. */
202 mods_match = (modifiers_state == 0);
204 mods_match = ((modifiers_state & modifiers_mask) == modifiers_mask);
206 const bool state_matches = (groups_match && mods_match);
208 DLOG("binding groups_match = %s, mods_match = %s, state_matches = %s\n",
209 (groups_match ? "yes" : "no"),
210 (mods_match ? "yes" : "no"),
211 (state_matches ? "yes" : "no"));
212 /* First compare the state_filtered (unless this is a
213 * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
215 if (bind->input_type != input_type)
217 if (!state_matches &&
218 (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
222 /* For keyboard bindings where a symbol was specified by the user, we
223 * need to look in the array of translated keycodes for the event’s
225 if (input_type == B_KEYBOARD && bind->symbol != NULL) {
226 xcb_keycode_t input_keycode = (xcb_keycode_t)input_code;
227 if (memmem(bind->translated_to,
228 bind->number_keycodes * sizeof(xcb_keycode_t),
229 &input_keycode, sizeof(xcb_keycode_t)) == NULL)
232 /* This case is easier: The user specified a keycode */
233 if (bind->keycode != input_code)
237 /* If this binding is a release binding, it matches the key which the
238 * user pressed. We therefore mark it as B_UPON_KEYRELEASE_IGNORE_MODS
239 * for later, so that the user can release the modifiers before the
240 * actual key or button and the release event will still be matched. */
241 if (bind->release == B_UPON_KEYRELEASE && !is_release)
242 bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
244 /* Check if the binding is for a press or a release event */
245 if ((bind->release == B_UPON_KEYPRESS && is_release) ||
246 (bind->release >= B_UPON_KEYRELEASE && !is_release))
252 return (bind == TAILQ_END(bindings) ? NULL : bind);
256 * Returns a pointer to the Binding that matches the given xcb button or key
257 * event or NULL if no such binding exists.
260 Binding *get_binding_from_xcb_event(xcb_generic_event_t *event) {
261 const bool is_release = (event->response_type == XCB_KEY_RELEASE ||
262 event->response_type == XCB_BUTTON_RELEASE);
264 const input_type_t input_type = ((event->response_type == XCB_BUTTON_RELEASE ||
265 event->response_type == XCB_BUTTON_PRESS)
269 const uint16_t event_state = ((xcb_key_press_event_t *)event)->state;
270 const uint16_t event_detail = ((xcb_key_press_event_t *)event)->detail;
272 /* Remove the numlock bit */
273 i3_event_state_mask_t state_filtered = event_state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
274 DLOG("(removed numlock, state = 0x%x)\n", state_filtered);
275 /* Transform the keyboard_group from bit 13 and bit 14 into an
276 * i3_xkb_group_mask_t, so that get_binding() can just bitwise AND the
277 * configured bindings against |state_filtered|.
279 * These bits are only set because we set the XKB client flags
280 * XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE and
281 * XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED. See also doc/kbproto
283 * http://www.x.org/releases/X11R7.7/doc/kbproto/xkbproto.html#Computing_A_State_Field_from_an_XKB_State */
284 switch ((event_state & 0x6000) >> 13) {
285 case XCB_XKB_GROUP_1:
286 state_filtered |= (I3_XKB_GROUP_MASK_1 << 16);
288 case XCB_XKB_GROUP_2:
289 state_filtered |= (I3_XKB_GROUP_MASK_2 << 16);
291 case XCB_XKB_GROUP_3:
292 state_filtered |= (I3_XKB_GROUP_MASK_3 << 16);
294 case XCB_XKB_GROUP_4:
295 state_filtered |= (I3_XKB_GROUP_MASK_4 << 16);
298 state_filtered &= ~0x6000;
299 DLOG("(transformed keyboard group, state = 0x%x)\n", state_filtered);
300 return get_binding(state_filtered, is_release, event_detail, input_type);
304 /* The binding which we are resolving. */
307 /* |bind|’s keysym (translated to xkb_keysym_t), e.g. XKB_KEY_R. */
310 /* The xkb state built from the user-provided modifiers and group. */
311 struct xkb_state *xkb_state;
313 /* Like |xkb_state|, just without the shift modifier, if shift was specified. */
314 struct xkb_state *xkb_state_no_shift;
318 * add_keycode_if_matches is called for each keycode in the keymap and will add
319 * the keycode to |data->bind| if the keycode can result in the keysym
323 static void add_keycode_if_matches(struct xkb_keymap *keymap, xkb_keycode_t key, void *data) {
324 const struct resolve *resolving = data;
325 xkb_keysym_t sym = xkb_state_key_get_one_sym(resolving->xkb_state, key);
326 if (sym != resolving->keysym) {
327 /* Check if Shift was specified, and try resolving the symbol without
328 * shift, so that “bindsym $mod+Shift+a nop” actually works. */
329 const xkb_layout_index_t layout = xkb_state_key_get_layout(resolving->xkb_state, key);
330 if (layout == XKB_LAYOUT_INVALID)
332 if (xkb_state_key_get_level(resolving->xkb_state, key, layout) > 1)
334 sym = xkb_state_key_get_one_sym(resolving->xkb_state_no_shift, key);
335 if (sym != resolving->keysym)
338 Binding *bind = resolving->bind;
339 bind->number_keycodes++;
340 bind->translated_to = srealloc(bind->translated_to,
341 (sizeof(xcb_keycode_t) *
342 bind->number_keycodes));
343 bind->translated_to[bind->number_keycodes - 1] = key;
347 * Translates keysymbols to keycodes for all bindings which use keysyms.
350 void translate_keysyms(void) {
351 struct xkb_state *dummy_state = xkb_state_new(xkb_keymap);
352 if (dummy_state == NULL) {
353 ELOG("Could not create XKB state, cannot translate keysyms.\n");
357 struct xkb_state *dummy_state_no_shift = xkb_state_new(xkb_keymap);
358 if (dummy_state_no_shift == NULL) {
359 ELOG("Could not create XKB state, cannot translate keysyms.\n");
363 bool has_errors = false;
365 TAILQ_FOREACH(bind, bindings, bindings) {
366 if (bind->input_type == B_MOUSE) {
368 long button = strtol(bind->symbol + (sizeof("button") - 1), &endptr, 10);
369 bind->keycode = button;
371 if (button == LONG_MAX || button == LONG_MIN || button < 0 || *endptr != '\0' || endptr == bind->symbol)
372 ELOG("Could not translate string to button: \"%s\"\n", bind->symbol);
377 if (bind->keycode > 0)
380 /* We need to translate the symbol to a keycode */
381 const xkb_keysym_t keysym = xkb_keysym_from_name(bind->symbol, XKB_KEYSYM_NO_FLAGS);
382 if (keysym == XKB_KEY_NoSymbol) {
383 ELOG("Could not translate string to key symbol: \"%s\"\n",
388 xkb_layout_index_t group = XCB_XKB_GROUP_1;
389 if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
390 group = XCB_XKB_GROUP_2;
391 else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
392 group = XCB_XKB_GROUP_3;
393 else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
394 group = XCB_XKB_GROUP_4;
396 DLOG("group = %d, event_state_mask = %d, &2 = %s, &3 = %s, &4 = %s\n", group,
397 bind->event_state_mask,
398 (bind->event_state_mask & I3_XKB_GROUP_MASK_2) ? "yes" : "no",
399 (bind->event_state_mask & I3_XKB_GROUP_MASK_3) ? "yes" : "no",
400 (bind->event_state_mask & I3_XKB_GROUP_MASK_4) ? "yes" : "no");
401 (void)xkb_state_update_mask(
403 (bind->event_state_mask & 0x1FFF) /* xkb_mod_mask_t base_mods, */,
404 0 /* xkb_mod_mask_t latched_mods, */,
405 0 /* xkb_mod_mask_t locked_mods, */,
406 0 /* xkb_layout_index_t base_group, */,
407 0 /* xkb_layout_index_t latched_group, */,
408 group /* xkb_layout_index_t locked_group, */);
410 (void)xkb_state_update_mask(
411 dummy_state_no_shift,
412 (bind->event_state_mask & 0x1FFF) ^ XCB_KEY_BUT_MASK_SHIFT /* xkb_mod_mask_t base_mods, */,
413 0 /* xkb_mod_mask_t latched_mods, */,
414 0 /* xkb_mod_mask_t locked_mods, */,
415 0 /* xkb_layout_index_t base_group, */,
416 0 /* xkb_layout_index_t latched_group, */,
417 group /* xkb_layout_index_t locked_group, */);
419 struct resolve resolving = {
422 .xkb_state = dummy_state,
423 .xkb_state_no_shift = dummy_state_no_shift,
425 FREE(bind->translated_to);
426 bind->number_keycodes = 0;
427 xkb_keymap_key_for_each(xkb_keymap, add_keycode_if_matches, &resolving);
428 char *keycodes = sstrdup("");
429 for (uint32_t n = 0; n < bind->number_keycodes; n++) {
431 sasprintf(&tmp, "%s %d", keycodes, bind->translated_to[n]);
435 /* check for duplicate bindings */
437 TAILQ_FOREACH(check, bindings, bindings) {
440 if (check->symbol != NULL)
442 if (check->keycode != bind->translated_to[n] ||
443 check->event_state_mask != bind->event_state_mask ||
444 check->release != bind->release)
447 ELOG("Duplicate keybinding in config file:\n keysym = %s, keycode = %d, state_mask = 0x%x\n", bind->symbol, check->keycode, bind->event_state_mask);
450 DLOG("state=0x%x, cfg=\"%s\", sym=0x%x → keycodes%s (%d)\n",
451 bind->event_state_mask, bind->symbol, keysym, keycodes, bind->number_keycodes);
455 xkb_state_unref(dummy_state);
456 xkb_state_unref(dummy_state_no_shift);
459 start_config_error_nagbar(current_configpath, true);
464 * Switches the key bindings to the given mode, if the mode exists
467 void switch_mode(const char *new_mode) {
470 DLOG("Switching to mode %s\n", new_mode);
472 SLIST_FOREACH(mode, &modes, modes) {
473 if (strcasecmp(mode->name, new_mode) != 0)
476 ungrab_all_keys(conn);
477 bindings = mode->bindings;
482 sasprintf(&event_msg, "{\"change\":\"%s\", \"pango_markup\":%s}",
483 mode->name, (mode->pango_markup ? "true" : "false"));
485 ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
491 ELOG("ERROR: Mode not found\n");
494 static int reorder_binding_cmp(const void *a, const void *b) {
495 Binding *first = *((Binding **)a);
496 Binding *second = *((Binding **)b);
497 if (first->event_state_mask < second->event_state_mask) {
499 } else if (first->event_state_mask == second->event_state_mask) {
506 static void reorder_bindings_of_mode(struct Mode *mode) {
507 /* Copy the bindings into an array, so that we can use qsort(3). */
510 TAILQ_FOREACH(current, mode->bindings, bindings) {
513 Binding **tmp = scalloc(n, sizeof(Binding *));
515 TAILQ_FOREACH(current, mode->bindings, bindings) {
519 qsort(tmp, n, sizeof(Binding *), reorder_binding_cmp);
521 struct bindings_head *reordered = scalloc(1, sizeof(struct bindings_head));
522 TAILQ_INIT(reordered);
523 for (int i = 0; i < n; i++) {
525 TAILQ_REMOVE(mode->bindings, current, bindings);
526 TAILQ_INSERT_TAIL(reordered, current, bindings);
529 assert(TAILQ_EMPTY(mode->bindings));
530 /* Free the old bindings_head, which is now empty. */
531 free(mode->bindings);
532 mode->bindings = reordered;
536 * Reorders bindings by event_state_mask descendingly so that get_binding()
537 * correctly matches more specific bindings before more generic bindings. Take
538 * the following binding configuration as an example:
540 * bindsym n nop lower-case n pressed
541 * bindsym Shift+n nop upper-case n pressed
543 * Without reordering, the first binding’s event_state_mask of 0x0 would match
544 * the actual event_stat_mask of 0x1 and hence trigger instead of the second
548 void reorder_bindings(void) {
550 SLIST_FOREACH(mode, &modes, modes) {
551 const bool current_mode = (mode->bindings == bindings);
552 reorder_bindings_of_mode(mode);
554 bindings = mode->bindings;
559 * Checks for duplicate key bindings (the same keycode or keysym is configured
560 * more than once). If a duplicate binding is found, a message is printed to
561 * stderr and the has_errors variable is set to true, which will start
565 void check_for_duplicate_bindings(struct context *context) {
566 Binding *bind, *current;
567 TAILQ_FOREACH(current, bindings, bindings) {
568 TAILQ_FOREACH(bind, bindings, bindings) {
569 /* Abort when we reach the current keybinding, only check the
574 /* Check if the input types are different */
575 if (bind->input_type != current->input_type)
578 /* Check if one is using keysym while the other is using bindsym.
580 if ((bind->symbol == NULL && current->symbol != NULL) ||
581 (bind->symbol != NULL && current->symbol == NULL))
584 /* If bind is NULL, current has to be NULL, too (see above).
585 * If the keycodes differ, it can't be a duplicate. */
586 if (bind->symbol != NULL &&
587 strcasecmp(bind->symbol, current->symbol) != 0)
590 /* Check if the keycodes or modifiers are different. If so, they
591 * can't be duplicate */
592 if (bind->keycode != current->keycode ||
593 bind->event_state_mask != current->event_state_mask ||
594 bind->release != current->release)
597 context->has_errors = true;
598 if (current->keycode != 0) {
599 ELOG("Duplicate keybinding in config file:\n state mask 0x%x with keycode %d, command \"%s\"\n",
600 current->event_state_mask, current->keycode, current->command);
602 ELOG("Duplicate keybinding in config file:\n state mask 0x%x with keysym %s, command \"%s\"\n",
603 current->event_state_mask, current->symbol, current->command);
610 * Creates a dynamically allocated copy of bind.
612 static Binding *binding_copy(Binding *bind) {
613 Binding *ret = smalloc(sizeof(Binding));
615 if (bind->symbol != NULL)
616 ret->symbol = sstrdup(bind->symbol);
617 if (bind->command != NULL)
618 ret->command = sstrdup(bind->command);
619 if (bind->translated_to != NULL) {
620 ret->translated_to = smalloc(sizeof(xcb_keycode_t) * bind->number_keycodes);
621 memcpy(ret->translated_to, bind->translated_to, sizeof(xcb_keycode_t) * bind->number_keycodes);
627 * Frees the binding. If bind is null, it simply returns.
629 void binding_free(Binding *bind) {
635 FREE(bind->translated_to);
641 * Runs the given binding and handles parse errors. If con is passed, it will
642 * execute the command binding with that container selected by criteria.
643 * Returns a CommandResult for running the binding's command. Free with
644 * command_result_free().
647 CommandResult *run_binding(Binding *bind, Con *con) {
650 /* We need to copy the binding and command since “reload” may be part of
651 * the command, and then the memory that bind points to may not contain the
652 * same data anymore. */
654 command = sstrdup(bind->command);
656 sasprintf(&command, "[con_id=\"%p\"] %s", con, bind->command);
658 Binding *bind_cp = binding_copy(bind);
659 CommandResult *result = parse_command(command, NULL);
662 if (result->needs_tree_render)
665 if (result->parse_error) {
667 sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
669 NULL, /* will be replaced by the executable path */
675 "The configured command for this shortcut could not be run successfully.",
680 start_nagbar(&command_error_nagbar_pid, argv);
684 ipc_send_binding_event("run", bind_cp);
685 binding_free(bind_cp);
690 static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names) {
691 xcb_intern_atom_reply_t *atom_reply;
692 size_t content_max_words = 256;
694 xcb_window_t root = root_screen->root;
696 atom_reply = xcb_intern_atom_reply(
697 conn, xcb_intern_atom(conn, 0, strlen("_XKB_RULES_NAMES"), "_XKB_RULES_NAMES"), NULL);
698 if (atom_reply == NULL)
701 xcb_get_property_cookie_t prop_cookie;
702 xcb_get_property_reply_t *prop_reply;
703 prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
704 XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
705 prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
706 if (prop_reply == NULL) {
710 if (xcb_get_property_value_length(prop_reply) > 0 && prop_reply->bytes_after > 0) {
711 /* We received an incomplete value. Ask again but with a properly
713 content_max_words += ceil(prop_reply->bytes_after / 4.0);
714 /* Repeat the request, with adjusted size */
716 prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
717 XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
718 prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
719 if (prop_reply == NULL) {
724 if (xcb_get_property_value_length(prop_reply) == 0) {
730 const char *walk = (const char *)xcb_get_property_value(prop_reply);
731 int remaining = xcb_get_property_value_length(prop_reply);
732 for (int i = 0; i < 5 && remaining > 0; i++) {
733 const int len = strnlen(walk, remaining);
737 sasprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
740 sasprintf((char **)&(xkb_names->model), "%.*s", len, walk);
743 sasprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
746 sasprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
749 sasprintf((char **)&(xkb_names->options), "%.*s", len, walk);
752 DLOG("component %d of _XKB_RULES_NAMES is \"%.*s\"\n", i, len, walk);
762 * Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
765 bool load_keymap(void) {
766 if (xkb_context == NULL) {
767 if ((xkb_context = xkb_context_new(0)) == NULL) {
768 ELOG("Could not create xkbcommon context\n");
773 struct xkb_keymap *new_keymap = NULL;
775 if (xkb_supported && (device_id = xkb_x11_get_core_keyboard_device_id(conn)) > -1) {
776 if ((new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
777 ELOG("xkb_x11_keymap_new_from_device failed\n");
781 /* Likely there is no XKB support on this server, possibly because it
782 * is a VNC server. */
783 LOG("No XKB / core keyboard device? Assembling keymap from local RMLVO.\n");
784 struct xkb_rule_names names = {
790 if (fill_rmlvo_from_root(&names) == -1) {
791 ELOG("Could not get _XKB_RULES_NAMES atom from root window, falling back to defaults.\n");
792 if ((new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0)) == NULL) {
793 ELOG("xkb_keymap_new_from_names(NULL) failed\n");
797 new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0);
798 free((char *)names.rules);
799 free((char *)names.model);
800 free((char *)names.layout);
801 free((char *)names.variant);
802 free((char *)names.options);
803 if (new_keymap == NULL) {
804 ELOG("xkb_keymap_new_from_names(RMLVO) failed\n");
808 xkb_keymap_unref(xkb_keymap);
809 xkb_keymap = new_keymap;
815 * Returns a list of buttons that should be grabbed on a window.
816 * This list will always contain 1–3, all higher buttons will only be returned
817 * if there is a whole-window binding for it on some window in the current
819 * The list is terminated by a 0.
821 int *bindings_get_buttons_to_grab(void) {
822 /* Let's make the reasonable assumption that there's no more than 25
829 /* We always return buttons 1 through 3. */
835 TAILQ_FOREACH(bind, bindings, bindings) {
836 if (num + 1 == num_max)
839 /* We are only interested in whole window mouse bindings. */
840 if (bind->input_type != B_MOUSE || !bind->whole_window)
844 long button = strtol(bind->symbol + (sizeof("button") - 1), &endptr, 10);
845 if (button == LONG_MAX || button == LONG_MIN || button < 0 || *endptr != '\0' || endptr == bind->symbol) {
846 ELOG("Could not parse button number, skipping this binding. Please report this bug in i3.\n");
850 /* Avoid duplicates. */
851 for (int i = 0; i < num_max; i++) {
852 if (buffer[i] == button)
856 buffer[num++] = button;
860 int *buttons = scalloc(num, sizeof(int));
861 memcpy(buttons, buffer, num * sizeof(int));