4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
17 /* We need Xlib for XStringToKeysym */
20 #include <xcb/xcb_keysyms.h>
27 #include "workspace.h"
31 bool config_use_lexer = false;
34 * This function resolves ~ in pathnames.
37 static char *glob_path(const char *path) {
38 static glob_t globbuf;
39 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
41 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
47 * This function does a very simple replacement of each instance of key with value.
50 static void replace_variable(char *buffer, const char *key, const char *value) {
52 /* To prevent endless recursions when the user makes an error configuring,
53 * we stop after 100 replacements. That should be vastly more than enough. */
55 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
56 char *rest = pos + strlen(key);
59 asprintf(&replaced, "%s%s%s", buffer, value, rest);
60 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
61 * So we need to hard-code the dimensions here. */
62 strncpy(buffer, replaced, 1026);
68 * Ungrabs all keys, to be called before re-grabbing the keys because of a
69 * mapping_notify event or a configuration file reload
72 void ungrab_all_keys(xcb_connection_t *conn) {
73 LOG("Ungrabbing all keys\n");
74 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
77 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
78 LOG("Grabbing %d\n", keycode);
79 if ((bind->mods & BIND_MODE_SWITCH) != 0)
80 xcb_grab_key(conn, 0, root, 0, keycode,
81 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
83 /* Grab the key in all combinations */
84 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
85 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
87 GRAB_KEY(bind->mods | xcb_numlock_mask);
88 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
93 * Grab the bound keys (tell X to send us keypress events for those keycodes)
96 void grab_all_keys(xcb_connection_t *conn) {
98 TAILQ_FOREACH(bind, &bindings, bindings) {
99 /* The easy case: the user specified a keycode directly. */
100 if (bind->keycode > 0) {
101 grab_keycode_for_binding(conn, bind, bind->keycode);
105 /* We need to translate the symbol to a keycode */
106 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
107 if (keysym == NoSymbol) {
108 LOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
112 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
113 if (keycodes == NULL) {
114 LOG("Could not translate symbol \"%s\"\n", bind->symbol);
118 uint32_t last_keycode = 0;
119 bind->number_keycodes = 0;
120 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
121 /* We hope duplicate keycodes will be returned in order
123 if (last_keycode == *walk)
125 grab_keycode_for_binding(conn, bind, *walk);
126 last_keycode = *walk;
127 bind->number_keycodes++;
129 LOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
130 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
131 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
137 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
139 * If you specify override_configpath, only this path is used to look for a
140 * configuration file.
143 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
145 /* First ungrab the keys */
146 ungrab_all_keys(conn);
148 /* Clear the old binding and assignment lists */
150 while (!TAILQ_EMPTY(&bindings)) {
151 bind = TAILQ_FIRST(&bindings);
152 TAILQ_REMOVE(&bindings, bind, bindings);
157 struct Assignment *assign;
158 while (!TAILQ_EMPTY(&assignments)) {
159 assign = TAILQ_FIRST(&assignments);
160 FREE(assign->windowclass_title);
161 TAILQ_REMOVE(&assignments, assign, assignments);
166 SLIST_HEAD(variables_head, Variable) variables;
168 #define OPTION_STRING(name) \
169 if (strcasecmp(key, #name) == 0) { \
170 config.name = sstrdup(value); \
174 #define REQUIRED_OPTION(name) \
175 if (config.name == NULL) \
176 die("You did not specify required configuration option " #name "\n");
178 #define OPTION_COLORTRIPLE(opt, name) \
179 if (strcasecmp(key, opt) == 0) { \
180 char border[8], background[8], text[8]; \
181 memset(border, 0, sizeof(border)); \
182 memset(background, 0, sizeof(background)); \
183 memset(text, 0, sizeof(text)); \
184 border[0] = background[0] = text[0] = '#'; \
185 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
186 border + 1, background + 1, text + 1) != 3 || \
187 strlen(border) != 7 || \
188 strlen(background) != 7 || \
190 die("invalid color code line: %s\n", value); \
191 config.name.border = get_colorpixel(conn, border); \
192 config.name.background = get_colorpixel(conn, background); \
193 config.name.text = get_colorpixel(conn, text); \
197 /* Clear the old config or initialize the data structure */
198 memset(&config, 0, sizeof(config));
200 SLIST_INIT(&variables);
202 /* Initialize default colors */
203 config.client.focused.border = get_colorpixel(conn, "#4c7899");
204 config.client.focused.background = get_colorpixel(conn, "#285577");
205 config.client.focused.text = get_colorpixel(conn, "#ffffff");
207 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
208 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
209 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
211 config.client.unfocused.border = get_colorpixel(conn, "#333333");
212 config.client.unfocused.background = get_colorpixel(conn, "#222222");
213 config.client.unfocused.text = get_colorpixel(conn, "#888888");
215 config.client.urgent.border = get_colorpixel(conn, "#2f343a");
216 config.client.urgent.background = get_colorpixel(conn, "#900000");
217 config.client.urgent.text = get_colorpixel(conn, "#ffffff");
219 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
220 config.bar.focused.background = get_colorpixel(conn, "#285577");
221 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
223 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
224 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
225 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
227 config.bar.urgent.border = get_colorpixel(conn, "#2f343a");
228 config.bar.urgent.background = get_colorpixel(conn, "#900000");
229 config.bar.urgent.text = get_colorpixel(conn, "#ffffff");
231 if (config_use_lexer) {
232 /* Yes, this will be cleaned up soon. */
233 if (override_configpath != NULL) {
234 parse_file(override_configpath);
237 char *globbed = glob_path("~/.i3/config");
238 if ((handle = fopen(globbed, "r")) == NULL) {
239 if ((handle = fopen("/etc/i3/config", "r")) == NULL) {
240 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
242 parse_file("/etc/i3/config");
251 if (override_configpath != NULL) {
252 if ((handle = fopen(override_configpath, "r")) == NULL)
253 die("Could not open configfile \"%s\".\n", override_configpath);
255 /* We first check for ~/.i3/config, then for /etc/i3/config */
256 char *globbed = glob_path("~/.i3/config");
257 if ((handle = fopen(globbed, "r")) == NULL)
258 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
259 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
262 char key[512], value[512], buffer[1026];
264 while (!feof(handle)) {
265 if (fgets(buffer, 1024, handle) == NULL) {
266 /* fgets returns NULL on EOF and on error, so see which one it is. */
269 die("Could not read configuration file\n");
272 if (config.terminal != NULL)
273 replace_variable(buffer, "$terminal", config.terminal);
275 /* Replace all custom variables */
276 struct Variable *current;
277 SLIST_FOREACH(current, &variables, variables)
278 replace_variable(buffer, current->key, current->value);
280 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
281 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
282 key[0] == '#' || strlen(key) < 3)
285 OPTION_STRING(terminal);
289 OPTION_COLORTRIPLE("client.focused", client.focused);
290 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
291 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
292 OPTION_COLORTRIPLE("client.urgent", client.urgent);
293 OPTION_COLORTRIPLE("bar.focused", bar.focused);
294 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
295 OPTION_COLORTRIPLE("bar.urgent", bar.urgent);
297 /* exec-lines (autostart) */
298 if (strcasecmp(key, "exec") == 0) {
299 struct Autostart *new = smalloc(sizeof(struct Autostart));
300 new->command = sstrdup(value);
301 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
306 if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
307 #define CHECK_MODIFIER(name) \
308 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
309 modifiers |= BIND_##name; \
310 walk += strlen(#name) + 1; \
313 char *walk = value, *rest;
314 uint32_t modifiers = 0;
316 while (*walk != '\0') {
317 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
318 CHECK_MODIFIER(SHIFT);
319 CHECK_MODIFIER(CONTROL);
320 CHECK_MODIFIER(MODE_SWITCH);
321 CHECK_MODIFIER(MOD1);
322 CHECK_MODIFIER(MOD2);
323 CHECK_MODIFIER(MOD3);
324 CHECK_MODIFIER(MOD4);
325 CHECK_MODIFIER(MOD5);
327 /* No modifier found? Then we’re done with this step */
331 Binding *new = scalloc(sizeof(Binding));
333 /* Now check for the keycode or copy the symbol */
334 if (strcasecmp(key, "bind") == 0) {
335 int keycode = strtol(walk, &rest, 10);
336 if (!rest || *rest != ' ')
337 die("Invalid binding (keycode)\n");
338 new->keycode = keycode;
342 while (*rest != '\0' && *rest != ' ')
345 die("Invalid binding (keysym)\n");
346 #if defined(__OpenBSD__)
347 size_t len = strlen(sym);
348 if (len > (rest - sym))
350 new->symbol = smalloc(len + 1);
351 memcpy(new->symbol, sym, len+1);
352 new->symbol[len]='\0';
354 new->symbol = strndup(sym, (rest - sym));
358 LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
359 new->mods = modifiers;
360 new->command = sstrdup(rest);
361 TAILQ_INSERT_TAIL(&bindings, new, bindings);
365 if (strcasecmp(key, "floating_modifier") == 0) {
367 uint32_t modifiers = 0;
369 while (*walk != '\0') {
370 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
371 CHECK_MODIFIER(SHIFT);
372 CHECK_MODIFIER(CONTROL);
373 CHECK_MODIFIER(MODE_SWITCH);
374 CHECK_MODIFIER(MOD1);
375 CHECK_MODIFIER(MOD2);
376 CHECK_MODIFIER(MOD3);
377 CHECK_MODIFIER(MOD4);
378 CHECK_MODIFIER(MOD5);
380 /* No modifier found? Then we’re done with this step */
384 LOG("Floating modifiers = %d\n", modifiers);
385 config.floating_modifier = modifiers;
389 /* workspace "workspace number" [screen <screen>] ["name of the workspace"]
390 * with screen := <number> | <position>, e.g. screen 1280 or screen 1 */
391 if (strcasecmp(key, "name") == 0 || strcasecmp(key, "workspace") == 0) {
392 LOG("workspace: %s\n",value);
393 char *ws_str = sstrdup(value);
394 char *end = strchr(ws_str, ' ');
396 die("Malformed name, couln't find terminating space\n");
399 /* Strip trailing whitespace */
400 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
401 value[strlen(value)-1] = '\0';
403 int ws_num = atoi(ws_str);
405 if (ws_num < 1 || ws_num > 10)
406 die("Malformed name, invalid workspace number\n");
410 name += strlen(ws_str) + 1;
412 if (strncasecmp(name, "screen ", strlen("screen ")) == 0) {
413 char *screen = strdup(name + strlen("screen "));
414 if ((end = strchr(screen, ' ')) != NULL)
416 LOG("Setting preferred screen for workspace %d to \"%s\"\n", ws_num, screen);
417 workspaces[ws_num - 1].preferred_screen = screen;
419 name += strlen("screen ") + strlen(screen);
422 /* Strip leading whitespace */
423 while (*name != '\0' && *name == ' ')
426 LOG("rest to parse = %s\n", name);
433 LOG("setting name to \"%s\"\n", name);
436 workspace_set_name(&(workspaces[ws_num - 1]), name);
441 /* assign window class[/window title] → workspace */
442 if (strcasecmp(key, "assign") == 0) {
443 LOG("assign: \"%s\"\n", value);
448 /* If the window class/title is quoted we skip quotes */
449 if (value[0] == '"') {
450 class_title = sstrdup(value+1);
451 end = strchr(class_title, '"');
453 class_title = sstrdup(value);
454 /* If it is not quoted, we terminate it at the first space */
455 end = strchr(class_title, ' ');
458 die("Malformed assignment, couldn't find terminating quote\n");
461 /* Strip trailing whitespace */
462 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
463 value[strlen(value)-1] = '\0';
465 /* The target is the last argument separated by a space */
466 if ((target = strrchr(value, ' ')) == NULL)
467 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
470 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
471 die("Malformed assignment, invalid workspace number\n");
473 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
475 struct Assignment *new = scalloc(sizeof(struct Assignment));
476 new->windowclass_title = class_title;
477 if (strchr(target, '~') != NULL)
478 new->floating = ASSIGN_FLOATING_ONLY;
480 while (*target == '~')
483 if (atoi(target) >= 1 && atoi(target) <= 10) {
484 if (new->floating == ASSIGN_FLOATING_ONLY)
485 new->floating = ASSIGN_FLOATING;
486 new->workspace = atoi(target);
488 TAILQ_INSERT_TAIL(&assignments, new, assignments);
490 LOG("Assignment loaded: \"%s\":\n", class_title);
491 if (new->floating != ASSIGN_FLOATING_ONLY)
492 LOG(" to workspace %d\n", new->workspace);
494 if (new->floating != ASSIGN_FLOATING_NO)
495 LOG(" will be floating\n");
500 /* set a custom variable */
501 if (strcasecmp(key, "set") == 0) {
503 die("Malformed variable assignment, name has to start with $\n");
505 /* get key/value for this variable */
506 char *v_key = value, *v_value;
507 if ((v_value = strstr(value, " ")) == NULL)
508 die("Malformed variable assignment, need a value\n");
512 struct Variable *new = scalloc(sizeof(struct Variable));
513 new->key = sstrdup(v_key);
514 new->value = sstrdup(v_value);
515 SLIST_INSERT_HEAD(&variables, new, variables);
516 LOG("Got new variable %s = %s\n", v_key, v_value);
520 if (strcasecmp(key, "ipc-socket") == 0) {
521 config.ipc_socket_path = sstrdup(value);
525 die("Unknown configfile option: %s\n", key);
527 /* now grab all keys again */
532 while (!SLIST_EMPTY(&variables)) {
533 struct Variable *v = SLIST_FIRST(&variables);
534 SLIST_REMOVE_HEAD(&variables, variables);
541 REQUIRED_OPTION(terminal);
542 REQUIRED_OPTION(font);
544 /* Set an empty name for every workspace which got no name */
545 for (int i = 0; i < 10; i++) {
546 Workspace *ws = &(workspaces[i]);
547 if (ws->name != NULL) {
548 /* If the font was not specified when the workspace name
549 * was loaded, we need to predict the text width now */
550 if (ws->text_width == 0)
551 ws->text_width = predict_text_width(global_conn,
552 config.font, ws->name, ws->name_len);
556 workspace_set_name(&(workspaces[i]), NULL);