4 * i3 - an improved dynamic tiling window manager
6 * © 2009 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 if ((bind->mods & BIND_MODE_SWITCH) != 0)
74 xcb_grab_key(conn, 0, root, 0, keycode,
75 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
77 /* Grab the key in all combinations */
78 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
79 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
81 GRAB_KEY(bind->mods | xcb_numlock_mask);
82 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
87 * Grab the bound keys (tell X to send us keypress events for those keycodes)
90 void grab_all_keys(xcb_connection_t *conn) {
92 TAILQ_FOREACH(bind, bindings, bindings) {
93 /* The easy case: the user specified a keycode directly. */
94 if (bind->keycode > 0) {
95 grab_keycode_for_binding(conn, bind, bind->keycode);
99 /* We need to translate the symbol to a keycode */
100 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
101 if (keysym == NoSymbol) {
102 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
106 #ifdef OLD_XCB_KEYSYMS_API
107 bind->number_keycodes = 1;
108 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
109 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
110 grab_keycode_for_binding(conn, bind, code);
111 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
112 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
114 uint32_t last_keycode = 0;
115 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
116 if (keycodes == NULL) {
117 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
121 bind->number_keycodes = 0;
123 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
124 /* We hope duplicate keycodes will be returned in order
126 if (last_keycode == *walk)
128 grab_keycode_for_binding(conn, bind, *walk);
129 last_keycode = *walk;
130 bind->number_keycodes++;
132 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
133 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
134 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
141 * Switches the key bindings to the given mode, if the mode exists
144 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
147 LOG("Switching to mode %s\n", new_mode);
149 SLIST_FOREACH(mode, &modes, modes) {
150 if (strcasecmp(mode->name, new_mode) != 0)
153 ungrab_all_keys(conn);
154 bindings = mode->bindings;
159 ELOG("ERROR: Mode not found\n");
163 * Get the path of the first configuration file found. Checks the XDG folders
164 * first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths.
167 static char *get_config_path() {
168 /* 1: check for $XDG_CONFIG_HOME/i3/config */
169 char *xdg_config_home, *xdg_config_dirs, *config_path;
171 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
172 xdg_config_home = "~/.config";
174 xdg_config_home = glob_path(xdg_config_home);
175 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
176 die("asprintf() failed");
177 free(xdg_config_home);
179 if (path_exists(config_path))
183 /* 2: check for $XDG_CONFIG_DIRS/i3/config */
184 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
185 xdg_config_dirs = "/etc/xdg";
187 char *buf = strdup(xdg_config_dirs);
188 char *tok = strtok(buf, ":");
189 while (tok != NULL) {
190 tok = glob_path(tok);
191 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
192 die("asprintf() failed");
194 if (path_exists(config_path)) {
199 tok = strtok(NULL, ":");
203 /* 3: check traditional paths */
204 config_path = glob_path("~/.i3/config");
205 if (path_exists(config_path))
208 config_path = strdup("/etc/i3/config");
209 if (!path_exists(config_path))
210 die("Neither $XDG_CONFIG_HOME/i3/config, nor "
211 "$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
212 "/etc/i3/config exist.");
218 * Finds the configuration file to use (either the one specified by
219 * override_configpath), the user’s one or the system default) and calls
223 static void parse_configuration(const char *override_configpath) {
224 if (override_configpath != NULL) {
225 parse_file(override_configpath);
229 char *path = get_config_path();
230 DLOG("Parsing configfile %s\n", path);
236 * (Re-)loads the configuration file (sets useful defaults before).
239 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
241 /* First ungrab the keys */
242 ungrab_all_keys(conn);
246 while (!SLIST_EMPTY(&modes)) {
247 mode = SLIST_FIRST(&modes);
250 /* Clear the old binding list */
251 bindings = mode->bindings;
252 while (!TAILQ_EMPTY(bindings)) {
253 bind = TAILQ_FIRST(bindings);
254 TAILQ_REMOVE(bindings, bind, bindings);
255 FREE(bind->translated_to);
260 SLIST_REMOVE(&modes, mode, Mode, modes);
263 struct Assignment *assign;
264 while (!TAILQ_EMPTY(&assignments)) {
265 assign = TAILQ_FIRST(&assignments);
266 FREE(assign->windowclass_title);
267 TAILQ_REMOVE(&assignments, assign, assignments);
274 struct Mode *default_mode = scalloc(sizeof(struct Mode));
275 default_mode->name = sstrdup("default");
276 default_mode->bindings = scalloc(sizeof(struct bindings_head));
277 TAILQ_INIT(default_mode->bindings);
278 SLIST_INSERT_HEAD(&modes, default_mode, modes);
280 bindings = default_mode->bindings;
282 #define REQUIRED_OPTION(name) \
283 if (config.name == NULL) \
284 die("You did not specify required configuration option " #name "\n");
286 /* Clear the old config or initialize the data structure */
287 memset(&config, 0, sizeof(config));
289 /* Initialize default colors */
290 #define INIT_COLOR(x, cborder, cbackground, ctext) \
292 x.border = get_colorpixel(conn, cborder); \
293 x.background = get_colorpixel(conn, cbackground); \
294 x.text = get_colorpixel(conn, ctext); \
297 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
298 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
299 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
300 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
301 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
302 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
303 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
305 parse_configuration(override_configpath);
310 REQUIRED_OPTION(font);
312 /* Set an empty name for every workspace which got no name */
314 TAILQ_FOREACH(ws, workspaces, workspaces) {
315 if (ws->name != NULL) {
316 /* If the font was not specified when the workspace name
317 * was loaded, we need to predict the text width now */
318 if (ws->text_width == 0)
319 ws->text_width = predict_text_width(global_conn,
320 config.font, ws->name, ws->name_len);
324 workspace_set_name(ws, NULL);