4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
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
18 #include <sys/types.h>
24 /* We need Xlib for XStringToKeysym */
27 #include <xcb/xcb_keysyms.h>
34 #include "workspace.h"
38 struct modes_head modes;
41 * This function resolves ~ in pathnames.
44 char *glob_path(const char *path) {
45 static glob_t globbuf;
46 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
48 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
51 /* If the file does not exist yet, we still may need to resolve tilde,
53 if (strcmp(result, path) == 0) {
55 wordexp(path, &we, WRDE_NOCMD);
56 if (we.we_wordc > 0) {
58 result = sstrdup(we.we_wordv[0]);
67 * Checks if the given path exists by calling stat().
70 bool path_exists(const char *path) {
72 return (stat(path, &buf) == 0);
76 * Ungrabs all keys, to be called before re-grabbing the keys because of a
77 * mapping_notify event or a configuration file reload
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);
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) \
90 xcb_grab_key(conn, 0, root, modifier, keycode, \
91 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
93 int mods = bind->mods;
94 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
95 mods &= ~BIND_MODE_SWITCH;
97 mods = XCB_MOD_MASK_ANY;
100 GRAB_KEY(mods | xcb_numlock_mask);
101 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
105 * Returns a pointer to the Binding with the specified modifiers and keycode
106 * or NULL if no such binding exists.
109 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
112 TAILQ_FOREACH(bind, bindings, bindings) {
113 /* First compare the modifiers */
114 if (bind->mods != modifiers)
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)
125 /* This case is easier: The user specified a keycode */
126 if (bind->keycode == keycode)
131 return (bind == TAILQ_END(bindings) ? NULL : bind);
135 * Translates keysymbols to keycodes for all bindings which use keysyms.
138 void translate_keysyms() {
140 TAILQ_FOREACH(bind, bindings, bindings) {
141 if (bind->keycode > 0)
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);
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));
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);
166 bind->number_keycodes = 0;
168 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
169 /* We hope duplicate keycodes will be returned in order
171 if (last_keycode == *walk)
173 last_keycode = *walk;
174 bind->number_keycodes++;
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));
185 * Grab the bound keys (tell X to send us keypress events for those keycodes)
188 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
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))
195 /* The easy case: the user specified a keycode directly. */
196 if (bind->keycode > 0) {
197 grab_keycode_for_binding(conn, bind, bind->keycode);
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);
208 * Switches the key bindings to the given mode, if the mode exists
211 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
214 LOG("Switching to mode %s\n", new_mode);
216 SLIST_FOREACH(mode, &modes, modes) {
217 if (strcasecmp(mode->name, new_mode) != 0)
220 ungrab_all_keys(conn);
221 bindings = mode->bindings;
223 grab_all_keys(conn, false);
227 ELOG("ERROR: Mode not found\n");
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.
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;
239 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
240 xdg_config_home = "~/.config";
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);
247 if (path_exists(config_path))
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";
255 char *buf = strdup(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");
262 if (path_exists(config_path)) {
267 tok = strtok(NULL, ":");
271 /* 3: check traditional paths */
272 config_path = glob_path("~/.i3/config");
273 if (path_exists(config_path))
276 config_path = strdup("/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.");
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
291 static void parse_configuration(const char *override_configpath) {
292 if (override_configpath != NULL) {
293 parse_file(override_configpath);
297 char *path = get_config_path();
298 DLOG("Parsing configfile %s\n", path);
304 * (Re-)loads the configuration file (sets useful defaults before).
307 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
309 /* First ungrab the keys */
310 ungrab_all_keys(conn);
314 while (!SLIST_EMPTY(&modes)) {
315 mode = SLIST_FIRST(&modes);
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);
328 SLIST_REMOVE(&modes, mode, Mode, modes);
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);
339 /* Clear workspace names */
341 TAILQ_FOREACH(ws, workspaces, workspaces)
342 workspace_set_name(ws, NULL);
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);
353 bindings = default_mode->bindings;
355 #define REQUIRED_OPTION(name) \
356 if (config.name == NULL) \
357 die("You did not specify required configuration option " #name "\n");
359 /* Clear the old config or initialize the data structure */
360 memset(&config, 0, sizeof(config));
362 /* Initialize default colors */
363 #define INIT_COLOR(x, cborder, cbackground, ctext) \
365 x.border = get_colorpixel(conn, cborder); \
366 x.background = get_colorpixel(conn, cbackground); \
367 x.text = get_colorpixel(conn, ctext); \
370 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
371 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
372 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
373 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
374 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
375 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
376 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
378 parse_configuration(override_configpath);
382 grab_all_keys(conn, false);
385 REQUIRED_OPTION(font);
387 /* Set an empty name for every workspace which got no name */
389 TAILQ_FOREACH(ws, workspaces, workspaces) {
390 if (ws->name != NULL) {
391 /* If the font was not specified when the workspace name
392 * was loaded, we need to predict the text width now */
393 if (ws->text_width == 0)
394 ws->text_width = predict_text_width(global_conn,
395 config.font, ws->name, ws->name_len);
399 workspace_set_name(ws, NULL);