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 #define OPTION_COLORTRIPLE(opt, name) \
56 if (strcasecmp(key, opt) == 0) { \
57 struct Colortriple buffer; \
58 memset(&buffer, 0, sizeof(struct Colortriple)); \
59 buffer.border[0] = buffer.background[0] = buffer.text[0] = '#'; \
60 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
61 buffer.border + 1, buffer.background + 1, buffer.text + 1) != 3 || \
62 strlen(buffer.border) != 7 || \
63 strlen(buffer.background) != 7 || \
64 strlen(buffer.text) != 7) \
65 die("invalid color code line: %s\n", value); \
66 memcpy(&config.name, &buffer, sizeof(struct Colortriple)); \
70 /* Clear the old config or initialize the data structure */
71 memset(&config, 0, sizeof(config));
73 /* Initialize default colors */
74 strcpy(config.client.focused.border, "#4c7899");
75 strcpy(config.client.focused.background, "#285577");
76 strcpy(config.client.focused.text, "#ffffff");
78 strcpy(config.client.focused_inactive.border, "#4c7899");
79 strcpy(config.client.focused_inactive.background, "#555555");
80 strcpy(config.client.focused_inactive.text, "#ffffff");
82 strcpy(config.client.unfocused.border, "#333333");
83 strcpy(config.client.unfocused.background, "#222222");
84 strcpy(config.client.unfocused.text, "#888888");
86 strcpy(config.bar.focused.border, "#4c7899");
87 strcpy(config.bar.focused.background, "#285577");
88 strcpy(config.bar.focused.text, "#ffffff");
90 strcpy(config.bar.unfocused.border, "#333333");
91 strcpy(config.bar.unfocused.background, "#222222");
92 strcpy(config.bar.unfocused.text, "#888888");
95 if (override_configpath != NULL) {
96 if ((handle = fopen(override_configpath, "r")) == NULL)
97 die("Could not open configfile \"%s\".\n", override_configpath);
99 /* We first check for ~/.i3/config, then for /etc/i3/config */
100 char *globbed = glob_path("~/.i3/config");
101 if ((handle = fopen(globbed, "r")) == NULL)
102 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
103 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
106 char key[512], value[512], buffer[1026];
108 while (!feof(handle)) {
109 if (fgets(buffer, 1024, handle) == NULL) {
110 /* fgets returns NULL on EOF and on error, so see which one it is. */
113 die("Could not read configuration file\n");
116 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
117 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
118 key[0] == '#' || strlen(key) < 3)
121 OPTION_STRING(terminal);
125 OPTION_COLORTRIPLE("client.focused", client.focused);
126 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
127 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
128 OPTION_COLORTRIPLE("client.focused", client.focused);
129 OPTION_COLORTRIPLE("bar.focused", bar.focused);
130 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
132 /* exec-lines (autostart) */
133 if (strcasecmp(key, "exec") == 0) {
134 struct Autostart *new = smalloc(sizeof(struct Autostart));
135 new->command = sstrdup(value);
136 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
141 if (strcasecmp(key, "bind") == 0) {
142 #define CHECK_MODIFIER(name) \
143 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
144 modifiers |= BIND_##name; \
145 walk += strlen(#name) + 1; \
148 char *walk = value, *rest;
149 uint32_t modifiers = 0;
151 while (*walk != '\0') {
152 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
153 CHECK_MODIFIER(SHIFT);
154 CHECK_MODIFIER(CONTROL);
155 CHECK_MODIFIER(MODE_SWITCH);
156 CHECK_MODIFIER(MOD1);
157 CHECK_MODIFIER(MOD2);
158 CHECK_MODIFIER(MOD3);
159 CHECK_MODIFIER(MOD4);
160 CHECK_MODIFIER(MOD5);
162 /* No modifier found? Then we’re done with this step */
166 /* Now check for the keycode */
167 int keycode = strtol(walk, &rest, 10);
168 if (!rest || *rest != ' ')
169 die("Invalid binding\n");
171 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
172 Binding *new = smalloc(sizeof(Binding));
173 new->keycode = keycode;
174 new->mods = modifiers;
175 new->command = sstrdup(rest);
176 TAILQ_INSERT_TAIL(&bindings, new, bindings);
180 /* assign window class[/window title] → workspace */
181 if (strcasecmp(key, "assign") == 0) {
182 LOG("assign: \"%s\"\n", value);
183 char *class_title = sstrdup(value);
186 /* If the window class/title is quoted we skip quotes */
187 if (class_title[0] == '"') {
189 char *end = strchr(class_title, '"');
191 die("Malformed assignment, couldn't find terminating quote\n");
194 /* If it is not quoted, we terminate it at the first space */
195 char *end = strchr(class_title, ' ');
197 die("Malformed assignment, couldn't find terminating space\n");
201 /* The target is the last argument separated by a space */
202 if ((target = strrchr(value, ' ')) == NULL)
203 die("Malformed assignment, couldn't find target\n");
206 if (atoi(target) < 1 || atoi(target) > 10)
207 die("Malformed assignment, invalid workspace number\n");
209 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
211 struct Assignment *new = smalloc(sizeof(struct Assignment));
212 new->windowclass_title = class_title;
213 new->workspace = atoi(target);
214 TAILQ_INSERT_TAIL(&assignments, new, assignments);
218 die("Unknown configfile option: %s\n", key);
222 REQUIRED_OPTION(terminal);
223 REQUIRED_OPTION(font);