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>
23 /* We need Xlib for XStringToKeysym */
26 #include <xcb/xcb_keysyms.h>
33 #include "workspace.h"
37 struct modes_head modes;
40 * This function resolves ~ in pathnames.
41 * It may resolve wildcards in the first part of the path, but if no match
42 * or multiple matches are found, it just returns a copy of path as given.
45 char *resolve_tilde(const char *path) {
46 static glob_t globbuf;
47 char *head, *tail, *result;
49 tail = strchr(path, '/');
50 head = strndup(path, tail ? tail - path : strlen(path));
52 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
54 /* no match, or many wildcard matches are bad */
55 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
56 result = sstrdup(path);
60 head = globbuf.gl_pathv[0];
61 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
62 strncpy(result, head, strlen(head));
63 strncat(result, tail, strlen(tail));
71 * Checks if the given path exists by calling stat().
74 bool path_exists(const char *path) {
76 return (stat(path, &buf) == 0);
80 * Ungrabs all keys, to be called before re-grabbing the keys because of a
81 * mapping_notify event or a configuration file reload
84 void ungrab_all_keys(xcb_connection_t *conn) {
85 DLOG("Ungrabbing all keys\n");
86 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
89 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
90 DLOG("Grabbing %d\n", keycode);
91 /* Grab the key in all combinations */
92 #define GRAB_KEY(modifier) \
94 xcb_grab_key(conn, 0, root, modifier, keycode, \
95 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
97 int mods = bind->mods;
98 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
99 mods &= ~BIND_MODE_SWITCH;
101 mods = XCB_MOD_MASK_ANY;
104 GRAB_KEY(mods | xcb_numlock_mask);
105 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
109 * Returns a pointer to the Binding with the specified modifiers and keycode
110 * or NULL if no such binding exists.
113 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
116 TAILQ_FOREACH(bind, bindings, bindings) {
117 /* First compare the modifiers */
118 if (bind->mods != modifiers)
121 /* If a symbol was specified by the user, we need to look in
122 * the array of translated keycodes for the event’s keycode */
123 if (bind->symbol != NULL) {
124 if (memmem(bind->translated_to,
125 bind->number_keycodes * sizeof(xcb_keycode_t),
126 &keycode, sizeof(xcb_keycode_t)) != NULL)
129 /* This case is easier: The user specified a keycode */
130 if (bind->keycode == keycode)
135 return (bind == TAILQ_END(bindings) ? NULL : bind);
139 * Translates keysymbols to keycodes for all bindings which use keysyms.
142 void translate_keysyms() {
144 TAILQ_FOREACH(bind, bindings, bindings) {
145 if (bind->keycode > 0)
148 /* We need to translate the symbol to a keycode */
149 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
150 if (keysym == NoSymbol) {
151 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
155 #ifdef OLD_XCB_KEYSYMS_API
156 bind->number_keycodes = 1;
157 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
158 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
159 grab_keycode_for_binding(global_conn, bind, code);
160 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
161 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
163 uint32_t last_keycode = 0;
164 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
165 if (keycodes == NULL) {
166 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
170 bind->number_keycodes = 0;
172 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
173 /* We hope duplicate keycodes will be returned in order
175 if (last_keycode == *walk)
177 last_keycode = *walk;
178 bind->number_keycodes++;
180 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
181 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
182 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
189 * Grab the bound keys (tell X to send us keypress events for those keycodes)
192 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
194 TAILQ_FOREACH(bind, bindings, bindings) {
195 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
196 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
199 /* The easy case: the user specified a keycode directly. */
200 if (bind->keycode > 0) {
201 grab_keycode_for_binding(conn, bind, bind->keycode);
205 xcb_keycode_t *walk = bind->translated_to;
206 for (int i = 0; i < bind->number_keycodes; i++)
207 grab_keycode_for_binding(conn, bind, *walk);
212 * Switches the key bindings to the given mode, if the mode exists
215 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
218 LOG("Switching to mode %s\n", new_mode);
220 SLIST_FOREACH(mode, &modes, modes) {
221 if (strcasecmp(mode->name, new_mode) != 0)
224 ungrab_all_keys(conn);
225 bindings = mode->bindings;
227 grab_all_keys(conn, false);
231 ELOG("ERROR: Mode not found\n");
235 * Get the path of the first configuration file found. Checks the XDG folders
236 * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
239 static char *get_config_path() {
240 /* 1: check for $XDG_CONFIG_HOME/i3/config */
241 char *xdg_config_home, *xdg_config_dirs, *config_path;
243 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
244 xdg_config_home = "~/.config";
246 xdg_config_home = resolve_tilde(xdg_config_home);
247 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
248 die("asprintf() failed");
249 free(xdg_config_home);
251 if (path_exists(config_path))
255 /* 2: check for $XDG_CONFIG_DIRS/i3/config */
256 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
257 xdg_config_dirs = "/etc/xdg";
259 char *buf = strdup(xdg_config_dirs);
260 char *tok = strtok(buf, ":");
261 while (tok != NULL) {
262 tok = resolve_tilde(tok);
263 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
264 die("asprintf() failed");
266 if (path_exists(config_path)) {
271 tok = strtok(NULL, ":");
275 /* 3: check traditional paths */
276 config_path = resolve_tilde("~/.i3/config");
277 if (path_exists(config_path))
280 config_path = strdup("/etc/i3/config");
281 if (!path_exists(config_path))
282 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
283 "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
284 "/etc/i3/config exist.");
290 * Finds the configuration file to use (either the one specified by
291 * override_configpath), the user’s one or the system default) and calls
295 static void parse_configuration(const char *override_configpath) {
296 if (override_configpath != NULL) {
297 parse_file(override_configpath);
301 char *path = get_config_path();
302 DLOG("Parsing configfile %s\n", path);
308 * (Re-)loads the configuration file (sets useful defaults before).
311 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
313 /* First ungrab the keys */
314 ungrab_all_keys(conn);
318 while (!SLIST_EMPTY(&modes)) {
319 mode = SLIST_FIRST(&modes);
322 /* Clear the old binding list */
323 bindings = mode->bindings;
324 while (!TAILQ_EMPTY(bindings)) {
325 bind = TAILQ_FIRST(bindings);
326 TAILQ_REMOVE(bindings, bind, bindings);
327 FREE(bind->translated_to);
332 SLIST_REMOVE(&modes, mode, Mode, modes);
335 struct Assignment *assign;
336 while (!TAILQ_EMPTY(&assignments)) {
337 assign = TAILQ_FIRST(&assignments);
338 FREE(assign->windowclass_title);
339 TAILQ_REMOVE(&assignments, assign, assignments);
343 /* Clear workspace names */
345 TAILQ_FOREACH(ws, workspaces, workspaces)
346 workspace_set_name(ws, NULL);
351 struct Mode *default_mode = scalloc(sizeof(struct Mode));
352 default_mode->name = sstrdup("default");
353 default_mode->bindings = scalloc(sizeof(struct bindings_head));
354 TAILQ_INIT(default_mode->bindings);
355 SLIST_INSERT_HEAD(&modes, default_mode, modes);
357 bindings = default_mode->bindings;
359 #define REQUIRED_OPTION(name) \
360 if (config.name == NULL) \
361 die("You did not specify required configuration option " #name "\n");
363 /* Clear the old config or initialize the data structure */
364 memset(&config, 0, sizeof(config));
366 /* Initialize default colors */
367 #define INIT_COLOR(x, cborder, cbackground, ctext) \
369 x.border = get_colorpixel(conn, cborder); \
370 x.background = get_colorpixel(conn, cbackground); \
371 x.text = get_colorpixel(conn, ctext); \
374 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
375 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
376 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
377 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
378 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
379 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
380 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
382 parse_configuration(override_configpath);
386 grab_all_keys(conn, false);
389 REQUIRED_OPTION(font);
391 /* Set an empty name for every workspace which got no name */
393 TAILQ_FOREACH(ws, workspaces, workspaces) {
394 if (ws->name != NULL) {
395 /* If the font was not specified when the workspace name
396 * was loaded, we need to predict the text width now */
397 if (ws->text_width == 0)
398 ws->text_width = predict_text_width(global_conn,
399 config.font, ws->name, ws->name_len);
403 workspace_set_name(ws, NULL);