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