4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
25 * This function resolves ~ in pathnames.
28 static char *glob_path(const char *path) {
29 static glob_t globbuf;
30 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
32 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
39 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
41 * If you specify override_configpath, only this path is used to look for a
45 void load_configuration(xcb_connection_t *conn, const char *override_configpath) {
46 #define OPTION_STRING(name) \
47 if (strcasecmp(key, #name) == 0) { \
48 config.name = sstrdup(value); \
52 #define REQUIRED_OPTION(name) \
53 if (config.name == NULL) \
54 die("You did not specify required configuration option " #name "\n");
56 #define OPTION_COLORTRIPLE(opt, name) \
57 if (strcasecmp(key, opt) == 0) { \
58 char border[8], background[8], text[8]; \
59 memset(border, 0, sizeof(border)); \
60 memset(background, 0, sizeof(background)); \
61 memset(text, 0, sizeof(text)); \
62 border[0] = background[0] = text[0] = '#'; \
63 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
64 border + 1, background + 1, text + 1) != 3 || \
65 strlen(border) != 7 || \
66 strlen(background) != 7 || \
68 die("invalid color code line: %s\n", value); \
69 config.name.border = get_colorpixel(conn, border); \
70 config.name.background = get_colorpixel(conn, background); \
71 config.name.text = get_colorpixel(conn, text); \
75 /* Clear the old config or initialize the data structure */
76 memset(&config, 0, sizeof(config));
78 /* Initialize default colors */
79 config.client.focused.border = get_colorpixel(conn, "#4c7899");
80 config.client.focused.background = get_colorpixel(conn, "#285577");
81 config.client.focused.text = get_colorpixel(conn, "#ffffff");
83 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
84 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
85 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
87 config.client.unfocused.border = get_colorpixel(conn, "#333333");
88 config.client.unfocused.background = get_colorpixel(conn, "#222222");
89 config.client.unfocused.text = get_colorpixel(conn, "#888888");
91 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
92 config.bar.focused.background = get_colorpixel(conn, "#285577");
93 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
95 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
96 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
97 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
100 if (override_configpath != NULL) {
101 if ((handle = fopen(override_configpath, "r")) == NULL)
102 die("Could not open configfile \"%s\".\n", override_configpath);
104 /* We first check for ~/.i3/config, then for /etc/i3/config */
105 char *globbed = glob_path("~/.i3/config");
106 if ((handle = fopen(globbed, "r")) == NULL)
107 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
108 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
111 char key[512], value[512], buffer[1026];
113 while (!feof(handle)) {
114 if (fgets(buffer, 1024, handle) == NULL) {
115 /* fgets returns NULL on EOF and on error, so see which one it is. */
118 die("Could not read configuration file\n");
121 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
122 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
123 key[0] == '#' || strlen(key) < 3)
126 OPTION_STRING(terminal);
130 OPTION_COLORTRIPLE("client.focused", client.focused);
131 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
132 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
133 OPTION_COLORTRIPLE("bar.focused", bar.focused);
134 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
136 /* exec-lines (autostart) */
137 if (strcasecmp(key, "exec") == 0) {
138 struct Autostart *new = smalloc(sizeof(struct Autostart));
139 new->command = sstrdup(value);
140 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
145 if (strcasecmp(key, "bind") == 0) {
146 #define CHECK_MODIFIER(name) \
147 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
148 modifiers |= BIND_##name; \
149 walk += strlen(#name) + 1; \
152 char *walk = value, *rest;
153 uint32_t modifiers = 0;
155 while (*walk != '\0') {
156 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
157 CHECK_MODIFIER(SHIFT);
158 CHECK_MODIFIER(CONTROL);
159 CHECK_MODIFIER(MODE_SWITCH);
160 CHECK_MODIFIER(MOD1);
161 CHECK_MODIFIER(MOD2);
162 CHECK_MODIFIER(MOD3);
163 CHECK_MODIFIER(MOD4);
164 CHECK_MODIFIER(MOD5);
166 /* No modifier found? Then we’re done with this step */
170 /* Now check for the keycode */
171 int keycode = strtol(walk, &rest, 10);
172 if (!rest || *rest != ' ')
173 die("Invalid binding\n");
175 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
176 Binding *new = smalloc(sizeof(Binding));
177 new->keycode = keycode;
178 new->mods = modifiers;
179 new->command = sstrdup(rest);
180 TAILQ_INSERT_TAIL(&bindings, new, bindings);
184 /* assign window class[/window title] → workspace */
185 if (strcasecmp(key, "assign") == 0) {
186 LOG("assign: \"%s\"\n", value);
187 char *class_title = sstrdup(value);
190 /* If the window class/title is quoted we skip quotes */
191 if (class_title[0] == '"') {
193 char *end = strchr(class_title, '"');
195 die("Malformed assignment, couldn't find terminating quote\n");
198 /* If it is not quoted, we terminate it at the first space */
199 char *end = strchr(class_title, ' ');
201 die("Malformed assignment, couldn't find terminating space\n");
205 /* The target is the last argument separated by a space */
206 if ((target = strrchr(value, ' ')) == NULL)
207 die("Malformed assignment, couldn't find target\n");
210 if (atoi(target) < 1 || atoi(target) > 10)
211 die("Malformed assignment, invalid workspace number\n");
213 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
215 struct Assignment *new = smalloc(sizeof(struct Assignment));
216 new->windowclass_title = class_title;
217 new->workspace = atoi(target);
218 TAILQ_INSERT_TAIL(&assignments, new, assignments);
222 die("Unknown configfile option: %s\n", key);
226 REQUIRED_OPTION(terminal);
227 REQUIRED_OPTION(font);