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