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