4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
22 #include "workspace.h"
27 * This function resolves ~ in pathnames.
30 static char *glob_path(const char *path) {
31 static glob_t globbuf;
32 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
34 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
40 * This function does a very simple replacement of each instance of key with value.
43 static void replace_variable(char *buffer, const char *key, const char *value) {
45 /* To prevent endless recursions when the user makes an error configuring,
46 * we stop after 100 replacements. That should be vastly more than enough. */
48 LOG("Replacing %s with %s\n", key, value);
49 while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
50 LOG("replacing variable %s in \"%s\" with \"%s\"\n", key, buffer, value);
51 char *rest = pos + strlen(key);
54 asprintf(&replaced, "%s%s%s", buffer, value, rest);
55 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
56 * So we need to hard-code the dimensions here. */
57 strncpy(buffer, replaced, 1026);
63 * Ungrab the bound keys
66 void ungrab_all_keys(xcb_connection_t *conn) {
68 TAILQ_FOREACH(bind, &bindings, bindings) {
69 LOG("Ungrabbing %d\n", bind->keycode);
70 xcb_ungrab_key(conn, bind->keycode, root, bind->keycode);
75 * Grab the bound keys (tell X to send us keypress events for those keycodes)
78 void grab_all_keys(xcb_connection_t *conn) {
80 TAILQ_FOREACH(bind, &bindings, bindings) {
81 LOG("Grabbing %d\n", bind->keycode);
82 if ((bind->mods & BIND_MODE_SWITCH) != 0)
83 xcb_grab_key(conn, 0, root, 0, bind->keycode,
84 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
86 /* Grab the key in all combinations */
87 #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
88 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
90 GRAB_KEY(bind->mods | xcb_numlock_mask);
91 GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
97 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
99 * If you specify override_configpath, only this path is used to look for a
100 * configuration file.
103 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
105 /* First ungrab the keys */
106 ungrab_all_keys(conn);
108 /* Clear the old binding and assignment lists */
110 while (!TAILQ_EMPTY(&bindings)) {
111 bind = TAILQ_FIRST(&bindings);
112 TAILQ_REMOVE(&bindings, bind, bindings);
117 struct Assignment *assign;
118 while (!TAILQ_EMPTY(&assignments)) {
119 assign = TAILQ_FIRST(&assignments);
120 FREE(assign->windowclass_title);
121 TAILQ_REMOVE(&assignments, assign, assignments);
126 SLIST_HEAD(variables_head, Variable) variables;
128 #define OPTION_STRING(name) \
129 if (strcasecmp(key, #name) == 0) { \
130 config.name = sstrdup(value); \
134 #define REQUIRED_OPTION(name) \
135 if (config.name == NULL) \
136 die("You did not specify required configuration option " #name "\n");
138 #define OPTION_COLORTRIPLE(opt, name) \
139 if (strcasecmp(key, opt) == 0) { \
140 char border[8], background[8], text[8]; \
141 memset(border, 0, sizeof(border)); \
142 memset(background, 0, sizeof(background)); \
143 memset(text, 0, sizeof(text)); \
144 border[0] = background[0] = text[0] = '#'; \
145 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
146 border + 1, background + 1, text + 1) != 3 || \
147 strlen(border) != 7 || \
148 strlen(background) != 7 || \
150 die("invalid color code line: %s\n", value); \
151 config.name.border = get_colorpixel(conn, border); \
152 config.name.background = get_colorpixel(conn, background); \
153 config.name.text = get_colorpixel(conn, text); \
157 /* Clear the old config or initialize the data structure */
158 memset(&config, 0, sizeof(config));
160 SLIST_INIT(&variables);
162 /* Initialize default colors */
163 config.client.focused.border = get_colorpixel(conn, "#4c7899");
164 config.client.focused.background = get_colorpixel(conn, "#285577");
165 config.client.focused.text = get_colorpixel(conn, "#ffffff");
167 config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
168 config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
169 config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
171 config.client.unfocused.border = get_colorpixel(conn, "#333333");
172 config.client.unfocused.background = get_colorpixel(conn, "#222222");
173 config.client.unfocused.text = get_colorpixel(conn, "#888888");
175 config.bar.focused.border = get_colorpixel(conn, "#4c7899");
176 config.bar.focused.background = get_colorpixel(conn, "#285577");
177 config.bar.focused.text = get_colorpixel(conn, "#ffffff");
179 config.bar.unfocused.border = get_colorpixel(conn, "#333333");
180 config.bar.unfocused.background = get_colorpixel(conn, "#222222");
181 config.bar.unfocused.text = get_colorpixel(conn, "#888888");
184 if (override_configpath != NULL) {
185 if ((handle = fopen(override_configpath, "r")) == NULL)
186 die("Could not open configfile \"%s\".\n", override_configpath);
188 /* We first check for ~/.i3/config, then for /etc/i3/config */
189 char *globbed = glob_path("~/.i3/config");
190 if ((handle = fopen(globbed, "r")) == NULL)
191 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
192 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
195 char key[512], value[512], buffer[1026];
197 while (!feof(handle)) {
198 if (fgets(buffer, 1024, handle) == NULL) {
199 /* fgets returns NULL on EOF and on error, so see which one it is. */
202 die("Could not read configuration file\n");
205 if (config.terminal != NULL)
206 replace_variable(buffer, "$terminal", config.terminal);
208 /* Replace all custom variables */
209 struct Variable *current;
210 SLIST_FOREACH(current, &variables, variables)
211 replace_variable(buffer, current->key, current->value);
213 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
214 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
215 key[0] == '#' || strlen(key) < 3)
218 OPTION_STRING(terminal);
222 OPTION_COLORTRIPLE("client.focused", client.focused);
223 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
224 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
225 OPTION_COLORTRIPLE("bar.focused", bar.focused);
226 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
228 /* exec-lines (autostart) */
229 if (strcasecmp(key, "exec") == 0) {
230 struct Autostart *new = smalloc(sizeof(struct Autostart));
231 new->command = sstrdup(value);
232 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
237 if (strcasecmp(key, "bind") == 0) {
238 #define CHECK_MODIFIER(name) \
239 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
240 modifiers |= BIND_##name; \
241 walk += strlen(#name) + 1; \
244 char *walk = value, *rest;
245 uint32_t modifiers = 0;
247 while (*walk != '\0') {
248 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
249 CHECK_MODIFIER(SHIFT);
250 CHECK_MODIFIER(CONTROL);
251 CHECK_MODIFIER(MODE_SWITCH);
252 CHECK_MODIFIER(MOD1);
253 CHECK_MODIFIER(MOD2);
254 CHECK_MODIFIER(MOD3);
255 CHECK_MODIFIER(MOD4);
256 CHECK_MODIFIER(MOD5);
258 /* No modifier found? Then we’re done with this step */
262 /* Now check for the keycode */
263 int keycode = strtol(walk, &rest, 10);
264 if (!rest || *rest != ' ')
265 die("Invalid binding\n");
267 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
268 Binding *new = smalloc(sizeof(Binding));
269 new->keycode = keycode;
270 new->mods = modifiers;
271 new->command = sstrdup(rest);
272 TAILQ_INSERT_TAIL(&bindings, new, bindings);
276 if (strcasecmp(key, "floating_modifier") == 0) {
278 uint32_t modifiers = 0;
280 while (*walk != '\0') {
281 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
282 CHECK_MODIFIER(SHIFT);
283 CHECK_MODIFIER(CONTROL);
284 CHECK_MODIFIER(MODE_SWITCH);
285 CHECK_MODIFIER(MOD1);
286 CHECK_MODIFIER(MOD2);
287 CHECK_MODIFIER(MOD3);
288 CHECK_MODIFIER(MOD4);
289 CHECK_MODIFIER(MOD5);
291 /* No modifier found? Then we’re done with this step */
295 LOG("Floating modifiers = %d\n", modifiers);
296 config.floating_modifier = modifiers;
300 /* name "workspace number" "name of the workspace" */
301 if (strcasecmp(key, "name") == 0) {
302 LOG("name workspace: %s\n",value);
303 char *ws_str = sstrdup(value);
304 char *end = strchr(ws_str, ' ');
306 die("Malformed name, couln't find terminating space\n");
309 /* Strip trailing whitespace */
310 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
311 value[strlen(value)-1] = '\0';
313 int ws_num = atoi(ws_str);
315 if (ws_num < 1 || ws_num > 10)
316 die("Malformed name, invalid workspace number\n");
320 name += strlen(ws_str) + 1;
327 workspace_set_name(&(workspaces[ws_num - 1]), name);
332 /* assign window class[/window title] → workspace */
333 if (strcasecmp(key, "assign") == 0) {
334 LOG("assign: \"%s\"\n", value);
339 /* If the window class/title is quoted we skip quotes */
340 if (value[0] == '"') {
341 class_title = sstrdup(value+1);
342 end = strchr(class_title, '"');
344 class_title = sstrdup(value);
345 /* If it is not quoted, we terminate it at the first space */
346 end = strchr(class_title, ' ');
349 die("Malformed assignment, couldn't find terminating quote\n");
352 /* Strip trailing whitespace */
353 while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
354 value[strlen(value)-1] = '\0';
356 /* The target is the last argument separated by a space */
357 if ((target = strrchr(value, ' ')) == NULL)
358 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
361 if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
362 die("Malformed assignment, invalid workspace number\n");
364 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
366 struct Assignment *new = scalloc(sizeof(struct Assignment));
367 new->windowclass_title = class_title;
368 if (strchr(target, '~') != NULL)
369 new->floating = ASSIGN_FLOATING_ONLY;
371 while (*target == '~')
374 if (atoi(target) >= 1 && atoi(target) <= 10) {
375 if (new->floating == ASSIGN_FLOATING_ONLY)
376 new->floating = ASSIGN_FLOATING;
377 new->workspace = atoi(target);
379 TAILQ_INSERT_TAIL(&assignments, new, assignments);
381 LOG("Assignment loaded: \"%s\":\n", class_title);
382 if (new->floating != ASSIGN_FLOATING_ONLY)
383 LOG(" to workspace %d\n", new->workspace);
385 if (new->floating != ASSIGN_FLOATING_NO)
386 LOG(" will be floating\n");
391 /* set a custom variable */
392 if (strcasecmp(key, "set") == 0) {
394 die("Malformed variable assignment, name has to start with $\n");
396 /* get key/value for this variable */
397 char *v_key = value, *v_value;
398 if ((v_value = strstr(value, " ")) == NULL)
399 die("Malformed variable assignment, need a value\n");
403 struct Variable *new = scalloc(sizeof(struct Variable));
404 new->key = sstrdup(v_key);
405 new->value = sstrdup(v_value);
406 SLIST_INSERT_HEAD(&variables, new, variables);
407 LOG("Got new variable %s = %s\n", v_key, v_value);
411 die("Unknown configfile option: %s\n", key);
413 /* now grab all keys again */
418 REQUIRED_OPTION(terminal);
419 REQUIRED_OPTION(font);
421 while (!SLIST_EMPTY(&variables)) {
422 struct Variable *v = SLIST_FIRST(&variables);
423 SLIST_REMOVE_HEAD(&variables, variables);
429 /* Set an empty name for every workspace which got no name */
430 for (int i = 0; i < 10; i++) {
431 if (workspaces[i].name != NULL)
434 workspace_set_name(&(workspaces[i]), NULL);