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_COLOR(opt, name) \
56 if (strcasecmp(key, opt) == 0) { \
57 config.name = sstrdup(value); \
61 #define VERIFY_COLOR(name, def) \
62 if (config.name == NULL) { \
63 config.name = (char *)malloc(7); \
64 memset(config.name, 0, 7); \
66 if ((strlen(config.name) != 7) || (config.name[0] != '#')) { \
67 strncpy(config.name, def, 7); \
70 /* Clear the old config or initialize the data structure */
71 memset(&config, 0, sizeof(config));
74 if (override_configpath != NULL) {
75 if ((handle = fopen(override_configpath, "r")) == NULL)
76 die("Could not open configfile \"%s\".\n", override_configpath);
78 /* We first check for ~/.i3/config, then for /etc/i3/config */
79 char *globbed = glob_path("~/.i3/config");
80 if ((handle = fopen(globbed, "r")) == NULL)
81 if ((handle = fopen("/etc/i3/config", "r")) == NULL)
82 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
85 char key[512], value[512], buffer[1026];
87 while (!feof(handle)) {
88 if (fgets(buffer, 1024, handle) == NULL) {
89 /* fgets returns NULL on EOF and on error, so see which one it is. */
92 die("Could not read configuration file\n");
95 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
96 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
97 key[0] == '#' || strlen(key) < 3)
100 OPTION_STRING(terminal);
104 OPTION_COLOR("client.focused.background.active",
105 client_focused_background_active);
106 OPTION_COLOR("client.focused.background.inactive",
107 client_focused_background_inactive);
108 OPTION_COLOR("client.focused.text",
109 client_focused_text);
110 OPTION_COLOR("client.focused.border",
111 client_focused_border);
112 OPTION_COLOR("client.unfocused.background",
113 client_unfocused_background);
114 OPTION_COLOR("client.unfocused.text",
115 client_unfocused_text);
116 OPTION_COLOR("client.unfocused.border",
117 client_unfocused_border);
118 OPTION_COLOR("bar.focused.background",
119 bar_focused_background);
120 OPTION_COLOR("bar.focused.text",
122 OPTION_COLOR("bar.focused.border",
124 OPTION_COLOR("bar.unfocused.background",
125 bar_unfocused_background);
126 OPTION_COLOR("bar.unfocused.text",
128 OPTION_COLOR("bar.unfocused.border",
129 bar_unfocused_border);
131 /* exec-lines (autostart) */
132 if (strcasecmp(key, "exec") == 0) {
133 struct Autostart *new = smalloc(sizeof(struct Autostart));
134 new->command = sstrdup(value);
135 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
140 if (strcasecmp(key, "bind") == 0) {
141 #define CHECK_MODIFIER(name) \
142 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
143 modifiers |= BIND_##name; \
144 walk += strlen(#name) + 1; \
147 char *walk = value, *rest;
148 uint32_t modifiers = 0;
150 while (*walk != '\0') {
151 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
152 CHECK_MODIFIER(SHIFT);
153 CHECK_MODIFIER(CONTROL);
154 CHECK_MODIFIER(MODE_SWITCH);
155 CHECK_MODIFIER(MOD1);
156 CHECK_MODIFIER(MOD2);
157 CHECK_MODIFIER(MOD3);
158 CHECK_MODIFIER(MOD4);
159 CHECK_MODIFIER(MOD5);
161 /* No modifier found? Then we’re done with this step */
165 /* Now check for the keycode */
166 int keycode = strtol(walk, &rest, 10);
167 if (!rest || *rest != ' ')
168 die("Invalid binding\n");
170 LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
171 Binding *new = smalloc(sizeof(Binding));
172 new->keycode = keycode;
173 new->mods = modifiers;
174 new->command = sstrdup(rest);
175 TAILQ_INSERT_TAIL(&bindings, new, bindings);
179 /* assign window class[/window title] → workspace */
180 if (strcasecmp(key, "assign") == 0) {
181 LOG("assign: \"%s\"\n", value);
182 char *class_title = sstrdup(value);
185 /* If the window class/title is quoted we skip quotes */
186 if (class_title[0] == '"') {
188 char *end = strchr(class_title, '"');
190 die("Malformed assignment, couldn't find terminating quote\n");
193 /* If it is not quoted, we terminate it at the first space */
194 char *end = strchr(class_title, ' ');
196 die("Malformed assignment, couldn't find terminating space\n");
200 /* The target is the last argument separated by a space */
201 if ((target = strrchr(value, ' ')) == NULL)
202 die("Malformed assignment, couldn't find target\n");
205 if (atoi(target) < 1 || atoi(target) > 10)
206 die("Malformed assignment, invalid workspace number\n");
208 LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
210 struct Assignment *new = smalloc(sizeof(struct Assignment));
211 new->windowclass_title = class_title;
212 new->workspace = atoi(target);
213 TAILQ_INSERT_TAIL(&assignments, new, assignments);
217 fprintf(stderr, "Unknown configfile option: %s\n", key);
222 VERIFY_COLOR(client_focused_background_active, "#285577");
223 VERIFY_COLOR(client_focused_background_inactive, "#555555");
224 VERIFY_COLOR(client_focused_text, "#ffffff");
225 VERIFY_COLOR(client_focused_border, "#4c7899");
226 VERIFY_COLOR(client_unfocused_background,"#222222");
227 VERIFY_COLOR(client_unfocused_text, "#888888");
228 VERIFY_COLOR(client_unfocused_border, "#333333");
229 VERIFY_COLOR(bar_focused_background, "#285577");
230 VERIFY_COLOR(bar_focused_text, "#ffffff");
231 VERIFY_COLOR(bar_focused_border, "#4c7899");
232 VERIFY_COLOR(bar_unfocused_background, "#222222");
233 VERIFY_COLOR(bar_unfocused_text, "#888888");
234 VERIFY_COLOR(bar_unfocused_border, "#333333");
236 REQUIRED_OPTION(terminal);
237 REQUIRED_OPTION(font);