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;
27 * This function resolves ~ in pathnames.
28 * It may resolve wildcards in the first part of the path, but if no match
29 * or multiple matches are found, it just returns a copy of path as given.
32 char *resolve_tilde(const char *path) {
33 static glob_t globbuf;
34 char *head, *tail, *result;
36 tail = strchr(path, '/');
37 head = strndup(path, tail ? tail - path : strlen(path));
39 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
41 /* no match, or many wildcard matches are bad */
42 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
43 result = sstrdup(path);
47 head = globbuf.gl_pathv[0];
48 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
49 strncpy(result, head, strlen(head));
50 strncat(result, tail, strlen(tail));
58 * Checks if the given path exists by calling stat().
61 bool path_exists(const char *path) {
63 return (stat(path, &buf) == 0);
67 * Ungrabs all keys, to be called before re-grabbing the keys because of a
68 * mapping_notify event or a configuration file reload
71 void ungrab_all_keys(xcb_connection_t *conn) {
72 DLOG("Ungrabbing all keys\n");
73 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
76 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
77 DLOG("Grabbing %d\n", keycode);
78 /* Grab the key in all combinations */
79 #define GRAB_KEY(modifier) \
81 xcb_grab_key(conn, 0, root, modifier, keycode, \
82 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
84 int mods = bind->mods;
85 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
86 mods &= ~BIND_MODE_SWITCH;
88 mods = XCB_MOD_MASK_ANY;
91 GRAB_KEY(mods | xcb_numlock_mask);
92 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
96 * Returns a pointer to the Binding with the specified modifiers and keycode
97 * or NULL if no such binding exists.
100 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
103 TAILQ_FOREACH(bind, bindings, bindings) {
104 /* First compare the modifiers */
105 if (bind->mods != modifiers)
108 /* If a symbol was specified by the user, we need to look in
109 * the array of translated keycodes for the event’s keycode */
110 if (bind->symbol != NULL) {
111 if (memmem(bind->translated_to,
112 bind->number_keycodes * sizeof(xcb_keycode_t),
113 &keycode, sizeof(xcb_keycode_t)) != NULL)
116 /* This case is easier: The user specified a keycode */
117 if (bind->keycode == keycode)
122 return (bind == TAILQ_END(bindings) ? NULL : bind);
126 * Translates keysymbols to keycodes for all bindings which use keysyms.
129 void translate_keysyms() {
131 TAILQ_FOREACH(bind, bindings, bindings) {
132 if (bind->keycode > 0)
135 /* We need to translate the symbol to a keycode */
136 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
137 if (keysym == NoSymbol) {
138 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
142 uint32_t last_keycode = 0;
143 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
144 if (keycodes == NULL) {
145 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
149 bind->number_keycodes = 0;
151 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
152 /* We hope duplicate keycodes will be returned in order
154 if (last_keycode == *walk)
156 last_keycode = *walk;
157 bind->number_keycodes++;
159 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
160 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
161 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
167 * Grab the bound keys (tell X to send us keypress events for those keycodes)
170 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
172 TAILQ_FOREACH(bind, bindings, bindings) {
173 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
174 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
177 /* The easy case: the user specified a keycode directly. */
178 if (bind->keycode > 0) {
179 grab_keycode_for_binding(conn, bind, bind->keycode);
183 xcb_keycode_t *walk = bind->translated_to;
184 for (int i = 0; i < bind->number_keycodes; i++)
185 grab_keycode_for_binding(conn, bind, *walk);
190 * Switches the key bindings to the given mode, if the mode exists
193 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
196 LOG("Switching to mode %s\n", new_mode);
198 SLIST_FOREACH(mode, &modes, modes) {
199 if (strcasecmp(mode->name, new_mode) != 0)
202 ungrab_all_keys(conn);
203 bindings = mode->bindings;
205 grab_all_keys(conn, false);
209 ELOG("ERROR: Mode not found\n");
213 * Get the path of the first configuration file found. Checks the home directory
214 * first, then the system directory first, always taking into account the XDG
215 * Base Directory Specification ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS)
218 static char *get_config_path() {
219 char *xdg_config_home, *xdg_config_dirs, *config_path;
221 /* 1: check the traditional path under the home directory */
222 config_path = resolve_tilde("~/.i3/config");
223 if (path_exists(config_path))
226 /* 2: check for $XDG_CONFIG_HOME/i3/config */
227 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
228 xdg_config_home = "~/.config";
230 xdg_config_home = resolve_tilde(xdg_config_home);
231 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
232 die("asprintf() failed");
233 free(xdg_config_home);
235 if (path_exists(config_path))
239 /* 3: check the traditional path under /etc */
240 config_path = SYSCONFDIR "/i3/config";
241 if (path_exists(config_path))
242 return sstrdup(config_path);
244 /* 4: check for $XDG_CONFIG_DIRS/i3/config */
245 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
246 xdg_config_dirs = "/etc/xdg";
248 char *buf = sstrdup(xdg_config_dirs);
249 char *tok = strtok(buf, ":");
250 while (tok != NULL) {
251 tok = resolve_tilde(tok);
252 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
253 die("asprintf() failed");
255 if (path_exists(config_path)) {
260 tok = strtok(NULL, ":");
264 die("Unable to find the configuration file (looked at "
265 "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
266 SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
270 * Finds the configuration file to use (either the one specified by
271 * override_configpath), the user’s one or the system default) and calls
275 static void parse_configuration(const char *override_configpath) {
276 if (override_configpath != NULL) {
277 parse_file(override_configpath);
281 char *path = get_config_path();
282 DLOG("Parsing configfile %s\n", path);
288 * (Re-)loads the configuration file (sets useful defaults before).
291 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
293 /* First ungrab the keys */
294 ungrab_all_keys(conn);
298 while (!SLIST_EMPTY(&modes)) {
299 mode = SLIST_FIRST(&modes);
302 /* Clear the old binding list */
303 bindings = mode->bindings;
304 while (!TAILQ_EMPTY(bindings)) {
305 bind = TAILQ_FIRST(bindings);
306 TAILQ_REMOVE(bindings, bind, bindings);
307 FREE(bind->translated_to);
312 SLIST_REMOVE(&modes, mode, Mode, modes);
316 struct Assignment *assign;
317 while (!TAILQ_EMPTY(&assignments)) {
318 assign = TAILQ_FIRST(&assignments);
319 FREE(assign->windowclass_title);
320 TAILQ_REMOVE(&assignments, assign, assignments);
325 /* Clear workspace names */
328 TAILQ_FOREACH(ws, workspaces, workspaces)
329 workspace_set_name(ws, NULL);
335 struct Mode *default_mode = scalloc(sizeof(struct Mode));
336 default_mode->name = sstrdup("default");
337 default_mode->bindings = scalloc(sizeof(struct bindings_head));
338 TAILQ_INIT(default_mode->bindings);
339 SLIST_INSERT_HEAD(&modes, default_mode, modes);
341 bindings = default_mode->bindings;
343 #define REQUIRED_OPTION(name) \
344 if (config.name == NULL) \
345 die("You did not specify required configuration option " #name "\n");
347 /* Clear the old config or initialize the data structure */
348 memset(&config, 0, sizeof(config));
350 /* Initialize default colors */
351 #define INIT_COLOR(x, cborder, cbackground, ctext) \
353 x.border = get_colorpixel(cborder); \
354 x.background = get_colorpixel(cbackground); \
355 x.text = get_colorpixel(ctext); \
358 config.client.background = get_colorpixel("#000000");
359 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
360 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
361 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
362 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
363 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
364 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
365 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
367 config.restart_state_path = "~/.i3/_restart.json";
368 config.default_border = BS_NORMAL;
370 parse_configuration(override_configpath);
374 grab_all_keys(conn, false);
377 REQUIRED_OPTION(font);
380 /* Set an empty name for every workspace which got no name */
382 TAILQ_FOREACH(ws, workspaces, workspaces) {
383 if (ws->name != NULL) {
384 /* If the font was not specified when the workspace name
385 * was loaded, we need to predict the text width now */
386 if (ws->text_width == 0)
387 ws->text_width = predict_text_width(global_conn,
388 config.font, ws->name, ws->name_len);
392 workspace_set_name(ws, NULL);