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