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 */
22 const char *current_configpath = NULL;
24 struct modes_head modes;
27 * Ungrabs all keys, to be called before re-grabbing the keys because of a
28 * mapping_notify event or a configuration file reload
31 void ungrab_all_keys(xcb_connection_t *conn) {
32 DLOG("Ungrabbing all keys\n");
33 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
36 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
37 DLOG("Grabbing %d\n", keycode);
38 /* Grab the key in all combinations */
39 #define GRAB_KEY(modifier) \
41 xcb_grab_key(conn, 0, root, modifier, keycode, \
42 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
44 int mods = bind->mods;
45 if ((bind->mods & BIND_MODE_SWITCH) != 0) {
46 mods &= ~BIND_MODE_SWITCH;
48 mods = XCB_MOD_MASK_ANY;
51 GRAB_KEY(mods | xcb_numlock_mask);
52 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
56 * Returns a pointer to the Binding with the specified modifiers and keycode
57 * or NULL if no such binding exists.
60 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
63 TAILQ_FOREACH(bind, bindings, bindings) {
64 /* First compare the modifiers */
65 if (bind->mods != modifiers)
68 /* If a symbol was specified by the user, we need to look in
69 * the array of translated keycodes for the event’s keycode */
70 if (bind->symbol != NULL) {
71 if (memmem(bind->translated_to,
72 bind->number_keycodes * sizeof(xcb_keycode_t),
73 &keycode, sizeof(xcb_keycode_t)) != NULL)
76 /* This case is easier: The user specified a keycode */
77 if (bind->keycode == keycode)
82 return (bind == TAILQ_END(bindings) ? NULL : bind);
86 * Translates keysymbols to keycodes for all bindings which use keysyms.
89 void translate_keysyms() {
91 TAILQ_FOREACH(bind, bindings, bindings) {
92 if (bind->keycode > 0)
95 /* We need to translate the symbol to a keycode */
96 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
97 if (keysym == NoSymbol) {
98 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
102 uint32_t last_keycode = 0;
103 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
104 if (keycodes == NULL) {
105 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
109 bind->number_keycodes = 0;
111 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
112 /* We hope duplicate keycodes will be returned in order
114 if (last_keycode == *walk)
116 last_keycode = *walk;
117 bind->number_keycodes++;
119 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
120 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
121 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
127 * Grab the bound keys (tell X to send us keypress events for those keycodes)
130 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
132 TAILQ_FOREACH(bind, bindings, bindings) {
133 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
134 (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
137 /* The easy case: the user specified a keycode directly. */
138 if (bind->keycode > 0) {
139 grab_keycode_for_binding(conn, bind, bind->keycode);
143 xcb_keycode_t *walk = bind->translated_to;
144 for (int i = 0; i < bind->number_keycodes; i++)
145 grab_keycode_for_binding(conn, bind, *walk);
150 * Switches the key bindings to the given mode, if the mode exists
153 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
156 LOG("Switching to mode %s\n", new_mode);
158 SLIST_FOREACH(mode, &modes, modes) {
159 if (strcasecmp(mode->name, new_mode) != 0)
162 ungrab_all_keys(conn);
163 bindings = mode->bindings;
165 grab_all_keys(conn, false);
169 ELOG("ERROR: Mode not found\n");
173 * Get the path of the first configuration file found. Checks the home directory
174 * first, then the system directory first, always taking into account the XDG
175 * Base Directory Specification ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS)
178 static char *get_config_path() {
179 char *xdg_config_home, *xdg_config_dirs, *config_path;
181 /* 1: check the traditional path under the home directory */
182 config_path = resolve_tilde("~/.i3/config");
183 if (path_exists(config_path))
186 /* 2: check for $XDG_CONFIG_HOME/i3/config */
187 if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
188 xdg_config_home = "~/.config";
190 xdg_config_home = resolve_tilde(xdg_config_home);
191 if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
192 die("asprintf() failed");
193 free(xdg_config_home);
195 if (path_exists(config_path))
199 /* 3: check the traditional path under /etc */
200 config_path = SYSCONFDIR "/i3/config";
201 if (path_exists(config_path))
202 return sstrdup(config_path);
204 /* 4: check for $XDG_CONFIG_DIRS/i3/config */
205 if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
206 xdg_config_dirs = "/etc/xdg";
208 char *buf = sstrdup(xdg_config_dirs);
209 char *tok = strtok(buf, ":");
210 while (tok != NULL) {
211 tok = resolve_tilde(tok);
212 if (asprintf(&config_path, "%s/i3/config", tok) == -1)
213 die("asprintf() failed");
215 if (path_exists(config_path)) {
220 tok = strtok(NULL, ":");
224 die("Unable to find the configuration file (looked at "
225 "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
226 SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
230 * Finds the configuration file to use (either the one specified by
231 * override_configpath), the user’s one or the system default) and calls
235 static void parse_configuration(const char *override_configpath) {
236 static const char *saved_configpath = NULL;
238 if (override_configpath != NULL) {
239 saved_configpath = override_configpath;
240 current_configpath = override_configpath;
241 parse_file(override_configpath);
244 else if (saved_configpath != NULL) {
245 current_configpath = saved_configpath;
246 parse_file(saved_configpath);
250 char *path = get_config_path();
251 DLOG("Parsing configfile %s\n", path);
252 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);
286 struct Assignment *assign;
287 while (!TAILQ_EMPTY(&assignments)) {
288 assign = TAILQ_FIRST(&assignments);
289 FREE(assign->windowclass_title);
290 TAILQ_REMOVE(&assignments, assign, assignments);
295 /* Clear workspace names */
298 TAILQ_FOREACH(ws, workspaces, workspaces)
299 workspace_set_name(ws, NULL);
305 struct Mode *default_mode = scalloc(sizeof(struct Mode));
306 default_mode->name = sstrdup("default");
307 default_mode->bindings = scalloc(sizeof(struct bindings_head));
308 TAILQ_INIT(default_mode->bindings);
309 SLIST_INSERT_HEAD(&modes, default_mode, modes);
311 bindings = default_mode->bindings;
313 #define REQUIRED_OPTION(name) \
314 if (config.name == NULL) \
315 die("You did not specify required configuration option " #name "\n");
317 /* Clear the old config or initialize the data structure */
318 memset(&config, 0, sizeof(config));
320 /* Initialize default colors */
321 #define INIT_COLOR(x, cborder, cbackground, ctext) \
323 x.border = get_colorpixel(cborder); \
324 x.background = get_colorpixel(cbackground); \
325 x.text = get_colorpixel(ctext); \
328 config.client.background = get_colorpixel("#000000");
329 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
330 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
331 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
332 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
333 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
334 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
335 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
337 config.default_border = BS_NORMAL;
338 /* Set default_orientation to NO_ORIENTATION for auto orientation. */
339 config.default_orientation = NO_ORIENTATION;
341 parse_configuration(override_configpath);
345 grab_all_keys(conn, false);
348 if (config.font.id == 0) {
349 ELOG("You did not specify required configuration option \"font\"\n");
350 config.font = load_font("fixed", true);
354 /* Set an empty name for every workspace which got no name */
356 TAILQ_FOREACH(ws, workspaces, workspaces) {
357 if (ws->name != NULL) {
358 /* If the font was not specified when the workspace name
359 * was loaded, we need to predict the text width now */
360 if (ws->text_width == 0)
361 ws->text_width = predict_text_width(global_conn,
362 config.font, ws->name, ws->name_len);
366 workspace_set_name(ws, NULL);