]> git.sur5r.net Git - i3/i3/blob - src/config.c
ee188537845cb81054628ec8072e63210ca2c1ae
[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  * Ungrab the bound keys
62  *
63  */
64 void ungrab_all_keys(xcb_connection_t *conn) {
65         Binding *bind;
66         TAILQ_FOREACH(bind, &bindings, bindings) {
67                 LOG("Ungrabbing %d\n", bind->keycode);
68                 xcb_ungrab_key(conn, bind->keycode, root, bind->keycode);
69         }
70 }
71
72 /*
73  * Grab the bound keys (tell X to send us keypress events for those keycodes)
74  *
75  */
76 void grab_all_keys(xcb_connection_t *conn) {
77         Binding *bind;
78         TAILQ_FOREACH(bind, &bindings, bindings) {
79                 LOG("Grabbing %d\n", bind->keycode);
80                 if ((bind->mods & BIND_MODE_SWITCH) != 0)
81                         xcb_grab_key(conn, 0, root, 0, bind->keycode,
82                                 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_SYNC);
83                 else {
84                         /* Grab the key in all combinations */
85                         #define GRAB_KEY(modifier) xcb_grab_key(conn, 0, root, modifier, bind->keycode, \
86                                                                 XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC)
87                         GRAB_KEY(bind->mods);
88                         GRAB_KEY(bind->mods | xcb_numlock_mask);
89                         GRAB_KEY(bind->mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
90                 }
91         }
92 }
93
94 /*
95  * Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
96  *
97  * If you specify override_configpath, only this path is used to look for a
98  * configuration file.
99  *
100  */
101 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
102         if (reload) {
103                 /* First ungrab the keys */
104                 ungrab_all_keys(conn);
105
106                 /* Clear the old binding and assignment lists */
107                 Binding *bind;
108                 while (!TAILQ_EMPTY(&bindings)) {
109                         bind = TAILQ_FIRST(&bindings);
110                         TAILQ_REMOVE(&bindings, bind, bindings);
111                         FREE(bind->command);
112                         FREE(bind);
113                 }
114
115                 struct Assignment *assign;
116                 while (!TAILQ_EMPTY(&assignments)) {
117                         assign = TAILQ_FIRST(&assignments);
118                         FREE(assign->windowclass_title);
119                         TAILQ_REMOVE(&assignments, assign, assignments);
120                         FREE(assign);
121                 }
122         }
123
124         SLIST_HEAD(variables_head, Variable) variables;
125
126 #define OPTION_STRING(name) \
127         if (strcasecmp(key, #name) == 0) { \
128                 config.name = sstrdup(value); \
129                 continue; \
130         }
131
132 #define REQUIRED_OPTION(name) \
133         if (config.name == NULL) \
134                 die("You did not specify required configuration option " #name "\n");
135
136 #define OPTION_COLORTRIPLE(opt, name) \
137         if (strcasecmp(key, opt) == 0) { \
138                 char border[8], background[8], text[8]; \
139                 memset(border, 0, sizeof(border)); \
140                 memset(background, 0, sizeof(background)); \
141                 memset(text, 0, sizeof(text)); \
142                 border[0] = background[0] = text[0] = '#'; \
143                 if (sscanf(value, "#%06[0-9a-fA-F] #%06[0-9a-fA-F] #%06[0-9a-fA-F]", \
144                     border + 1, background + 1, text + 1) != 3 || \
145                     strlen(border) != 7 || \
146                     strlen(background) != 7 || \
147                     strlen(text) != 7) \
148                         die("invalid color code line: %s\n", value); \
149                 config.name.border = get_colorpixel(conn, border); \
150                 config.name.background = get_colorpixel(conn, background); \
151                 config.name.text = get_colorpixel(conn, text); \
152                 continue; \
153         }
154
155         /* Clear the old config or initialize the data structure */
156         memset(&config, 0, sizeof(config));
157
158         SLIST_INIT(&variables);
159
160         /* Initialize default colors */
161         config.client.focused.border = get_colorpixel(conn, "#4c7899");
162         config.client.focused.background = get_colorpixel(conn, "#285577");
163         config.client.focused.text = get_colorpixel(conn, "#ffffff");
164
165         config.client.focused_inactive.border = get_colorpixel(conn, "#4c7899");
166         config.client.focused_inactive.background = get_colorpixel(conn, "#555555");
167         config.client.focused_inactive.text = get_colorpixel(conn, "#ffffff");
168
169         config.client.unfocused.border = get_colorpixel(conn, "#333333");
170         config.client.unfocused.background = get_colorpixel(conn, "#222222");
171         config.client.unfocused.text = get_colorpixel(conn, "#888888");
172
173         config.bar.focused.border = get_colorpixel(conn, "#4c7899");
174         config.bar.focused.background = get_colorpixel(conn, "#285577");
175         config.bar.focused.text = get_colorpixel(conn, "#ffffff");
176
177         config.bar.unfocused.border = get_colorpixel(conn, "#333333");
178         config.bar.unfocused.background = get_colorpixel(conn, "#222222");
179         config.bar.unfocused.text = get_colorpixel(conn, "#888888");
180
181         FILE *handle;
182         if (override_configpath != NULL) {
183                 if ((handle = fopen(override_configpath, "r")) == NULL)
184                         die("Could not open configfile \"%s\".\n", override_configpath);
185         } else {
186                 /* We first check for ~/.i3/config, then for /etc/i3/config */
187                 char *globbed = glob_path("~/.i3/config");
188                 if ((handle = fopen(globbed, "r")) == NULL)
189                         if ((handle = fopen("/etc/i3/config", "r")) == NULL)
190                                 die("Neither \"%s\" nor /etc/i3/config could be opened\n", globbed);
191                 free(globbed);
192         }
193         char key[512], value[512], buffer[1026];
194
195         while (!feof(handle)) {
196                 if (fgets(buffer, 1024, handle) == NULL) {
197                         /* fgets returns NULL on EOF and on error, so see which one it is. */
198                         if (feof(handle))
199                                 break;
200                         die("Could not read configuration file\n");
201                 }
202
203                 if (config.terminal != NULL)
204                         replace_variable(buffer, "$terminal", config.terminal);
205
206                 /* Replace all custom variables */
207                 struct Variable *current;
208                 SLIST_FOREACH(current, &variables, variables)
209                         replace_variable(buffer, current->key, current->value);
210
211                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
212                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
213                     key[0] == '#' || strlen(key) < 3)
214                         continue;
215
216                 OPTION_STRING(terminal);
217                 OPTION_STRING(font);
218
219                 /* Colors */
220                 OPTION_COLORTRIPLE("client.focused", client.focused);
221                 OPTION_COLORTRIPLE("client.focused_inactive", client.focused_inactive);
222                 OPTION_COLORTRIPLE("client.unfocused", client.unfocused);
223                 OPTION_COLORTRIPLE("bar.focused", bar.focused);
224                 OPTION_COLORTRIPLE("bar.unfocused", bar.unfocused);
225
226                 /* exec-lines (autostart) */
227                 if (strcasecmp(key, "exec") == 0) {
228                         struct Autostart *new = smalloc(sizeof(struct Autostart));
229                         new->command = sstrdup(value);
230                         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
231                         continue;
232                 }
233
234                 /* key bindings */
235                 if (strcasecmp(key, "bind") == 0) {
236                         #define CHECK_MODIFIER(name) \
237                                 if (strncasecmp(walk, #name, strlen(#name)) == 0) { \
238                                         modifiers |= BIND_##name; \
239                                         walk += strlen(#name) + 1; \
240                                         continue; \
241                                 }
242                         char *walk = value, *rest;
243                         uint32_t modifiers = 0;
244
245                         while (*walk != '\0') {
246                                 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
247                                 CHECK_MODIFIER(SHIFT);
248                                 CHECK_MODIFIER(CONTROL);
249                                 CHECK_MODIFIER(MODE_SWITCH);
250                                 CHECK_MODIFIER(MOD1);
251                                 CHECK_MODIFIER(MOD2);
252                                 CHECK_MODIFIER(MOD3);
253                                 CHECK_MODIFIER(MOD4);
254                                 CHECK_MODIFIER(MOD5);
255
256                                 /* No modifier found? Then we’re done with this step */
257                                 break;
258                         }
259
260                         /* Now check for the keycode */
261                         int keycode = strtol(walk, &rest, 10);
262                         if (!rest || *rest != ' ')
263                                 die("Invalid binding\n");
264                         rest++;
265                         LOG("keycode = %d, modifiers = %d, command = *%s*\n", keycode, modifiers, rest);
266                         Binding *new = smalloc(sizeof(Binding));
267                         new->keycode = keycode;
268                         new->mods = modifiers;
269                         new->command = sstrdup(rest);
270                         TAILQ_INSERT_TAIL(&bindings, new, bindings);
271                         continue;
272                 }
273
274                 if (strcasecmp(key, "floating_modifier") == 0) {
275                         char *walk = value;
276                         uint32_t modifiers = 0;
277
278                         while (*walk != '\0') {
279                                 /* Need to check for Mod1-5, Ctrl, Shift, Mode_switch */
280                                 CHECK_MODIFIER(SHIFT);
281                                 CHECK_MODIFIER(CONTROL);
282                                 CHECK_MODIFIER(MODE_SWITCH);
283                                 CHECK_MODIFIER(MOD1);
284                                 CHECK_MODIFIER(MOD2);
285                                 CHECK_MODIFIER(MOD3);
286                                 CHECK_MODIFIER(MOD4);
287                                 CHECK_MODIFIER(MOD5);
288
289                                 /* No modifier found? Then we’re done with this step */
290                                 break;
291                         }
292
293                         LOG("Floating modifiers = %d\n", modifiers);
294                         config.floating_modifier = modifiers;
295                         continue;
296                 }
297
298                 /* assign window class[/window title] → workspace */
299                 if (strcasecmp(key, "assign") == 0) {
300                         LOG("assign: \"%s\"\n", value);
301                         char *class_title;
302                         char *target;
303                         char *end;
304
305                         /* If the window class/title is quoted we skip quotes */
306                         if (value[0] == '"') {
307                                 class_title = sstrdup(value+1);
308                                 end = strchr(class_title, '"');
309                         } else {
310                                 class_title = sstrdup(value);
311                                 /* If it is not quoted, we terminate it at the first space */
312                                 end = strchr(class_title, ' ');
313                         }
314                         if (end == NULL)
315                                 die("Malformed assignment, couldn't find terminating quote\n");
316                         *end = '\0';
317
318                         /* Strip trailing whitespace */
319                         while (strlen(value) > 0 && value[strlen(value)-1] == ' ')
320                                 value[strlen(value)-1] = '\0';
321
322                         /* The target is the last argument separated by a space */
323                         if ((target = strrchr(value, ' ')) == NULL)
324                                 die("Malformed assignment, couldn't find target (\"%s\")\n", value);
325                         target++;
326
327                         if (strchr(target, '~') == NULL && (atoi(target) < 1 || atoi(target) > 10))
328                                 die("Malformed assignment, invalid workspace number\n");
329
330                         LOG("assignment parsed: \"%s\" to \"%s\"\n", class_title, target);
331
332                         struct Assignment *new = scalloc(sizeof(struct Assignment));
333                         new->windowclass_title = class_title;
334                         if (strchr(target, '~') != NULL)
335                                 new->floating = ASSIGN_FLOATING_ONLY;
336
337                         while (*target == '~')
338                                 target++;
339
340                         if (atoi(target) >= 1 && atoi(target) <= 10) {
341                                 if (new->floating == ASSIGN_FLOATING_ONLY)
342                                         new->floating = ASSIGN_FLOATING;
343                                 new->workspace = atoi(target);
344                         }
345                         TAILQ_INSERT_TAIL(&assignments, new, assignments);
346
347                         LOG("Assignment loaded: \"%s\":\n", class_title);
348                         if (new->floating != ASSIGN_FLOATING_ONLY)
349                                 LOG(" to workspace %d\n", new->workspace);
350
351                         if (new->floating != ASSIGN_FLOATING_NO)
352                                 LOG(" will be floating\n");
353
354                         continue;
355                 }
356
357                 /* set a custom variable */
358                 if (strcasecmp(key, "set") == 0) {
359                         if (value[0] != '$')
360                                 die("Malformed variable assignment, name has to start with $\n");
361
362                         /* get key/value for this variable */
363                         char *v_key = value, *v_value;
364                         if ((v_value = strstr(value, " ")) == NULL)
365                                 die("Malformed variable assignment, need a value\n");
366
367                         *(v_value++) = '\0';
368
369                         struct Variable *new = scalloc(sizeof(struct Variable));
370                         new->key = sstrdup(v_key);
371                         new->value = sstrdup(v_value);
372                         SLIST_INSERT_HEAD(&variables, new, variables);
373                         LOG("Got new variable %s = %s\n", v_key, v_value);
374                         continue;
375                 }
376
377                 die("Unknown configfile option: %s\n", key);
378         }
379         /* now grab all keys again */
380         if (reload)
381                 grab_all_keys(conn);
382         fclose(handle);
383
384         REQUIRED_OPTION(terminal);
385         REQUIRED_OPTION(font);
386
387         while (!SLIST_EMPTY(&variables)) {
388                 struct Variable *v = SLIST_FIRST(&variables);
389                 SLIST_REMOVE_HEAD(&variables, variables);
390                 free(v->key);
391                 free(v->value);
392                 free(v);
393         }
394  
395         return;
396 }