]> git.sur5r.net Git - i3/i3/blob - src/config.c
4babce1ba4b64700af4d491457fef2ae214bd10f
[i3/i3] / src / config.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 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         if ((bind->mods & BIND_MODE_SWITCH) != 0)
74                 xcb_grab_key(conn, 0, root, 0, keycode,
75                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
76         else {
77                 /* Grab the key in all combinations */
78                 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
79                                                         XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
80                 GRAB_KEY(bind->mods);
81                 GRAB_KEY(bind->mods | xcb_numlock_mask);
82                 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
83         }
84 }
85
86 /*
87  * Grab the bound keys (tell X to send us keypress events for those keycodes)
88  *
89  */
90 void grab_all_keys(xcb_connection_t *conn) {
91         Binding *bind;
92         TAILQ_FOREACH(bind, bindings, bindings) {
93                 /* The easy case: the user specified a keycode directly. */
94                 if (bind->keycode > 0) {
95                         grab_keycode_for_binding(conn, bind, bind->keycode);
96                         continue;
97                 }
98
99                 /* We need to translate the symbol to a keycode */
100                 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
101                 if (keysym == NoSymbol) {
102                         ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
103                         continue;
104                 }
105
106 #ifdef OLD_XCB_KEYSYMS_API
107                 bind->number_keycodes = 1;
108                 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
109                 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
110                 grab_keycode_for_binding(conn, bind, code);
111                 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
112                 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
113 #else
114                 uint32_t last_keycode = 0;
115                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
116                 if (keycodes == NULL) {
117                         DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
118                         continue;
119                 }
120
121                 bind->number_keycodes = 0;
122
123                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
124                         /* We hope duplicate keycodes will be returned in order
125                          * and skip them */
126                         if (last_keycode == *walk)
127                                 continue;
128                         grab_keycode_for_binding(conn, bind, *walk);
129                         last_keycode = *walk;
130                         bind->number_keycodes++;
131                 }
132                 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
133                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
134                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
135                 free(keycodes);
136 #endif
137         }
138 }
139
140 /*
141  * Switches the key bindings to the given mode, if the mode exists
142  *
143  */
144 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
145         struct Mode *mode;
146
147         LOG("Switching to mode %s\n", new_mode);
148
149         SLIST_FOREACH(mode, &modes, modes) {
150                 if (strcasecmp(mode->name, new_mode) != 0)
151                         continue;
152
153                 ungrab_all_keys(conn);
154                 bindings = mode->bindings;
155                 grab_all_keys(conn);
156                 return;
157         }
158
159         ELOG("ERROR: Mode not found\n");
160 }
161
162 /*
163  * Get the path of the first configuration file found. Checks the XDG folders
164  * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
165  *
166  */
167 static char *get_config_path() {
168         /* 1: check for $XDG_CONFIG_HOME/i3/config */
169         char *xdg_config_home, *xdg_config_dirs, *config_path;
170
171         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
172                 xdg_config_home = "~/.config";
173
174         xdg_config_home = glob_path(xdg_config_home);
175         if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
176                 die("asprintf() failed");
177         free(xdg_config_home);
178
179         if (path_exists(config_path))
180                 return config_path;
181         free(config_path);
182
183         /* 2: check for $XDG_CONFIG_DIRS/i3/config */
184         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
185                 xdg_config_dirs = "/etc/xdg";
186
187         char *buf = strdup(xdg_config_dirs);
188         char *tok = strtok(buf, ":");
189         while (tok != NULL) {
190                 tok = glob_path(tok);
191                 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
192                         die("asprintf() failed");
193                 free(tok);
194                 if (path_exists(config_path)) {
195                         free(buf);
196                         return config_path;
197                 }
198                 free(config_path);
199                 tok = strtok(NULL, ":");
200         }
201         free(buf);
202
203         /* 3: check traditional paths */
204         config_path = glob_path("~/.i3/config");
205         if (path_exists(config_path))
206                 return config_path;
207
208         config_path = strdup("/etc/i3/config");
209         if (!path_exists(config_path))
210                 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
211                     "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
212                     "/etc/i3/config exist.");
213
214         return config_path;
215 }
216
217 /*
218  * Finds the configuration file to use (either the one specified by
219  * override_configpath), the user’s one or the system default) and calls
220  * parse_file().
221  *
222  */
223 static void parse_configuration(const char *override_configpath) {
224         if (override_configpath != NULL) {
225                 parse_file(override_configpath);
226                 return;
227         }
228
229         char *path = get_config_path();
230         DLOG("Parsing configfile %s\n", path);
231         parse_file(path);
232         free(path);
233 }
234
235 /*
236  * (Re-)loads the configuration file (sets useful defaults before).
237  *
238  */
239 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
240         if (reload) {
241                 /* First ungrab the keys */
242                 ungrab_all_keys(conn);
243
244                 struct Mode *mode;
245                 Binding *bind;
246                 while (!SLIST_EMPTY(&modes)) {
247                         mode = SLIST_FIRST(&modes);
248                         FREE(mode->name);
249
250                         /* Clear the old binding list */
251                         bindings = mode->bindings;
252                         while (!TAILQ_EMPTY(bindings)) {
253                                 bind = TAILQ_FIRST(bindings);
254                                 TAILQ_REMOVE(bindings, bind, bindings);
255                                 FREE(bind->translated_to);
256                                 FREE(bind->command);
257                                 FREE(bind);
258                         }
259                         FREE(bindings);
260                         SLIST_REMOVE(&modes, mode, Mode, modes);
261                 }
262
263                 struct Assignment *assign;
264                 while (!TAILQ_EMPTY(&assignments)) {
265                         assign = TAILQ_FIRST(&assignments);
266                         FREE(assign->windowclass_title);
267                         TAILQ_REMOVE(&assignments, assign, assignments);
268                         FREE(assign);
269                 }
270         }
271
272         SLIST_INIT(&modes);
273
274         struct Mode *default_mode = scalloc(sizeof(struct Mode));
275         default_mode->name = sstrdup("default");
276         default_mode->bindings = scalloc(sizeof(struct bindings_head));
277         TAILQ_INIT(default_mode->bindings);
278         SLIST_INSERT_HEAD(&modes, default_mode, modes);
279
280         bindings = default_mode->bindings;
281
282 #define REQUIRED_OPTION(name) \
283         if (config.name == NULL) \
284                 die("You did not specify required configuration option " #name "\n");
285
286         /* Clear the old config or initialize the data structure */
287         memset(&config, 0, sizeof(config));
288
289         /* Initialize default colors */
290 #define INIT_COLOR(x, cborder, cbackground, ctext) \
291         do { \
292                 x.border = get_colorpixel(conn, cborder); \
293                 x.background = get_colorpixel(conn, cbackground); \
294                 x.text = get_colorpixel(conn, ctext); \
295         } while (0)
296
297         INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
298         INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
299         INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
300         INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
301         INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
302         INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
303         INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
304
305         parse_configuration(override_configpath);
306
307         if (reload)
308                 grab_all_keys(conn);
309
310         REQUIRED_OPTION(font);
311
312         /* Set an empty name for every workspace which got no name */
313         Workspace *ws;
314         TAILQ_FOREACH(ws, workspaces, workspaces) {
315                 if (ws->name != NULL) {
316                         /* If the font was not specified when the workspace name
317                          * was loaded, we need to predict the text width now */
318                         if (ws->text_width == 0)
319                                 ws->text_width = predict_text_width(global_conn,
320                                                 config.font, ws->name, ws->name_len);
321                         continue;
322                 }
323
324                 workspace_set_name(ws, NULL);
325         }
326 }