]> git.sur5r.net Git - i3/i3/blob - src/config.c
c7ef0af977d481eea4a1a71dc6a05a3827899959
[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  *
42  */
43 static char *glob_path(const char *path) {
44         static glob_t globbuf;
45         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
46                 die("glob() failed");
47         char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
48         globfree(&globbuf);
49         return result;
50 }
51
52 /*
53  * Checks if the given path exists by calling stat().
54  *
55  */
56 static 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 #ifdef OLD_XCB_KEYSYMS_API
138                 bind->number_keycodes = 1;
139                 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
140                 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
141                 grab_keycode_for_binding(global_conn, bind, code);
142                 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
143                 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
144 #else
145                 uint32_t last_keycode = 0;
146                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
147                 if (keycodes == NULL) {
148                         DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
149                         continue;
150                 }
151
152                 bind->number_keycodes = 0;
153
154                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
155                         /* We hope duplicate keycodes will be returned in order
156                          * and skip them */
157                         if (last_keycode == *walk)
158                                 continue;
159                         last_keycode = *walk;
160                         bind->number_keycodes++;
161                 }
162                 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
163                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
164                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
165                 free(keycodes);
166 #endif
167         }
168 }
169
170 /*
171  * Grab the bound keys (tell X to send us keypress events for those keycodes)
172  *
173  */
174 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
175         Binding *bind;
176         TAILQ_FOREACH(bind, bindings, bindings) {
177                 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
178                     (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
179                         continue;
180
181                 /* The easy case: the user specified a keycode directly. */
182                 if (bind->keycode > 0) {
183                         grab_keycode_for_binding(conn, bind, bind->keycode);
184                         continue;
185                 }
186
187                 xcb_keycode_t *walk = bind->translated_to;
188                 for (int i = 0; i < bind->number_keycodes; i++)
189                         grab_keycode_for_binding(conn, bind, *walk);
190         }
191 }
192
193 /*
194  * Switches the key bindings to the given mode, if the mode exists
195  *
196  */
197 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
198         struct Mode *mode;
199
200         LOG("Switching to mode %s\n", new_mode);
201
202         SLIST_FOREACH(mode, &modes, modes) {
203                 if (strcasecmp(mode->name, new_mode) != 0)
204                         continue;
205
206                 ungrab_all_keys(conn);
207                 bindings = mode->bindings;
208                 translate_keysyms();
209                 grab_all_keys(conn, false);
210                 return;
211         }
212
213         ELOG("ERROR: Mode not found\n");
214 }
215
216 /*
217  * Get the path of the first configuration file found. Checks the XDG folders
218  * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
219  *
220  */
221 static char *get_config_path() {
222         /* 1: check for $XDG_CONFIG_HOME/i3/config */
223         char *xdg_config_home, *xdg_config_dirs, *config_path;
224
225         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
226                 xdg_config_home = "~/.config";
227
228         xdg_config_home = glob_path(xdg_config_home);
229         if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
230                 die("asprintf() failed");
231         free(xdg_config_home);
232
233         if (path_exists(config_path))
234                 return config_path;
235         free(config_path);
236
237         /* 2: check for $XDG_CONFIG_DIRS/i3/config */
238         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
239                 xdg_config_dirs = "/etc/xdg";
240
241         char *buf = strdup(xdg_config_dirs);
242         char *tok = strtok(buf, ":");
243         while (tok != NULL) {
244                 tok = glob_path(tok);
245                 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
246                         die("asprintf() failed");
247                 free(tok);
248                 if (path_exists(config_path)) {
249                         free(buf);
250                         return config_path;
251                 }
252                 free(config_path);
253                 tok = strtok(NULL, ":");
254         }
255         free(buf);
256
257         /* 3: check traditional paths */
258         config_path = glob_path("~/.i3/config");
259         if (path_exists(config_path))
260                 return config_path;
261
262         config_path = strdup("/etc/i3/config");
263         if (!path_exists(config_path))
264                 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
265                     "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
266                     "/etc/i3/config exist.");
267
268         return config_path;
269 }
270
271 /*
272  * Finds the configuration file to use (either the one specified by
273  * override_configpath), the user’s one or the system default) and calls
274  * parse_file().
275  *
276  */
277 static void parse_configuration(const char *override_configpath) {
278         if (override_configpath != NULL) {
279                 parse_file(override_configpath);
280                 return;
281         }
282
283         char *path = get_config_path();
284         DLOG("Parsing configfile %s\n", path);
285         parse_file(path);
286         free(path);
287 }
288
289 /*
290  * (Re-)loads the configuration file (sets useful defaults before).
291  *
292  */
293 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
294         if (reload) {
295                 /* First ungrab the keys */
296                 ungrab_all_keys(conn);
297
298                 struct Mode *mode;
299                 Binding *bind;
300                 while (!SLIST_EMPTY(&modes)) {
301                         mode = SLIST_FIRST(&modes);
302                         FREE(mode->name);
303
304                         /* Clear the old binding list */
305                         bindings = mode->bindings;
306                         while (!TAILQ_EMPTY(bindings)) {
307                                 bind = TAILQ_FIRST(bindings);
308                                 TAILQ_REMOVE(bindings, bind, bindings);
309                                 FREE(bind->translated_to);
310                                 FREE(bind->command);
311                                 FREE(bind);
312                         }
313                         FREE(bindings);
314                         SLIST_REMOVE(&modes, mode, Mode, modes);
315                 }
316
317                 struct Assignment *assign;
318                 while (!TAILQ_EMPTY(&assignments)) {
319                         assign = TAILQ_FIRST(&assignments);
320                         FREE(assign->windowclass_title);
321                         TAILQ_REMOVE(&assignments, assign, assignments);
322                         FREE(assign);
323                 }
324
325                 /* Clear workspace names */
326                 Workspace *ws;
327                 TAILQ_FOREACH(ws, workspaces, workspaces)
328                         workspace_set_name(ws, NULL);
329         }
330
331         SLIST_INIT(&modes);
332
333         struct Mode *default_mode = scalloc(sizeof(struct Mode));
334         default_mode->name = sstrdup("default");
335         default_mode->bindings = scalloc(sizeof(struct bindings_head));
336         TAILQ_INIT(default_mode->bindings);
337         SLIST_INSERT_HEAD(&modes, default_mode, modes);
338
339         bindings = default_mode->bindings;
340
341 #define REQUIRED_OPTION(name) \
342         if (config.name == NULL) \
343                 die("You did not specify required configuration option " #name "\n");
344
345         /* Clear the old config or initialize the data structure */
346         memset(&config, 0, sizeof(config));
347
348         /* Initialize default colors */
349 #define INIT_COLOR(x, cborder, cbackground, ctext) \
350         do { \
351                 x.border = get_colorpixel(conn, cborder); \
352                 x.background = get_colorpixel(conn, cbackground); \
353                 x.text = get_colorpixel(conn, ctext); \
354         } while (0)
355
356         INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
357         INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
358         INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
359         INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
360         INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
361         INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
362         INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
363
364         parse_configuration(override_configpath);
365
366         if (reload) {
367                 translate_keysyms();
368                 grab_all_keys(conn, false);
369         }
370
371         REQUIRED_OPTION(font);
372
373         /* Set an empty name for every workspace which got no name */
374         Workspace *ws;
375         TAILQ_FOREACH(ws, workspaces, workspaces) {
376                 if (ws->name != NULL) {
377                         /* If the font was not specified when the workspace name
378                          * was loaded, we need to predict the text width now */
379                         if (ws->text_width == 0)
380                                 ws->text_width = predict_text_width(global_conn,
381                                                 config.font, ws->name, ws->name_len);
382                         continue;
383                 }
384
385                 workspace_set_name(ws, NULL);
386         }
387 }