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