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