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