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