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"
29 /* prototype for src/cfgparse.y, will be cleaned up as soon as we completely
30 * switched to the new scanner/parser. */
31 void parse_file(const char *f);
34 struct modes_head modes;
36 bool config_use_lexer = false;
39 * This function resolves ~ in pathnames.
42 static char *glob_path(const char *path) {
43 static glob_t globbuf;
44 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
46 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
52 * This function does a very simple replacement of each instance of key with value.
55 static void replace_variable(char *buffer, const char *key, const char *value) {
57 /* To prevent endless recursions when the user makes an error configuring,
58 * we stop after 100 replacements. That should be vastly more than enough. */
60 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
61 char *rest = pos + strlen(key);
64 asprintf(&replaced, "%s%s%s", buffer, value, rest);
65 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
66 * So we need to hard-code the dimensions here. */
67 strncpy(buffer, replaced, 1026);
73 * Ungrabs all keys, to be called before re-grabbing the keys because of a
74 * mapping_notify event or a configuration file reload
77 void ungrab_all_keys(xcb_connection_t *conn) {
78 LOG("Ungrabbing all keys\n");
79 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
82 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
83 LOG("Grabbing %d\n", keycode);
84 if ((bind->mods & BIND_MODE_SWITCH) != 0)
85 xcb_grab_key(conn, 0, root, 0, keycode,
86 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
88 /* Grab the key in all combinations */
89 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
90 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
92 GRAB_KEY(bind->mods | xcb_numlock_mask);
93 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
98 * Grab the bound keys (tell X to send us keypress events for those keycodes)
101 void grab_all_keys(xcb_connection_t *conn) {
103 TAILQ_FOREACH(bind, bindings, bindings) {
104 /* The easy case: the user specified a keycode directly. */
105 if (bind->keycode > 0) {
106 grab_keycode_for_binding(conn, bind, bind->keycode);
110 /* We need to translate the symbol to a keycode */
111 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
112 if (keysym == NoSymbol) {
113 LOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
117 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
118 if (keycodes == NULL) {
119 LOG("Could not translate symbol \"%s\"\n", bind->symbol);
123 uint32_t last_keycode = 0;
124 bind->number_keycodes = 0;
125 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
126 /* We hope duplicate keycodes will be returned in order
128 if (last_keycode == *walk)
130 grab_keycode_for_binding(conn, bind, *walk);
131 last_keycode = *walk;
132 bind->number_keycodes++;
134 LOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
135 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
136 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
142 * Switches the key bindings to the given mode, if the mode exists
145 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
148 LOG("Switching to mode %s\n", new_mode);
150 SLIST_FOREACH(mode, &modes, modes) {
151 if (strcasecmp(mode->name, new_mode) != 0)
154 ungrab_all_keys(conn);
155 bindings = mode->bindings;
160 LOG("ERROR: Mode not found\n");
164 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
166 * If you specify override_configpath, only this path is used to look for a
167 * configuration file.
170 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
172 /* First ungrab the keys */
173 ungrab_all_keys(conn);
177 while (!SLIST_EMPTY(&modes)) {
178 mode = SLIST_FIRST(&modes);
181 /* Clear the old binding list */
182 bindings = mode->bindings;
183 while (!TAILQ_EMPTY(bindings)) {
184 bind = TAILQ_FIRST(bindings);
185 TAILQ_REMOVE(bindings, bind, bindings);
186 FREE(bind->translated_to);
191 SLIST_REMOVE(&modes, mode, Mode, modes);
194 struct Assignment *assign;
195 while (!TAILQ_EMPTY(&assignments)) {
196 assign = TAILQ_FIRST(&assignments);
197 FREE(assign->windowclass_title);
198 TAILQ_REMOVE(&assignments, assign, assignments);
205 struct Mode *default_mode = scalloc(sizeof(struct Mode));
206 default_mode->name = sstrdup("default");
207 default_mode->bindings = scalloc(sizeof(struct bindings_head));
208 TAILQ_INIT(default_mode->bindings);
209 SLIST_INSERT_HEAD(&modes, default_mode, modes);
211 bindings = default_mode->bindings;
213 SLIST_HEAD(variables_head, Variable) variables;
215 #define OPTION_STRING(name) \
216 if (strcasecmp(key, #name) == 0) { \
217 config.name = sstrdup(value); \
221 #define REQUIRED_OPTION(name) \
222 if (config.name == NULL) \
223 die("You did not specify required configuration option " #name "\n");
225 #define OPTION_COLORTRIPLE(opt, name) \
226 if (strcasecmp(key, opt) == 0) { \
227 char border[8], background[8], text[8]; \
228 memset(border, 0, sizeof(border)); \
229 memset(background, 0, sizeof(background)); \
230 memset(text, 0, sizeof(text)); \
231 border[0] = background[0] = text[0] = '#'; \
232 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
233 border + 1, background + 1, text + 1) != 3 || \
234 strlen(border) != 7 || \
235 strlen(background) != 7 || \
237 die("invalid color code line: %s\n", value); \
238 config.name.border = get_colorpixel(conn, border); \
239 config.name.background = get_colorpixel(conn, background); \
240 config.name.text = get_colorpixel(conn, text); \
244 /* Clear the old config or initialize the data structure */
245 memset(&config, 0, sizeof(config));
247 SLIST_INIT(&variables);
249 /* Initialize default colors */
250 config.client.focused.border = get_colorpixel(conn, "#4c7899");
251 config.client.focused.background = get_colorpixel(conn, "#285577");
252 config.client.focused.text = get_colorpixel(conn, "#ffffff");
254 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
255 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
256 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
258 config.client.unfocused.border = get_colorpixel(conn, "#333333");
259 config.client.unfocused.background = get_colorpixel(conn, "#222222");
260 config.client.unfocused.text = get_colorpixel(conn, "#888888");
262 config.client.urgent.border = get_colorpixel(conn, "#2f343a");
263 config.client.urgent.background = get_colorpixel(conn, "#900000");
264 config.client.urgent.text = get_colorpixel(conn, "#ffffff");
266 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
267 config.bar.focused.background = get_colorpixel(conn, "#285577");
268 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
270 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
271 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
272 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
274 config.bar.urgent.border = get_colorpixel(conn, "#2f343a");
275 config.bar.urgent.background = get_colorpixel(conn, "#900000");
276 config.bar.urgent.text = get_colorpixel(conn, "#ffffff");
278 if (config_use_lexer) {
279 /* Yes, this will be cleaned up soon. */
280 if (override_configpath != NULL) {
281 parse_file(override_configpath);
284 char *globbed = glob_path("~/.i3/config");
285 if ((handle = fopen(globbed, "r")) == NULL) {
286 if ((handle = fopen("/etc/i3/config", "r")) == NULL) {
287 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
289 parse_file("/etc/i3/config");
300 if (override_configpath != NULL) {
301 if ((handle = fopen(override_configpath, "r")) == NULL)
302 die("Could not open configfile \"%s\".\n", override_configpath);
304 /* We first check for ~/.i3/config, then for /etc/i3/config */
305 char *globbed = glob_path("~/.i3/config");
306 if ((handle = fopen(globbed, "r")) == NULL)
307 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
308 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
311 char key[512], value[512], buffer[1026];
313 while (!feof(handle)) {
314 if (fgets(buffer, 1024, handle) == NULL) {
315 /* fgets returns NULL on EOF and on error, so see which one it is. */
318 die("Could not read configuration file\n");
321 if (config.terminal != NULL)
322 replace_variable(buffer, "$terminal", config.terminal);
324 /* Replace all custom variables */
325 struct Variable *current;
326 SLIST_FOREACH(current, &variables, variables)
327 replace_variable(buffer, current->key, current->value);
329 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
330 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
331 key[0] == '#' || strlen(key) < 3)
334 OPTION_STRING(terminal);
338 OPTION_COLORTRIPLE("client.focused", client.focused);
339 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
340 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
341 OPTION_COLORTRIPLE("client.urgent", client.urgent);
342 OPTION_COLORTRIPLE("bar.focused", bar.focused);
343 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
344 OPTION_COLORTRIPLE("bar.urgent", bar.urgent);
346 /* exec-lines (autostart) */
347 if (strcasecmp(key, "exec") == 0) {
348 struct Autostart *new = smalloc(sizeof(struct Autostart));
349 new->command = sstrdup(value);
350 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
355 if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
356 #define CHECK_MODIFIER(name) \
357 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
358 modifiers |= BIND_##name; \
359 walk += strlen(#name) + 1; \
362 char *walk = value, *rest;
363 uint32_t modifiers = 0;
365 while (*walk != '\0') {
366 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
367 CHECK_MODIFIER(SHIFT);
368 CHECK_MODIFIER(CONTROL);
369 CHECK_MODIFIER(MODE_SWITCH);
370 CHECK_MODIFIER(MOD1);
371 CHECK_MODIFIER(MOD2);
372 CHECK_MODIFIER(MOD3);
373 CHECK_MODIFIER(MOD4);
374 CHECK_MODIFIER(MOD5);
376 /* No modifier found? Then we’re done with this step */
380 Binding *new = scalloc(sizeof(Binding));
382 /* Now check for the keycode or copy the symbol */
383 if (strcasecmp(key, "bind") == 0) {
384 int keycode = strtol(walk, &rest, 10);
385 if (!rest || *rest != ' ')
386 die("Invalid binding (keycode)\n");
387 new->keycode = keycode;
391 while (*rest != '\0' && *rest != ' ')
394 die("Invalid binding (keysym)\n");
395 #if defined(__OpenBSD__)
396 size_t len = strlen(sym);
397 if (len > (rest - sym))
399 new->symbol = smalloc(len + 1);
400 memcpy(new->symbol, sym, len+1);
401 new->symbol[len]='\0';
403 new->symbol = strndup(sym, (rest - sym));
407 LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
408 new->mods = modifiers;
409 new->command = sstrdup(rest);
410 TAILQ_INSERT_TAIL(bindings, new, bindings);
414 if (strcasecmp(key, "floating_modifier") == 0) {
416 uint32_t modifiers = 0;
418 while (*walk != '\0') {
419 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
420 CHECK_MODIFIER(SHIFT);
421 CHECK_MODIFIER(CONTROL);
422 CHECK_MODIFIER(MODE_SWITCH);
423 CHECK_MODIFIER(MOD1);
424 CHECK_MODIFIER(MOD2);
425 CHECK_MODIFIER(MOD3);
426 CHECK_MODIFIER(MOD4);
427 CHECK_MODIFIER(MOD5);
429 /* No modifier found? Then we’re done with this step */
433 LOG("Floating modifiers = %d\n", modifiers);
434 config.floating_modifier = modifiers;
438 /* workspace "workspace number" [screen <screen>] ["name of the workspace"]
439 * with screen := <number> | <position>, e.g. screen 1280 or screen 1 */
440 if (strcasecmp(key, "name") == 0 || strcasecmp(key, "workspace") == 0) {
441 LOG("workspace: %s\n",value);
442 char *ws_str = sstrdup(value);
443 char *end = strchr(ws_str, ' ');
445 die("Malformed name, couln't find terminating space\n");
448 /* Strip trailing whitespace */
449 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
450 value[strlen(value)-1] = '\0';
452 int ws_num = atoi(ws_str);
454 if (ws_num < 1 || ws_num > 10)
455 die("Malformed name, invalid workspace number\n");
459 name += strlen(ws_str) + 1;
461 if (strncasecmp(name, "screen ", strlen("screen ")) == 0) {
462 char *screen = strdup(name + strlen("screen "));
463 if ((end = strchr(screen, ' ')) != NULL)
465 LOG("Setting preferred screen for workspace %d to \"%s\"\n", ws_num, screen);
466 workspace_get(ws_num-1)->preferred_screen = screen;
468 name += strlen("screen ") + strlen(screen);
471 /* Strip leading whitespace */
472 while (*name != '\0' && *name == ' ')
475 LOG("rest to parse = %s\n", name);
482 LOG("setting name to \"%s\"\n", name);
485 workspace_set_name(workspace_get(ws_num - 1), name);
490 /* assign window class[/window title] → workspace */
491 if (strcasecmp(key, "assign") == 0) {
492 LOG("assign: \"%s\"\n", value);
497 /* If the window class/title is quoted we skip quotes */
498 if (value[0] == '"') {
499 class_title = sstrdup(value+1);
500 end = strchr(class_title, '"');
502 class_title = sstrdup(value);
503 /* If it is not quoted, we terminate it at the first space */
504 end = strchr(class_title, ' ');
507 die("Malformed assignment, couldn't find terminating quote\n");
510 /* Strip trailing whitespace */
511 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
512 value[strlen(value)-1] = '\0';
514 /* The target is the last argument separated by a space */
515 if ((target = strrchr(value, ' ')) == NULL)
516 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
519 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
520 die("Malformed assignment, invalid workspace number\n");
522 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
524 struct Assignment *new = scalloc(sizeof(struct Assignment));
525 new->windowclass_title = class_title;
526 if (strchr(target, '~') != NULL)
527 new->floating = ASSIGN_FLOATING_ONLY;
529 while (*target == '~')
532 if (atoi(target) >= 1) {
533 if (new->floating == ASSIGN_FLOATING_ONLY)
534 new->floating = ASSIGN_FLOATING;
535 new->workspace = atoi(target);
537 TAILQ_INSERT_TAIL(&assignments, new, assignments);
539 LOG("Assignment loaded: \"%s\":\n", class_title);
540 if (new->floating != ASSIGN_FLOATING_ONLY)
541 LOG(" to workspace %d\n", new->workspace);
543 if (new->floating != ASSIGN_FLOATING_NO)
544 LOG(" will be floating\n");
549 /* set a custom variable */
550 if (strcasecmp(key, "set") == 0) {
552 die("Malformed variable assignment, name has to start with $\n");
554 /* get key/value for this variable */
555 char *v_key = value, *v_value;
556 if ((v_value = strstr(value, " ")) == NULL)
557 die("Malformed variable assignment, need a value\n");
561 struct Variable *new = scalloc(sizeof(struct Variable));
562 new->key = sstrdup(v_key);
563 new->value = sstrdup(v_value);
564 SLIST_INSERT_HEAD(&variables, new, variables);
565 LOG("Got new variable %s = %s\n", v_key, v_value);
569 if (strcasecmp(key, "ipc-socket") == 0) {
570 config.ipc_socket_path = sstrdup(value);
574 die("Unknown configfile option: %s\n", key);
576 /* now grab all keys again */
581 while (!SLIST_EMPTY(&variables)) {
582 struct Variable *v = SLIST_FIRST(&variables);
583 SLIST_REMOVE_HEAD(&variables, variables);
590 REQUIRED_OPTION(terminal);
591 REQUIRED_OPTION(font);
593 /* Set an empty name for every workspace which got no name */
595 TAILQ_FOREACH(ws, workspaces, workspaces) {
596 if (ws->name != NULL) {
597 /* If the font was not specified when the workspace name
598 * was loaded, we need to predict the text width now */
599 if (ws->text_width == 0)
600 ws->text_width = predict_text_width(global_conn,
601 config.font, ws->name, ws->name_len);
605 workspace_set_name(ws, NULL);