]> git.sur5r.net Git - i3/i3/blob - src/bindings.c
Implement bindsym mouse configuration
[i3/i3] / src / bindings.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2014 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 /*
12  * The name of the default mode.
13  *
14  */
15 const char *DEFAULT_BINDING_MODE = "default";
16
17 /*
18  * Returns the mode specified by `name` or creates a new mode and adds it to
19  * the list of modes.
20  *
21  */
22 static struct Mode *mode_from_name(const char *name) {
23     struct Mode *mode;
24
25     /* Try to find the mode in the list of modes and return it */
26     SLIST_FOREACH(mode, &modes, modes) {
27         if (strcmp(mode->name, name) == 0)
28             return mode;
29     }
30
31     /* If the mode was not found, create a new one */
32     mode = scalloc(sizeof(struct Mode));
33     mode->name = sstrdup(name);
34     mode->bindings = scalloc(sizeof(struct bindings_head));
35     TAILQ_INIT(mode->bindings);
36     SLIST_INSERT_HEAD(&modes, mode, modes);
37
38     return mode;
39 }
40
41 /*
42  * Adds a binding from config parameters given as strings and returns a
43  * pointer to the binding structure. Returns NULL if the input code could not
44  * be parsed.
45  *
46  */
47 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
48         const char *release, const char *command, const char *modename) {
49     Binding *new_binding = scalloc(sizeof(Binding));
50     DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
51     new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
52     if (strcmp(bindtype, "bindsym") == 0) {
53         new_binding->input_type = (strncasecmp(input_code, "button", (sizeof("button") - 1)) == 0
54             ? B_MOUSE
55             : B_KEYBOARD);
56
57         new_binding->symbol = sstrdup(input_code);
58     } else {
59         // TODO: strtol with proper error handling
60         new_binding->keycode = atoi(input_code);
61         new_binding->input_type = B_KEYBOARD;
62         if (new_binding->keycode == 0) {
63             ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
64             FREE(new_binding);
65             return NULL;
66         }
67     }
68     new_binding->mods = modifiers_from_str(modifiers);
69     new_binding->command = sstrdup(command);
70
71     struct Mode *mode = mode_from_name(modename);
72     TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
73
74     return new_binding;
75 }
76
77 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
78     if (bind->input_type != B_KEYBOARD)
79         return;
80
81     DLOG("Grabbing %d with modifiers %d (with mod_mask_lock %d)\n", keycode, bind->mods, bind->mods | XCB_MOD_MASK_LOCK);
82     /* Grab the key in all combinations */
83     #define GRAB_KEY(modifier) \
84         do { \
85             xcb_grab_key(conn, 0, root, modifier, keycode, \
86                          XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
87         } while (0)
88     int mods = bind->mods;
89     if ((bind->mods & BIND_MODE_SWITCH) != 0) {
90         mods &= ~BIND_MODE_SWITCH;
91         if (mods == 0)
92             mods = XCB_MOD_MASK_ANY;
93     }
94     GRAB_KEY(mods);
95     GRAB_KEY(mods | xcb_numlock_mask);
96     GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
97     GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
98 }
99
100
101 /*
102  * Grab the bound keys (tell X to send us keypress events for those keycodes)
103  *
104  */
105 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
106     Binding *bind;
107     TAILQ_FOREACH(bind, bindings, bindings) {
108         if (bind->input_type != B_KEYBOARD ||
109                 (bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
110                 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
111             continue;
112
113         /* The easy case: the user specified a keycode directly. */
114         if (bind->keycode > 0) {
115             grab_keycode_for_binding(conn, bind, bind->keycode);
116             continue;
117         }
118
119         xcb_keycode_t *walk = bind->translated_to;
120         for (uint32_t i = 0; i < bind->number_keycodes; i++)
121             grab_keycode_for_binding(conn, bind, *walk++);
122     }
123 }
124
125 /*
126  * Returns a pointer to the keyboard Binding with the specified modifiers and
127  * keycode or NULL if no such binding exists.
128  *
129  */
130 Binding *get_keyboard_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode) {
131     Binding *bind;
132
133     if (!key_release) {
134         /* On a KeyPress event, we first reset all
135          * B_UPON_KEYRELEASE_IGNORE_MODS bindings back to B_UPON_KEYRELEASE */
136         TAILQ_FOREACH(bind, bindings, bindings) {
137             if (bind->input_type != B_KEYBOARD)
138                 continue;
139             if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
140                 bind->release = B_UPON_KEYRELEASE;
141         }
142     }
143
144     TAILQ_FOREACH(bind, bindings, bindings) {
145         /* First compare the modifiers (unless this is a
146          * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
147          * event) */
148         if (bind->input_type != B_KEYBOARD)
149             continue;
150         if (bind->mods != modifiers &&
151             (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
152              !key_release))
153             continue;
154
155         /* If a symbol was specified by the user, we need to look in
156          * the array of translated keycodes for the event’s keycode */
157         if (bind->symbol != NULL) {
158             if (memmem(bind->translated_to,
159                        bind->number_keycodes * sizeof(xcb_keycode_t),
160                        &keycode, sizeof(xcb_keycode_t)) == NULL)
161                 continue;
162         } else {
163             /* This case is easier: The user specified a keycode */
164             if (bind->keycode != keycode)
165                 continue;
166         }
167
168         /* If this keybinding is a KeyRelease binding, it matches the key which
169          * the user pressed. We therefore mark it as
170          * B_UPON_KEYRELEASE_IGNORE_MODS for later, so that the user can
171          * release the modifiers before the actual key and the KeyRelease will
172          * still be matched. */
173         if (bind->release == B_UPON_KEYRELEASE && !key_release)
174             bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
175
176         /* Check if the binding is for a KeyPress or a KeyRelease event */
177         if ((bind->release == B_UPON_KEYPRESS && key_release) ||
178             (bind->release >= B_UPON_KEYRELEASE && !key_release))
179             continue;
180
181         break;
182     }
183
184     return (bind == TAILQ_END(bindings) ? NULL : bind);
185 }
186
187 /*
188  * Translates keysymbols to keycodes for all bindings which use keysyms.
189  *
190  */
191 void translate_keysyms(void) {
192     Binding *bind;
193     xcb_keysym_t keysym;
194     int col;
195     xcb_keycode_t i, min_keycode, max_keycode;
196
197     min_keycode = xcb_get_setup(conn)->min_keycode;
198     max_keycode = xcb_get_setup(conn)->max_keycode;
199
200     TAILQ_FOREACH(bind, bindings, bindings) {
201         if (bind->input_type == B_MOUSE) {
202             int button = atoi(bind->symbol + (sizeof("button") - 1));
203             bind->keycode = button;
204
205             if (button < 1)
206                 ELOG("Could not translate string to button: \"%s\"\n", bind->symbol);
207
208             continue;
209         }
210
211         if (bind->keycode > 0)
212             continue;
213
214         /* We need to translate the symbol to a keycode */
215         keysym = XStringToKeysym(bind->symbol);
216         if (keysym == NoSymbol) {
217             ELOG("Could not translate string to key symbol: \"%s\"\n",
218                  bind->symbol);
219             continue;
220         }
221
222         /* Base column we use for looking up key symbols. We always consider
223          * the base column and the corresponding shift column, so without
224          * mode_switch, we look in 0 and 1, with mode_switch we look in 2 and
225          * 3. */
226         col = (bind->mods & BIND_MODE_SWITCH ? 2 : 0);
227
228         FREE(bind->translated_to);
229         bind->number_keycodes = 0;
230
231         for (i = min_keycode; i && i <= max_keycode; i++) {
232             if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
233                 (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
234                 continue;
235             bind->number_keycodes++;
236             bind->translated_to = srealloc(bind->translated_to,
237                                            (sizeof(xcb_keycode_t) *
238                                             bind->number_keycodes));
239             bind->translated_to[bind->number_keycodes-1] = i;
240         }
241
242         DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
243              bind->number_keycodes);
244     }
245 }
246
247 /*
248  * Switches the key bindings to the given mode, if the mode exists
249  *
250  */
251 void switch_mode(const char *new_mode) {
252     struct Mode *mode;
253
254     DLOG("Switching to mode %s\n", new_mode);
255
256     SLIST_FOREACH(mode, &modes, modes) {
257         if (strcasecmp(mode->name, new_mode) != 0)
258             continue;
259
260         ungrab_all_keys(conn);
261         bindings = mode->bindings;
262         translate_keysyms();
263         grab_all_keys(conn, false);
264
265         char *event_msg;
266         sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name);
267
268         ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
269         FREE(event_msg);
270
271         return;
272     }
273
274     ELOG("ERROR: Mode not found\n");
275 }
276
277 /*
278  * Checks for duplicate key bindings (the same keycode or keysym is configured
279  * more than once). If a duplicate binding is found, a message is printed to
280  * stderr and the has_errors variable is set to true, which will start
281  * i3-nagbar.
282  *
283  */
284 void check_for_duplicate_bindings(struct context *context) {
285     Binding *bind, *current;
286     TAILQ_FOREACH(current, bindings, bindings) {
287         TAILQ_FOREACH(bind, bindings, bindings) {
288             /* Abort when we reach the current keybinding, only check the
289              * bindings before */
290             if (bind == current)
291                 break;
292
293             /* Check if the input types are different */
294             if (bind->input_type != current->input_type)
295                 continue;
296
297             /* Check if one is using keysym while the other is using bindsym.
298              * If so, skip. */
299             /* XXX: It should be checked at a later place (when translating the
300              * keysym to keycodes) if there are any duplicates */
301             if ((bind->symbol == NULL && current->symbol != NULL) ||
302                 (bind->symbol != NULL && current->symbol == NULL))
303                 continue;
304
305             /* If bind is NULL, current has to be NULL, too (see above).
306              * If the keycodes differ, it can't be a duplicate. */
307             if (bind->symbol != NULL &&
308                 strcasecmp(bind->symbol, current->symbol) != 0)
309                 continue;
310
311             /* Check if the keycodes or modifiers are different. If so, they
312              * can't be duplicate */
313             if (bind->keycode != current->keycode ||
314                 bind->mods != current->mods ||
315                 bind->release != current->release)
316                 continue;
317
318             context->has_errors = true;
319             if (current->keycode != 0) {
320                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keycode %d, command \"%s\"\n",
321                      current->mods, current->keycode, current->command);
322             } else {
323                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keysym %s, command \"%s\"\n",
324                      current->mods, current->symbol, current->command);
325             }
326         }
327     }
328 }