]> git.sur5r.net Git - i3/i3/blobdiff - src/bindings.c
t/*ewmh*: fix flakiness by syncing
[i3/i3] / src / bindings.c
index 4b5f225cb5d08d17fa9f6ece380758f268edfee9..cd91a3989c452c4d45189f474c39ea8994507b53 100644 (file)
@@ -8,6 +8,8 @@
  */
 #include "all.h"
 
+#include <xkbcommon/xkbcommon.h>
+
 pid_t command_error_nagbar_pid = -1;
 
 /*
@@ -47,14 +49,14 @@ 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 *command, const char *modename) {
+                           const char *release, const char *command, const char *modename) {
     Binding *new_binding = scalloc(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);
     if (strcmp(bindtype, "bindsym") == 0) {
         new_binding->input_type = (strncasecmp(input_code, "button", (sizeof("button") - 1)) == 0
-            ? B_MOUSE
-            : B_KEYBOARD);
+                                       ? B_MOUSE
+                                       : B_KEYBOARD);
 
         new_binding->symbol = sstrdup(input_code);
     } else {
@@ -81,12 +83,11 @@ static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint
         return;
 
     DLOG("Grabbing %d with modifiers %d (with mod_mask_lock %d)\n", keycode, bind->mods, bind->mods | XCB_MOD_MASK_LOCK);
-    /* Grab the key in all combinations */
-    #define GRAB_KEY(modifier) \
-        do { \
-            xcb_grab_key(conn, 0, root, modifier, keycode, \
-                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
-        } while (0)
+/* Grab the key in all combinations */
+#define GRAB_KEY(modifier)                                                                       \
+    do {                                                                                         \
+        xcb_grab_key(conn, 0, root, modifier, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
+    } while (0)
     int mods = bind->mods;
     if ((bind->mods & BIND_MODE_SWITCH) != 0) {
         mods &= ~BIND_MODE_SWITCH;
@@ -99,7 +100,6 @@ static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint
     GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
 }
 
-
 /*
  * Grab the bound keys (tell X to send us keypress events for those keycodes)
  *
@@ -108,8 +108,8 @@ void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
     Binding *bind;
     TAILQ_FOREACH(bind, bindings, bindings) {
         if (bind->input_type != B_KEYBOARD ||
-                (bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
-                (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
+            (bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
+            (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
             continue;
 
         /* The easy case: the user specified a keycode directly. */
@@ -192,13 +192,11 @@ static Binding *get_binding(uint16_t modifiers, bool is_release, uint16_t input_
  *
  */
 Binding *get_binding_from_xcb_event(xcb_generic_event_t *event) {
-    bool is_release = (event->response_type == XCB_KEY_RELEASE
-                        || event->response_type == XCB_BUTTON_RELEASE);
+    bool is_release = (event->response_type == XCB_KEY_RELEASE || event->response_type == XCB_BUTTON_RELEASE);
 
-    input_type_t input_type = ((event->response_type == XCB_BUTTON_RELEASE
-                                || event->response_type == XCB_BUTTON_PRESS)
-                                ? B_MOUSE
-                                : B_KEYBOARD);
+    input_type_t input_type = ((event->response_type == XCB_BUTTON_RELEASE || event->response_type == XCB_BUTTON_PRESS)
+                                   ? B_MOUSE
+                                   : B_KEYBOARD);
 
     uint16_t event_state = ((xcb_key_press_event_t *)event)->state;
     uint16_t event_detail = ((xcb_key_press_event_t *)event)->detail;
@@ -267,8 +265,8 @@ void translate_keysyms(void) {
             continue;
 
         /* We need to translate the symbol to a keycode */
-        keysym = XStringToKeysym(bind->symbol);
-        if (keysym == NoSymbol) {
+        keysym = xkb_keysym_from_name(bind->symbol, XKB_KEYSYM_NO_FLAGS);
+        if (keysym == XKB_KEY_NoSymbol) {
             ELOG("Could not translate string to key symbol: \"%s\"\n",
                  bind->symbol);
             continue;
@@ -285,13 +283,13 @@ void translate_keysyms(void) {
 
         for (i = min_keycode; i && i <= max_keycode; i++) {
             if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
-                (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
+                (xcb_key_symbols_get_keysym(keysyms, i, col + 1) != keysym))
                 continue;
             bind->number_keycodes++;
             bind->translated_to = srealloc(bind->translated_to,
                                            (sizeof(xcb_keycode_t) *
                                             bind->number_keycodes));
-            bind->translated_to[bind->number_keycodes-1] = i;
+            bind->translated_to[bind->number_keycodes - 1] = i;
         }
 
         DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
@@ -383,18 +381,25 @@ void check_for_duplicate_bindings(struct context *context) {
 }
 
 /*
- * Runs the given binding and handles parse errors. Returns a CommandResult for
- * running the binding's command. Caller should render tree if
- * needs_tree_render is true. Free with command_result_free().
+ * 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().
  *
  */
-CommandResult *run_binding(Binding *bind) {
+CommandResult *run_binding(Binding *bind, Con *con) {
+    char *command;
+
     /* We need to copy the command since “reload” may be part of the command,
      * and then the memory that bind->command points to may not contain the
      * same data anymore. */
-    char *command_copy = sstrdup(bind->command);
-    CommandResult *result = parse_command(command_copy, NULL);
-    free(command_copy);
+    if (con == NULL)
+        command = sstrdup(bind->command);
+    else
+        sasprintf(&command, "[con_id=\"%d\"] %s", con, bind->command);
+
+    CommandResult *result = parse_command(command, NULL);
+    free(command);
 
     if (result->needs_tree_render)
         tree_render();
@@ -413,8 +418,7 @@ CommandResult *run_binding(Binding *bind) {
             "-b",
             "show errors",
             pageraction,
-            NULL
-        };
+            NULL};
         start_nagbar(&command_error_nagbar_pid, argv);
         free(pageraction);
     }