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);
60 /* UnGrab the bound keys */
62 void ungrab_all_keys(xcb_connection_t *conn) {
64 TAILQ_FOREACH(bind, &bindings, bindings) {
65 LOG("UnGrabbing %d\n", bind->keycode);
66 #define UNGRAB_KEY(modifier) xcb_ungrab_key(conn,bind->keycode,root,modifier);
67 UNGRAB_KEY(bind->keycode);
71 /* Grab the bound keys */
72 void grab_all_keys(xcb_connection_t *conn) {
74 TAILQ_FOREACH(bind, &bindings, bindings) {
75 LOG("Grabbing %d\n", bind->keycode);
76 if ( bind->mods & BIND_MODE_SWITCH )
77 xcb_grab_key(conn, 0, root, 0, bind->keycode,
78 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
80 /* Grab the key in all combinations */
81 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
82 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
84 GRAB_KEY(bind->mods | xcb_numlock_mask);
85 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
91 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
93 * If you specify override_configpath, only this path is used to look for a
97 void load_configuration(xcb_connection_t *conn, const char *override_configpath,bool reload) {
100 /* First ungrab the keys */
101 ungrab_all_keys(conn);
104 TAILQ_FOREACH(bind,&bindings,bindings) {
105 TAILQ_REMOVE(&bindings,bind,bindings);
110 struct Assignment *assign;
111 TAILQ_FOREACH(assign,&assignments,assignments) {
112 TAILQ_REMOVE(&assignments,assign,assignments);
113 free(assign->windowclass_title);
119 SLIST_HEAD(variables_head, Variable) variables;
121 #define OPTION_STRING(name) \
122 if (strcasecmp(key, #name) == 0) { \
123 config.name = sstrdup(value); \
127 #define REQUIRED_OPTION(name) \
128 if (config.name == NULL) \
129 die("You did not specify required configuration option " #name "\n");
131 #define OPTION_COLORTRIPLE(opt, name) \
132 if (strcasecmp(key, opt) == 0) { \
133 char border[8], background[8], text[8]; \
134 memset(border, 0, sizeof(border)); \
135 memset(background, 0, sizeof(background)); \
136 memset(text, 0, sizeof(text)); \
137 border[0] = background[0] = text[0] = '#'; \
138 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
139 border + 1, background + 1, text + 1) != 3 || \
140 strlen(border) != 7 || \
141 strlen(background) != 7 || \
143 die("invalid color code line: %s\n", value); \
144 config.name.border = get_colorpixel(conn, border); \
145 config.name.background = get_colorpixel(conn, background); \
146 config.name.text = get_colorpixel(conn, text); \
150 /* Clear the old config or initialize the data structure */
151 memset(&config, 0, sizeof(config));
153 SLIST_INIT(&variables);
155 /* Initialize default colors */
156 config.client.focused.border = get_colorpixel(conn, "#4c7899");
157 config.client.focused.background = get_colorpixel(conn, "#285577");
158 config.client.focused.text = get_colorpixel(conn, "#ffffff");
160 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
161 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
162 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
164 config.client.unfocused.border = get_colorpixel(conn, "#333333");
165 config.client.unfocused.background = get_colorpixel(conn, "#222222");
166 config.client.unfocused.text = get_colorpixel(conn, "#888888");
168 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
169 config.bar.focused.background = get_colorpixel(conn, "#285577");
170 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
172 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
173 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
174 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
177 if (override_configpath != NULL) {
178 if ((handle = fopen(override_configpath, "r")) == NULL)
179 die("Could not open configfile \"%s\".\n", override_configpath);
181 /* We first check for ~/.i3/config, then for /etc/i3/config */
182 char *globbed = glob_path("~/.i3/config");
183 if ((handle = fopen(globbed, "r")) == NULL)
184 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
185 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
188 char key[512], value[512], buffer[1026];
190 while (!feof(handle)) {
191 if (fgets(buffer, 1024, handle) == NULL) {
192 /* fgets returns NULL on EOF and on error, so see which one it is. */
195 die("Could not read configuration file\n");
198 if (config.terminal != NULL)
199 replace_variable(buffer, "$terminal", config.terminal);
201 /* Replace all custom variables */
202 struct Variable *current;
203 SLIST_FOREACH(current, &variables, variables)
204 replace_variable(buffer, current->key, current->value);
206 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
207 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
208 key[0] == '#' || strlen(key) < 3)
211 OPTION_STRING(terminal);
215 OPTION_COLORTRIPLE("client.focused", client.focused);
216 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
217 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
218 OPTION_COLORTRIPLE("bar.focused", bar.focused);
219 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
221 /* exec-lines (autostart) */
222 if (strcasecmp(key, "exec") == 0) {
223 struct Autostart *new = smalloc(sizeof(struct Autostart));
224 new->command = sstrdup(value);
225 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
230 if (strcasecmp(key, "bind") == 0) {
231 #define CHECK_MODIFIER(name) \
232 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
233 modifiers |= BIND_##name; \
234 walk += strlen(#name) + 1; \
237 char *walk = value, *rest;
238 uint32_t modifiers = 0;
240 while (*walk != '\0') {
241 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
242 CHECK_MODIFIER(SHIFT);
243 CHECK_MODIFIER(CONTROL);
244 CHECK_MODIFIER(MODE_SWITCH);
245 CHECK_MODIFIER(MOD1);
246 CHECK_MODIFIER(MOD2);
247 CHECK_MODIFIER(MOD3);
248 CHECK_MODIFIER(MOD4);
249 CHECK_MODIFIER(MOD5);
251 /* No modifier found? Then we’re done with this step */
255 /* Now check for the keycode */
256 int keycode = strtol(walk, &rest, 10);
257 if (!rest || *rest != ' ')
258 die("Invalid binding\n");
260 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
261 Binding *new = smalloc(sizeof(Binding));
262 new->keycode = keycode;
263 new->mods = modifiers;
264 new->command = sstrdup(rest);
265 TAILQ_INSERT_TAIL(&bindings, new, bindings);
269 if (strcasecmp(key, "floating_modifier") == 0) {
271 uint32_t modifiers = 0;
273 while (*walk != '\0') {
274 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
275 CHECK_MODIFIER(SHIFT);
276 CHECK_MODIFIER(CONTROL);
277 CHECK_MODIFIER(MODE_SWITCH);
278 CHECK_MODIFIER(MOD1);
279 CHECK_MODIFIER(MOD2);
280 CHECK_MODIFIER(MOD3);
281 CHECK_MODIFIER(MOD4);
282 CHECK_MODIFIER(MOD5);
284 /* No modifier found? Then we’re done with this step */
288 LOG("Floating modifiers = %d\n", modifiers);
289 config.floating_modifier = modifiers;
293 /* assign window class[/window title] → workspace */
294 if (strcasecmp(key, "assign") == 0) {
295 LOG("assign: \"%s\"\n", value);
296 char *class_title = sstrdup(value);
299 /* If the window class/title is quoted we skip quotes */
300 if (class_title[0] == '"') {
302 char *end = strchr(class_title, '"');
304 die("Malformed assignment, couldn't find terminating quote\n");
307 /* If it is not quoted, we terminate it at the first space */
308 char *end = strchr(class_title, ' ');
310 die("Malformed assignment, couldn't find terminating space\n");
314 /* Strip trailing whitespace */
315 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
316 value[strlen(value)-1] = '\0';
318 /* The target is the last argument separated by a space */
319 if ((target = strrchr(value, ' ')) == NULL)
320 die("Malformed assignment, couldn't find target\n");
323 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
324 die("Malformed assignment, invalid workspace number\n");
326 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
328 struct Assignment *new = scalloc(sizeof(struct Assignment));
329 new->windowclass_title = class_title;
330 if (strchr(target, '~') != NULL)
331 new->floating = ASSIGN_FLOATING_ONLY;
333 while (*target == '~')
336 if (atoi(target) >= 1 && atoi(target) <= 10) {
337 if (new->floating == ASSIGN_FLOATING_ONLY)
338 new->floating = ASSIGN_FLOATING;
339 new->workspace = atoi(target);
341 TAILQ_INSERT_TAIL(&assignments, new, assignments);
343 LOG("Assignment loaded: \"%s\":\n", class_title);
344 if (new->floating != ASSIGN_FLOATING_ONLY)
345 LOG(" to workspace %d\n", new->workspace);
347 if (new->floating != ASSIGN_FLOATING_NO)
348 LOG(" will be floating\n");
353 /* set a custom variable */
354 if (strcasecmp(key, "set") == 0) {
356 die("Malformed variable assignment, name has to start with $\n");
358 /* get key/value for this variable */
359 char *v_key = value, *v_value;
360 if ((v_value = strstr(value, " ")) == NULL)
361 die("Malformed variable assignment, need a value\n");
365 struct Variable *new = scalloc(sizeof(struct Variable));
366 new->key = sstrdup(v_key);
367 new->value = sstrdup(v_value);
368 SLIST_INSERT_HEAD(&variables, new, variables);
369 LOG("Got new variable %s = %s\n", v_key, v_value);
373 die("Unknown configfile option: %s\n", key);
375 /* now grab all keys again */
380 REQUIRED_OPTION(terminal);
381 REQUIRED_OPTION(font);
384 while (!SLIST_EMPTY(&variables)) {
385 struct Variable *v = SLIST_FIRST(&variables);
386 SLIST_REMOVE_HEAD(&variables, variables);