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