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.
43 static char *glob_path(const char *path) {
44 static glob_t globbuf;
45 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
47 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
53 * Checks if the given path exists by calling stat().
56 static 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 #ifdef OLD_XCB_KEYSYMS_API
138 bind->number_keycodes = 1;
139 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
140 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
141 grab_keycode_for_binding(global_conn, bind, code);
142 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
143 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
145 uint32_t last_keycode = 0;
146 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
147 if (keycodes == NULL) {
148 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
152 bind->number_keycodes = 0;
154 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
155 /* We hope duplicate keycodes will be returned in order
157 if (last_keycode == *walk)
159 last_keycode = *walk;
160 bind->number_keycodes++;
162 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
163 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
164 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
171 * Grab the bound keys (tell X to send us keypress events for those keycodes)
174 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
176 TAILQ_FOREACH(bind, bindings, bindings) {
177 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
178 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
181 /* The easy case: the user specified a keycode directly. */
182 if (bind->keycode > 0) {
183 grab_keycode_for_binding(conn, bind, bind->keycode);
187 xcb_keycode_t *walk = bind->translated_to;
188 for (int i = 0; i < bind->number_keycodes; i++)
189 grab_keycode_for_binding(conn, bind, *walk);
194 * Switches the key bindings to the given mode, if the mode exists
197 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
200 LOG("Switching to mode %s\n", new_mode);
202 SLIST_FOREACH(mode, &modes, modes) {
203 if (strcasecmp(mode->name, new_mode) != 0)
206 ungrab_all_keys(conn);
207 bindings = mode->bindings;
209 grab_all_keys(conn, false);
213 ELOG("ERROR: Mode not found\n");
217 * Get the path of the first configuration file found. Checks the XDG folders
218 * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
221 static char *get_config_path() {
222 /* 1: check for $XDG_CONFIG_HOME/i3/config */
223 char *xdg_config_home, *xdg_config_dirs, *config_path;
225 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
226 xdg_config_home = "~/.config";
228 xdg_config_home = glob_path(xdg_config_home);
229 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
230 die("asprintf() failed");
231 free(xdg_config_home);
233 if (path_exists(config_path))
237 /* 2: check for $XDG_CONFIG_DIRS/i3/config */
238 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
239 xdg_config_dirs = "/etc/xdg";
241 char *buf = strdup(xdg_config_dirs);
242 char *tok = strtok(buf, ":");
243 while (tok != NULL) {
244 tok = glob_path(tok);
245 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
246 die("asprintf() failed");
248 if (path_exists(config_path)) {
253 tok = strtok(NULL, ":");
257 /* 3: check traditional paths */
258 config_path = glob_path("~/.i3/config");
259 if (path_exists(config_path))
262 config_path = strdup("/etc/i3/config");
263 if (!path_exists(config_path))
264 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
265 "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
266 "/etc/i3/config exist.");
272 * Finds the configuration file to use (either the one specified by
273 * override_configpath), the user’s one or the system default) and calls
277 static void parse_configuration(const char *override_configpath) {
278 if (override_configpath != NULL) {
279 parse_file(override_configpath);
283 char *path = get_config_path();
284 DLOG("Parsing configfile %s\n", path);
290 * (Re-)loads the configuration file (sets useful defaults before).
293 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
295 /* First ungrab the keys */
296 ungrab_all_keys(conn);
300 while (!SLIST_EMPTY(&modes)) {
301 mode = SLIST_FIRST(&modes);
304 /* Clear the old binding list */
305 bindings = mode->bindings;
306 while (!TAILQ_EMPTY(bindings)) {
307 bind = TAILQ_FIRST(bindings);
308 TAILQ_REMOVE(bindings, bind, bindings);
309 FREE(bind->translated_to);
314 SLIST_REMOVE(&modes, mode, Mode, modes);
317 struct Assignment *assign;
318 while (!TAILQ_EMPTY(&assignments)) {
319 assign = TAILQ_FIRST(&assignments);
320 FREE(assign->windowclass_title);
321 TAILQ_REMOVE(&assignments, assign, assignments);
325 /* Clear workspace names */
327 TAILQ_FOREACH(ws, workspaces, workspaces)
328 workspace_set_name(ws, NULL);
333 struct Mode *default_mode = scalloc(sizeof(struct Mode));
334 default_mode->name = sstrdup("default");
335 default_mode->bindings = scalloc(sizeof(struct bindings_head));
336 TAILQ_INIT(default_mode->bindings);
337 SLIST_INSERT_HEAD(&modes, default_mode, modes);
339 bindings = default_mode->bindings;
341 #define REQUIRED_OPTION(name) \
342 if (config.name == NULL) \
343 die("You did not specify required configuration option " #name "\n");
345 /* Clear the old config or initialize the data structure */
346 memset(&config, 0, sizeof(config));
348 /* Initialize default colors */
349 #define INIT_COLOR(x, cborder, cbackground, ctext) \
351 x.border = get_colorpixel(conn, cborder); \
352 x.background = get_colorpixel(conn, cbackground); \
353 x.text = get_colorpixel(conn, ctext); \
356 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
357 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
358 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
359 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
360 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
361 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
362 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
364 parse_configuration(override_configpath);
368 grab_all_keys(conn, false);
371 REQUIRED_OPTION(font);
373 /* Set an empty name for every workspace which got no name */
375 TAILQ_FOREACH(ws, workspaces, workspaces) {
376 if (ws->name != NULL) {
377 /* If the font was not specified when the workspace name
378 * was loaded, we need to predict the text width now */
379 if (ws->text_width == 0)
380 ws->text_width = predict_text_width(global_conn,
381 config.font, ws->name, ws->name_len);
385 workspace_set_name(ws, NULL);