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);
35 bool config_use_lexer = false;
38 * This function resolves ~ in pathnames.
41 static char *glob_path(const char *path) {
42 static glob_t globbuf;
43 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
45 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
51 * This function does a very simple replacement of each instance of key with value.
54 static void replace_variable(char *buffer, const char *key, const char *value) {
56 /* To prevent endless recursions when the user makes an error configuring,
57 * we stop after 100 replacements. That should be vastly more than enough. */
59 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
60 char *rest = pos + strlen(key);
63 asprintf(&replaced, "%s%s%s", buffer, value, rest);
64 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
65 * So we need to hard-code the dimensions here. */
66 strncpy(buffer, replaced, 1026);
72 * Ungrabs all keys, to be called before re-grabbing the keys because of a
73 * mapping_notify event or a configuration file reload
76 void ungrab_all_keys(xcb_connection_t *conn) {
77 LOG("Ungrabbing all keys\n");
78 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
81 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
82 LOG("Grabbing %d\n", keycode);
83 if ((bind->mods & BIND_MODE_SWITCH) != 0)
84 xcb_grab_key(conn, 0, root, 0, keycode,
85 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
87 /* Grab the key in all combinations */
88 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
89 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
91 GRAB_KEY(bind->mods | xcb_numlock_mask);
92 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
97 * Grab the bound keys (tell X to send us keypress events for those keycodes)
100 void grab_all_keys(xcb_connection_t *conn) {
102 TAILQ_FOREACH(bind, &bindings, bindings) {
103 /* The easy case: the user specified a keycode directly. */
104 if (bind->keycode > 0) {
105 grab_keycode_for_binding(conn, bind, bind->keycode);
109 /* We need to translate the symbol to a keycode */
110 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
111 if (keysym == NoSymbol) {
112 LOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
116 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
117 if (keycodes == NULL) {
118 LOG("Could not translate symbol \"%s\"\n", bind->symbol);
122 uint32_t last_keycode = 0;
123 bind->number_keycodes = 0;
124 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
125 /* We hope duplicate keycodes will be returned in order
127 if (last_keycode == *walk)
129 grab_keycode_for_binding(conn, bind, *walk);
130 last_keycode = *walk;
131 bind->number_keycodes++;
133 LOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
134 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
135 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
141 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
143 * If you specify override_configpath, only this path is used to look for a
144 * configuration file.
147 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
149 /* First ungrab the keys */
150 ungrab_all_keys(conn);
152 /* Clear the old binding and assignment lists */
154 while (!TAILQ_EMPTY(&bindings)) {
155 bind = TAILQ_FIRST(&bindings);
156 TAILQ_REMOVE(&bindings, bind, bindings);
161 struct Assignment *assign;
162 while (!TAILQ_EMPTY(&assignments)) {
163 assign = TAILQ_FIRST(&assignments);
164 FREE(assign->windowclass_title);
165 TAILQ_REMOVE(&assignments, assign, assignments);
170 SLIST_HEAD(variables_head, Variable) variables;
172 #define OPTION_STRING(name) \
173 if (strcasecmp(key, #name) == 0) { \
174 config.name = sstrdup(value); \
178 #define REQUIRED_OPTION(name) \
179 if (config.name == NULL) \
180 die("You did not specify required configuration option " #name "\n");
182 #define OPTION_COLORTRIPLE(opt, name) \
183 if (strcasecmp(key, opt) == 0) { \
184 char border[8], background[8], text[8]; \
185 memset(border, 0, sizeof(border)); \
186 memset(background, 0, sizeof(background)); \
187 memset(text, 0, sizeof(text)); \
188 border[0] = background[0] = text[0] = '#'; \
189 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
190 border + 1, background + 1, text + 1) != 3 || \
191 strlen(border) != 7 || \
192 strlen(background) != 7 || \
194 die("invalid color code line: %s\n", value); \
195 config.name.border = get_colorpixel(conn, border); \
196 config.name.background = get_colorpixel(conn, background); \
197 config.name.text = get_colorpixel(conn, text); \
201 /* Clear the old config or initialize the data structure */
202 memset(&config, 0, sizeof(config));
204 SLIST_INIT(&variables);
206 /* Initialize default colors */
207 config.client.focused.border = get_colorpixel(conn, "#4c7899");
208 config.client.focused.background = get_colorpixel(conn, "#285577");
209 config.client.focused.text = get_colorpixel(conn, "#ffffff");
211 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
212 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
213 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
215 config.client.unfocused.border = get_colorpixel(conn, "#333333");
216 config.client.unfocused.background = get_colorpixel(conn, "#222222");
217 config.client.unfocused.text = get_colorpixel(conn, "#888888");
219 config.client.urgent.border = get_colorpixel(conn, "#2f343a");
220 config.client.urgent.background = get_colorpixel(conn, "#900000");
221 config.client.urgent.text = get_colorpixel(conn, "#ffffff");
223 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
224 config.bar.focused.background = get_colorpixel(conn, "#285577");
225 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
227 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
228 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
229 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
231 config.bar.urgent.border = get_colorpixel(conn, "#2f343a");
232 config.bar.urgent.background = get_colorpixel(conn, "#900000");
233 config.bar.urgent.text = get_colorpixel(conn, "#ffffff");
235 if (config_use_lexer) {
236 /* Yes, this will be cleaned up soon. */
237 if (override_configpath != NULL) {
238 parse_file(override_configpath);
241 char *globbed = glob_path("~/.i3/config");
242 if ((handle = fopen(globbed, "r")) == NULL) {
243 if ((handle = fopen("/etc/i3/config", "r")) == NULL) {
244 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
246 parse_file("/etc/i3/config");
255 if (override_configpath != NULL) {
256 if ((handle = fopen(override_configpath, "r")) == NULL)
257 die("Could not open configfile \"%s\".\n", override_configpath);
259 /* We first check for ~/.i3/config, then for /etc/i3/config */
260 char *globbed = glob_path("~/.i3/config");
261 if ((handle = fopen(globbed, "r")) == NULL)
262 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
263 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
266 char key[512], value[512], buffer[1026];
268 while (!feof(handle)) {
269 if (fgets(buffer, 1024, handle) == NULL) {
270 /* fgets returns NULL on EOF and on error, so see which one it is. */
273 die("Could not read configuration file\n");
276 if (config.terminal != NULL)
277 replace_variable(buffer, "$terminal", config.terminal);
279 /* Replace all custom variables */
280 struct Variable *current;
281 SLIST_FOREACH(current, &variables, variables)
282 replace_variable(buffer, current->key, current->value);
284 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
285 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
286 key[0] == '#' || strlen(key) < 3)
289 OPTION_STRING(terminal);
293 OPTION_COLORTRIPLE("client.focused", client.focused);
294 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
295 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
296 OPTION_COLORTRIPLE("client.urgent", client.urgent);
297 OPTION_COLORTRIPLE("bar.focused", bar.focused);
298 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
299 OPTION_COLORTRIPLE("bar.urgent", bar.urgent);
301 /* exec-lines (autostart) */
302 if (strcasecmp(key, "exec") == 0) {
303 struct Autostart *new = smalloc(sizeof(struct Autostart));
304 new->command = sstrdup(value);
305 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
310 if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
311 #define CHECK_MODIFIER(name) \
312 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
313 modifiers |= BIND_##name; \
314 walk += strlen(#name) + 1; \
317 char *walk = value, *rest;
318 uint32_t modifiers = 0;
320 while (*walk != '\0') {
321 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
322 CHECK_MODIFIER(SHIFT);
323 CHECK_MODIFIER(CONTROL);
324 CHECK_MODIFIER(MODE_SWITCH);
325 CHECK_MODIFIER(MOD1);
326 CHECK_MODIFIER(MOD2);
327 CHECK_MODIFIER(MOD3);
328 CHECK_MODIFIER(MOD4);
329 CHECK_MODIFIER(MOD5);
331 /* No modifier found? Then we’re done with this step */
335 Binding *new = scalloc(sizeof(Binding));
337 /* Now check for the keycode or copy the symbol */
338 if (strcasecmp(key, "bind") == 0) {
339 int keycode = strtol(walk, &rest, 10);
340 if (!rest || *rest != ' ')
341 die("Invalid binding (keycode)\n");
342 new->keycode = keycode;
346 while (*rest != '\0' && *rest != ' ')
349 die("Invalid binding (keysym)\n");
350 #if defined(__OpenBSD__)
351 size_t len = strlen(sym);
352 if (len > (rest - sym))
354 new->symbol = smalloc(len + 1);
355 memcpy(new->symbol, sym, len+1);
356 new->symbol[len]='\0';
358 new->symbol = strndup(sym, (rest - sym));
362 LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
363 new->mods = modifiers;
364 new->command = sstrdup(rest);
365 TAILQ_INSERT_TAIL(&bindings, new, bindings);
369 if (strcasecmp(key, "floating_modifier") == 0) {
371 uint32_t modifiers = 0;
373 while (*walk != '\0') {
374 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
375 CHECK_MODIFIER(SHIFT);
376 CHECK_MODIFIER(CONTROL);
377 CHECK_MODIFIER(MODE_SWITCH);
378 CHECK_MODIFIER(MOD1);
379 CHECK_MODIFIER(MOD2);
380 CHECK_MODIFIER(MOD3);
381 CHECK_MODIFIER(MOD4);
382 CHECK_MODIFIER(MOD5);
384 /* No modifier found? Then we’re done with this step */
388 LOG("Floating modifiers = %d\n", modifiers);
389 config.floating_modifier = modifiers;
393 /* workspace "workspace number" [screen <screen>] ["name of the workspace"]
394 * with screen := <number> | <position>, e.g. screen 1280 or screen 1 */
395 if (strcasecmp(key, "name") == 0 || strcasecmp(key, "workspace") == 0) {
396 LOG("workspace: %s\n",value);
397 char *ws_str = sstrdup(value);
398 char *end = strchr(ws_str, ' ');
400 die("Malformed name, couln't find terminating space\n");
403 /* Strip trailing whitespace */
404 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
405 value[strlen(value)-1] = '\0';
407 int ws_num = atoi(ws_str);
409 if (ws_num < 1 || ws_num > 10)
410 die("Malformed name, invalid workspace number\n");
414 name += strlen(ws_str) + 1;
416 if (strncasecmp(name, "screen ", strlen("screen ")) == 0) {
417 char *screen = strdup(name + strlen("screen "));
418 if ((end = strchr(screen, ' ')) != NULL)
420 LOG("Setting preferred screen for workspace %d to \"%s\"\n", ws_num, screen);
421 workspace_get(ws_num-1)->preferred_screen = screen;
423 name += strlen("screen ") + strlen(screen);
426 /* Strip leading whitespace */
427 while (*name != '\0' && *name == ' ')
430 LOG("rest to parse = %s\n", name);
437 LOG("setting name to \"%s\"\n", name);
440 workspace_set_name(&(workspaces[ws_num - 1]), name);
445 /* assign window class[/window title] → workspace */
446 if (strcasecmp(key, "assign") == 0) {
447 LOG("assign: \"%s\"\n", value);
452 /* If the window class/title is quoted we skip quotes */
453 if (value[0] == '"') {
454 class_title = sstrdup(value+1);
455 end = strchr(class_title, '"');
457 class_title = sstrdup(value);
458 /* If it is not quoted, we terminate it at the first space */
459 end = strchr(class_title, ' ');
462 die("Malformed assignment, couldn't find terminating quote\n");
465 /* Strip trailing whitespace */
466 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
467 value[strlen(value)-1] = '\0';
469 /* The target is the last argument separated by a space */
470 if ((target = strrchr(value, ' ')) == NULL)
471 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
474 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
475 die("Malformed assignment, invalid workspace number\n");
477 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
479 struct Assignment *new = scalloc(sizeof(struct Assignment));
480 new->windowclass_title = class_title;
481 if (strchr(target, '~') != NULL)
482 new->floating = ASSIGN_FLOATING_ONLY;
484 while (*target == '~')
487 if (atoi(target) >= 1) {
488 if (new->floating == ASSIGN_FLOATING_ONLY)
489 new->floating = ASSIGN_FLOATING;
490 new->workspace = atoi(target);
492 TAILQ_INSERT_TAIL(&assignments, new, assignments);
494 LOG("Assignment loaded: \"%s\":\n", class_title);
495 if (new->floating != ASSIGN_FLOATING_ONLY)
496 LOG(" to workspace %d\n", new->workspace);
498 if (new->floating != ASSIGN_FLOATING_NO)
499 LOG(" will be floating\n");
504 /* set a custom variable */
505 if (strcasecmp(key, "set") == 0) {
507 die("Malformed variable assignment, name has to start with $\n");
509 /* get key/value for this variable */
510 char *v_key = value, *v_value;
511 if ((v_value = strstr(value, " ")) == NULL)
512 die("Malformed variable assignment, need a value\n");
516 struct Variable *new = scalloc(sizeof(struct Variable));
517 new->key = sstrdup(v_key);
518 new->value = sstrdup(v_value);
519 SLIST_INSERT_HEAD(&variables, new, variables);
520 LOG("Got new variable %s = %s\n", v_key, v_value);
524 if (strcasecmp(key, "ipc-socket") == 0) {
525 config.ipc_socket_path = sstrdup(value);
529 die("Unknown configfile option: %s\n", key);
531 /* now grab all keys again */
536 while (!SLIST_EMPTY(&variables)) {
537 struct Variable *v = SLIST_FIRST(&variables);
538 SLIST_REMOVE_HEAD(&variables, variables);
545 REQUIRED_OPTION(terminal);
546 REQUIRED_OPTION(font);
548 /* Set an empty name for every workspace which got no name */
549 for (int i = 0; i < num_workspaces; i++) {
550 Workspace *ws = &(workspaces[i]);
551 if (ws->name != NULL) {
552 /* If the font was not specified when the workspace name
553 * was loaded, we need to predict the text width now */
554 if (ws->text_width == 0)
555 ws->text_width = predict_text_width(global_conn,
556 config.font, ws->name, ws->name_len);
560 workspace_set_name(&(workspaces[i]), NULL);