2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * config.c: Configuration file (calling the parser (src/cfgparse.y) with the
8 * correct path, switching key bindings mode).
13 /* We need Xlib for XStringToKeysym */
16 char *current_configpath = NULL;
18 struct modes_head modes;
19 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
22 * Ungrabs all keys, to be called before re-grabbing the keys because of a
23 * mapping_notify event or a configuration file reload
26 void ungrab_all_keys(xcb_connection_t *conn) {
27 DLOG("Ungrabbing all keys\n");
28 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
31 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
32 DLOG("Grabbing %d\n", keycode);
33 /* Grab the key in all combinations */
34 #define GRAB_KEY(modifier) \
36 xcb_grab_key(conn, 0, root, modifier, keycode, \
37 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
39 int mods = bind->mods;
40 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
41 mods &= ~BIND_MODE_SWITCH;
43 mods = XCB_MOD_MASK_ANY;
46 GRAB_KEY(mods | xcb_numlock_mask);
47 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
51 * Returns a pointer to the Binding with the specified modifiers and keycode
52 * or NULL if no such binding exists.
55 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
58 TAILQ_FOREACH(bind, bindings, bindings) {
59 /* First compare the modifiers */
60 if (bind->mods != modifiers)
63 /* If a symbol was specified by the user, we need to look in
64 * the array of translated keycodes for the event’s keycode */
65 if (bind->symbol != NULL) {
66 if (memmem(bind->translated_to,
67 bind->number_keycodes * sizeof(xcb_keycode_t),
68 &keycode, sizeof(xcb_keycode_t)) != NULL)
71 /* This case is easier: The user specified a keycode */
72 if (bind->keycode == keycode)
77 return (bind == TAILQ_END(bindings) ? NULL : bind);
81 * Translates keysymbols to keycodes for all bindings which use keysyms.
84 void translate_keysyms() {
86 TAILQ_FOREACH(bind, bindings, bindings) {
87 if (bind->keycode > 0)
90 /* We need to translate the symbol to a keycode */
91 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
92 if (keysym == NoSymbol) {
93 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
97 uint32_t last_keycode = 0;
98 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
99 if (keycodes == NULL) {
100 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
104 bind->number_keycodes = 0;
106 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
107 /* We hope duplicate keycodes will be returned in order
109 if (last_keycode == *walk)
111 last_keycode = *walk;
112 bind->number_keycodes++;
114 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
115 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
116 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
122 * Grab the bound keys (tell X to send us keypress events for those keycodes)
125 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
127 TAILQ_FOREACH(bind, bindings, bindings) {
128 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
129 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
132 /* The easy case: the user specified a keycode directly. */
133 if (bind->keycode > 0) {
134 grab_keycode_for_binding(conn, bind, bind->keycode);
138 xcb_keycode_t *walk = bind->translated_to;
139 for (int i = 0; i < bind->number_keycodes; i++)
140 grab_keycode_for_binding(conn, bind, *walk++);
145 * Switches the key bindings to the given mode, if the mode exists
148 void switch_mode(const char *new_mode) {
151 LOG("Switching to mode %s\n", new_mode);
153 SLIST_FOREACH(mode, &modes, modes) {
154 if (strcasecmp(mode->name, new_mode) != 0)
157 ungrab_all_keys(conn);
158 bindings = mode->bindings;
160 grab_all_keys(conn, false);
164 ELOG("ERROR: Mode not found\n");
168 * Get the path of the first configuration file found. If override_configpath
169 * is specified, that path is returned and saved for further calls. Otherwise,
170 * checks the home directory first, then the system directory first, always
171 * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
175 static char *get_config_path(const char *override_configpath) {
176 char *xdg_config_home, *xdg_config_dirs, *config_path;
178 static const char *saved_configpath = NULL;
180 if (override_configpath != NULL) {
181 saved_configpath = override_configpath;
182 return sstrdup(saved_configpath);
185 if (saved_configpath != NULL)
186 return sstrdup(saved_configpath);
188 /* 1: check the traditional path under the home directory */
189 config_path = resolve_tilde("~/.i3/config");
190 if (path_exists(config_path))
194 /* 2: check for $XDG_CONFIG_HOME/i3/config */
195 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
196 xdg_config_home = "~/.config";
198 xdg_config_home = resolve_tilde(xdg_config_home);
199 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
200 free(xdg_config_home);
202 if (path_exists(config_path))
206 /* 3: check the traditional path under /etc */
207 config_path = SYSCONFDIR "/i3/config";
208 if (path_exists(config_path))
209 return sstrdup(config_path);
211 /* 4: check for $XDG_CONFIG_DIRS/i3/config */
212 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
213 xdg_config_dirs = "/etc/xdg";
215 char *buf = sstrdup(xdg_config_dirs);
216 char *tok = strtok(buf, ":");
217 while (tok != NULL) {
218 tok = resolve_tilde(tok);
219 sasprintf(&config_path, "%s/i3/config", tok);
221 if (path_exists(config_path)) {
226 tok = strtok(NULL, ":");
230 die("Unable to find the configuration file (looked at "
231 "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
232 SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
236 * Finds the configuration file to use (either the one specified by
237 * override_configpath), the user’s one or the system default) and calls
241 static void parse_configuration(const char *override_configpath) {
242 char *path = get_config_path(override_configpath);
243 DLOG("Parsing configfile %s\n", path);
244 FREE(current_configpath);
245 current_configpath = path;
250 * (Re-)loads the configuration file (sets useful defaults before).
253 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
255 /* First ungrab the keys */
256 ungrab_all_keys(conn);
260 while (!SLIST_EMPTY(&modes)) {
261 mode = SLIST_FIRST(&modes);
264 /* Clear the old binding list */
265 bindings = mode->bindings;
266 while (!TAILQ_EMPTY(bindings)) {
267 bind = TAILQ_FIRST(bindings);
268 TAILQ_REMOVE(bindings, bind, bindings);
269 FREE(bind->translated_to);
274 SLIST_REMOVE(&modes, mode, Mode, modes);
277 struct Assignment *assign;
278 while (!TAILQ_EMPTY(&assignments)) {
279 assign = TAILQ_FIRST(&assignments);
280 if (assign->type == A_TO_WORKSPACE)
281 FREE(assign->dest.workspace);
282 else if (assign->type == A_TO_OUTPUT)
283 FREE(assign->dest.output);
284 else if (assign->type == A_COMMAND)
285 FREE(assign->dest.command);
286 match_free(&(assign->match));
287 TAILQ_REMOVE(&assignments, assign, assignments);
291 /* Clear bar configs */
292 Barconfig *barconfig;
293 while (!TAILQ_EMPTY(&barconfigs)) {
294 barconfig = TAILQ_FIRST(&barconfigs);
296 for (int c = 0; c < barconfig->num_outputs; c++)
297 free(barconfig->outputs[c]);
298 FREE(barconfig->outputs);
299 FREE(barconfig->tray_output);
300 FREE(barconfig->socket_path);
301 FREE(barconfig->status_command);
302 FREE(barconfig->font);
303 FREE(barconfig->colors.background);
304 FREE(barconfig->colors.statusline);
305 FREE(barconfig->colors.focused_workspace_text);
306 FREE(barconfig->colors.focused_workspace_bg);
307 FREE(barconfig->colors.active_workspace_text);
308 FREE(barconfig->colors.active_workspace_bg);
309 FREE(barconfig->colors.inactive_workspace_text);
310 FREE(barconfig->colors.inactive_workspace_bg);
311 FREE(barconfig->colors.urgent_workspace_text);
312 FREE(barconfig->colors.urgent_workspace_bg);
313 TAILQ_REMOVE(&barconfigs, barconfig, configs);
317 /* Clear workspace names */
320 TAILQ_FOREACH(ws, workspaces, workspaces)
321 workspace_set_name(ws, NULL);
327 struct Mode *default_mode = scalloc(sizeof(struct Mode));
328 default_mode->name = sstrdup("default");
329 default_mode->bindings = scalloc(sizeof(struct bindings_head));
330 TAILQ_INIT(default_mode->bindings);
331 SLIST_INSERT_HEAD(&modes, default_mode, modes);
333 bindings = default_mode->bindings;
335 #define REQUIRED_OPTION(name) \
336 if (config.name == NULL) \
337 die("You did not specify required configuration option " #name "\n");
339 /* Clear the old config or initialize the data structure */
340 memset(&config, 0, sizeof(config));
342 /* Initialize default colors */
343 #define INIT_COLOR(x, cborder, cbackground, ctext) \
345 x.border = get_colorpixel(cborder); \
346 x.background = get_colorpixel(cbackground); \
347 x.text = get_colorpixel(ctext); \
350 config.client.background = get_colorpixel("#000000");
351 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
352 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
353 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
354 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
355 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
356 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
357 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
359 config.default_border = BS_NORMAL;
360 config.default_floating_border = BS_NORMAL;
361 /* Set default_orientation to NO_ORIENTATION for auto orientation. */
362 config.default_orientation = NO_ORIENTATION;
364 parse_configuration(override_configpath);
368 grab_all_keys(conn, false);
371 if (config.font.id == 0) {
372 ELOG("You did not specify required configuration option \"font\"\n");
373 config.font = load_font("fixed", true);
377 /* Set an empty name for every workspace which got no name */
379 TAILQ_FOREACH(ws, workspaces, workspaces) {
380 if (ws->name != NULL) {
381 /* If the font was not specified when the workspace name
382 * was loaded, we need to predict the text width now */
383 if (ws->text_width == 0)
384 ws->text_width = predict_text_width(global_conn,
385 config.font, ws->name, ws->name_len);
389 workspace_set_name(ws, NULL);