4 * i3 - an improved dynamic tiling window manager
6 * © 2009 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
21 /* We need Xlib for XStringToKeysym */
24 #include <xcb/xcb_keysyms.h>
31 #include "workspace.h"
35 struct modes_head modes;
38 * This function resolves ~ in pathnames.
41 static char *glob_path(const char *path) {
42 static glob_t globbuf;
43 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
45 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
51 * Ungrabs all keys, to be called before re-grabbing the keys because of a
52 * mapping_notify event or a configuration file reload
55 void ungrab_all_keys(xcb_connection_t *conn) {
56 DLOG("Ungrabbing all keys\n");
57 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
60 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
61 DLOG("Grabbing %d\n", keycode);
62 if ((bind->mods & BIND_MODE_SWITCH) != 0)
63 xcb_grab_key(conn, 0, root, 0, keycode,
64 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
66 /* Grab the key in all combinations */
67 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
68 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
70 GRAB_KEY(bind->mods | xcb_numlock_mask);
71 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
76 * Grab the bound keys (tell X to send us keypress events for those keycodes)
79 void grab_all_keys(xcb_connection_t *conn) {
81 TAILQ_FOREACH(bind, bindings, bindings) {
82 /* The easy case: the user specified a keycode directly. */
83 if (bind->keycode > 0) {
84 grab_keycode_for_binding(conn, bind, bind->keycode);
88 /* We need to translate the symbol to a keycode */
89 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
90 if (keysym == NoSymbol) {
91 ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
95 #ifdef OLD_XCB_KEYSYMS_API
96 bind->number_keycodes = 1;
97 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
98 DLOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
99 grab_keycode_for_binding(conn, bind, code);
100 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
101 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
103 uint32_t last_keycode = 0;
104 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
105 if (keycodes == NULL) {
106 DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
110 bind->number_keycodes = 0;
112 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
113 /* We hope duplicate keycodes will be returned in order
115 if (last_keycode == *walk)
117 grab_keycode_for_binding(conn, bind, *walk);
118 last_keycode = *walk;
119 bind->number_keycodes++;
121 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
122 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
123 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
130 * Switches the key bindings to the given mode, if the mode exists
133 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
136 LOG("Switching to mode %s\n", new_mode);
138 SLIST_FOREACH(mode, &modes, modes) {
139 if (strcasecmp(mode->name, new_mode) != 0)
142 ungrab_all_keys(conn);
143 bindings = mode->bindings;
148 ELOG("ERROR: Mode not found\n");
152 * Finds the configuration file to use (either the one specified by
153 * override_configpath), the user’s one or the system default) and calls
157 static void parse_configuration(const char *override_configpath) {
158 if (override_configpath != NULL) {
159 parse_file(override_configpath);
164 char *globbed = glob_path("~/.i3/config");
165 if ((handle = fopen(globbed, "r")) == NULL) {
166 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
167 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
169 parse_file("/etc/i3/config");
179 * (Re-)loads the configuration file (sets useful defaults before).
182 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
184 /* First ungrab the keys */
185 ungrab_all_keys(conn);
189 while (!SLIST_EMPTY(&modes)) {
190 mode = SLIST_FIRST(&modes);
193 /* Clear the old binding list */
194 bindings = mode->bindings;
195 while (!TAILQ_EMPTY(bindings)) {
196 bind = TAILQ_FIRST(bindings);
197 TAILQ_REMOVE(bindings, bind, bindings);
198 FREE(bind->translated_to);
203 SLIST_REMOVE(&modes, mode, Mode, modes);
206 struct Assignment *assign;
207 while (!TAILQ_EMPTY(&assignments)) {
208 assign = TAILQ_FIRST(&assignments);
209 FREE(assign->windowclass_title);
210 TAILQ_REMOVE(&assignments, assign, assignments);
217 struct Mode *default_mode = scalloc(sizeof(struct Mode));
218 default_mode->name = sstrdup("default");
219 default_mode->bindings = scalloc(sizeof(struct bindings_head));
220 TAILQ_INIT(default_mode->bindings);
221 SLIST_INSERT_HEAD(&modes, default_mode, modes);
223 bindings = default_mode->bindings;
225 #define REQUIRED_OPTION(name) \
226 if (config.name == NULL) \
227 die("You did not specify required configuration option " #name "\n");
229 /* Clear the old config or initialize the data structure */
230 memset(&config, 0, sizeof(config));
232 /* Initialize default colors */
233 #define INIT_COLOR(x, cborder, cbackground, ctext) \
235 x.border = get_colorpixel(conn, cborder); \
236 x.background = get_colorpixel(conn, cbackground); \
237 x.text = get_colorpixel(conn, ctext); \
240 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
241 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
242 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
243 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
244 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
245 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
246 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
248 parse_configuration(override_configpath);
253 REQUIRED_OPTION(font);
255 /* Set an empty name for every workspace which got no name */
257 TAILQ_FOREACH(ws, workspaces, workspaces) {
258 if (ws->name != NULL) {
259 /* If the font was not specified when the workspace name
260 * was loaded, we need to predict the text width now */
261 if (ws->text_width == 0)
262 ws->text_width = predict_text_width(global_conn,
263 config.font, ws->name, ws->name_len);
267 workspace_set_name(ws, NULL);