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