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