4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
26 * This function resolves ~ in pathnames.
29 static char *glob_path(const char *path) {
30 static glob_t globbuf;
31 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
33 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
39 * This function does a very simple replacement of each instance of key with value.
42 static void replace_variable(char *buffer, const char *key, const char *value) {
44 /* To prevent endless recursions when the user makes an error configuring,
45 * we stop after 100 replacements. That should be vastly more than enough. */
47 LOG("Replacing %s with %s\n", key, value);
48 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
49 LOG("replacing variable %s in \"%s\" with \"%s\"\n", key, buffer, value);
50 char *rest = pos + strlen(key);
53 asprintf(&replaced, "%s%s%s", buffer, value, rest);
54 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
55 * So we need to hard-code the dimensions here. */
56 strncpy(buffer, replaced, 1026);
62 * Ungrab the bound keys
65 void ungrab_all_keys(xcb_connection_t *conn) {
67 TAILQ_FOREACH(bind, &bindings, bindings) {
68 LOG("Ungrabbing %d\n", bind->keycode);
69 xcb_ungrab_key(conn, bind->keycode, root, bind->keycode);
74 * Grab the bound keys (tell X to send us keypress events for those keycodes)
77 void grab_all_keys(xcb_connection_t *conn) {
79 TAILQ_FOREACH(bind, &bindings, bindings) {
80 LOG("Grabbing %d\n", bind->keycode);
81 if ((bind->mods & BIND_MODE_SWITCH) != 0)
82 xcb_grab_key(conn, 0, root, 0, bind->keycode,
83 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
85 /* Grab the key in all combinations */
86 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
87 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
89 GRAB_KEY(bind->mods | xcb_numlock_mask);
90 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
96 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
98 * If you specify override_configpath, only this path is used to look for a
102 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
104 /* First ungrab the keys */
105 ungrab_all_keys(conn);
107 /* Clear the old binding and assignment lists */
109 while (!TAILQ_EMPTY(&bindings)) {
110 bind = TAILQ_FIRST(&bindings);
111 TAILQ_REMOVE(&bindings, bind, bindings);
116 struct Assignment *assign;
117 while (!TAILQ_EMPTY(&assignments)) {
118 assign = TAILQ_FIRST(&assignments);
119 FREE(assign->windowclass_title);
120 TAILQ_REMOVE(&assignments, assign, assignments);
125 SLIST_HEAD(variables_head, Variable) variables;
127 #define OPTION_STRING(name) \
128 if (strcasecmp(key, #name) == 0) { \
129 config.name = sstrdup(value); \
133 #define REQUIRED_OPTION(name) \
134 if (config.name == NULL) \
135 die("You did not specify required configuration option " #name "\n");
137 #define OPTION_COLORTRIPLE(opt, name) \
138 if (strcasecmp(key, opt) == 0) { \
139 char border[8], background[8], text[8]; \
140 memset(border, 0, sizeof(border)); \
141 memset(background, 0, sizeof(background)); \
142 memset(text, 0, sizeof(text)); \
143 border[0] = background[0] = text[0] = '#'; \
144 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
145 border + 1, background + 1, text + 1) != 3 || \
146 strlen(border) != 7 || \
147 strlen(background) != 7 || \
149 die("invalid color code line: %s\n", value); \
150 config.name.border = get_colorpixel(conn, border); \
151 config.name.background = get_colorpixel(conn, background); \
152 config.name.text = get_colorpixel(conn, text); \
156 /* Clear the old config or initialize the data structure */
157 memset(&config, 0, sizeof(config));
159 SLIST_INIT(&variables);
161 /* Initialize default colors */
162 config.client.focused.border = get_colorpixel(conn, "#4c7899");
163 config.client.focused.background = get_colorpixel(conn, "#285577");
164 config.client.focused.text = get_colorpixel(conn, "#ffffff");
166 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
167 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
168 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
170 config.client.unfocused.border = get_colorpixel(conn, "#333333");
171 config.client.unfocused.background = get_colorpixel(conn, "#222222");
172 config.client.unfocused.text = get_colorpixel(conn, "#888888");
174 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
175 config.bar.focused.background = get_colorpixel(conn, "#285577");
176 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
178 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
179 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
180 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
183 if (override_configpath != NULL) {
184 if ((handle = fopen(override_configpath, "r")) == NULL)
185 die("Could not open configfile \"%s\".\n", override_configpath);
187 /* We first check for ~/.i3/config, then for /etc/i3/config */
188 char *globbed = glob_path("~/.i3/config");
189 if ((handle = fopen(globbed, "r")) == NULL)
190 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
191 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
194 char key[512], value[512], buffer[1026];
196 while (!feof(handle)) {
197 if (fgets(buffer, 1024, handle) == NULL) {
198 /* fgets returns NULL on EOF and on error, so see which one it is. */
201 die("Could not read configuration file\n");
204 if (config.terminal != NULL)
205 replace_variable(buffer, "$terminal", config.terminal);
207 /* Replace all custom variables */
208 struct Variable *current;
209 SLIST_FOREACH(current, &variables, variables)
210 replace_variable(buffer, current->key, current->value);
212 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
213 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
214 key[0] == '#' || strlen(key) < 3)
217 OPTION_STRING(terminal);
221 OPTION_COLORTRIPLE("client.focused", client.focused);
222 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
223 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
224 OPTION_COLORTRIPLE("bar.focused", bar.focused);
225 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
227 /* exec-lines (autostart) */
228 if (strcasecmp(key, "exec") == 0) {
229 struct Autostart *new = smalloc(sizeof(struct Autostart));
230 new->command = sstrdup(value);
231 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
236 if (strcasecmp(key, "bind") == 0) {
237 #define CHECK_MODIFIER(name) \
238 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
239 modifiers |= BIND_##name; \
240 walk += strlen(#name) + 1; \
243 char *walk = value, *rest;
244 uint32_t modifiers = 0;
246 while (*walk != '\0') {
247 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
248 CHECK_MODIFIER(SHIFT);
249 CHECK_MODIFIER(CONTROL);
250 CHECK_MODIFIER(MODE_SWITCH);
251 CHECK_MODIFIER(MOD1);
252 CHECK_MODIFIER(MOD2);
253 CHECK_MODIFIER(MOD3);
254 CHECK_MODIFIER(MOD4);
255 CHECK_MODIFIER(MOD5);
257 /* No modifier found? Then we’re done with this step */
261 /* Now check for the keycode */
262 int keycode = strtol(walk, &rest, 10);
263 if (!rest || *rest != ' ')
264 die("Invalid binding\n");
266 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
267 Binding *new = smalloc(sizeof(Binding));
268 new->keycode = keycode;
269 new->mods = modifiers;
270 new->command = sstrdup(rest);
271 TAILQ_INSERT_TAIL(&bindings, new, bindings);
275 if (strcasecmp(key, "floating_modifier") == 0) {
277 uint32_t modifiers = 0;
279 while (*walk != '\0') {
280 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
281 CHECK_MODIFIER(SHIFT);
282 CHECK_MODIFIER(CONTROL);
283 CHECK_MODIFIER(MODE_SWITCH);
284 CHECK_MODIFIER(MOD1);
285 CHECK_MODIFIER(MOD2);
286 CHECK_MODIFIER(MOD3);
287 CHECK_MODIFIER(MOD4);
288 CHECK_MODIFIER(MOD5);
290 /* No modifier found? Then we’re done with this step */
294 LOG("Floating modifiers = %d\n", modifiers);
295 config.floating_modifier = modifiers;
299 /* name "workspace number" "name of the workspace" */
300 if (strcasecmp(key, "name") == 0) {
301 LOG("name workspace: %s\n",value);
302 char *ws_str = sstrdup(value);
303 char *end = strchr(ws_str, ' ');
305 die("Malformed name, couln't find terminating space\n");
308 /* Strip trailing whitespace */
309 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
310 value[strlen(value)-1] = '\0';
312 int ws_num=atoi(ws_str);
314 if ( ws_num < 1 || ws_num > 10 )
315 die("Malformed name, invalid workspace Number\n");
319 name += strlen(ws_str) + 1;
321 /* if no name reinitialize the name to NULL for reload */
323 workspaces[ws_num - 1].name=NULL;
326 workspaces[ws_num - 1].name=sstrdup(name);
330 /* assign window class[/window title] → workspace */
331 if (strcasecmp(key, "assign") == 0) {
332 LOG("assign: \"%s\"\n", value);
337 /* If the window class/title is quoted we skip quotes */
338 if (value[0] == '"') {
339 class_title = sstrdup(value+1);
340 end = strchr(class_title, '"');
342 class_title = sstrdup(value);
343 /* If it is not quoted, we terminate it at the first space */
344 end = strchr(class_title, ' ');
347 die("Malformed assignment, couldn't find terminating quote\n");
350 /* Strip trailing whitespace */
351 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
352 value[strlen(value)-1] = '\0';
354 /* The target is the last argument separated by a space */
355 if ((target = strrchr(value, ' ')) == NULL)
356 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
359 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
360 die("Malformed assignment, invalid workspace number\n");
362 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
364 struct Assignment *new = scalloc(sizeof(struct Assignment));
365 new->windowclass_title = class_title;
366 if (strchr(target, '~') != NULL)
367 new->floating = ASSIGN_FLOATING_ONLY;
369 while (*target == '~')
372 if (atoi(target) >= 1 && atoi(target) <= 10) {
373 if (new->floating == ASSIGN_FLOATING_ONLY)
374 new->floating = ASSIGN_FLOATING;
375 new->workspace = atoi(target);
377 TAILQ_INSERT_TAIL(&assignments, new, assignments);
379 LOG("Assignment loaded: \"%s\":\n", class_title);
380 if (new->floating != ASSIGN_FLOATING_ONLY)
381 LOG(" to workspace %d\n", new->workspace);
383 if (new->floating != ASSIGN_FLOATING_NO)
384 LOG(" will be floating\n");
389 /* set a custom variable */
390 if (strcasecmp(key, "set") == 0) {
392 die("Malformed variable assignment, name has to start with $\n");
394 /* get key/value for this variable */
395 char *v_key = value, *v_value;
396 if ((v_value = strstr(value, " ")) == NULL)
397 die("Malformed variable assignment, need a value\n");
401 struct Variable *new = scalloc(sizeof(struct Variable));
402 new->key = sstrdup(v_key);
403 new->value = sstrdup(v_value);
404 SLIST_INSERT_HEAD(&variables, new, variables);
405 LOG("Got new variable %s = %s\n", v_key, v_value);
409 die("Unknown configfile option: %s\n", key);
411 /* now grab all keys again */
416 REQUIRED_OPTION(terminal);
417 REQUIRED_OPTION(font);
419 while (!SLIST_EMPTY(&variables)) {
420 struct Variable *v = SLIST_FIRST(&variables);
421 SLIST_REMOVE_HEAD(&variables, variables);