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