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