]> git.sur5r.net Git - i3/i3/blob - src/bindings.c
32aac05af3d605e5c9c0f6fa4b7442e6db726e23
[i3/i3] / src / bindings.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * bindings.c: Functions for configuring, finding and, running bindings.
8  */
9 #include "all.h"
10
11 #include <xkbcommon/xkbcommon.h>
12 #include <xkbcommon/xkbcommon-x11.h>
13
14 static struct xkb_context *xkb_context;
15 static struct xkb_keymap *xkb_keymap;
16
17 pid_t command_error_nagbar_pid = -1;
18
19 /*
20  * The name of the default mode.
21  *
22  */
23 const char *DEFAULT_BINDING_MODE = "default";
24
25 /*
26  * Returns the mode specified by `name` or creates a new mode and adds it to
27  * the list of modes.
28  *
29  */
30 static struct Mode *mode_from_name(const char *name) {
31     struct Mode *mode;
32
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)
36             return mode;
37     }
38
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->bindings = scalloc(1, sizeof(struct bindings_head));
43     TAILQ_INIT(mode->bindings);
44     SLIST_INSERT_HEAD(&modes, mode, modes);
45
46     return mode;
47 }
48
49 /*
50  * Adds a binding from config parameters given as strings and returns a
51  * pointer to the binding structure. Returns NULL if the input code could not
52  * be parsed.
53  *
54  */
55 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
56                            const char *release, const char *border, const char *whole_window,
57                            const char *command, const char *modename) {
58     Binding *new_binding = scalloc(1, sizeof(Binding));
59     DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
60     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
61     new_binding->border = (border != NULL);
62     new_binding->whole_window = (whole_window != NULL);
63     if (strcmp(bindtype, "bindsym") == 0) {
64         new_binding->input_type = (strncasecmp(input_code, "button", (sizeof("button") - 1)) == 0
65                                        ? B_MOUSE
66                                        : B_KEYBOARD);
67
68         new_binding->symbol = sstrdup(input_code);
69     } else {
70         char *endptr;
71         long keycode = strtol(input_code, &endptr, 10);
72         new_binding->keycode = keycode;
73         new_binding->input_type = B_KEYBOARD;
74         if (keycode == LONG_MAX || keycode == LONG_MIN || keycode < 0 || *endptr != '\0' || endptr == input_code) {
75             ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
76             FREE(new_binding);
77             return NULL;
78         }
79     }
80     new_binding->command = sstrdup(command);
81     new_binding->event_state_mask = event_state_from_str(modifiers);
82     int group_bits_set = 0;
83     if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_1)
84         group_bits_set++;
85     if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
86         group_bits_set++;
87     if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
88         group_bits_set++;
89     if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
90         group_bits_set++;
91     if (group_bits_set > 1)
92         ELOG("Keybinding has more than one Group specified, but your X server is always in precisely one group. The keybinding can never trigger.\n");
93
94     struct Mode *mode = mode_from_name(modename);
95     TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
96
97     return new_binding;
98 }
99
100 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
101     if (bind->input_type != B_KEYBOARD)
102         return;
103
104 /* Grab the key in all combinations */
105 #define GRAB_KEY(modifier)                                                                       \
106     do {                                                                                         \
107         xcb_grab_key(conn, 0, root, modifier, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
108     } while (0)
109     int mods = bind->event_state_mask;
110     if (((mods >> 16) & I3_XKB_GROUP_MASK_1) && xkb_current_group != XCB_XKB_GROUP_1)
111         return;
112     if (((mods >> 16) & I3_XKB_GROUP_MASK_2) && xkb_current_group != XCB_XKB_GROUP_2)
113         return;
114     if (((mods >> 16) & I3_XKB_GROUP_MASK_3) && xkb_current_group != XCB_XKB_GROUP_3)
115         return;
116     if (((mods >> 16) & I3_XKB_GROUP_MASK_4) && xkb_current_group != XCB_XKB_GROUP_4)
117         return;
118     mods &= 0xFFFF;
119     DLOG("Grabbing keycode %d with event state mask 0x%x (mods 0x%x)\n",
120          keycode, bind->event_state_mask, mods);
121     GRAB_KEY(mods);
122     GRAB_KEY(mods | xcb_numlock_mask);
123     GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
124     GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
125 }
126
127 /*
128  * Grab the bound keys (tell X to send us keypress events for those keycodes)
129  *
130  */
131 void grab_all_keys(xcb_connection_t *conn) {
132     Binding *bind;
133     TAILQ_FOREACH(bind, bindings, bindings) {
134         if (bind->input_type != B_KEYBOARD)
135             continue;
136
137         /* The easy case: the user specified a keycode directly. */
138         if (bind->keycode > 0) {
139             grab_keycode_for_binding(conn, bind, bind->keycode);
140             continue;
141         }
142
143         for (uint32_t i = 0; i < bind->number_keycodes; i++)
144             grab_keycode_for_binding(conn, bind, bind->translated_to[i]);
145     }
146 }
147
148 /*
149  * Returns a pointer to the Binding with the specified modifiers and
150  * keycode or NULL if no such binding exists.
151  *
152  */
153 static Binding *get_binding(i3_event_state_mask_t state_filtered, bool is_release, uint16_t input_code, input_type_t input_type) {
154     Binding *bind;
155
156     if (!is_release) {
157         /* On a press event, we first reset all B_UPON_KEYRELEASE_IGNORE_MODS
158          * bindings back to B_UPON_KEYRELEASE */
159         TAILQ_FOREACH(bind, bindings, bindings) {
160             if (bind->input_type != input_type)
161                 continue;
162             if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
163                 bind->release = B_UPON_KEYRELEASE;
164         }
165     }
166
167     TAILQ_FOREACH(bind, bindings, bindings) {
168         DLOG("binding with event_state_mask 0x%x, state_filtered 0x%x, match: %s\n",
169              bind->event_state_mask, state_filtered,
170              ((state_filtered & bind->event_state_mask) == bind->event_state_mask) ? "yes" : "no");
171         /* First compare the state_filtered (unless this is a
172          * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
173          * event) */
174         if (bind->input_type != input_type)
175             continue;
176         if ((state_filtered & bind->event_state_mask) != bind->event_state_mask &&
177             (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
178              !is_release))
179             continue;
180
181         /* For keyboard bindings where a symbol was specified by the user, we
182          * need to look in the array of translated keycodes for the event’s
183          * keycode */
184         if (input_type == B_KEYBOARD && bind->symbol != NULL) {
185             xcb_keycode_t input_keycode = (xcb_keycode_t)input_code;
186             if (memmem(bind->translated_to,
187                        bind->number_keycodes * sizeof(xcb_keycode_t),
188                        &input_keycode, sizeof(xcb_keycode_t)) == NULL)
189                 continue;
190         } else {
191             /* This case is easier: The user specified a keycode */
192             if (bind->keycode != input_code)
193                 continue;
194         }
195
196         /* If this binding is a release binding, it matches the key which the
197          * user pressed. We therefore mark it as B_UPON_KEYRELEASE_IGNORE_MODS
198          * for later, so that the user can release the modifiers before the
199          * actual key or button and the release event will still be matched. */
200         if (bind->release == B_UPON_KEYRELEASE && !is_release)
201             bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
202
203         /* Check if the binding is for a press or a release event */
204         if ((bind->release == B_UPON_KEYPRESS && is_release) ||
205             (bind->release >= B_UPON_KEYRELEASE && !is_release))
206             continue;
207
208         break;
209     }
210
211     return (bind == TAILQ_END(bindings) ? NULL : bind);
212 }
213
214 /*
215  * Returns a pointer to the Binding that matches the given xcb button or key
216  * event or NULL if no such binding exists.
217  *
218  */
219 Binding *get_binding_from_xcb_event(xcb_generic_event_t *event) {
220     const bool is_release = (event->response_type == XCB_KEY_RELEASE ||
221                              event->response_type == XCB_BUTTON_RELEASE);
222
223     const input_type_t input_type = ((event->response_type == XCB_BUTTON_RELEASE ||
224                                       event->response_type == XCB_BUTTON_PRESS)
225                                          ? B_MOUSE
226                                          : B_KEYBOARD);
227
228     const uint16_t event_state = ((xcb_key_press_event_t *)event)->state;
229     const uint16_t event_detail = ((xcb_key_press_event_t *)event)->detail;
230
231     /* Remove the numlock bit */
232     i3_event_state_mask_t state_filtered = event_state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
233     DLOG("(removed numlock, state = 0x%x)\n", state_filtered);
234     /* Transform the keyboard_group from bit 13 and bit 14 into an
235      * i3_xkb_group_mask_t, so that get_binding() can just bitwise AND the
236      * configured bindings against |state_filtered|.
237      *
238      * These bits are only set because we set the XKB client flags
239      * XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE and
240      * XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED. See also doc/kbproto
241      * section 2.2.2:
242      * http://www.x.org/releases/X11R7.7/doc/kbproto/xkbproto.html#Computing_A_State_Field_from_an_XKB_State */
243     switch ((event_state & 0x6000) >> 13) {
244         case XCB_XKB_GROUP_1:
245             state_filtered |= (I3_XKB_GROUP_MASK_1 << 16);
246             break;
247         case XCB_XKB_GROUP_2:
248             state_filtered |= (I3_XKB_GROUP_MASK_2 << 16);
249             break;
250         case XCB_XKB_GROUP_3:
251             state_filtered |= (I3_XKB_GROUP_MASK_3 << 16);
252             break;
253         case XCB_XKB_GROUP_4:
254             state_filtered |= (I3_XKB_GROUP_MASK_4 << 16);
255             break;
256     }
257     state_filtered &= ~0x6000;
258     DLOG("(transformed keyboard group, state = 0x%x)\n", state_filtered);
259     return get_binding(state_filtered, is_release, event_detail, input_type);
260 }
261
262 struct resolve {
263     /* The binding which we are resolving. */
264     Binding *bind;
265
266     /* |bind|’s keysym (translated to xkb_keysym_t), e.g. XKB_KEY_R. */
267     xkb_keysym_t keysym;
268
269     /* The xkb state built from the user-provided modifiers and group. */
270     struct xkb_state *xkb_state;
271
272     /* Like |xkb_state|, just without the shift modifier, if shift was specified. */
273     struct xkb_state *xkb_state_no_shift;
274 };
275
276 /*
277  * add_keycode_if_matches is called for each keycode in the keymap and will add
278  * the keycode to |data->bind| if the keycode can result in the keysym
279  * |data->resolving|.
280  *
281  */
282 static void add_keycode_if_matches(struct xkb_keymap *keymap, xkb_keycode_t key, void *data) {
283     const struct resolve *resolving = data;
284     xkb_keysym_t sym = xkb_state_key_get_one_sym(resolving->xkb_state, key);
285     if (sym != resolving->keysym) {
286         /* Check if Shift was specified, and try resolving the symbol without
287          * shift, so that “bindsym $mod+Shift+a nop” actually works. */
288         const xkb_layout_index_t layout = xkb_state_key_get_layout(resolving->xkb_state, key);
289         if (layout == XKB_LAYOUT_INVALID)
290             return;
291         if (xkb_state_key_get_level(resolving->xkb_state, key, layout) > 1)
292             return;
293         sym = xkb_state_key_get_one_sym(resolving->xkb_state_no_shift, key);
294         if (sym != resolving->keysym)
295             return;
296     }
297     Binding *bind = resolving->bind;
298     bind->number_keycodes++;
299     bind->translated_to = srealloc(bind->translated_to,
300                                    (sizeof(xcb_keycode_t) *
301                                     bind->number_keycodes));
302     bind->translated_to[bind->number_keycodes - 1] = key;
303 }
304
305 /*
306  * Translates keysymbols to keycodes for all bindings which use keysyms.
307  *
308  */
309 void translate_keysyms(void) {
310     struct xkb_state *dummy_state = xkb_state_new(xkb_keymap);
311     if (dummy_state == NULL) {
312         ELOG("Could not create XKB state, cannot translate keysyms.\n");
313         return;
314     }
315
316     struct xkb_state *dummy_state_no_shift = xkb_state_new(xkb_keymap);
317     if (dummy_state_no_shift == NULL) {
318         ELOG("Could not create XKB state, cannot translate keysyms.\n");
319         return;
320     }
321
322     Binding *bind;
323     TAILQ_FOREACH(bind, bindings, bindings) {
324         if (bind->input_type == B_MOUSE) {
325             char *endptr;
326             long button = strtol(bind->symbol + (sizeof("button") - 1), &endptr, 10);
327             bind->keycode = button;
328
329             if (button == LONG_MAX || button == LONG_MIN || button < 0 || *endptr != '\0' || endptr == bind->symbol)
330                 ELOG("Could not translate string to button: \"%s\"\n", bind->symbol);
331
332             continue;
333         }
334
335         if (bind->keycode > 0)
336             continue;
337
338         /* We need to translate the symbol to a keycode */
339         const xkb_keysym_t keysym = xkb_keysym_from_name(bind->symbol, XKB_KEYSYM_NO_FLAGS);
340         if (keysym == XKB_KEY_NoSymbol) {
341             ELOG("Could not translate string to key symbol: \"%s\"\n",
342                  bind->symbol);
343             continue;
344         }
345
346         xkb_layout_index_t group = XCB_XKB_GROUP_1;
347         if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
348             group = XCB_XKB_GROUP_2;
349         else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
350             group = XCB_XKB_GROUP_3;
351         else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
352             group = XCB_XKB_GROUP_4;
353
354         DLOG("group = %d, event_state_mask = %d, &2 = %s, &3 = %s, &4 = %s\n", group,
355              bind->event_state_mask,
356              (bind->event_state_mask & I3_XKB_GROUP_MASK_2) ? "yes" : "no",
357              (bind->event_state_mask & I3_XKB_GROUP_MASK_3) ? "yes" : "no",
358              (bind->event_state_mask & I3_XKB_GROUP_MASK_4) ? "yes" : "no");
359         (void)xkb_state_update_mask(
360             dummy_state,
361             (bind->event_state_mask & 0x1FFF) /* xkb_mod_mask_t base_mods, */,
362             0 /* xkb_mod_mask_t latched_mods, */,
363             0 /* xkb_mod_mask_t locked_mods, */,
364             0 /* xkb_layout_index_t base_group, */,
365             0 /* xkb_layout_index_t latched_group, */,
366             group /* xkb_layout_index_t locked_group, */);
367
368         (void)xkb_state_update_mask(
369             dummy_state_no_shift,
370             (bind->event_state_mask & 0x1FFF) ^ XCB_KEY_BUT_MASK_SHIFT /* xkb_mod_mask_t base_mods, */,
371             0 /* xkb_mod_mask_t latched_mods, */,
372             0 /* xkb_mod_mask_t locked_mods, */,
373             0 /* xkb_layout_index_t base_group, */,
374             0 /* xkb_layout_index_t latched_group, */,
375             group /* xkb_layout_index_t locked_group, */);
376
377         struct resolve resolving = {
378             .bind = bind,
379             .keysym = keysym,
380             .xkb_state = dummy_state,
381             .xkb_state_no_shift = dummy_state_no_shift,
382         };
383         FREE(bind->translated_to);
384         bind->number_keycodes = 0;
385         xkb_keymap_key_for_each(xkb_keymap, add_keycode_if_matches, &resolving);
386         char *keycodes = sstrdup("");
387         for (uint32_t n = 0; n < bind->number_keycodes; n++) {
388             char *tmp;
389             sasprintf(&tmp, "%s %d", keycodes, bind->translated_to[n]);
390             free(keycodes);
391             keycodes = tmp;
392         }
393         DLOG("state=0x%x, cfg=\"%s\", sym=0x%x → keycodes%s (%d)\n",
394              bind->event_state_mask, bind->symbol, keysym, keycodes, bind->number_keycodes);
395         free(keycodes);
396     }
397
398     xkb_state_unref(dummy_state);
399 }
400
401 /*
402  * Switches the key bindings to the given mode, if the mode exists
403  *
404  */
405 void switch_mode(const char *new_mode) {
406     struct Mode *mode;
407
408     DLOG("Switching to mode %s\n", new_mode);
409
410     SLIST_FOREACH(mode, &modes, modes) {
411         if (strcasecmp(mode->name, new_mode) != 0)
412             continue;
413
414         ungrab_all_keys(conn);
415         bindings = mode->bindings;
416         translate_keysyms();
417         grab_all_keys(conn);
418
419         char *event_msg;
420         sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name);
421
422         ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
423         FREE(event_msg);
424
425         return;
426     }
427
428     ELOG("ERROR: Mode not found\n");
429 }
430
431 static int reorder_binding_cmp(const void *a, const void *b) {
432     Binding *first = *((Binding **)a);
433     Binding *second = *((Binding **)b);
434     if (first->event_state_mask < second->event_state_mask) {
435         return 1;
436     } else if (first->event_state_mask == second->event_state_mask) {
437         return 0;
438     } else {
439         return -1;
440     }
441 }
442
443 static void reorder_bindings_of_mode(struct Mode *mode) {
444     /* Copy the bindings into an array, so that we can use qsort(3). */
445     int n = 0;
446     Binding *current;
447     TAILQ_FOREACH(current, mode->bindings, bindings) {
448         n++;
449     }
450     Binding **tmp = scalloc(n, sizeof(Binding *));
451     n = 0;
452     TAILQ_FOREACH(current, mode->bindings, bindings) {
453         tmp[n++] = current;
454     }
455
456     qsort(tmp, n, sizeof(Binding *), reorder_binding_cmp);
457
458     struct bindings_head *reordered = scalloc(1, sizeof(struct bindings_head));
459     TAILQ_INIT(reordered);
460     for (int i = 0; i < n; i++) {
461         current = tmp[i];
462         TAILQ_REMOVE(mode->bindings, current, bindings);
463         TAILQ_INSERT_TAIL(reordered, current, bindings);
464     }
465     free(tmp);
466     assert(TAILQ_EMPTY(mode->bindings));
467     /* Free the old bindings_head, which is now empty. */
468     free(mode->bindings);
469     mode->bindings = reordered;
470 }
471
472 /*
473  * Reorders bindings by event_state_mask descendingly so that get_binding()
474  * correctly matches more specific bindings before more generic bindings. Take
475  * the following binding configuration as an example:
476  *
477  *   bindsym n nop lower-case n pressed
478  *   bindsym Shift+n nop upper-case n pressed
479  *
480  * Without reordering, the first binding’s event_state_mask of 0x0 would match
481  * the actual event_stat_mask of 0x1 and hence trigger instead of the second
482  * keybinding.
483  *
484  */
485 void reorder_bindings(void) {
486     struct Mode *mode;
487     SLIST_FOREACH(mode, &modes, modes) {
488         const bool current_mode = (mode->bindings == bindings);
489         reorder_bindings_of_mode(mode);
490         if (current_mode)
491             bindings = mode->bindings;
492     }
493 }
494
495 /*
496  * Checks for duplicate key bindings (the same keycode or keysym is configured
497  * more than once). If a duplicate binding is found, a message is printed to
498  * stderr and the has_errors variable is set to true, which will start
499  * i3-nagbar.
500  *
501  */
502 void check_for_duplicate_bindings(struct context *context) {
503     Binding *bind, *current;
504     TAILQ_FOREACH(current, bindings, bindings) {
505         TAILQ_FOREACH(bind, bindings, bindings) {
506             /* Abort when we reach the current keybinding, only check the
507              * bindings before */
508             if (bind == current)
509                 break;
510
511             /* Check if the input types are different */
512             if (bind->input_type != current->input_type)
513                 continue;
514
515             /* Check if one is using keysym while the other is using bindsym.
516              * If so, skip. */
517             /* XXX: It should be checked at a later place (when translating the
518              * keysym to keycodes) if there are any duplicates */
519             if ((bind->symbol == NULL && current->symbol != NULL) ||
520                 (bind->symbol != NULL && current->symbol == NULL))
521                 continue;
522
523             /* If bind is NULL, current has to be NULL, too (see above).
524              * If the keycodes differ, it can't be a duplicate. */
525             if (bind->symbol != NULL &&
526                 strcasecmp(bind->symbol, current->symbol) != 0)
527                 continue;
528
529             /* Check if the keycodes or modifiers are different. If so, they
530              * can't be duplicate */
531             if (bind->keycode != current->keycode ||
532                 bind->event_state_mask != current->event_state_mask ||
533                 bind->release != current->release)
534                 continue;
535
536             context->has_errors = true;
537             if (current->keycode != 0) {
538                 ELOG("Duplicate keybinding in config file:\n  state mask 0x%x with keycode %d, command \"%s\"\n",
539                      current->event_state_mask, current->keycode, current->command);
540             } else {
541                 ELOG("Duplicate keybinding in config file:\n  state mask 0x%x with keysym %s, command \"%s\"\n",
542                      current->event_state_mask, current->symbol, current->command);
543             }
544         }
545     }
546 }
547
548 /*
549  * Creates a dynamically allocated copy of bind.
550  */
551 static Binding *binding_copy(Binding *bind) {
552     Binding *ret = smalloc(sizeof(Binding));
553     *ret = *bind;
554     if (bind->symbol != NULL)
555         ret->symbol = sstrdup(bind->symbol);
556     if (bind->command != NULL)
557         ret->command = sstrdup(bind->command);
558     if (bind->translated_to != NULL) {
559         ret->translated_to = smalloc(sizeof(xcb_keycode_t) * bind->number_keycodes);
560         memcpy(ret->translated_to, bind->translated_to, sizeof(xcb_keycode_t) * bind->number_keycodes);
561     }
562     return ret;
563 }
564
565 /*
566  * Frees the binding. If bind is null, it simply returns.
567  */
568 void binding_free(Binding *bind) {
569     if (bind == NULL) {
570         return;
571     }
572
573     FREE(bind->symbol);
574     FREE(bind->translated_to);
575     FREE(bind->command);
576     FREE(bind);
577 }
578
579 /*
580  * Runs the given binding and handles parse errors. If con is passed, it will
581  * execute the command binding with that container selected by criteria.
582  * Returns a CommandResult for running the binding's command. Caller should
583  * render tree if needs_tree_render is true. Free with command_result_free().
584  *
585  */
586 CommandResult *run_binding(Binding *bind, Con *con) {
587     char *command;
588
589     /* We need to copy the binding and command since “reload” may be part of
590      * the command, and then the memory that bind points to may not contain the
591      * same data anymore. */
592     if (con == NULL)
593         command = sstrdup(bind->command);
594     else
595         sasprintf(&command, "[con_id=\"%p\"] %s", con, bind->command);
596
597     Binding *bind_cp = binding_copy(bind);
598     CommandResult *result = parse_command(command, NULL);
599     free(command);
600
601     if (result->needs_tree_render)
602         tree_render();
603
604     if (result->parse_error) {
605         char *pageraction;
606         sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
607         char *argv[] = {
608             NULL, /* will be replaced by the executable path */
609             "-f",
610             config.font.pattern,
611             "-t",
612             "error",
613             "-m",
614             "The configured command for this shortcut could not be run successfully.",
615             "-b",
616             "show errors",
617             pageraction,
618             NULL};
619         start_nagbar(&command_error_nagbar_pid, argv);
620         free(pageraction);
621     }
622
623     ipc_send_binding_event("run", bind_cp);
624     binding_free(bind_cp);
625
626     return result;
627 }
628
629 /*
630  * Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
631  *
632  */
633 bool load_keymap(void) {
634     if (xkb_context == NULL) {
635         if ((xkb_context = xkb_context_new(0)) == NULL) {
636             ELOG("Could not create xkbcommon context\n");
637             return false;
638         }
639     }
640
641     struct xkb_keymap *new_keymap;
642     const int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
643     DLOG("device_id = %d\n", device_id);
644     if ((new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
645         ELOG("xkb_x11_keymap_new_from_device failed\n");
646         return false;
647     }
648     xkb_keymap_unref(xkb_keymap);
649     xkb_keymap = new_keymap;
650
651     return true;
652 }