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