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);
190 SLIST_REMOVE(&modes, mode, Mode, modes);
193 struct Assignment *assign;
194 while (!TAILQ_EMPTY(&assignments)) {
195 assign = TAILQ_FIRST(&assignments);
196 FREE(assign->windowclass_title);
197 TAILQ_REMOVE(&assignments, assign, assignments);
204 struct Mode *default_mode = scalloc(sizeof(struct Mode));
205 default_mode->name = sstrdup("default");
206 default_mode->bindings = scalloc(sizeof(struct bindings_head));
207 TAILQ_INIT(default_mode->bindings);
208 SLIST_INSERT_HEAD(&modes, default_mode, modes);
210 bindings = default_mode->bindings;
212 SLIST_HEAD(variables_head, Variable) variables;
214 #define OPTION_STRING(name) \
215 if (strcasecmp(key, #name) == 0) { \
216 config.name = sstrdup(value); \
220 #define REQUIRED_OPTION(name) \
221 if (config.name == NULL) \
222 die("You did not specify required configuration option " #name "\n");
224 #define OPTION_COLORTRIPLE(opt, name) \
225 if (strcasecmp(key, opt) == 0) { \
226 char border[8], background[8], text[8]; \
227 memset(border, 0, sizeof(border)); \
228 memset(background, 0, sizeof(background)); \
229 memset(text, 0, sizeof(text)); \
230 border[0] = background[0] = text[0] = '#'; \
231 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
232 border + 1, background + 1, text + 1) != 3 || \
233 strlen(border) != 7 || \
234 strlen(background) != 7 || \
236 die("invalid color code line: %s\n", value); \
237 config.name.border = get_colorpixel(conn, border); \
238 config.name.background = get_colorpixel(conn, background); \
239 config.name.text = get_colorpixel(conn, text); \
243 /* Clear the old config or initialize the data structure */
244 memset(&config, 0, sizeof(config));
246 SLIST_INIT(&variables);
248 /* Initialize default colors */
249 config.client.focused.border = get_colorpixel(conn, "#4c7899");
250 config.client.focused.background = get_colorpixel(conn, "#285577");
251 config.client.focused.text = get_colorpixel(conn, "#ffffff");
253 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
254 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
255 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
257 config.client.unfocused.border = get_colorpixel(conn, "#333333");
258 config.client.unfocused.background = get_colorpixel(conn, "#222222");
259 config.client.unfocused.text = get_colorpixel(conn, "#888888");
261 config.client.urgent.border = get_colorpixel(conn, "#2f343a");
262 config.client.urgent.background = get_colorpixel(conn, "#900000");
263 config.client.urgent.text = get_colorpixel(conn, "#ffffff");
265 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
266 config.bar.focused.background = get_colorpixel(conn, "#285577");
267 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
269 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
270 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
271 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
273 config.bar.urgent.border = get_colorpixel(conn, "#2f343a");
274 config.bar.urgent.background = get_colorpixel(conn, "#900000");
275 config.bar.urgent.text = get_colorpixel(conn, "#ffffff");
277 if (config_use_lexer) {
278 /* Yes, this will be cleaned up soon. */
279 if (override_configpath != NULL) {
280 parse_file(override_configpath);
283 char *globbed = glob_path("~/.i3/config");
284 if ((handle = fopen(globbed, "r")) == NULL) {
285 if ((handle = fopen("/etc/i3/config", "r")) == NULL) {
286 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
288 parse_file("/etc/i3/config");
299 if (override_configpath != NULL) {
300 if ((handle = fopen(override_configpath, "r")) == NULL)
301 die("Could not open configfile \"%s\".\n", override_configpath);
303 /* We first check for ~/.i3/config, then for /etc/i3/config */
304 char *globbed = glob_path("~/.i3/config");
305 if ((handle = fopen(globbed, "r")) == NULL)
306 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
307 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
310 char key[512], value[512], buffer[1026];
312 while (!feof(handle)) {
313 if (fgets(buffer, 1024, handle) == NULL) {
314 /* fgets returns NULL on EOF and on error, so see which one it is. */
317 die("Could not read configuration file\n");
320 if (config.terminal != NULL)
321 replace_variable(buffer, "$terminal", config.terminal);
323 /* Replace all custom variables */
324 struct Variable *current;
325 SLIST_FOREACH(current, &variables, variables)
326 replace_variable(buffer, current->key, current->value);
328 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
329 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
330 key[0] == '#' || strlen(key) < 3)
333 OPTION_STRING(terminal);
337 OPTION_COLORTRIPLE("client.focused", client.focused);
338 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
339 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
340 OPTION_COLORTRIPLE("client.urgent", client.urgent);
341 OPTION_COLORTRIPLE("bar.focused", bar.focused);
342 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
343 OPTION_COLORTRIPLE("bar.urgent", bar.urgent);
345 /* exec-lines (autostart) */
346 if (strcasecmp(key, "exec") == 0) {
347 struct Autostart *new = smalloc(sizeof(struct Autostart));
348 new->command = sstrdup(value);
349 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
354 if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
355 #define CHECK_MODIFIER(name) \
356 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
357 modifiers |= BIND_##name; \
358 walk += strlen(#name) + 1; \
361 char *walk = value, *rest;
362 uint32_t modifiers = 0;
364 while (*walk != '\0') {
365 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
366 CHECK_MODIFIER(SHIFT);
367 CHECK_MODIFIER(CONTROL);
368 CHECK_MODIFIER(MODE_SWITCH);
369 CHECK_MODIFIER(MOD1);
370 CHECK_MODIFIER(MOD2);
371 CHECK_MODIFIER(MOD3);
372 CHECK_MODIFIER(MOD4);
373 CHECK_MODIFIER(MOD5);
375 /* No modifier found? Then we’re done with this step */
379 Binding *new = scalloc(sizeof(Binding));
381 /* Now check for the keycode or copy the symbol */
382 if (strcasecmp(key, "bind") == 0) {
383 int keycode = strtol(walk, &rest, 10);
384 if (!rest || *rest != ' ')
385 die("Invalid binding (keycode)\n");
386 new->keycode = keycode;
390 while (*rest != '\0' && *rest != ' ')
393 die("Invalid binding (keysym)\n");
394 #if defined(__OpenBSD__)
395 size_t len = strlen(sym);
396 if (len > (rest - sym))
398 new->symbol = smalloc(len + 1);
399 memcpy(new->symbol, sym, len+1);
400 new->symbol[len]='\0';
402 new->symbol = strndup(sym, (rest - sym));
406 LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
407 new->mods = modifiers;
408 new->command = sstrdup(rest);
409 TAILQ_INSERT_TAIL(bindings, new, bindings);
413 if (strcasecmp(key, "floating_modifier") == 0) {
415 uint32_t modifiers = 0;
417 while (*walk != '\0') {
418 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
419 CHECK_MODIFIER(SHIFT);
420 CHECK_MODIFIER(CONTROL);
421 CHECK_MODIFIER(MODE_SWITCH);
422 CHECK_MODIFIER(MOD1);
423 CHECK_MODIFIER(MOD2);
424 CHECK_MODIFIER(MOD3);
425 CHECK_MODIFIER(MOD4);
426 CHECK_MODIFIER(MOD5);
428 /* No modifier found? Then we’re done with this step */
432 LOG("Floating modifiers = %d\n", modifiers);
433 config.floating_modifier = modifiers;
437 /* workspace "workspace number" [screen <screen>] ["name of the workspace"]
438 * with screen := <number> | <position>, e.g. screen 1280 or screen 1 */
439 if (strcasecmp(key, "name") == 0 || strcasecmp(key, "workspace") == 0) {
440 LOG("workspace: %s\n",value);
441 char *ws_str = sstrdup(value);
442 char *end = strchr(ws_str, ' ');
444 die("Malformed name, couln't find terminating space\n");
447 /* Strip trailing whitespace */
448 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
449 value[strlen(value)-1] = '\0';
451 int ws_num = atoi(ws_str);
453 if (ws_num < 1 || ws_num > 10)
454 die("Malformed name, invalid workspace number\n");
458 name += strlen(ws_str) + 1;
460 if (strncasecmp(name, "screen ", strlen("screen ")) == 0) {
461 char *screen = strdup(name + strlen("screen "));
462 if ((end = strchr(screen, ' ')) != NULL)
464 LOG("Setting preferred screen for workspace %d to \"%s\"\n", ws_num, screen);
465 workspace_get(ws_num-1)->preferred_screen = screen;
467 name += strlen("screen ") + strlen(screen);
470 /* Strip leading whitespace */
471 while (*name != '\0' && *name == ' ')
474 LOG("rest to parse = %s\n", name);
481 LOG("setting name to \"%s\"\n", name);
484 workspace_set_name(&(workspaces[ws_num - 1]), name);
489 /* assign window class[/window title] → workspace */
490 if (strcasecmp(key, "assign") == 0) {
491 LOG("assign: \"%s\"\n", value);
496 /* If the window class/title is quoted we skip quotes */
497 if (value[0] == '"') {
498 class_title = sstrdup(value+1);
499 end = strchr(class_title, '"');
501 class_title = sstrdup(value);
502 /* If it is not quoted, we terminate it at the first space */
503 end = strchr(class_title, ' ');
506 die("Malformed assignment, couldn't find terminating quote\n");
509 /* Strip trailing whitespace */
510 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
511 value[strlen(value)-1] = '\0';
513 /* The target is the last argument separated by a space */
514 if ((target = strrchr(value, ' ')) == NULL)
515 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
518 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
519 die("Malformed assignment, invalid workspace number\n");
521 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
523 struct Assignment *new = scalloc(sizeof(struct Assignment));
524 new->windowclass_title = class_title;
525 if (strchr(target, '~') != NULL)
526 new->floating = ASSIGN_FLOATING_ONLY;
528 while (*target == '~')
531 if (atoi(target) >= 1) {
532 if (new->floating == ASSIGN_FLOATING_ONLY)
533 new->floating = ASSIGN_FLOATING;
534 new->workspace = atoi(target);
536 TAILQ_INSERT_TAIL(&assignments, new, assignments);
538 LOG("Assignment loaded: \"%s\":\n", class_title);
539 if (new->floating != ASSIGN_FLOATING_ONLY)
540 LOG(" to workspace %d\n", new->workspace);
542 if (new->floating != ASSIGN_FLOATING_NO)
543 LOG(" will be floating\n");
548 /* set a custom variable */
549 if (strcasecmp(key, "set") == 0) {
551 die("Malformed variable assignment, name has to start with $\n");
553 /* get key/value for this variable */
554 char *v_key = value, *v_value;
555 if ((v_value = strstr(value, " ")) == NULL)
556 die("Malformed variable assignment, need a value\n");
560 struct Variable *new = scalloc(sizeof(struct Variable));
561 new->key = sstrdup(v_key);
562 new->value = sstrdup(v_value);
563 SLIST_INSERT_HEAD(&variables, new, variables);
564 LOG("Got new variable %s = %s\n", v_key, v_value);
568 if (strcasecmp(key, "ipc-socket") == 0) {
569 config.ipc_socket_path = sstrdup(value);
573 die("Unknown configfile option: %s\n", key);
575 /* now grab all keys again */
580 while (!SLIST_EMPTY(&variables)) {
581 struct Variable *v = SLIST_FIRST(&variables);
582 SLIST_REMOVE_HEAD(&variables, variables);
589 REQUIRED_OPTION(terminal);
590 REQUIRED_OPTION(font);
592 /* Set an empty name for every workspace which got no name */
593 for (int i = 0; i < num_workspaces; i++) {
594 Workspace *ws = &(workspaces[i]);
595 if (ws->name != NULL) {
596 /* If the font was not specified when the workspace name
597 * was loaded, we need to predict the text width now */
598 if (ws->text_width == 0)
599 ws->text_width = predict_text_width(global_conn,
600 config.font, ws->name, ws->name_len);
604 workspace_set_name(&(workspaces[i]), NULL);