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() {
89 min_keycode = xcb_get_setup(conn)->min_keycode,
90 max_keycode = xcb_get_setup(conn)->max_keycode;
92 TAILQ_FOREACH(bind, bindings, bindings) {
93 if (bind->keycode > 0)
96 /* We need to translate the symbol to a keycode */
97 keysym = XStringToKeysym(bind->symbol);
98 if (keysym == NoSymbol) {
99 ELOG("Could not translate string to key symbol: \"%s\"\n",
104 /* Base column we use for looking up key symbols. We always consider
105 * the base column and the corresponding shift column, so without
106 * mode_switch, we look in 0 and 1, with mode_switch we look in 2 and
108 col = (bind->mods & BIND_MODE_SWITCH ? 2 : 0);
110 FREE(bind->translated_to);
111 bind->number_keycodes = 0;
113 for (i = min_keycode; i && i <= max_keycode; i++) {
114 if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
115 (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
117 bind->number_keycodes++;
118 bind->translated_to = srealloc(bind->translated_to,
119 (sizeof(xcb_keycode_t) *
120 bind->number_keycodes));
121 bind->translated_to[bind->number_keycodes-1] = i;
124 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
125 bind->number_keycodes);
130 * Grab the bound keys (tell X to send us keypress events for those keycodes)
133 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
135 TAILQ_FOREACH(bind, bindings, bindings) {
136 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
137 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
140 /* The easy case: the user specified a keycode directly. */
141 if (bind->keycode > 0) {
142 grab_keycode_for_binding(conn, bind, bind->keycode);
146 xcb_keycode_t *walk = bind->translated_to;
147 for (int i = 0; i < bind->number_keycodes; i++)
148 grab_keycode_for_binding(conn, bind, *walk++);
153 * Switches the key bindings to the given mode, if the mode exists
156 void switch_mode(const char *new_mode) {
159 LOG("Switching to mode %s\n", new_mode);
161 SLIST_FOREACH(mode, &modes, modes) {
162 if (strcasecmp(mode->name, new_mode) != 0)
165 ungrab_all_keys(conn);
166 bindings = mode->bindings;
168 grab_all_keys(conn, false);
172 ELOG("ERROR: Mode not found\n");
176 * Get the path of the first configuration file found. If override_configpath
177 * is specified, that path is returned and saved for further calls. Otherwise,
178 * checks the home directory first, then the system directory first, always
179 * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
183 static char *get_config_path(const char *override_configpath) {
184 char *xdg_config_home, *xdg_config_dirs, *config_path;
186 static const char *saved_configpath = NULL;
188 if (override_configpath != NULL) {
189 saved_configpath = override_configpath;
190 return sstrdup(saved_configpath);
193 if (saved_configpath != NULL)
194 return sstrdup(saved_configpath);
196 /* 1: check the traditional path under the home directory */
197 config_path = resolve_tilde("~/.i3/config");
198 if (path_exists(config_path))
202 /* 2: check for $XDG_CONFIG_HOME/i3/config */
203 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
204 xdg_config_home = "~/.config";
206 xdg_config_home = resolve_tilde(xdg_config_home);
207 sasprintf(&config_path, "%s/i3/config", xdg_config_home);
208 free(xdg_config_home);
210 if (path_exists(config_path))
214 /* 3: check the traditional path under /etc */
215 config_path = SYSCONFDIR "/i3/config";
216 if (path_exists(config_path))
217 return sstrdup(config_path);
219 /* 4: check for $XDG_CONFIG_DIRS/i3/config */
220 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
221 xdg_config_dirs = "/etc/xdg";
223 char *buf = sstrdup(xdg_config_dirs);
224 char *tok = strtok(buf, ":");
225 while (tok != NULL) {
226 tok = resolve_tilde(tok);
227 sasprintf(&config_path, "%s/i3/config", tok);
229 if (path_exists(config_path)) {
234 tok = strtok(NULL, ":");
238 die("Unable to find the configuration file (looked at "
239 "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
240 SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
244 * Finds the configuration file to use (either the one specified by
245 * override_configpath), the user’s one or the system default) and calls
249 static void parse_configuration(const char *override_configpath) {
250 char *path = get_config_path(override_configpath);
251 DLOG("Parsing configfile %s\n", path);
252 FREE(current_configpath);
253 current_configpath = path;
258 * (Re-)loads the configuration file (sets useful defaults before).
261 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
263 /* First ungrab the keys */
264 ungrab_all_keys(conn);
268 while (!SLIST_EMPTY(&modes)) {
269 mode = SLIST_FIRST(&modes);
272 /* Clear the old binding list */
273 bindings = mode->bindings;
274 while (!TAILQ_EMPTY(bindings)) {
275 bind = TAILQ_FIRST(bindings);
276 TAILQ_REMOVE(bindings, bind, bindings);
277 FREE(bind->translated_to);
282 SLIST_REMOVE(&modes, mode, Mode, modes);
285 struct Assignment *assign;
286 while (!TAILQ_EMPTY(&assignments)) {
287 assign = TAILQ_FIRST(&assignments);
288 if (assign->type == A_TO_WORKSPACE)
289 FREE(assign->dest.workspace);
290 else if (assign->type == A_TO_OUTPUT)
291 FREE(assign->dest.output);
292 else if (assign->type == A_COMMAND)
293 FREE(assign->dest.command);
294 match_free(&(assign->match));
295 TAILQ_REMOVE(&assignments, assign, assignments);
299 /* Clear bar configs */
300 Barconfig *barconfig;
301 while (!TAILQ_EMPTY(&barconfigs)) {
302 barconfig = TAILQ_FIRST(&barconfigs);
304 for (int c = 0; c < barconfig->num_outputs; c++)
305 free(barconfig->outputs[c]);
306 FREE(barconfig->outputs);
307 FREE(barconfig->tray_output);
308 FREE(barconfig->socket_path);
309 FREE(barconfig->status_command);
310 FREE(barconfig->font);
311 FREE(barconfig->colors.background);
312 FREE(barconfig->colors.statusline);
313 FREE(barconfig->colors.focused_workspace_text);
314 FREE(barconfig->colors.focused_workspace_bg);
315 FREE(barconfig->colors.active_workspace_text);
316 FREE(barconfig->colors.active_workspace_bg);
317 FREE(barconfig->colors.inactive_workspace_text);
318 FREE(barconfig->colors.inactive_workspace_bg);
319 FREE(barconfig->colors.urgent_workspace_text);
320 FREE(barconfig->colors.urgent_workspace_bg);
321 TAILQ_REMOVE(&barconfigs, barconfig, configs);
325 /* Clear workspace names */
328 TAILQ_FOREACH(ws, workspaces, workspaces)
329 workspace_set_name(ws, NULL);
335 struct Mode *default_mode = scalloc(sizeof(struct Mode));
336 default_mode->name = sstrdup("default");
337 default_mode->bindings = scalloc(sizeof(struct bindings_head));
338 TAILQ_INIT(default_mode->bindings);
339 SLIST_INSERT_HEAD(&modes, default_mode, modes);
341 bindings = default_mode->bindings;
343 #define REQUIRED_OPTION(name) \
344 if (config.name == NULL) \
345 die("You did not specify required configuration option " #name "\n");
347 /* Clear the old config or initialize the data structure */
348 memset(&config, 0, sizeof(config));
350 /* Initialize default colors */
351 #define INIT_COLOR(x, cborder, cbackground, ctext) \
353 x.border = get_colorpixel(cborder); \
354 x.background = get_colorpixel(cbackground); \
355 x.text = get_colorpixel(ctext); \
358 config.client.background = get_colorpixel("#000000");
359 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
360 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
361 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
362 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
363 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
364 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
365 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
367 config.default_border = BS_NORMAL;
368 config.default_floating_border = BS_NORMAL;
369 /* Set default_orientation to NO_ORIENTATION for auto orientation. */
370 config.default_orientation = NO_ORIENTATION;
372 parse_configuration(override_configpath);
376 grab_all_keys(conn, false);
379 if (config.font.id == 0) {
380 ELOG("You did not specify required configuration option \"font\"\n");
381 config.font = load_font("fixed", true);
385 /* Set an empty name for every workspace which got no name */
387 TAILQ_FOREACH(ws, workspaces, workspaces) {
388 if (ws->name != NULL) {
389 /* If the font was not specified when the workspace name
390 * was loaded, we need to predict the text width now */
391 if (ws->text_width == 0)
392 ws->text_width = predict_text_width(global_conn,
393 config.font, ws->name, ws->name_len);
397 workspace_set_name(ws, NULL);