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