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