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);
61 head = globbuf.gl_pathv[0];
62 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
63 strncpy(result, head, strlen(head));
64 strncat(result, tail, strlen(tail));
72 * Checks if the given path exists by calling stat().
75 bool path_exists(const char *path) {
77 return (stat(path, &buf) == 0);
81 * Ungrabs all keys, to be called before re-grabbing the keys because of a
82 * mapping_notify event or a configuration file reload
85 void ungrab_all_keys(xcb_connection_t *conn) {
86 DLOG("Ungrabbing all keys\n");
87 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
90 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
91 DLOG("Grabbing %d\n", keycode);
92 /* Grab the key in all combinations */
93 #define GRAB_KEY(modifier) \
95 xcb_grab_key(conn, 0, root, modifier, keycode, \
96 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
98 int mods = bind->mods;
99 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
100 mods &= ~BIND_MODE_SWITCH;
102 mods = XCB_MOD_MASK_ANY;
105 GRAB_KEY(mods | xcb_numlock_mask);
106 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
110 * Returns a pointer to the Binding with the specified modifiers and keycode
111 * or NULL if no such binding exists.
114 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
117 TAILQ_FOREACH(bind, bindings, bindings) {
118 /* First compare the modifiers */
119 if (bind->mods != modifiers)
122 /* If a symbol was specified by the user, we need to look in
123 * the array of translated keycodes for the event’s keycode */
124 if (bind->symbol != NULL) {
125 if (memmem(bind->translated_to,
126 bind->number_keycodes * sizeof(xcb_keycode_t),
127 &keycode, sizeof(xcb_keycode_t)) != NULL)
130 /* This case is easier: The user specified a keycode */
131 if (bind->keycode == keycode)
136 return (bind == TAILQ_END(bindings) ? NULL : bind);
140 * Translates keysymbols to keycodes for all bindings which use keysyms.
143 void translate_keysyms() {
145 TAILQ_FOREACH(bind, bindings, bindings) {
146 if (bind->keycode > 0)
149 /* We need to translate the symbol to a keycode */
150 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
151 if (keysym == NoSymbol) {
152 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
156 #ifdef OLD_XCB_KEYSYMS_API
157 bind->number_keycodes = 1;
158 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
159 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
160 grab_keycode_for_binding(global_conn, bind, code);
161 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
162 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
164 uint32_t last_keycode = 0;
165 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
166 if (keycodes == NULL) {
167 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
171 bind->number_keycodes = 0;
173 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
174 /* We hope duplicate keycodes will be returned in order
176 if (last_keycode == *walk)
178 last_keycode = *walk;
179 bind->number_keycodes++;
181 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
182 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
183 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
190 * Grab the bound keys (tell X to send us keypress events for those keycodes)
193 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
195 TAILQ_FOREACH(bind, bindings, bindings) {
196 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
197 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
200 /* The easy case: the user specified a keycode directly. */
201 if (bind->keycode > 0) {
202 grab_keycode_for_binding(conn, bind, bind->keycode);
206 xcb_keycode_t *walk = bind->translated_to;
207 for (int i = 0; i < bind->number_keycodes; i++)
208 grab_keycode_for_binding(conn, bind, *walk);
213 * Switches the key bindings to the given mode, if the mode exists
216 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
219 LOG("Switching to mode %s\n", new_mode);
221 SLIST_FOREACH(mode, &modes, modes) {
222 if (strcasecmp(mode->name, new_mode) != 0)
225 ungrab_all_keys(conn);
226 bindings = mode->bindings;
228 grab_all_keys(conn, false);
232 ELOG("ERROR: Mode not found\n");
236 * Get the path of the first configuration file found. Checks the XDG folders
237 * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
240 static char *get_config_path() {
241 /* 1: check for $XDG_CONFIG_HOME/i3/config */
242 char *xdg_config_home, *xdg_config_dirs, *config_path;
244 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
245 xdg_config_home = "~/.config";
247 xdg_config_home = resolve_tilde(xdg_config_home);
248 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
249 die("asprintf() failed");
250 free(xdg_config_home);
252 if (path_exists(config_path))
256 /* 2: check for $XDG_CONFIG_DIRS/i3/config */
257 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
258 xdg_config_dirs = "/etc/xdg";
260 char *buf = strdup(xdg_config_dirs);
261 char *tok = strtok(buf, ":");
262 while (tok != NULL) {
263 tok = resolve_tilde(tok);
264 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
265 die("asprintf() failed");
267 if (path_exists(config_path)) {
272 tok = strtok(NULL, ":");
276 /* 3: check traditional paths */
277 config_path = resolve_tilde("~/.i3/config");
278 if (path_exists(config_path))
281 config_path = strdup("/etc/i3/config");
282 if (!path_exists(config_path))
283 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
284 "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
285 "/etc/i3/config exist.");
291 * Finds the configuration file to use (either the one specified by
292 * override_configpath), the user’s one or the system default) and calls
296 static void parse_configuration(const char *override_configpath) {
297 if (override_configpath != NULL) {
298 parse_file(override_configpath);
302 char *path = get_config_path();
303 DLOG("Parsing configfile %s\n", path);
309 * (Re-)loads the configuration file (sets useful defaults before).
312 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
314 /* First ungrab the keys */
315 ungrab_all_keys(conn);
319 while (!SLIST_EMPTY(&modes)) {
320 mode = SLIST_FIRST(&modes);
323 /* Clear the old binding list */
324 bindings = mode->bindings;
325 while (!TAILQ_EMPTY(bindings)) {
326 bind = TAILQ_FIRST(bindings);
327 TAILQ_REMOVE(bindings, bind, bindings);
328 FREE(bind->translated_to);
333 SLIST_REMOVE(&modes, mode, Mode, modes);
336 struct Assignment *assign;
337 while (!TAILQ_EMPTY(&assignments)) {
338 assign = TAILQ_FIRST(&assignments);
339 FREE(assign->windowclass_title);
340 TAILQ_REMOVE(&assignments, assign, assignments);
344 /* Clear workspace names */
346 TAILQ_FOREACH(ws, workspaces, workspaces)
347 workspace_set_name(ws, NULL);
352 struct Mode *default_mode = scalloc(sizeof(struct Mode));
353 default_mode->name = sstrdup("default");
354 default_mode->bindings = scalloc(sizeof(struct bindings_head));
355 TAILQ_INIT(default_mode->bindings);
356 SLIST_INSERT_HEAD(&modes, default_mode, modes);
358 bindings = default_mode->bindings;
360 #define REQUIRED_OPTION(name) \
361 if (config.name == NULL) \
362 die("You did not specify required configuration option " #name "\n");
364 /* Clear the old config or initialize the data structure */
365 memset(&config, 0, sizeof(config));
367 /* Initialize default colors */
368 #define INIT_COLOR(x, cborder, cbackground, ctext) \
370 x.border = get_colorpixel(conn, cborder); \
371 x.background = get_colorpixel(conn, cbackground); \
372 x.text = get_colorpixel(conn, ctext); \
375 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
376 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
377 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
378 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
379 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
380 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
381 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
383 parse_configuration(override_configpath);
387 grab_all_keys(conn, false);
390 REQUIRED_OPTION(font);
392 /* Set an empty name for every workspace which got no name */
394 TAILQ_FOREACH(ws, workspaces, workspaces) {
395 if (ws->name != NULL) {
396 /* If the font was not specified when the workspace name
397 * was loaded, we need to predict the text width now */
398 if (ws->text_width == 0)
399 ws->text_width = predict_text_width(global_conn,
400 config.font, ws->name, ws->name_len);
404 workspace_set_name(ws, NULL);