]> git.sur5r.net Git - i3/i3/blob - src/config.c
Update website to use the new design
[i3/i3] / src / config.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <stdlib.h>
15 #include <glob.h>
16
17 #include "i3.h"
18 #include "util.h"
19 #include "config.h"
20 #include "xcb.h"
21
22 Config config;
23
24 /*
25  * This function resolves ~ in pathnames.
26  *
27  */
28 static char *glob_path(const char *path) {
29         static glob_t globbuf;
30         if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0)
31                 die("glob() failed");
32         char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
33         globfree(&globbuf);
34         return result;
35 }
36
37 /*
38  * This function does a very simple replacement of each instance of key with value.
39  *
40  */
41 static void replace_variable(char *buffer, const char *key, const char *value) {
42         char *pos;
43         /* To prevent endless recursions when the user makes an error configuring,
44          * we stop after 100 replacements. That should be vastly more than enough. */
45         int c = 0;
46         LOG("Replacing %s with %s\n", key, value);
47         while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
48                 LOG("replacing variable %s in \"%s\" with \"%s\"\n", key, buffer, value);
49                 char *rest = pos + strlen(key);
50                 *pos = '\0';
51                 char *replaced;
52                 asprintf(&replaced, "%s%s%s", buffer, value, rest);
53                 /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
54                  * So we need to hard-code the dimensions here. */
55                 strncpy(buffer, replaced, 1026);
56                 free(replaced);
57         }
58 }
59
60 /*
61  * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
62  *
63  * If you specify override_configpath, only this path is used to look for a
64  * configuration file.
65  *
66  */
67 void load_configuration(xcb_connection_t *conn, const char *override_configpath) {
68         SLIST_HEAD(variables_head, Variable) variables;
69
70 #define OPTION_STRING(name) \
71         if (strcasecmp(key, #name) == 0) { \
72                 config.name = sstrdup(value); \
73                 continue; \
74         }
75
76 #define REQUIRED_OPTION(name) \
77         if (config.name == NULL) \
78                 die("You did not specify required configuration option " #name "\n");
79
80 #define OPTION_COLORTRIPLE(opt, name) \
81         if (strcasecmp(key, opt) == 0) { \
82                 char border[8], background[8], text[8]; \
83                 memset(border, 0, sizeof(border)); \
84                 memset(background, 0, sizeof(background)); \
85                 memset(text, 0, sizeof(text)); \
86                 border[0] = background[0] = text[0] = '#'; \
87                 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
88                     border + 1, background + 1, text + 1) != 3 || \
89                     strlen(border) != 7 || \
90                     strlen(background) != 7 || \
91                     strlen(text) != 7) \
92                         die("invalid color code line: %s\n", value); \
93                 config.name.border = get_colorpixel(conn, border); \
94                 config.name.background = get_colorpixel(conn, background); \
95                 config.name.text = get_colorpixel(conn, text); \
96                 continue; \
97         }
98
99         /* Clear the old config or initialize the data structure */
100         memset(&config, 0, sizeof(config));
101
102         SLIST_INIT(&variables);
103
104         /* Initialize default colors */
105         config.client.focused.border = get_colorpixel(conn, "#4c7899");
106         config.client.focused.background = get_colorpixel(conn, "#285577");
107         config.client.focused.text = get_colorpixel(conn, "#ffffff");
108
109         config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
110         config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
111         config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
112
113         config.client.unfocused.border = get_colorpixel(conn, "#333333");
114         config.client.unfocused.background = get_colorpixel(conn, "#222222");
115         config.client.unfocused.text = get_colorpixel(conn, "#888888");
116
117         config.bar.focused.border = get_colorpixel(conn, "#4c7899");
118         config.bar.focused.background = get_colorpixel(conn, "#285577");
119         config.bar.focused.text = get_colorpixel(conn, "#ffffff");
120
121         config.bar.unfocused.border = get_colorpixel(conn, "#333333");
122         config.bar.unfocused.background = get_colorpixel(conn, "#222222");
123         config.bar.unfocused.text = get_colorpixel(conn, "#888888");
124
125         FILE *handle;
126         if (override_configpath != NULL) {
127                 if ((handle = fopen(override_configpath, "r")) == NULL)
128                         die("Could not open configfile \"%s\".\n", override_configpath);
129         } else {
130                 /* We first check for ~/.i3/config, then for /etc/i3/config */
131                 char *globbed = glob_path("~/.i3/config");
132                 if ((handle = fopen(globbed, "r")) == NULL)
133                         if ((handle = fopen("/etc/i3/config", "r")) == NULL)
134                                 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
135                 free(globbed);
136         }
137         char key[512], value[512], buffer[1026];
138
139         while (!feof(handle)) {
140                 if (fgets(buffer, 1024, handle) == NULL) {
141                         /* fgets returns NULL on EOF and on error, so see which one it is. */
142                         if (feof(handle))
143                                 break;
144                         die("Could not read configuration file\n");
145                 }
146
147                 if (config.terminal != NULL)
148                         replace_variable(buffer, "$terminal", config.terminal);
149
150                 /* Replace all custom variables */
151                 struct Variable *current;
152                 SLIST_FOREACH(current, &variables, variables)
153                         replace_variable(buffer, current->key, current->value);
154
155                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
156                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
157                     key[0] == '#' || strlen(key) < 3)
158                         continue;
159
160                 OPTION_STRING(terminal);
161                 OPTION_STRING(font);
162
163                 /* Colors */
164                 OPTION_COLORTRIPLE("client.focused", client.focused);
165                 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
166                 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
167                 OPTION_COLORTRIPLE("bar.focused", bar.focused);
168                 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
169
170                 /* exec-lines (autostart) */
171                 if (strcasecmp(key, "exec") == 0) {
172                         struct Autostart *new = smalloc(sizeof(struct Autostart));
173                         new->command = sstrdup(value);
174                         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
175                         continue;
176                 }
177
178                 /* key bindings */
179                 if (strcasecmp(key, "bind") == 0) {
180                         #define CHECK_MODIFIER(name) \
181                                 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
182                                         modifiers |= BIND_##name; \
183                                         walk += strlen(#name) + 1; \
184                                         continue; \
185                                 }
186                         char *walk = value, *rest;
187                         uint32_t modifiers = 0;
188
189                         while (*walk != '\0') {
190                                 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
191                                 CHECK_MODIFIER(SHIFT);
192                                 CHECK_MODIFIER(CONTROL);
193                                 CHECK_MODIFIER(MODE_SWITCH);
194                                 CHECK_MODIFIER(MOD1);
195                                 CHECK_MODIFIER(MOD2);
196                                 CHECK_MODIFIER(MOD3);
197                                 CHECK_MODIFIER(MOD4);
198                                 CHECK_MODIFIER(MOD5);
199
200                                 /* No modifier found? Then we’re done with this step */
201                                 break;
202                         }
203
204                         /* Now check for the keycode */
205                         int keycode = strtol(walk, &rest, 10);
206                         if (!rest || *rest != ' ')
207                                 die("Invalid binding\n");
208                         rest++;
209                         LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
210                         Binding *new = smalloc(sizeof(Binding));
211                         new->keycode = keycode;
212                         new->mods = modifiers;
213                         new->command = sstrdup(rest);
214                         TAILQ_INSERT_TAIL(&bindings, new, bindings);
215                         continue;
216                 }
217
218                 if (strcasecmp(key, "floating_modifier") == 0) {
219                         char *walk = value;
220                         uint32_t modifiers = 0;
221
222                         while (*walk != '\0') {
223                                 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
224                                 CHECK_MODIFIER(SHIFT);
225                                 CHECK_MODIFIER(CONTROL);
226                                 CHECK_MODIFIER(MODE_SWITCH);
227                                 CHECK_MODIFIER(MOD1);
228                                 CHECK_MODIFIER(MOD2);
229                                 CHECK_MODIFIER(MOD3);
230                                 CHECK_MODIFIER(MOD4);
231                                 CHECK_MODIFIER(MOD5);
232
233                                 /* No modifier found? Then we’re done with this step */
234                                 break;
235                         }
236
237                         LOG("Floating modifiers = %d\n", modifiers);
238                         config.floating_modifier = modifiers;
239                         continue;
240                 }
241
242                 /* assign window class[/window title] → workspace */
243                 if (strcasecmp(key, "assign") == 0) {
244                         LOG("assign: \"%s\"\n", value);
245                         char *class_title = sstrdup(value);
246                         char *target;
247
248                         /* If the window class/title is quoted we skip quotes */
249                         if (class_title[0] == '"') {
250                                 class_title++;
251                                 char *end = strchr(class_title, '"');
252                                 if (end == NULL)
253                                         die("Malformed assignment, couldn't find terminating quote\n");
254                                 *end = '\0';
255                         } else {
256                                 /* If it is not quoted, we terminate it at the first space */
257                                 char *end = strchr(class_title, ' ');
258                                 if (end == NULL)
259                                         die("Malformed assignment, couldn't find terminating space\n");
260                                 *end = '\0';
261                         }
262
263                         /* Strip trailing whitespace */
264                         while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
265                                 value[strlen(value)-1] = '\0';
266
267                         /* The target is the last argument separated by a space */
268                         if ((target = strrchr(value, ' ')) == NULL)
269                                 die("Malformed assignment, couldn't find target\n");
270                         target++;
271
272                         if (*target != '~' && (atoi(target) < 1 || atoi(target) > 10))
273                                 die("Malformed assignment, invalid workspace number\n");
274
275                         LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
276
277                         struct Assignment *new = scalloc(sizeof(struct Assignment));
278                         new->windowclass_title = class_title;
279                         if (*target == '~')
280                                 new->floating = true;
281                         else new->workspace = atoi(target);
282                         TAILQ_INSERT_TAIL(&assignments, new, assignments);
283                         continue;
284                 }
285
286                 /* set a custom variable */
287                 if (strcasecmp(key, "set") == 0) {
288                         if (value[0] != '$')
289                                 die("Malformed variable assignment, name has to start with $\n");
290
291                         /* get key/value for this variable */
292                         char *v_key = value, *v_value;
293                         if ((v_value = strstr(value, " ")) == NULL)
294                                 die("Malformed variable assignment, need a value\n");
295
296                         *(v_value++) = '\0';
297
298                         struct Variable *new = scalloc(sizeof(struct Variable));
299                         new->key = sstrdup(v_key);
300                         new->value = sstrdup(v_value);
301                         SLIST_INSERT_HEAD(&variables, new, variables);
302                         LOG("Got new variable %s = %s\n", v_key, v_value);
303                         continue;
304                 }
305
306                 die("Unknown configfile option: %s\n", key);
307         }
308         fclose(handle);
309
310         REQUIRED_OPTION(terminal);
311         REQUIRED_OPTION(font);
312
313
314         while (!SLIST_EMPTY(&variables)) {
315                 struct Variable *v = SLIST_FIRST(&variables);
316                 SLIST_REMOVE_HEAD(&variables, variables);
317                 free(v->key);
318                 free(v->value);
319                 free(v);
320         }
321  
322         return;
323 }