2 * vim:ts=4:sw=4:expandtab
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;
26 * Ungrabs all keys, to be called before re-grabbing the keys because of a
27 * mapping_notify event or a configuration file reload
30 void ungrab_all_keys(xcb_connection_t *conn) {
31 DLOG("Ungrabbing all keys\n");
32 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
35 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
36 DLOG("Grabbing %d\n", keycode);
37 /* Grab the key in all combinations */
38 #define GRAB_KEY(modifier) \
40 xcb_grab_key(conn, 0, root, modifier, keycode, \
41 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
43 int mods = bind->mods;
44 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
45 mods &= ~BIND_MODE_SWITCH;
47 mods = XCB_MOD_MASK_ANY;
50 GRAB_KEY(mods | xcb_numlock_mask);
51 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
55 * Returns a pointer to the Binding with the specified modifiers and keycode
56 * or NULL if no such binding exists.
59 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
62 TAILQ_FOREACH(bind, bindings, bindings) {
63 /* First compare the modifiers */
64 if (bind->mods != modifiers)
67 /* If a symbol was specified by the user, we need to look in
68 * the array of translated keycodes for the event’s keycode */
69 if (bind->symbol != NULL) {
70 if (memmem(bind->translated_to,
71 bind->number_keycodes * sizeof(xcb_keycode_t),
72 &keycode, sizeof(xcb_keycode_t)) != NULL)
75 /* This case is easier: The user specified a keycode */
76 if (bind->keycode == keycode)
81 return (bind == TAILQ_END(bindings) ? NULL : bind);
85 * Translates keysymbols to keycodes for all bindings which use keysyms.
88 void translate_keysyms() {
90 TAILQ_FOREACH(bind, bindings, bindings) {
91 if (bind->keycode > 0)
94 /* We need to translate the symbol to a keycode */
95 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
96 if (keysym == NoSymbol) {
97 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
101 uint32_t last_keycode = 0;
102 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
103 if (keycodes == NULL) {
104 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
108 bind->number_keycodes = 0;
110 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
111 /* We hope duplicate keycodes will be returned in order
113 if (last_keycode == *walk)
115 last_keycode = *walk;
116 bind->number_keycodes++;
118 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
119 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
120 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
126 * Grab the bound keys (tell X to send us keypress events for those keycodes)
129 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
131 TAILQ_FOREACH(bind, bindings, bindings) {
132 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
133 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
136 /* The easy case: the user specified a keycode directly. */
137 if (bind->keycode > 0) {
138 grab_keycode_for_binding(conn, bind, bind->keycode);
142 xcb_keycode_t *walk = bind->translated_to;
143 for (int i = 0; i < bind->number_keycodes; i++)
144 grab_keycode_for_binding(conn, bind, *walk);
149 * Switches the key bindings to the given mode, if the mode exists
152 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
155 LOG("Switching to mode %s\n", new_mode);
157 SLIST_FOREACH(mode, &modes, modes) {
158 if (strcasecmp(mode->name, new_mode) != 0)
161 ungrab_all_keys(conn);
162 bindings = mode->bindings;
164 grab_all_keys(conn, false);
168 ELOG("ERROR: Mode not found\n");
172 * Get the path of the first configuration file found. Checks the home directory
173 * first, then the system directory first, always taking into account the XDG
174 * Base Directory Specification ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS)
177 static char *get_config_path() {
178 char *xdg_config_home, *xdg_config_dirs, *config_path;
180 /* 1: check the traditional path under the home directory */
181 config_path = resolve_tilde("~/.i3/config");
182 if (path_exists(config_path))
185 /* 2: check for $XDG_CONFIG_HOME/i3/config */
186 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
187 xdg_config_home = "~/.config";
189 xdg_config_home = resolve_tilde(xdg_config_home);
190 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
191 die("asprintf() failed");
192 free(xdg_config_home);
194 if (path_exists(config_path))
198 /* 3: check the traditional path under /etc */
199 config_path = SYSCONFDIR "/i3/config";
200 if (path_exists(config_path))
201 return sstrdup(config_path);
203 /* 4: check for $XDG_CONFIG_DIRS/i3/config */
204 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
205 xdg_config_dirs = "/etc/xdg";
207 char *buf = sstrdup(xdg_config_dirs);
208 char *tok = strtok(buf, ":");
209 while (tok != NULL) {
210 tok = resolve_tilde(tok);
211 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
212 die("asprintf() failed");
214 if (path_exists(config_path)) {
219 tok = strtok(NULL, ":");
223 die("Unable to find the configuration file (looked at "
224 "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
225 SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
229 * Finds the configuration file to use (either the one specified by
230 * override_configpath), the user’s one or the system default) and calls
234 static void parse_configuration(const char *override_configpath) {
235 static const char *saved_configpath = NULL;
237 if (override_configpath != NULL) {
238 saved_configpath = override_configpath;
239 parse_file(override_configpath);
242 else if (saved_configpath != NULL) {
243 parse_file(saved_configpath);
247 char *path = get_config_path();
248 DLOG("Parsing configfile %s\n", path);
254 * (Re-)loads the configuration file (sets useful defaults before).
257 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
259 /* First ungrab the keys */
260 ungrab_all_keys(conn);
264 while (!SLIST_EMPTY(&modes)) {
265 mode = SLIST_FIRST(&modes);
268 /* Clear the old binding list */
269 bindings = mode->bindings;
270 while (!TAILQ_EMPTY(bindings)) {
271 bind = TAILQ_FIRST(bindings);
272 TAILQ_REMOVE(bindings, bind, bindings);
273 FREE(bind->translated_to);
278 SLIST_REMOVE(&modes, mode, Mode, modes);
282 struct Assignment *assign;
283 while (!TAILQ_EMPTY(&assignments)) {
284 assign = TAILQ_FIRST(&assignments);
285 FREE(assign->windowclass_title);
286 TAILQ_REMOVE(&assignments, assign, assignments);
291 /* Clear workspace names */
294 TAILQ_FOREACH(ws, workspaces, workspaces)
295 workspace_set_name(ws, NULL);
301 struct Mode *default_mode = scalloc(sizeof(struct Mode));
302 default_mode->name = sstrdup("default");
303 default_mode->bindings = scalloc(sizeof(struct bindings_head));
304 TAILQ_INIT(default_mode->bindings);
305 SLIST_INSERT_HEAD(&modes, default_mode, modes);
307 bindings = default_mode->bindings;
309 #define REQUIRED_OPTION(name) \
310 if (config.name == NULL) \
311 die("You did not specify required configuration option " #name "\n");
313 /* Clear the old config or initialize the data structure */
314 memset(&config, 0, sizeof(config));
316 /* Initialize default colors */
317 #define INIT_COLOR(x, cborder, cbackground, ctext) \
319 x.border = get_colorpixel(cborder); \
320 x.background = get_colorpixel(cbackground); \
321 x.text = get_colorpixel(ctext); \
324 config.client.background = get_colorpixel("#000000");
325 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
326 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
327 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
328 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
329 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
330 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
331 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
333 config.default_border = BS_NORMAL;
334 /* Set default_orientation to NO_ORIENTATION for auto orientation. */
335 config.default_orientation = NO_ORIENTATION;
337 parse_configuration(override_configpath);
341 grab_all_keys(conn, false);
344 if (config.font.id == 0) {
345 ELOG("You did not specify required configuration option \"font\"\n");
346 config.font = load_font("fixed", true);
350 /* Set an empty name for every workspace which got no name */
352 TAILQ_FOREACH(ws, workspaces, workspaces) {
353 if (ws->name != NULL) {
354 /* If the font was not specified when the workspace name
355 * was loaded, we need to predict the text width now */
356 if (ws->text_width == 0)
357 ws->text_width = predict_text_width(global_conn,
358 config.font, ws->name, ws->name_len);
362 workspace_set_name(ws, NULL);