i3 - improved tiling WM


Move grab_all_keys to bindings.[ch]

Patch status: merged

Patch by Tony Crisci

Long description:

Also add checks for binding input_type to filter bindings that are not
keyboard bindings.

To apply this patch, use:
curl http://cr.i3wm.org/patch/425/raw.patch | git am

b/include/bindings.h

18
@@ -23,3 +23,9 @@ const char *DEFAULT_BINDING_MODE;
19
  */
20
 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
21
         const char *release, const char *command, const char *mode);
22
+
23
+/**
24
+ * Grab the bound keys (tell X to send us keypress events for those keycodes)
25
+ *
26
+ */
27
+void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch);

b/include/config.h

32
@@ -322,12 +322,6 @@ void translate_keysyms(void);
33
 void ungrab_all_keys(xcb_connection_t *conn);
34
 
35
 /**
36
- * Grab the bound keys (tell X to send us keypress events for those keycodes)
37
- *
38
- */
39
-void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch);
40
-
41
-/**
42
  * Switches the key bindings to the given mode, if the mode exists
43
  *
44
  */

b/src/bindings.c

49
@@ -69,3 +69,51 @@ Binding *configure_binding(const char *bindtype, const char *modifiers, const ch
50
 
51
     return new_binding;
52
 }
53
+
54
+static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
55
+    if (bind->input_type != B_KEYBOARD)
56
+        return;
57
+
58
+    DLOG("Grabbing %d with modifiers %d (with mod_mask_lock %d)\n", keycode, bind->mods, bind->mods | XCB_MOD_MASK_LOCK);
59
+    /* Grab the key in all combinations */
60
+    #define GRAB_KEY(modifier) \
61
+        do { \
62
+            xcb_grab_key(conn, 0, root, modifier, keycode, \
63
+                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
64
+        } while (0)
65
+    int mods = bind->mods;
66
+    if ((bind->mods & BIND_MODE_SWITCH) != 0) {
67
+        mods &= ~BIND_MODE_SWITCH;
68
+        if (mods == 0)
69
+            mods = XCB_MOD_MASK_ANY;
70
+    }
71
+    GRAB_KEY(mods);
72
+    GRAB_KEY(mods | xcb_numlock_mask);
73
+    GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
74
+    GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
75
+}
76
+
77
+
78
+/*
79
+ * Grab the bound keys (tell X to send us keypress events for those keycodes)
80
+ *
81
+ */
82
+void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
83
+    Binding *bind;
84
+    TAILQ_FOREACH(bind, bindings, bindings) {
85
+        if (bind->input_type != B_KEYBOARD ||
86
+                (bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
87
+                (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
88
+            continue;
89
+
90
+        /* The easy case: the user specified a keycode directly. */
91
+        if (bind->keycode > 0) {
92
+            grab_keycode_for_binding(conn, bind, bind->keycode);
93
+            continue;
94
+        }
95
+
96
+        xcb_keycode_t *walk = bind->translated_to;
97
+        for (uint32_t i = 0; i < bind->number_keycodes; i++)
98
+            grab_keycode_for_binding(conn, bind, *walk++);
99
+    }
100
+}

b/src/config.c

105
@@ -30,26 +30,6 @@ void ungrab_all_keys(xcb_connection_t *conn) {
106
     xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
107
 }
108
 
109
-static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
110
-    DLOG("Grabbing %d with modifiers %d (with mod_mask_lock %d)\n", keycode, bind->mods, bind->mods | XCB_MOD_MASK_LOCK);
111
-    /* Grab the key in all combinations */
112
-    #define GRAB_KEY(modifier) \
113
-        do { \
114
-            xcb_grab_key(conn, 0, root, modifier, keycode, \
115
-                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
116
-        } while (0)
117
-    int mods = bind->mods;
118
-    if ((bind->mods & BIND_MODE_SWITCH) != 0) {
119
-        mods &= ~BIND_MODE_SWITCH;
120
-        if (mods == 0)
121
-            mods = XCB_MOD_MASK_ANY;
122
-    }
123
-    GRAB_KEY(mods);
124
-    GRAB_KEY(mods | xcb_numlock_mask);
125
-    GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
126
-    GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
127
-}
128
-
129
 /*
130
  * Returns a pointer to the Binding with the specified modifiers and keycode
131
  * or NULL if no such binding exists.
132
@@ -158,29 +138,6 @@ void translate_keysyms(void) {
133
 }
134
 
135
 /*
136
- * Grab the bound keys (tell X to send us keypress events for those keycodes)
137
- *
138
- */
139
-void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
140
-    Binding *bind;
141
-    TAILQ_FOREACH(bind, bindings, bindings) {
142
-        if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
143
-            (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
144
-            continue;
145
-
146
-        /* The easy case: the user specified a keycode directly. */
147
-        if (bind->keycode > 0) {
148
-            grab_keycode_for_binding(conn, bind, bind->keycode);
149
-            continue;
150
-        }
151
-
152
-        xcb_keycode_t *walk = bind->translated_to;
153
-        for (uint32_t i = 0; i < bind->number_keycodes; i++)
154
-            grab_keycode_for_binding(conn, bind, *walk++);
155
-    }
156
-}
157
-
158
-/*
159
  * Switches the key bindings to the given mode, if the mode exists
160
  *
161
  */