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"
34 struct modes_head modes;
37 * This function resolves ~ in pathnames.
40 static char *glob_path(const char *path) {
41 static glob_t globbuf;
42 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
44 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
50 * Ungrabs all keys, to be called before re-grabbing the keys because of a
51 * mapping_notify event or a configuration file reload
54 void ungrab_all_keys(xcb_connection_t *conn) {
55 LOG("Ungrabbing all keys\n");
56 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
59 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
60 LOG("Grabbing %d\n", keycode);
61 if ((bind->mods & BIND_MODE_SWITCH) != 0)
62 xcb_grab_key(conn, 0, root, 0, keycode,
63 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
65 /* Grab the key in all combinations */
66 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
67 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
69 GRAB_KEY(bind->mods | xcb_numlock_mask);
70 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
75 * Grab the bound keys (tell X to send us keypress events for those keycodes)
78 void grab_all_keys(xcb_connection_t *conn) {
80 TAILQ_FOREACH(bind, bindings, bindings) {
81 /* The easy case: the user specified a keycode directly. */
82 if (bind->keycode > 0) {
83 grab_keycode_for_binding(conn, bind, bind->keycode);
87 /* We need to translate the symbol to a keycode */
88 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
89 if (keysym == NoSymbol) {
90 LOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
94 #ifdef OLD_XCB_KEYSYMS_API
95 bind->number_keycodes = 1;
96 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
97 LOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
98 grab_keycode_for_binding(conn, bind, code);
99 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
100 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
102 uint32_t last_keycode = 0;
103 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
104 if (keycodes == NULL) {
105 LOG("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 grab_keycode_for_binding(conn, bind, *walk);
117 last_keycode = *walk;
118 bind->number_keycodes++;
120 LOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
121 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
122 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
129 * Switches the key bindings to the given mode, if the mode exists
132 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
135 LOG("Switching to mode %s\n", new_mode);
137 SLIST_FOREACH(mode, &modes, modes) {
138 if (strcasecmp(mode->name, new_mode) != 0)
141 ungrab_all_keys(conn);
142 bindings = mode->bindings;
147 LOG("ERROR: Mode not found\n");
151 * Finds the configuration file to use (either the one specified by
152 * override_configpath), the user’s one or the system default) and calls
156 static void parse_configuration(const char *override_configpath) {
157 if (override_configpath != NULL) {
158 parse_file(override_configpath);
163 char *globbed = glob_path("~/.i3/config");
164 if ((handle = fopen(globbed, "r")) == NULL) {
165 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
166 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
168 parse_file("/etc/i3/config");
177 * (Re-)loads the configuration file (sets useful defaults before).
180 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
182 /* First ungrab the keys */
183 ungrab_all_keys(conn);
187 while (!SLIST_EMPTY(&modes)) {
188 mode = SLIST_FIRST(&modes);
191 /* Clear the old binding list */
192 bindings = mode->bindings;
193 while (!TAILQ_EMPTY(bindings)) {
194 bind = TAILQ_FIRST(bindings);
195 TAILQ_REMOVE(bindings, bind, bindings);
196 FREE(bind->translated_to);
201 SLIST_REMOVE(&modes, mode, Mode, modes);
204 struct Assignment *assign;
205 while (!TAILQ_EMPTY(&assignments)) {
206 assign = TAILQ_FIRST(&assignments);
207 FREE(assign->windowclass_title);
208 TAILQ_REMOVE(&assignments, assign, assignments);
215 struct Mode *default_mode = scalloc(sizeof(struct Mode));
216 default_mode->name = sstrdup("default");
217 default_mode->bindings = scalloc(sizeof(struct bindings_head));
218 TAILQ_INIT(default_mode->bindings);
219 SLIST_INSERT_HEAD(&modes, default_mode, modes);
221 bindings = default_mode->bindings;
223 #define REQUIRED_OPTION(name) \
224 if (config.name == NULL) \
225 die("You did not specify required configuration option " #name "\n");
227 /* Clear the old config or initialize the data structure */
228 memset(&config, 0, sizeof(config));
230 /* Initialize default colors */
231 #define INIT_COLOR(x, cborder, cbackground, ctext) \
233 x.border = get_colorpixel(conn, cborder); \
234 x.background = get_colorpixel(conn, cbackground); \
235 x.text = get_colorpixel(conn, ctext); \
238 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
239 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
240 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
241 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
242 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
243 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
244 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
246 parse_configuration(override_configpath);
251 REQUIRED_OPTION(font);
253 /* Set an empty name for every workspace which got no name */
255 TAILQ_FOREACH(ws, workspaces, workspaces) {
256 if (ws->name != NULL) {
257 /* If the font was not specified when the workspace name
258 * was loaded, we need to predict the text width now */
259 if (ws->text_width == 0)
260 ws->text_width = predict_text_width(global_conn,
261 config.font, ws->name, ws->name_len);
265 workspace_set_name(ws, NULL);