]> git.sur5r.net Git - i3/i3/blob - src/config.c
Don’t use SYNC key bindings for Mode_switch but re-grab keys
[i3/i3] / src / config.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * src/config.c: Contains all functions handling the configuration file (calling
11  * the parser (src/cfgparse.y) with the correct path, switching key bindings
12  * mode).
13  *
14  */
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <stdlib.h>
20 #include <glob.h>
21 #include <unistd.h>
22
23 /* We need Xlib for XStringToKeysym */
24 #include <X11/Xlib.h>
25
26 #include <xcb/xcb_keysyms.h>
27
28 #include "i3.h"
29 #include "util.h"
30 #include "config.h"
31 #include "xcb.h"
32 #include "table.h"
33 #include "workspace.h"
34 #include "log.h"
35
36 Config config;
37 struct modes_head modes;
38
39 /*
40  * This function resolves ~ in pathnames.
41  *
42  */
43 static char *glob_path(const char *path) {
44         static glob_t globbuf;
45         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
46                 die("glob() failed");
47         char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
48         globfree(&globbuf);
49         return result;
50 }
51
52 /*
53  * Checks if the given path exists by calling stat().
54  *
55  */
56 static bool path_exists(const char *path) {
57         struct stat buf;
58         return (stat(path, &buf) == 0);
59 }
60
61 /**
62  * Ungrabs all keys, to be called before re-grabbing the keys because of a
63  * mapping_notify event or a configuration file reload
64  *
65  */
66 void ungrab_all_keys(xcb_connection_t *conn) {
67         DLOG("Ungrabbing all keys\n");
68         xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
69 }
70
71 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
72         DLOG("Grabbing %d\n", keycode);
73         /* Grab the key in all combinations */
74         #define GRAB_KEY(modifier) \
75                 do { \
76                         xcb_grab_key(conn, 0, root, modifier, keycode, \
77                                      XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
78                 } while (0)
79         int mods = bind->mods;
80         if ((bind->mods & BIND_MODE_SWITCH) != 0) {
81                 mods &= ~BIND_MODE_SWITCH;
82                 if (mods == 0)
83                         mods = XCB_MOD_MASK_ANY;
84         }
85         GRAB_KEY(mods);
86         GRAB_KEY(mods | xcb_numlock_mask);
87         GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
88 }
89
90 /*
91  * Returns a pointer to the Binding with the specified modifiers and keycode
92  * or NULL if no such binding exists.
93  *
94  */
95 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
96         Binding *bind;
97
98         TAILQ_FOREACH(bind, bindings, bindings) {
99                 /* First compare the modifiers */
100                 if (bind->mods != modifiers)
101                         continue;
102
103                 /* If a symbol was specified by the user, we need to look in
104                  * the array of translated keycodes for the event’s keycode */
105                 if (bind->symbol != NULL) {
106                         if (memmem(bind->translated_to,
107                                    bind->number_keycodes * sizeof(xcb_keycode_t),
108                                    &keycode, sizeof(xcb_keycode_t)) != NULL)
109                                 break;
110                 } else {
111                         /* This case is easier: The user specified a keycode */
112                         if (bind->keycode == keycode)
113                                 break;
114                 }
115         }
116
117         return (bind == TAILQ_END(bindings) ? NULL : bind);
118 }
119
120 /*
121  * Translates keysymbols to keycodes for all bindings which use keysyms.
122  *
123  */
124 void translate_keysyms() {
125         Binding *bind;
126         TAILQ_FOREACH(bind, bindings, bindings) {
127                 if (bind->keycode > 0)
128                         continue;
129
130                 /* We need to translate the symbol to a keycode */
131                 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
132                 if (keysym == NoSymbol) {
133                         ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
134                         continue;
135                 }
136
137 #ifdef OLD_XCB_KEYSYMS_API
138                 bind->number_keycodes = 1;
139                 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
140                 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
141                 grab_keycode_for_binding(conn, bind, code);
142                 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
143                 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
144 #else
145                 uint32_t last_keycode = 0;
146                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
147                 if (keycodes == NULL) {
148                         DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
149                         continue;
150                 }
151
152                 bind->number_keycodes = 0;
153
154                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
155                         /* We hope duplicate keycodes will be returned in order
156                          * and skip them */
157                         if (last_keycode == *walk)
158                                 continue;
159                         last_keycode = *walk;
160                         bind->number_keycodes++;
161                 }
162                 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
163                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
164                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
165                 free(keycodes);
166 #endif
167         }
168 }
169
170 /*
171  * Grab the bound keys (tell X to send us keypress events for those keycodes)
172  *
173  */
174 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
175         Binding *bind;
176         TAILQ_FOREACH(bind, bindings, bindings) {
177                 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
178                     (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
179                         continue;
180
181                 /* The easy case: the user specified a keycode directly. */
182                 if (bind->keycode > 0) {
183                         grab_keycode_for_binding(conn, bind, bind->keycode);
184                         continue;
185                 }
186
187                 xcb_keycode_t *walk = bind->translated_to;
188                 for (int i = 0; i < bind->number_keycodes; i++)
189                         grab_keycode_for_binding(conn, bind, *walk);
190         }
191 }
192
193 /*
194  * Switches the key bindings to the given mode, if the mode exists
195  *
196  */
197 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
198         struct Mode *mode;
199
200         LOG("Switching to mode %s\n", new_mode);
201
202         SLIST_FOREACH(mode, &modes, modes) {
203                 if (strcasecmp(mode->name, new_mode) != 0)
204                         continue;
205
206                 ungrab_all_keys(conn);
207                 bindings = mode->bindings;
208                 grab_all_keys(conn, false);
209                 return;
210         }
211
212         ELOG("ERROR: Mode not found\n");
213 }
214
215 /*
216  * Get the path of the first configuration file found. Checks the XDG folders
217  * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
218  *
219  */
220 static char *get_config_path() {
221         /* 1: check for $XDG_CONFIG_HOME/i3/config */
222         char *xdg_config_home, *xdg_config_dirs, *config_path;
223
224         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
225                 xdg_config_home = "~/.config";
226
227         xdg_config_home = glob_path(xdg_config_home);
228         if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
229                 die("asprintf() failed");
230         free(xdg_config_home);
231
232         if (path_exists(config_path))
233                 return config_path;
234         free(config_path);
235
236         /* 2: check for $XDG_CONFIG_DIRS/i3/config */
237         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
238                 xdg_config_dirs = "/etc/xdg";
239
240         char *buf = strdup(xdg_config_dirs);
241         char *tok = strtok(buf, ":");
242         while (tok != NULL) {
243                 tok = glob_path(tok);
244                 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
245                         die("asprintf() failed");
246                 free(tok);
247                 if (path_exists(config_path)) {
248                         free(buf);
249                         return config_path;
250                 }
251                 free(config_path);
252                 tok = strtok(NULL, ":");
253         }
254         free(buf);
255
256         /* 3: check traditional paths */
257         config_path = glob_path("~/.i3/config");
258         if (path_exists(config_path))
259                 return config_path;
260
261         config_path = strdup("/etc/i3/config");
262         if (!path_exists(config_path))
263                 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
264                     "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
265                     "/etc/i3/config exist.");
266
267         return config_path;
268 }
269
270 /*
271  * Finds the configuration file to use (either the one specified by
272  * override_configpath), the user’s one or the system default) and calls
273  * parse_file().
274  *
275  */
276 static void parse_configuration(const char *override_configpath) {
277         if (override_configpath != NULL) {
278                 parse_file(override_configpath);
279                 return;
280         }
281
282         char *path = get_config_path();
283         DLOG("Parsing configfile %s\n", path);
284         parse_file(path);
285         free(path);
286 }
287
288 /*
289  * (Re-)loads the configuration file (sets useful defaults before).
290  *
291  */
292 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
293         if (reload) {
294                 /* First ungrab the keys */
295                 ungrab_all_keys(conn);
296
297                 struct Mode *mode;
298                 Binding *bind;
299                 while (!SLIST_EMPTY(&modes)) {
300                         mode = SLIST_FIRST(&modes);
301                         FREE(mode->name);
302
303                         /* Clear the old binding list */
304                         bindings = mode->bindings;
305                         while (!TAILQ_EMPTY(bindings)) {
306                                 bind = TAILQ_FIRST(bindings);
307                                 TAILQ_REMOVE(bindings, bind, bindings);
308                                 FREE(bind->translated_to);
309                                 FREE(bind->command);
310                                 FREE(bind);
311                         }
312                         FREE(bindings);
313                         SLIST_REMOVE(&modes, mode, Mode, modes);
314                 }
315
316                 struct Assignment *assign;
317                 while (!TAILQ_EMPTY(&assignments)) {
318                         assign = TAILQ_FIRST(&assignments);
319                         FREE(assign->windowclass_title);
320                         TAILQ_REMOVE(&assignments, assign, assignments);
321                         FREE(assign);
322                 }
323         }
324
325         SLIST_INIT(&modes);
326
327         struct Mode *default_mode = scalloc(sizeof(struct Mode));
328         default_mode->name = sstrdup("default");
329         default_mode->bindings = scalloc(sizeof(struct bindings_head));
330         TAILQ_INIT(default_mode->bindings);
331         SLIST_INSERT_HEAD(&modes, default_mode, modes);
332
333         bindings = default_mode->bindings;
334
335 #define REQUIRED_OPTION(name) \
336         if (config.name == NULL) \
337                 die("You did not specify required configuration option " #name "\n");
338
339         /* Clear the old config or initialize the data structure */
340         memset(&config, 0, sizeof(config));
341
342         /* Initialize default colors */
343 #define INIT_COLOR(x, cborder, cbackground, ctext) \
344         do { \
345                 x.border = get_colorpixel(conn, cborder); \
346                 x.background = get_colorpixel(conn, cbackground); \
347                 x.text = get_colorpixel(conn, ctext); \
348         } while (0)
349
350         INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
351         INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
352         INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
353         INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
354         INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
355         INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
356         INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
357
358         parse_configuration(override_configpath);
359
360         if (reload) {
361                 translate_keysyms();
362                 grab_all_keys(conn, false);
363         }
364
365         REQUIRED_OPTION(font);
366
367         /* Set an empty name for every workspace which got no name */
368         Workspace *ws;
369         TAILQ_FOREACH(ws, workspaces, workspaces) {
370                 if (ws->name != NULL) {
371                         /* If the font was not specified when the workspace name
372                          * was loaded, we need to predict the text width now */
373                         if (ws->text_width == 0)
374                                 ws->text_width = predict_text_width(global_conn,
375                                                 config.font, ws->name, ws->name_len);
376                         continue;
377                 }
378
379                 workspace_set_name(ws, NULL);
380         }
381 }