2 #define I3__FILE__ "config.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
9 * config.c: Configuration file (calling the parser (src/config_parser.c) with
10 * the correct path, switching key bindings mode).
15 /* We need Xlib for XStringToKeysym */
18 char *current_configpath = NULL;
20 struct modes_head modes;
21 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
24 * Ungrabs all keys, to be called before re-grabbing the keys because of a
25 * mapping_notify event or a configuration file reload
28 void ungrab_all_keys(xcb_connection_t *conn) {
29 DLOG("Ungrabbing all keys\n");
30 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
34 * Translates keysymbols to keycodes for all bindings which use keysyms.
37 void translate_keysyms(void) {
42 min_keycode = xcb_get_setup(conn)->min_keycode,
43 max_keycode = xcb_get_setup(conn)->max_keycode;
45 TAILQ_FOREACH(bind, bindings, bindings) {
46 if (bind->keycode > 0)
49 /* We need to translate the symbol to a keycode */
50 keysym = XStringToKeysym(bind->symbol);
51 if (keysym == NoSymbol) {
52 ELOG("Could not translate string to key symbol: \"%s\"\n",
57 /* Base column we use for looking up key symbols. We always consider
58 * the base column and the corresponding shift column, so without
59 * mode_switch, we look in 0 and 1, with mode_switch we look in 2 and
61 col = (bind->mods & BIND_MODE_SWITCH ? 2 : 0);
63 FREE(bind->translated_to);
64 bind->number_keycodes = 0;
66 for (i = min_keycode; i && i <= max_keycode; i++) {
67 if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
68 (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
70 bind->number_keycodes++;
71 bind->translated_to = srealloc(bind->translated_to,
72 (sizeof(xcb_keycode_t) *
73 bind->number_keycodes));
74 bind->translated_to[bind->number_keycodes-1] = i;
77 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
78 bind->number_keycodes);
83 * Switches the key bindings to the given mode, if the mode exists
86 void switch_mode(const char *new_mode) {
89 LOG("Switching to mode %s\n", new_mode);
91 SLIST_FOREACH(mode, &modes, modes) {
92 if (strcasecmp(mode->name, new_mode) != 0)
95 ungrab_all_keys(conn);
96 bindings = mode->bindings;
98 grab_all_keys(conn, false);
101 sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name);
103 ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
109 ELOG("ERROR: Mode not found\n");
113 * Sends the current bar configuration as an event to all barconfig_update listeners.
114 * This update mechnism currently only includes the hidden_state and the mode in the config.
117 void update_barconfig() {
119 TAILQ_FOREACH(current, &barconfigs, configs) {
120 /* Build json message */
122 switch (current->hidden_state) {
124 hidden_state ="show";
128 hidden_state = "hide";
133 switch (current->mode) {
146 /* Send an event to all barconfig listeners*/
148 sasprintf(&event_msg, "{ \"id\":\"%s\", \"hidden_state\":\"%s\", \"mode\":\"%s\" }", current->id, hidden_state, mode);
150 ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, event_msg);
156 * Get the path of the first configuration file found. If override_configpath
157 * is specified, that path is returned and saved for further calls. Otherwise,
158 * checks the home directory first, then the system directory first, always
159 * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
163 static char *get_config_path(const char *override_configpath) {
164 char *xdg_config_home, *xdg_config_dirs, *config_path;
166 static const char *saved_configpath = NULL;
168 if (override_configpath != NULL) {
169 saved_configpath = override_configpath;
170 return sstrdup(saved_configpath);
173 if (saved_configpath != NULL)
174 return sstrdup(saved_configpath);
176 /* 1: check the traditional path under the home directory */
177 config_path = resolve_tilde("~/.i3/config");
178 if (path_exists(config_path))
182 /* 2: check for $XDG_CONFIG_HOME/i3/config */
183 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
184 xdg_config_home = "~/.config";
186 xdg_config_home = resolve_tilde(xdg_config_home);
187 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
188 free(xdg_config_home);
190 if (path_exists(config_path))
194 /* 3: check the traditional path under /etc */
195 config_path = SYSCONFDIR "/i3/config";
196 if (path_exists(config_path))
197 return sstrdup(config_path);
199 /* 4: check for $XDG_CONFIG_DIRS/i3/config */
200 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
201 xdg_config_dirs = "/etc/xdg";
203 char *buf = sstrdup(xdg_config_dirs);
204 char *tok = strtok(buf, ":");
205 while (tok != NULL) {
206 tok = resolve_tilde(tok);
207 sasprintf(&config_path, "%s/i3/config", tok);
209 if (path_exists(config_path)) {
214 tok = strtok(NULL, ":");
218 die("Unable to find the configuration file (looked at "
219 "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
220 SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
224 * Finds the configuration file to use (either the one specified by
225 * override_configpath), the user’s one or the system default) and calls
229 static void parse_configuration(const char *override_configpath) {
230 char *path = get_config_path(override_configpath);
231 LOG("Parsing configfile %s\n", path);
232 FREE(current_configpath);
233 current_configpath = path;
238 * (Re-)loads the configuration file (sets useful defaults before).
241 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
243 /* First ungrab the keys */
244 ungrab_all_keys(conn);
248 while (!SLIST_EMPTY(&modes)) {
249 mode = SLIST_FIRST(&modes);
252 /* Clear the old binding list */
253 bindings = mode->bindings;
254 while (!TAILQ_EMPTY(bindings)) {
255 bind = TAILQ_FIRST(bindings);
256 TAILQ_REMOVE(bindings, bind, bindings);
257 FREE(bind->translated_to);
262 SLIST_REMOVE(&modes, mode, Mode, modes);
265 struct Assignment *assign;
266 while (!TAILQ_EMPTY(&assignments)) {
267 assign = TAILQ_FIRST(&assignments);
268 if (assign->type == A_TO_WORKSPACE)
269 FREE(assign->dest.workspace);
270 else if (assign->type == A_TO_OUTPUT)
271 FREE(assign->dest.output);
272 else if (assign->type == A_COMMAND)
273 FREE(assign->dest.command);
274 match_free(&(assign->match));
275 TAILQ_REMOVE(&assignments, assign, assignments);
279 /* Clear bar configs */
280 Barconfig *barconfig;
281 while (!TAILQ_EMPTY(&barconfigs)) {
282 barconfig = TAILQ_FIRST(&barconfigs);
284 for (int c = 0; c < barconfig->num_outputs; c++)
285 free(barconfig->outputs[c]);
286 FREE(barconfig->outputs);
287 FREE(barconfig->tray_output);
288 FREE(barconfig->socket_path);
289 FREE(barconfig->status_command);
290 FREE(barconfig->i3bar_command);
291 FREE(barconfig->font);
292 FREE(barconfig->colors.background);
293 FREE(barconfig->colors.statusline);
294 FREE(barconfig->colors.focused_workspace_border);
295 FREE(barconfig->colors.focused_workspace_bg);
296 FREE(barconfig->colors.focused_workspace_text);
297 FREE(barconfig->colors.active_workspace_border);
298 FREE(barconfig->colors.active_workspace_bg);
299 FREE(barconfig->colors.active_workspace_text);
300 FREE(barconfig->colors.inactive_workspace_border);
301 FREE(barconfig->colors.inactive_workspace_bg);
302 FREE(barconfig->colors.inactive_workspace_text);
303 FREE(barconfig->colors.urgent_workspace_border);
304 FREE(barconfig->colors.urgent_workspace_bg);
305 FREE(barconfig->colors.urgent_workspace_text);
306 TAILQ_REMOVE(&barconfigs, barconfig, configs);
310 /* Clear workspace names */
313 TAILQ_FOREACH(ws, workspaces, workspaces)
314 workspace_set_name(ws, NULL);
317 /* Invalidate pixmap caches in case font or colors changed */
319 TAILQ_FOREACH(con, &all_cons, all_cons)
320 FREE(con->deco_render_params);
322 /* Get rid of the current font */
328 struct Mode *default_mode = scalloc(sizeof(struct Mode));
329 default_mode->name = sstrdup("default");
330 default_mode->bindings = scalloc(sizeof(struct bindings_head));
331 TAILQ_INIT(default_mode->bindings);
332 SLIST_INSERT_HEAD(&modes, default_mode, modes);
334 bindings = default_mode->bindings;
336 #define REQUIRED_OPTION(name) \
337 if (config.name == NULL) \
338 die("You did not specify required configuration option " #name "\n");
340 /* Clear the old config or initialize the data structure */
341 memset(&config, 0, sizeof(config));
343 /* Initialize default colors */
344 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
346 x.border = get_colorpixel(cborder); \
347 x.background = get_colorpixel(cbackground); \
348 x.text = get_colorpixel(ctext); \
349 x.indicator = get_colorpixel(cindicator); \
352 config.client.background = get_colorpixel("#000000");
353 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
354 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
355 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
356 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
358 /* border and indicator color are ignored for placeholder contents */
359 INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
361 /* the last argument (indicator color) is ignored for bar colors */
362 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
363 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
364 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
366 config.default_border = BS_NORMAL;
367 config.default_floating_border = BS_NORMAL;
368 config.default_border_width = 2;
369 /* Set default_orientation to NO_ORIENTATION for auto orientation. */
370 config.default_orientation = NO_ORIENTATION;
372 /* Set default urgency reset delay to 500ms */
373 if (config.workspace_urgency_timer == 0)
374 config.workspace_urgency_timer = 0.5;
376 parse_configuration(override_configpath);
380 grab_all_keys(conn, false);
383 if (config.font.type == FONT_TYPE_NONE) {
384 ELOG("You did not specify required configuration option \"font\"\n");
385 config.font = load_font("fixed", true);
386 set_font(&config.font);
389 /* Redraw the currently visible decorations on reload, so that
390 * the possibly new drawing parameters changed. */
392 x_deco_recurse(croot);
397 /* Set an empty name for every workspace which got no name */
399 TAILQ_FOREACH(ws, workspaces, workspaces) {
400 if (ws->name != NULL) {
401 /* If the font was not specified when the workspace name
402 * was loaded, we need to predict the text width now */
403 if (ws->text_width == 0)
404 ws->text_width = predict_text_width(global_conn,
405 config.font, ws->name, ws->name_len);
409 workspace_set_name(ws, NULL);