]> git.sur5r.net Git - i3/i3/blob - src/config.c
7e5f83b18d69328d0c8bae22abf9d2b302f845ec
[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 XDG folders
236  * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
237  *
238  */
239 static char *get_config_path() {
240         /* 1: check for $XDG_CONFIG_HOME/i3/config */
241         char *xdg_config_home, *xdg_config_dirs, *config_path;
242
243         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
244                 xdg_config_home = "~/.config";
245
246         xdg_config_home = resolve_tilde(xdg_config_home);
247         if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
248                 die("asprintf() failed");
249         free(xdg_config_home);
250
251         if (path_exists(config_path))
252                 return config_path;
253         free(config_path);
254
255         /* 2: check for $XDG_CONFIG_DIRS/i3/config */
256         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
257                 xdg_config_dirs = "/etc/xdg";
258
259         char *buf = sstrdup(xdg_config_dirs);
260         char *tok = strtok(buf, ":");
261         while (tok != NULL) {
262                 tok = resolve_tilde(tok);
263                 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
264                         die("asprintf() failed");
265                 free(tok);
266                 if (path_exists(config_path)) {
267                         free(buf);
268                         return config_path;
269                 }
270                 free(config_path);
271                 tok = strtok(NULL, ":");
272         }
273         free(buf);
274
275         /* 3: check traditional paths */
276         config_path = resolve_tilde("~/.i3/config");
277         if (path_exists(config_path))
278                 return config_path;
279
280         config_path = sstrdup(SYSCONFDIR "/i3/config");
281         if (!path_exists(config_path))
282                 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
283                     "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
284                     SYSCONFDIR "/i3/config exist.");
285
286         return config_path;
287 }
288
289 /*
290  * Finds the configuration file to use (either the one specified by
291  * override_configpath), the user’s one or the system default) and calls
292  * parse_file().
293  *
294  */
295 static void parse_configuration(const char *override_configpath) {
296         if (override_configpath != NULL) {
297                 parse_file(override_configpath);
298                 return;
299         }
300
301         char *path = get_config_path();
302         DLOG("Parsing configfile %s\n", path);
303         parse_file(path);
304         free(path);
305 }
306
307 /*
308  * (Re-)loads the configuration file (sets useful defaults before).
309  *
310  */
311 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
312         if (reload) {
313                 /* First ungrab the keys */
314                 ungrab_all_keys(conn);
315
316                 struct Mode *mode;
317                 Binding *bind;
318                 while (!SLIST_EMPTY(&modes)) {
319                         mode = SLIST_FIRST(&modes);
320                         FREE(mode->name);
321
322                         /* Clear the old binding list */
323                         bindings = mode->bindings;
324                         while (!TAILQ_EMPTY(bindings)) {
325                                 bind = TAILQ_FIRST(bindings);
326                                 TAILQ_REMOVE(bindings, bind, bindings);
327                                 FREE(bind->translated_to);
328                                 FREE(bind->command);
329                                 FREE(bind);
330                         }
331                         FREE(bindings);
332                         SLIST_REMOVE(&modes, mode, Mode, modes);
333                 }
334
335                 struct Assignment *assign;
336                 while (!TAILQ_EMPTY(&assignments)) {
337                         assign = TAILQ_FIRST(&assignments);
338                         FREE(assign->windowclass_title);
339                         TAILQ_REMOVE(&assignments, assign, assignments);
340                         FREE(assign);
341                 }
342
343                 /* Clear workspace names */
344                 Workspace *ws;
345                 TAILQ_FOREACH(ws, workspaces, workspaces)
346                         workspace_set_name(ws, NULL);
347         }
348
349         SLIST_INIT(&modes);
350
351         struct Mode *default_mode = scalloc(sizeof(struct Mode));
352         default_mode->name = sstrdup("default");
353         default_mode->bindings = scalloc(sizeof(struct bindings_head));
354         TAILQ_INIT(default_mode->bindings);
355         SLIST_INSERT_HEAD(&modes, default_mode, modes);
356
357         bindings = default_mode->bindings;
358
359 #define REQUIRED_OPTION(name) \
360         if (config.name == NULL) \
361                 die("You did not specify required configuration option " #name "\n");
362
363         /* Clear the old config or initialize the data structure */
364         memset(&config, 0, sizeof(config));
365
366         /* Initialize default colors */
367 #define INIT_COLOR(x, cborder, cbackground, ctext) \
368         do { \
369                 x.border = get_colorpixel(conn, cborder); \
370                 x.background = get_colorpixel(conn, cbackground); \
371                 x.text = get_colorpixel(conn, ctext); \
372         } while (0)
373
374         config.client.background = get_colorpixel(conn, "#000000");
375         INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
376         INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
377         INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
378         INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
379         INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
380         INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
381         INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
382
383         parse_configuration(override_configpath);
384
385         if (reload) {
386                 translate_keysyms();
387                 grab_all_keys(conn, false);
388         }
389
390         REQUIRED_OPTION(font);
391
392         /* Set an empty name for every workspace which got no name */
393         Workspace *ws;
394         TAILQ_FOREACH(ws, workspaces, workspaces) {
395                 if (ws->name != NULL) {
396                         /* If the font was not specified when the workspace name
397                          * was loaded, we need to predict the text width now */
398                         if (ws->text_width == 0)
399                                 ws->text_width = predict_text_width(global_conn,
400                                                 config.font, ws->name, ws->name_len);
401                         continue;
402                 }
403
404                 workspace_set_name(ws, NULL);
405         }
406 }