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
16 /* We need Xlib for XStringToKeysym */
23 struct modes_head modes;
26 * This function resolves ~ in pathnames.
29 char *glob_path(const char *path) {
30 static glob_t globbuf;
31 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
33 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
36 /* If the file does not exist yet, we still may need to resolve tilde,
38 if (strcmp(result, path) == 0) {
40 wordexp(path, &we, WRDE_NOCMD);
41 if (we.we_wordc > 0) {
43 result = sstrdup(we.we_wordv[0]);
53 * Checks if the given path exists by calling stat().
56 bool path_exists(const char *path) {
58 return (stat(path, &buf) == 0);
62 * Ungrabs all keys, to be called before re-grabbing the keys because of a
63 * mapping_notify event or a configuration file reload
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);
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) \
76 xcb_grab_key(conn, 0, root, modifier, keycode, \
77 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
79 int mods = bind->mods;
80 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
81 mods &= ~BIND_MODE_SWITCH;
83 mods = XCB_MOD_MASK_ANY;
86 GRAB_KEY(mods | xcb_numlock_mask);
87 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
91 * Returns a pointer to the Binding with the specified modifiers and keycode
92 * or NULL if no such binding exists.
95 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
98 TAILQ_FOREACH(bind, bindings, bindings) {
99 /* First compare the modifiers */
100 if (bind->mods != modifiers)
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)
111 /* This case is easier: The user specified a keycode */
112 if (bind->keycode == keycode)
117 return (bind == TAILQ_END(bindings) ? NULL : bind);
121 * Translates keysymbols to keycodes for all bindings which use keysyms.
124 void translate_keysyms() {
126 TAILQ_FOREACH(bind, bindings, bindings) {
127 if (bind->keycode > 0)
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);
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);
144 bind->number_keycodes = 0;
146 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
147 /* We hope duplicate keycodes will be returned in order
149 if (last_keycode == *walk)
151 last_keycode = *walk;
152 bind->number_keycodes++;
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));
162 * Grab the bound keys (tell X to send us keypress events for those keycodes)
165 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
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))
172 /* The easy case: the user specified a keycode directly. */
173 if (bind->keycode > 0) {
174 grab_keycode_for_binding(conn, bind, bind->keycode);
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);
185 * Switches the key bindings to the given mode, if the mode exists
188 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
191 LOG("Switching to mode %s\n", new_mode);
193 SLIST_FOREACH(mode, &modes, modes) {
194 if (strcasecmp(mode->name, new_mode) != 0)
197 ungrab_all_keys(conn);
198 bindings = mode->bindings;
200 grab_all_keys(conn, false);
204 ELOG("ERROR: Mode not found\n");
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.
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;
216 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
217 xdg_config_home = "~/.config";
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);
224 if (path_exists(config_path))
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";
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");
239 if (path_exists(config_path)) {
244 tok = strtok(NULL, ":");
248 /* 3: check traditional paths */
249 config_path = glob_path("~/.i3/config");
250 if (path_exists(config_path))
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.");
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
268 static void parse_configuration(const char *override_configpath) {
269 if (override_configpath != NULL) {
270 parse_file(override_configpath);
274 char *path = get_config_path();
275 DLOG("Parsing configfile %s\n", path);
281 * (Re-)loads the configuration file (sets useful defaults before).
284 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
286 /* First ungrab the keys */
287 ungrab_all_keys(conn);
291 while (!SLIST_EMPTY(&modes)) {
292 mode = SLIST_FIRST(&modes);
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);
305 SLIST_REMOVE(&modes, mode, Mode, modes);
309 struct Assignment *assign;
310 while (!TAILQ_EMPTY(&assignments)) {
311 assign = TAILQ_FIRST(&assignments);
312 FREE(assign->windowclass_title);
313 TAILQ_REMOVE(&assignments, assign, assignments);
318 /* Clear workspace names */
321 TAILQ_FOREACH(ws, workspaces, workspaces)
322 workspace_set_name(ws, NULL);
328 struct Mode *default_mode = scalloc(sizeof(struct Mode));
329 default_mode->name = sstrdup("default");
330 default_mode->bindings = scalloc(sizeof(struct bindings_head));
331 TAILQ_INIT(default_mode->bindings);
332 SLIST_INSERT_HEAD(&modes, default_mode, modes);
334 bindings = default_mode->bindings;
336 #define REQUIRED_OPTION(name) \
337 if (config.name == NULL) \
338 die("You did not specify required configuration option " #name "\n");
340 /* Clear the old config or initialize the data structure */
341 memset(&config, 0, sizeof(config));
343 /* Initialize default colors */
344 #define INIT_COLOR(x, cborder, cbackground, ctext) \
346 x.border = get_colorpixel(cborder); \
347 x.background = get_colorpixel(cbackground); \
348 x.text = get_colorpixel(ctext); \
351 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
352 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
353 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
354 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
355 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
356 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
357 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
359 parse_configuration(override_configpath);
363 grab_all_keys(conn, false);
366 REQUIRED_OPTION(font);
369 /* Set an empty name for every workspace which got no name */
371 TAILQ_FOREACH(ws, workspaces, workspaces) {
372 if (ws->name != NULL) {
373 /* If the font was not specified when the workspace name
374 * was loaded, we need to predict the text width now */
375 if (ws->text_width == 0)
376 ws->text_width = predict_text_width(global_conn,
377 config.font, ws->name, ws->name_len);
381 workspace_set_name(ws, NULL);