4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
24 * This function resolves ~ in pathnames.
27 static char *glob_path(const char *path) {
28 static glob_t globbuf;
29 if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
31 char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
38 * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
40 * If you specify override_configpath, only this path is used to look for a
44 void load_configuration(const char *override_configpath) {
45 #define OPTION_STRING(name) \
46 if (strcasecmp(key, #name) == 0) { \
47 config.name = sstrdup(value); \
51 #define REQUIRED_OPTION(name) \
52 if (config.name == NULL) \
53 die("You did not specify required configuration option " #name "\n");
55 /* Clear the old config or initialize the data structure */
56 memset(&config, 0, sizeof(config));
59 if (override_configpath != NULL) {
60 if ((handle = fopen(override_configpath, "r")) == NULL)
61 die("Could not open configfile \"%s\".\n", override_configpath);
63 /* We first check for ~/.i3/config, then for /etc/i3/config */
64 char *globbed = glob_path("~/.i3/config");
65 if ((handle = fopen(globbed, "r")) == NULL)
66 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
67 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
70 char key[512], value[512], buffer[1026];
72 while (!feof(handle)) {
73 if (fgets(buffer, 1024, handle) == NULL) {
74 /* fgets returns NULL on EOF and on error, so see which one it is. */
77 die("Could not read configuration file\n");
80 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
81 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
82 key[0] == '#' || strlen(key) < 3)
85 OPTION_STRING(terminal);
88 /* exec-lines (autostart) */
89 if (strcasecmp(key, "exec") == 0) {
90 struct Autostart *new = smalloc(sizeof(struct Autostart));
91 new->command = sstrdup(value);
92 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
97 if (strcasecmp(key, "bind") == 0) {
98 #define CHECK_MODIFIER(name) \
99 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
100 modifiers |= BIND_##name; \
101 walk += strlen(#name) + 1; \
104 char *walk = value, *rest;
105 uint32_t modifiers = 0;
107 while (*walk != '\0') {
108 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
109 CHECK_MODIFIER(SHIFT);
110 CHECK_MODIFIER(CONTROL);
111 CHECK_MODIFIER(MODE_SWITCH);
112 CHECK_MODIFIER(MOD1);
113 CHECK_MODIFIER(MOD2);
114 CHECK_MODIFIER(MOD3);
115 CHECK_MODIFIER(MOD4);
116 CHECK_MODIFIER(MOD5);
118 /* No modifier found? Then we’re done with this step */
122 /* Now check for the keycode */
123 int keycode = strtol(walk, &rest, 10);
124 if (!rest || *rest != ' ')
125 die("Invalid binding\n");
127 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
128 Binding *new = smalloc(sizeof(Binding));
129 new->keycode = keycode;
130 new->mods = modifiers;
131 new->command = sstrdup(rest);
132 TAILQ_INSERT_TAIL(&bindings, new, bindings);
136 /* assign window class[/window title] → workspace */
137 if (strcasecmp(key, "assign") == 0) {
138 LOG("assign: \"%s\"\n", value);
139 char *class_title = sstrdup(value);
142 /* If the window class/title is quoted we skip quotes */
143 if (class_title[0] == '"') {
145 char *end = strchr(class_title, '"');
147 die("Malformatted assignment, couldn't find finishing quote\n");
150 /* If it is not quoted, we terminate it at the first space */
151 char *end = strchr(class_title, ' ');
153 die("Malformed assignment, couldn't find terminating space\n");
157 /* The target is the last argument separated by a space */
158 if ((target = strrchr(value, ' ')) == NULL)
159 die("Malformed assignment, couldn't find target\n");
162 if (atoi(target) < 1 || atoi(target) > 10)
163 die("Malformed assignment, invalid workspace number\n");
165 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
167 struct Assignment *new = smalloc(sizeof(struct Assignment));
168 new->windowclass_title = class_title;
169 new->workspace = atoi(target);
170 TAILQ_INSERT_TAIL(&assignments, new, assignments);
174 fprintf(stderr, "Unknown configfile option: %s\n", key);
179 REQUIRED_OPTION(terminal);
180 REQUIRED_OPTION(font);