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 #ifdef OLD_XCB_KEYSYMS_API
118 bind->number_keycodes = 1;
119 xcb_keycode_t code = xcb_key_symbols_get_keycode(keysyms, keysym);
120 LOG("Translated symbol \"%s\" to 1 keycode (%d)\n", bind->symbol, code);
121 grab_keycode_for_binding(conn, bind, code);
122 bind->translated_to = smalloc(sizeof(xcb_keycode_t));
123 memcpy(bind->translated_to, &code, sizeof(xcb_keycode_t));
125 uint32_t last_keycode = 0;
126 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
127 if (keycodes == NULL) {
128 LOG("Could not translate symbol \"%s\"\n", bind->symbol);
132 bind->number_keycodes = 0;
134 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
135 /* We hope duplicate keycodes will be returned in order
137 if (last_keycode == *walk)
139 grab_keycode_for_binding(conn, bind, *walk);
140 last_keycode = *walk;
141 bind->number_keycodes++;
143 LOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
144 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
145 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
152 * Switches the key bindings to the given mode, if the mode exists
155 void switch_mode(xcb_connection_t *conn, const char *new_mode) {
158 LOG("Switching to mode %s\n", new_mode);
160 SLIST_FOREACH(mode, &modes, modes) {
161 if (strcasecmp(mode->name, new_mode) != 0)
164 ungrab_all_keys(conn);
165 bindings = mode->bindings;
170 LOG("ERROR: Mode not found\n");
174 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
176 * If you specify override_configpath, only this path is used to look for a
177 * configuration file.
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 SLIST_HEAD(variables_head, Variable) variables;
225 #define OPTION_STRING(name) \
226 if (strcasecmp(key, #name) == 0) { \
227 config.name = sstrdup(value); \
231 #define REQUIRED_OPTION(name) \
232 if (config.name == NULL) \
233 die("You did not specify required configuration option " #name "\n");
235 #define OPTION_COLORTRIPLE(opt, name) \
236 if (strcasecmp(key, opt) == 0) { \
237 char border[8], background[8], text[8]; \
238 memset(border, 0, sizeof(border)); \
239 memset(background, 0, sizeof(background)); \
240 memset(text, 0, sizeof(text)); \
241 border[0] = background[0] = text[0] = '#'; \
242 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
243 border + 1, background + 1, text + 1) != 3 || \
244 strlen(border) != 7 || \
245 strlen(background) != 7 || \
247 die("invalid color code line: %s\n", value); \
248 config.name.border = get_colorpixel(conn, border); \
249 config.name.background = get_colorpixel(conn, background); \
250 config.name.text = get_colorpixel(conn, text); \
254 /* Clear the old config or initialize the data structure */
255 memset(&config, 0, sizeof(config));
257 SLIST_INIT(&variables);
259 /* Initialize default colors */
260 config.client.focused.border = get_colorpixel(conn, "#4c7899");
261 config.client.focused.background = get_colorpixel(conn, "#285577");
262 config.client.focused.text = get_colorpixel(conn, "#ffffff");
264 config.client.focused_inactive.border = get_colorpixel(conn, "#333333");
265 config.client.focused_inactive.background = get_colorpixel(conn, "#5f676a");
266 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
268 config.client.unfocused.border = get_colorpixel(conn, "#333333");
269 config.client.unfocused.background = get_colorpixel(conn, "#222222");
270 config.client.unfocused.text = get_colorpixel(conn, "#888888");
272 config.client.urgent.border = get_colorpixel(conn, "#2f343a");
273 config.client.urgent.background = get_colorpixel(conn, "#900000");
274 config.client.urgent.text = get_colorpixel(conn, "#ffffff");
276 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
277 config.bar.focused.background = get_colorpixel(conn, "#285577");
278 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
280 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
281 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
282 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
284 config.bar.urgent.border = get_colorpixel(conn, "#2f343a");
285 config.bar.urgent.background = get_colorpixel(conn, "#900000");
286 config.bar.urgent.text = get_colorpixel(conn, "#ffffff");
288 if (config_use_lexer) {
289 /* Yes, this will be cleaned up soon. */
290 if (override_configpath != NULL) {
291 parse_file(override_configpath);
294 char *globbed = glob_path("~/.i3/config");
295 if ((handle = fopen(globbed, "r")) == NULL) {
296 if ((handle = fopen("/etc/i3/config", "r")) == NULL) {
297 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
299 parse_file("/etc/i3/config");
310 if (override_configpath != NULL) {
311 if ((handle = fopen(override_configpath, "r")) == NULL)
312 die("Could not open configfile \"%s\".\n", override_configpath);
314 /* We first check for ~/.i3/config, then for /etc/i3/config */
315 char *globbed = glob_path("~/.i3/config");
316 if ((handle = fopen(globbed, "r")) == NULL)
317 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
318 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
321 char key[512], value[512], buffer[1026];
323 while (!feof(handle)) {
324 if (fgets(buffer, 1024, handle) == NULL) {
325 /* fgets returns NULL on EOF and on error, so see which one it is. */
328 die("Could not read configuration file\n");
331 if (config.terminal != NULL)
332 replace_variable(buffer, "$terminal", config.terminal);
334 /* Replace all custom variables */
335 struct Variable *current;
336 SLIST_FOREACH(current, &variables, variables)
337 replace_variable(buffer, current->key, current->value);
339 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
340 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
341 key[0] == '#' || strlen(key) < 3)
344 OPTION_STRING(terminal);
348 OPTION_COLORTRIPLE("client.focused", client.focused);
349 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
350 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
351 OPTION_COLORTRIPLE("client.urgent", client.urgent);
352 OPTION_COLORTRIPLE("bar.focused", bar.focused);
353 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
354 OPTION_COLORTRIPLE("bar.urgent", bar.urgent);
356 /* exec-lines (autostart) */
357 if (strcasecmp(key, "exec") == 0) {
358 struct Autostart *new = smalloc(sizeof(struct Autostart));
359 new->command = sstrdup(value);
360 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
365 if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
366 #define CHECK_MODIFIER(name) \
367 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
368 modifiers |= BIND_##name; \
369 walk += strlen(#name) + 1; \
372 char *walk = value, *rest;
373 uint32_t modifiers = 0;
375 while (*walk != '\0') {
376 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
377 CHECK_MODIFIER(SHIFT);
378 CHECK_MODIFIER(CONTROL);
379 CHECK_MODIFIER(MODE_SWITCH);
380 CHECK_MODIFIER(MOD1);
381 CHECK_MODIFIER(MOD2);
382 CHECK_MODIFIER(MOD3);
383 CHECK_MODIFIER(MOD4);
384 CHECK_MODIFIER(MOD5);
386 /* No modifier found? Then we’re done with this step */
390 Binding *new = scalloc(sizeof(Binding));
392 /* Now check for the keycode or copy the symbol */
393 if (strcasecmp(key, "bind") == 0) {
394 int keycode = strtol(walk, &rest, 10);
395 if (!rest || *rest != ' ')
396 die("Invalid binding (keycode)\n");
397 new->keycode = keycode;
401 while (*rest != '\0' && *rest != ' ')
404 die("Invalid binding (keysym)\n");
405 #if defined(__OpenBSD__)
406 size_t len = strlen(sym);
407 if (len > (rest - sym))
409 new->symbol = smalloc(len + 1);
410 memcpy(new->symbol, sym, len+1);
411 new->symbol[len]='\0';
413 new->symbol = strndup(sym, (rest - sym));
417 LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
418 new->mods = modifiers;
419 new->command = sstrdup(rest);
420 TAILQ_INSERT_TAIL(bindings, new, bindings);
424 if (strcasecmp(key, "floating_modifier") == 0) {
426 uint32_t modifiers = 0;
428 while (*walk != '\0') {
429 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
430 CHECK_MODIFIER(SHIFT);
431 CHECK_MODIFIER(CONTROL);
432 CHECK_MODIFIER(MODE_SWITCH);
433 CHECK_MODIFIER(MOD1);
434 CHECK_MODIFIER(MOD2);
435 CHECK_MODIFIER(MOD3);
436 CHECK_MODIFIER(MOD4);
437 CHECK_MODIFIER(MOD5);
439 /* No modifier found? Then we’re done with this step */
443 LOG("Floating modifiers = %d\n", modifiers);
444 config.floating_modifier = modifiers;
448 /* workspace "workspace number" [screen <screen>] ["name of the workspace"]
449 * with screen := <number> | <position>, e.g. screen 1280 or screen 1 */
450 if (strcasecmp(key, "name") == 0 || strcasecmp(key, "workspace") == 0) {
451 LOG("workspace: %s\n",value);
452 char *ws_str = sstrdup(value);
453 char *end = strchr(ws_str, ' ');
455 die("Malformed name, couln't find terminating space\n");
458 /* Strip trailing whitespace */
459 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
460 value[strlen(value)-1] = '\0';
462 int ws_num = atoi(ws_str);
464 if (ws_num < 1 || ws_num > 10)
465 die("Malformed name, invalid workspace number\n");
469 name += strlen(ws_str) + 1;
471 if (strncasecmp(name, "screen ", strlen("screen ")) == 0) {
472 char *screen = strdup(name + strlen("screen "));
473 if ((end = strchr(screen, ' ')) != NULL)
475 LOG("Setting preferred screen for workspace %d to \"%s\"\n", ws_num, screen);
476 workspace_get(ws_num-1)->preferred_screen = screen;
478 name += strlen("screen ") + strlen(screen);
481 /* Strip leading whitespace */
482 while (*name != '\0' && *name == ' ')
485 LOG("rest to parse = %s\n", name);
492 LOG("setting name to \"%s\"\n", name);
495 workspace_set_name(workspace_get(ws_num - 1), name);
500 /* assign window class[/window title] → workspace */
501 if (strcasecmp(key, "assign") == 0) {
502 LOG("assign: \"%s\"\n", value);
507 /* If the window class/title is quoted we skip quotes */
508 if (value[0] == '"') {
509 class_title = sstrdup(value+1);
510 end = strchr(class_title, '"');
512 class_title = sstrdup(value);
513 /* If it is not quoted, we terminate it at the first space */
514 end = strchr(class_title, ' ');
517 die("Malformed assignment, couldn't find terminating quote\n");
520 /* Strip trailing whitespace */
521 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
522 value[strlen(value)-1] = '\0';
524 /* Strip trailing whitespace */
525 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
526 value[strlen(value)-1] = '\0';
528 /* The target is the last argument separated by a space */
529 if ((target = strrchr(value, ' ')) == NULL)
530 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
533 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
534 die("Malformed assignment, invalid workspace number\n");
536 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
538 struct Assignment *new = scalloc(sizeof(struct Assignment));
539 new->windowclass_title = class_title;
540 if (strchr(target, '~') != NULL)
541 new->floating = ASSIGN_FLOATING_ONLY;
543 while (*target == '~')
546 if (atoi(target) >= 1) {
547 if (new->floating == ASSIGN_FLOATING_ONLY)
548 new->floating = ASSIGN_FLOATING;
549 new->workspace = atoi(target);
551 TAILQ_INSERT_TAIL(&assignments, new, assignments);
553 LOG("Assignment loaded: \"%s\":\n", class_title);
554 if (new->floating != ASSIGN_FLOATING_ONLY)
555 LOG(" to workspace %d\n", new->workspace);
557 if (new->floating != ASSIGN_FLOATING_NO)
558 LOG(" will be floating\n");
563 /* set a custom variable */
564 if (strcasecmp(key, "set") == 0) {
566 die("Malformed variable assignment, name has to start with $\n");
568 /* get key/value for this variable */
569 char *v_key = value, *v_value;
570 if ((v_value = strstr(value, " ")) == NULL)
571 die("Malformed variable assignment, need a value\n");
575 struct Variable *new = scalloc(sizeof(struct Variable));
576 new->key = sstrdup(v_key);
577 new->value = sstrdup(v_value);
578 SLIST_INSERT_HEAD(&variables, new, variables);
579 LOG("Got new variable %s = %s\n", v_key, v_value);
583 if (strcasecmp(key, "ipc-socket") == 0) {
584 config.ipc_socket_path = sstrdup(value);
588 die("Unknown configfile option: %s\n", key);
590 /* now grab all keys again */
595 while (!SLIST_EMPTY(&variables)) {
596 struct Variable *v = SLIST_FIRST(&variables);
597 SLIST_REMOVE_HEAD(&variables, variables);
604 REQUIRED_OPTION(terminal);
605 REQUIRED_OPTION(font);
607 /* Set an empty name for every workspace which got no name */
609 TAILQ_FOREACH(ws, workspaces, workspaces) {
610 if (ws->name != NULL) {
611 /* If the font was not specified when the workspace name
612 * was loaded, we need to predict the text width now */
613 if (ws->text_width == 0)
614 ws->text_width = predict_text_width(global_conn,
615 config.font, ws->name, ws->name_len);
619 workspace_set_name(ws, NULL);