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"
32 * This function resolves ~ in pathnames.
35 static char *glob_path(const char *path) {
36 static glob_t globbuf;
37 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
39 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
45 * This function does a very simple replacement of each instance of key with value.
48 static void replace_variable(char *buffer, const char *key, const char *value) {
50 /* To prevent endless recursions when the user makes an error configuring,
51 * we stop after 100 replacements. That should be vastly more than enough. */
53 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
54 char *rest = pos + strlen(key);
57 asprintf(&replaced, "%s%s%s", buffer, value, rest);
58 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
59 * So we need to hard-code the dimensions here. */
60 strncpy(buffer, replaced, 1026);
66 * Ungrabs all keys, to be called before re-grabbing the keys because of a
67 * mapping_notify event or a configuration file reload
70 void ungrab_all_keys(xcb_connection_t *conn) {
71 LOG("Ungrabbing all keys\n");
72 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
75 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
76 LOG("Grabbing %d\n", keycode);
77 if ((bind->mods & BIND_MODE_SWITCH) != 0)
78 xcb_grab_key(conn, 0, root, 0, keycode,
79 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
81 /* Grab the key in all combinations */
82 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, keycode, \
83 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
85 GRAB_KEY(bind->mods | xcb_numlock_mask);
86 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
91 * Grab the bound keys (tell X to send us keypress events for those keycodes)
94 void grab_all_keys(xcb_connection_t *conn) {
96 TAILQ_FOREACH(bind, &bindings, bindings) {
97 /* The easy case: the user specified a keycode directly. */
98 if (bind->keycode > 0) {
99 grab_keycode_for_binding(conn, bind, bind->keycode);
103 /* We need to translate the symbol to a keycode */
104 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
105 if (keysym == NoSymbol) {
106 LOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
110 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
111 if (keycodes == NULL) {
112 LOG("Could not translate symbol \"%s\"\n", bind->symbol);
116 uint32_t last_keycode = 0;
117 bind->number_keycodes = 0;
118 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
119 /* We hope duplicate keycodes will be returned in order
121 if (last_keycode == *walk)
123 grab_keycode_for_binding(conn, bind, *walk);
124 last_keycode = *walk;
125 bind->number_keycodes++;
127 LOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
128 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
129 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
135 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
137 * If you specify override_configpath, only this path is used to look for a
138 * configuration file.
141 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
143 /* First ungrab the keys */
144 ungrab_all_keys(conn);
146 /* Clear the old binding and assignment lists */
148 while (!TAILQ_EMPTY(&bindings)) {
149 bind = TAILQ_FIRST(&bindings);
150 TAILQ_REMOVE(&bindings, bind, bindings);
155 struct Assignment *assign;
156 while (!TAILQ_EMPTY(&assignments)) {
157 assign = TAILQ_FIRST(&assignments);
158 FREE(assign->windowclass_title);
159 TAILQ_REMOVE(&assignments, assign, assignments);
164 SLIST_HEAD(variables_head, Variable) variables;
166 #define OPTION_STRING(name) \
167 if (strcasecmp(key, #name) == 0) { \
168 config.name = sstrdup(value); \
172 #define REQUIRED_OPTION(name) \
173 if (config.name == NULL) \
174 die("You did not specify required configuration option " #name "\n");
176 #define OPTION_COLORTRIPLE(opt, name) \
177 if (strcasecmp(key, opt) == 0) { \
178 char border[8], background[8], text[8]; \
179 memset(border, 0, sizeof(border)); \
180 memset(background, 0, sizeof(background)); \
181 memset(text, 0, sizeof(text)); \
182 border[0] = background[0] = text[0] = '#'; \
183 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
184 border + 1, background + 1, text + 1) != 3 || \
185 strlen(border) != 7 || \
186 strlen(background) != 7 || \
188 die("invalid color code line: %s\n", value); \
189 config.name.border = get_colorpixel(conn, border); \
190 config.name.background = get_colorpixel(conn, background); \
191 config.name.text = get_colorpixel(conn, text); \
195 /* Clear the old config or initialize the data structure */
196 memset(&config, 0, sizeof(config));
198 SLIST_INIT(&variables);
200 /* Initialize default colors */
201 config.client.focused.border = get_colorpixel(conn, "#4c7899");
202 config.client.focused.background = get_colorpixel(conn, "#285577");
203 config.client.focused.text = get_colorpixel(conn, "#ffffff");
205 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
206 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
207 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
209 config.client.unfocused.border = get_colorpixel(conn, "#333333");
210 config.client.unfocused.background = get_colorpixel(conn, "#222222");
211 config.client.unfocused.text = get_colorpixel(conn, "#888888");
213 config.client.urgent.border = get_colorpixel(conn, "#2f343a");
214 config.client.urgent.background = get_colorpixel(conn, "#900000");
215 config.client.urgent.text = get_colorpixel(conn, "#ffffff");
217 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
218 config.bar.focused.background = get_colorpixel(conn, "#285577");
219 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
221 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
222 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
223 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
225 config.bar.urgent.border = get_colorpixel(conn, "#2f343a");
226 config.bar.urgent.background = get_colorpixel(conn, "#900000");
227 config.bar.urgent.text = get_colorpixel(conn, "#ffffff");
230 if (override_configpath != NULL) {
231 if ((handle = fopen(override_configpath, "r")) == NULL)
232 die("Could not open configfile \"%s\".\n", override_configpath);
234 /* We first check for ~/.i3/config, then for /etc/i3/config */
235 char *globbed = glob_path("~/.i3/config");
236 if ((handle = fopen(globbed, "r")) == NULL)
237 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
238 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
241 char key[512], value[512], buffer[1026];
243 while (!feof(handle)) {
244 if (fgets(buffer, 1024, handle) == NULL) {
245 /* fgets returns NULL on EOF and on error, so see which one it is. */
248 die("Could not read configuration file\n");
251 if (config.terminal != NULL)
252 replace_variable(buffer, "$terminal", config.terminal);
254 /* Replace all custom variables */
255 struct Variable *current;
256 SLIST_FOREACH(current, &variables, variables)
257 replace_variable(buffer, current->key, current->value);
259 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
260 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
261 key[0] == '#' || strlen(key) < 3)
264 OPTION_STRING(terminal);
268 OPTION_COLORTRIPLE("client.focused", client.focused);
269 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
270 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
271 OPTION_COLORTRIPLE("client.urgent", client.urgent);
272 OPTION_COLORTRIPLE("bar.focused", bar.focused);
273 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
274 OPTION_COLORTRIPLE("bar.urgent", bar.urgent);
276 /* exec-lines (autostart) */
277 if (strcasecmp(key, "exec") == 0) {
278 struct Autostart *new = smalloc(sizeof(struct Autostart));
279 new->command = sstrdup(value);
280 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
285 if (strcasecmp(key, "bind") == 0 || strcasecmp(key, "bindsym") == 0) {
286 #define CHECK_MODIFIER(name) \
287 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
288 modifiers |= BIND_##name; \
289 walk += strlen(#name) + 1; \
292 char *walk = value, *rest;
293 uint32_t modifiers = 0;
295 while (*walk != '\0') {
296 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
297 CHECK_MODIFIER(SHIFT);
298 CHECK_MODIFIER(CONTROL);
299 CHECK_MODIFIER(MODE_SWITCH);
300 CHECK_MODIFIER(MOD1);
301 CHECK_MODIFIER(MOD2);
302 CHECK_MODIFIER(MOD3);
303 CHECK_MODIFIER(MOD4);
304 CHECK_MODIFIER(MOD5);
306 /* No modifier found? Then we’re done with this step */
310 Binding *new = scalloc(sizeof(Binding));
312 /* Now check for the keycode or copy the symbol */
313 if (strcasecmp(key, "bind") == 0) {
314 int keycode = strtol(walk, &rest, 10);
315 if (!rest || *rest != ' ')
316 die("Invalid binding (keycode)\n");
317 new->keycode = keycode;
321 while (*rest != '\0' && *rest != ' ')
324 die("Invalid binding (keysym)\n");
325 #if defined(__OpenBSD__)
326 size_t len = strlen(sym);
327 if (len > (rest - sym))
329 new->symbol = smalloc(len + 1);
330 memcpy(new->symbol, sym, len+1);
331 new->symbol[len]='\0';
333 new->symbol = strndup(sym, (rest - sym));
337 LOG("keycode = %d, symbol = %s, modifiers = %d, command = *%s*\n", new->keycode, new->symbol, modifiers, rest);
338 new->mods = modifiers;
339 new->command = sstrdup(rest);
340 TAILQ_INSERT_TAIL(&bindings, new, bindings);
344 if (strcasecmp(key, "floating_modifier") == 0) {
346 uint32_t modifiers = 0;
348 while (*walk != '\0') {
349 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
350 CHECK_MODIFIER(SHIFT);
351 CHECK_MODIFIER(CONTROL);
352 CHECK_MODIFIER(MODE_SWITCH);
353 CHECK_MODIFIER(MOD1);
354 CHECK_MODIFIER(MOD2);
355 CHECK_MODIFIER(MOD3);
356 CHECK_MODIFIER(MOD4);
357 CHECK_MODIFIER(MOD5);
359 /* No modifier found? Then we’re done with this step */
363 LOG("Floating modifiers = %d\n", modifiers);
364 config.floating_modifier = modifiers;
368 /* workspace "workspace number" [screen <screen>] ["name of the workspace"]
369 * with screen := <number> | <position>, e.g. screen 1280 or screen 1 */
370 if (strcasecmp(key, "name") == 0 || strcasecmp(key, "workspace") == 0) {
371 LOG("workspace: %s\n",value);
372 char *ws_str = sstrdup(value);
373 char *end = strchr(ws_str, ' ');
375 die("Malformed name, couln't find terminating space\n");
378 /* Strip trailing whitespace */
379 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
380 value[strlen(value)-1] = '\0';
382 int ws_num = atoi(ws_str);
384 if (ws_num < 1 || ws_num > 10)
385 die("Malformed name, invalid workspace number\n");
389 name += strlen(ws_str) + 1;
391 if (strncasecmp(name, "screen ", strlen("screen ")) == 0) {
392 char *screen = strdup(name + strlen("screen "));
393 if ((end = strchr(screen, ' ')) != NULL)
395 LOG("Setting preferred screen for workspace %d to \"%s\"\n", ws_num, screen);
396 workspaces[ws_num - 1].preferred_screen = screen;
398 name += strlen("screen ") + strlen(screen);
401 /* Strip leading whitespace */
402 while (*name != '\0' && *name == ' ')
405 LOG("rest to parse = %s\n", name);
412 LOG("setting name to \"%s\"\n", name);
415 workspace_set_name(&(workspaces[ws_num - 1]), name);
420 /* assign window class[/window title] → workspace */
421 if (strcasecmp(key, "assign") == 0) {
422 LOG("assign: \"%s\"\n", value);
427 /* If the window class/title is quoted we skip quotes */
428 if (value[0] == '"') {
429 class_title = sstrdup(value+1);
430 end = strchr(class_title, '"');
432 class_title = sstrdup(value);
433 /* If it is not quoted, we terminate it at the first space */
434 end = strchr(class_title, ' ');
437 die("Malformed assignment, couldn't find terminating quote\n");
440 /* Strip trailing whitespace */
441 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
442 value[strlen(value)-1] = '\0';
444 /* The target is the last argument separated by a space */
445 if ((target = strrchr(value, ' ')) == NULL)
446 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
449 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
450 die("Malformed assignment, invalid workspace number\n");
452 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
454 struct Assignment *new = scalloc(sizeof(struct Assignment));
455 new->windowclass_title = class_title;
456 if (strchr(target, '~') != NULL)
457 new->floating = ASSIGN_FLOATING_ONLY;
459 while (*target == '~')
462 if (atoi(target) >= 1 && atoi(target) <= 10) {
463 if (new->floating == ASSIGN_FLOATING_ONLY)
464 new->floating = ASSIGN_FLOATING;
465 new->workspace = atoi(target);
467 TAILQ_INSERT_TAIL(&assignments, new, assignments);
469 LOG("Assignment loaded: \"%s\":\n", class_title);
470 if (new->floating != ASSIGN_FLOATING_ONLY)
471 LOG(" to workspace %d\n", new->workspace);
473 if (new->floating != ASSIGN_FLOATING_NO)
474 LOG(" will be floating\n");
479 /* set a custom variable */
480 if (strcasecmp(key, "set") == 0) {
482 die("Malformed variable assignment, name has to start with $\n");
484 /* get key/value for this variable */
485 char *v_key = value, *v_value;
486 if ((v_value = strstr(value, " ")) == NULL)
487 die("Malformed variable assignment, need a value\n");
491 struct Variable *new = scalloc(sizeof(struct Variable));
492 new->key = sstrdup(v_key);
493 new->value = sstrdup(v_value);
494 SLIST_INSERT_HEAD(&variables, new, variables);
495 LOG("Got new variable %s = %s\n", v_key, v_value);
499 if (strcasecmp(key, "ipc-socket") == 0) {
500 config.ipc_socket_path = sstrdup(value);
504 die("Unknown configfile option: %s\n", key);
506 /* now grab all keys again */
511 REQUIRED_OPTION(terminal);
512 REQUIRED_OPTION(font);
514 while (!SLIST_EMPTY(&variables)) {
515 struct Variable *v = SLIST_FIRST(&variables);
516 SLIST_REMOVE_HEAD(&variables, variables);
522 /* Set an empty name for every workspace which got no name */
523 for (int i = 0; i < 10; i++) {
524 Workspace *ws = &(workspaces[i]);
525 if (ws->name != NULL) {
526 /* If the font was not specified when the workspace name
527 * was loaded, we need to predict the text width now */
528 if (ws->text_width == 0)
529 ws->text_width = predict_text_width(global_conn,
530 config.font, ws->name, ws->name_len);
534 workspace_set_name(&(workspaces[i]), NULL);