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);
38 * This function does a very simple replacement of each instance of key with value.
41 static void replace_variable(char *buffer, const char *key, const char *value) {
43 /* To prevent endless recursions when the user makes an error configuring,
44 * we stop after 100 replacements. That should be vastly more than enough. */
46 LOG("Replacing %s with %s\n", key, value);
47 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
48 LOG("replacing variable %s in \"%s\" with \"%s\"\n", key, buffer, value);
49 char *rest = pos + strlen(key);
52 asprintf(&replaced, "%s%s%s", buffer, value, rest);
53 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
54 * So we need to hard-code the dimensions here. */
55 strncpy(buffer, replaced, 1026);
61 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
63 * If you specify override_configpath, only this path is used to look for a
67 void load_configuration(xcb_connection_t *conn, const char *override_configpath) {
68 SLIST_HEAD(variables_head, Variable) variables;
70 #define OPTION_STRING(name) \
71 if (strcasecmp(key, #name) == 0) { \
72 config.name = sstrdup(value); \
76 #define REQUIRED_OPTION(name) \
77 if (config.name == NULL) \
78 die("You did not specify required configuration option " #name "\n");
80 #define OPTION_COLORTRIPLE(opt, name) \
81 if (strcasecmp(key, opt) == 0) { \
82 char border[8], background[8], text[8]; \
83 memset(border, 0, sizeof(border)); \
84 memset(background, 0, sizeof(background)); \
85 memset(text, 0, sizeof(text)); \
86 border[0] = background[0] = text[0] = '#'; \
87 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
88 border + 1, background + 1, text + 1) != 3 || \
89 strlen(border) != 7 || \
90 strlen(background) != 7 || \
92 die("invalid color code line: %s\n", value); \
93 config.name.border = get_colorpixel(conn, border); \
94 config.name.background = get_colorpixel(conn, background); \
95 config.name.text = get_colorpixel(conn, text); \
99 /* Clear the old config or initialize the data structure */
100 memset(&config, 0, sizeof(config));
102 SLIST_INIT(&variables);
104 /* Initialize default colors */
105 config.client.focused.border = get_colorpixel(conn, "#4c7899");
106 config.client.focused.background = get_colorpixel(conn, "#285577");
107 config.client.focused.text = get_colorpixel(conn, "#ffffff");
109 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
110 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
111 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
113 config.client.unfocused.border = get_colorpixel(conn, "#333333");
114 config.client.unfocused.background = get_colorpixel(conn, "#222222");
115 config.client.unfocused.text = get_colorpixel(conn, "#888888");
117 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
118 config.bar.focused.background = get_colorpixel(conn, "#285577");
119 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
121 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
122 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
123 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
126 if (override_configpath != NULL) {
127 if ((handle = fopen(override_configpath, "r")) == NULL)
128 die("Could not open configfile \"%s\".\n", override_configpath);
130 /* We first check for ~/.i3/config, then for /etc/i3/config */
131 char *globbed = glob_path("~/.i3/config");
132 if ((handle = fopen(globbed, "r")) == NULL)
133 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
134 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
137 char key[512], value[512], buffer[1026];
139 while (!feof(handle)) {
140 if (fgets(buffer, 1024, handle) == NULL) {
141 /* fgets returns NULL on EOF and on error, so see which one it is. */
144 die("Could not read configuration file\n");
147 if (config.terminal != NULL)
148 replace_variable(buffer, "$terminal", config.terminal);
150 /* Replace all custom variables */
151 struct Variable *current;
152 SLIST_FOREACH(current, &variables, variables)
153 replace_variable(buffer, current->key, current->value);
155 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
156 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
157 key[0] == '#' || strlen(key) < 3)
160 OPTION_STRING(terminal);
164 OPTION_COLORTRIPLE("client.focused", client.focused);
165 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
166 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
167 OPTION_COLORTRIPLE("bar.focused", bar.focused);
168 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
170 /* exec-lines (autostart) */
171 if (strcasecmp(key, "exec") == 0) {
172 struct Autostart *new = smalloc(sizeof(struct Autostart));
173 new->command = sstrdup(value);
174 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
179 if (strcasecmp(key, "bind") == 0) {
180 #define CHECK_MODIFIER(name) \
181 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
182 modifiers |= BIND_##name; \
183 walk += strlen(#name) + 1; \
186 char *walk = value, *rest;
187 uint32_t modifiers = 0;
189 while (*walk != '\0') {
190 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
191 CHECK_MODIFIER(SHIFT);
192 CHECK_MODIFIER(CONTROL);
193 CHECK_MODIFIER(MODE_SWITCH);
194 CHECK_MODIFIER(MOD1);
195 CHECK_MODIFIER(MOD2);
196 CHECK_MODIFIER(MOD3);
197 CHECK_MODIFIER(MOD4);
198 CHECK_MODIFIER(MOD5);
200 /* No modifier found? Then we’re done with this step */
204 /* Now check for the keycode */
205 int keycode = strtol(walk, &rest, 10);
206 if (!rest || *rest != ' ')
207 die("Invalid binding\n");
209 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
210 Binding *new = smalloc(sizeof(Binding));
211 new->keycode = keycode;
212 new->mods = modifiers;
213 new->command = sstrdup(rest);
214 TAILQ_INSERT_TAIL(&bindings, new, bindings);
218 if (strcasecmp(key, "floating_modifier") == 0) {
220 uint32_t modifiers = 0;
222 while (*walk != '\0') {
223 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
224 CHECK_MODIFIER(SHIFT);
225 CHECK_MODIFIER(CONTROL);
226 CHECK_MODIFIER(MODE_SWITCH);
227 CHECK_MODIFIER(MOD1);
228 CHECK_MODIFIER(MOD2);
229 CHECK_MODIFIER(MOD3);
230 CHECK_MODIFIER(MOD4);
231 CHECK_MODIFIER(MOD5);
233 /* No modifier found? Then we’re done with this step */
237 LOG("Floating modifiers = %d\n", modifiers);
238 config.floating_modifier = modifiers;
242 /* assign window class[/window title] → workspace */
243 if (strcasecmp(key, "assign") == 0) {
244 LOG("assign: \"%s\"\n", value);
245 char *class_title = sstrdup(value);
248 /* If the window class/title is quoted we skip quotes */
249 if (class_title[0] == '"') {
251 char *end = strchr(class_title, '"');
253 die("Malformed assignment, couldn't find terminating quote\n");
256 /* If it is not quoted, we terminate it at the first space */
257 char *end = strchr(class_title, ' ');
259 die("Malformed assignment, couldn't find terminating space\n");
263 /* The target is the last argument separated by a space */
264 if ((target = strrchr(value, ' ')) == NULL)
265 die("Malformed assignment, couldn't find target\n");
268 if (*target != '~' && (atoi(target) < 1 || atoi(target) > 10))
269 die("Malformed assignment, invalid workspace number\n");
271 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
273 struct Assignment *new = scalloc(sizeof(struct Assignment));
274 new->windowclass_title = class_title;
276 new->floating = true;
277 else new->workspace = atoi(target);
278 TAILQ_INSERT_TAIL(&assignments, new, assignments);
282 /* set a custom variable */
283 if (strcasecmp(key, "set") == 0) {
285 die("Malformed variable assignment, name has to start with $\n");
287 /* get key/value for this variable */
288 char *v_key = value, *v_value;
289 if ((v_value = strstr(value, " ")) == NULL)
290 die("Malformed variable assignment, need a value\n");
294 struct Variable *new = scalloc(sizeof(struct Variable));
295 new->key = sstrdup(v_key);
296 new->value = sstrdup(v_value);
297 SLIST_INSERT_HEAD(&variables, new, variables);
298 LOG("Got new variable %s = %s\n", v_key, v_value);
302 die("Unknown configfile option: %s\n", key);
306 REQUIRED_OPTION(terminal);
307 REQUIRED_OPTION(font);
310 while (!SLIST_EMPTY(&variables)) {
311 struct Variable *v = SLIST_FIRST(&variables);
312 SLIST_REMOVE_HEAD(&variables, variables);