]> git.sur5r.net Git - i3/i3/blob - src/config.c
Merge branch 'master' into next
[i3/i3] / src / config.c
1 #undef I3__FILE__
2 #define I3__FILE__ "config.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * config.c: Configuration file (calling the parser (src/config_parser.c) with
10  *           the correct path, switching key bindings mode).
11  *
12  */
13 #include "all.h"
14
15 /* We need Xlib for XStringToKeysym */
16 #include <X11/Xlib.h>
17
18 char *current_configpath = NULL;
19 Config config;
20 struct modes_head modes;
21 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
22
23 /**
24  * Ungrabs all keys, to be called before re-grabbing the keys because of a
25  * mapping_notify event or a configuration file reload
26  *
27  */
28 void ungrab_all_keys(xcb_connection_t *conn) {
29     DLOG("Ungrabbing all keys\n");
30     xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
31 }
32
33 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
34     DLOG("Grabbing %d with modifiers %d (with mod_mask_lock %d)\n", keycode, bind->mods, bind->mods | XCB_MOD_MASK_LOCK);
35     /* Grab the key in all combinations */
36     #define GRAB_KEY(modifier) \
37         do { \
38             xcb_grab_key(conn, 0, root, modifier, keycode, \
39                          XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
40         } while (0)
41     int mods = bind->mods;
42     if ((bind->mods & BIND_MODE_SWITCH) != 0) {
43         mods &= ~BIND_MODE_SWITCH;
44         if (mods == 0)
45             mods = XCB_MOD_MASK_ANY;
46     }
47     GRAB_KEY(mods);
48     GRAB_KEY(mods | xcb_numlock_mask);
49     GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
50     GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
51 }
52
53 /*
54  * Returns a pointer to the Binding with the specified modifiers and keycode
55  * or NULL if no such binding exists.
56  *
57  */
58 Binding *get_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode) {
59     Binding *bind;
60
61     if (!key_release) {
62         /* On a KeyPress event, we first reset all
63          * B_UPON_KEYRELEASE_IGNORE_MODS bindings back to B_UPON_KEYRELEASE */
64         TAILQ_FOREACH(bind, bindings, bindings) {
65             if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
66                 bind->release = B_UPON_KEYRELEASE;
67         }
68     }
69
70     TAILQ_FOREACH(bind, bindings, bindings) {
71         /* First compare the modifiers (unless this is a
72          * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
73          * event) */
74         if (bind->mods != modifiers &&
75             (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
76              !key_release))
77             continue;
78
79         /* If a symbol was specified by the user, we need to look in
80          * the array of translated keycodes for the event’s keycode */
81         if (bind->symbol != NULL) {
82             if (memmem(bind->translated_to,
83                        bind->number_keycodes * sizeof(xcb_keycode_t),
84                        &keycode, sizeof(xcb_keycode_t)) == NULL)
85                 continue;
86         } else {
87             /* This case is easier: The user specified a keycode */
88             if (bind->keycode != keycode)
89                 continue;
90         }
91
92         /* If this keybinding is a KeyRelease binding, it matches the key which
93          * the user pressed. We therefore mark it as
94          * B_UPON_KEYRELEASE_IGNORE_MODS for later, so that the user can
95          * release the modifiers before the actual key and the KeyRelease will
96          * still be matched. */
97         if (bind->release == B_UPON_KEYRELEASE && !key_release)
98             bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
99
100         /* Check if the binding is for a KeyPress or a KeyRelease event */
101         if ((bind->release == B_UPON_KEYPRESS && key_release) ||
102             (bind->release >= B_UPON_KEYRELEASE && !key_release))
103             continue;
104
105         break;
106     }
107
108     return (bind == TAILQ_END(bindings) ? NULL : bind);
109 }
110
111 /*
112  * Translates keysymbols to keycodes for all bindings which use keysyms.
113  *
114  */
115 void translate_keysyms(void) {
116     Binding *bind;
117     xcb_keysym_t keysym;
118     int col;
119     xcb_keycode_t i,
120                   min_keycode = xcb_get_setup(conn)->min_keycode,
121                   max_keycode = xcb_get_setup(conn)->max_keycode;
122
123     TAILQ_FOREACH(bind, bindings, bindings) {
124         if (bind->keycode > 0)
125             continue;
126
127         /* We need to translate the symbol to a keycode */
128         keysym = XStringToKeysym(bind->symbol);
129         if (keysym == NoSymbol) {
130             ELOG("Could not translate string to key symbol: \"%s\"\n",
131                  bind->symbol);
132             continue;
133         }
134
135         /* Base column we use for looking up key symbols. We always consider
136          * the base column and the corresponding shift column, so without
137          * mode_switch, we look in 0 and 1, with mode_switch we look in 2 and
138          * 3. */
139         col = (bind->mods & BIND_MODE_SWITCH ? 2 : 0);
140
141         FREE(bind->translated_to);
142         bind->number_keycodes = 0;
143
144         for (i = min_keycode; i && i <= max_keycode; i++) {
145             if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
146                 (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
147                 continue;
148             bind->number_keycodes++;
149             bind->translated_to = srealloc(bind->translated_to,
150                                            (sizeof(xcb_keycode_t) *
151                                             bind->number_keycodes));
152             bind->translated_to[bind->number_keycodes-1] = i;
153         }
154
155         DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
156              bind->number_keycodes);
157     }
158 }
159
160 /*
161  * Grab the bound keys (tell X to send us keypress events for those keycodes)
162  *
163  */
164 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
165     Binding *bind;
166     TAILQ_FOREACH(bind, bindings, bindings) {
167         if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
168             (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
169             continue;
170
171         /* The easy case: the user specified a keycode directly. */
172         if (bind->keycode > 0) {
173             grab_keycode_for_binding(conn, bind, bind->keycode);
174             continue;
175         }
176
177         xcb_keycode_t *walk = bind->translated_to;
178         for (uint32_t i = 0; i < bind->number_keycodes; i++)
179             grab_keycode_for_binding(conn, bind, *walk++);
180     }
181 }
182
183 /*
184  * Switches the key bindings to the given mode, if the mode exists
185  *
186  */
187 void switch_mode(const char *new_mode) {
188     struct Mode *mode;
189
190     LOG("Switching to mode %s\n", new_mode);
191
192     SLIST_FOREACH(mode, &modes, modes) {
193         if (strcasecmp(mode->name, new_mode) != 0)
194             continue;
195
196         ungrab_all_keys(conn);
197         bindings = mode->bindings;
198         translate_keysyms();
199         grab_all_keys(conn, false);
200
201         char *event_msg;
202         sasprintf(&event_msg, "{\"change\":\"%s\"}", mode->name);
203
204         ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
205         FREE(event_msg);
206
207         return;
208     }
209
210     ELOG("ERROR: Mode not found\n");
211 }
212
213 /*
214  * Sends the current bar configuration as an event to all barconfig_update listeners.
215  * This update mechnism currently only includes the hidden_state and the mode in the config.
216  *
217  */
218 void update_barconfig() {
219     Barconfig *current;
220     TAILQ_FOREACH(current, &barconfigs, configs) {
221         /* Build json message */
222         char *hidden_state;
223         switch (current->hidden_state) {
224             case S_SHOW:
225                 hidden_state ="show";
226                 break;
227             case S_HIDE:
228             default:
229                 hidden_state = "hide";
230                 break;
231         }
232
233         char *mode;
234         switch (current->mode) {
235             case M_HIDE:
236                 mode ="hide";
237                 break;
238             case M_INVISIBLE:
239                 mode ="invisible";
240                 break;
241             case M_DOCK:
242             default:
243                 mode = "dock";
244                 break;
245         }
246
247         /* Send an event to all barconfig listeners*/
248         char *event_msg;
249         sasprintf(&event_msg, "{ \"id\":\"%s\", \"hidden_state\":\"%s\", \"mode\":\"%s\" }", current->id, hidden_state, mode);
250
251         ipc_send_event("barconfig_update", I3_IPC_EVENT_BARCONFIG_UPDATE, event_msg);
252         FREE(event_msg);
253     }
254 }
255
256 /*
257  * Get the path of the first configuration file found. If override_configpath
258  * is specified, that path is returned and saved for further calls. Otherwise,
259  * checks the home directory first, then the system directory first, always
260  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
261  * $XDG_CONFIG_DIRS)
262  *
263  */
264 static char *get_config_path(const char *override_configpath) {
265     char *xdg_config_home, *xdg_config_dirs, *config_path;
266
267     static const char *saved_configpath = NULL;
268
269     if (override_configpath != NULL) {
270         saved_configpath = override_configpath;
271         return sstrdup(saved_configpath);
272     }
273
274     if (saved_configpath != NULL)
275         return sstrdup(saved_configpath);
276
277     /* 1: check the traditional path under the home directory */
278     config_path = resolve_tilde("~/.i3/config");
279     if (path_exists(config_path))
280         return config_path;
281     free(config_path);
282
283     /* 2: check for $XDG_CONFIG_HOME/i3/config */
284     if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
285         xdg_config_home = "~/.config";
286
287     xdg_config_home = resolve_tilde(xdg_config_home);
288     sasprintf(&config_path, "%s/i3/config", xdg_config_home);
289     free(xdg_config_home);
290
291     if (path_exists(config_path))
292         return config_path;
293     free(config_path);
294
295     /* 3: check the traditional path under /etc */
296     config_path = SYSCONFDIR "/i3/config";
297     if (path_exists(config_path))
298         return sstrdup(config_path);
299
300     /* 4: check for $XDG_CONFIG_DIRS/i3/config */
301     if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
302         xdg_config_dirs = "/etc/xdg";
303
304     char *buf = sstrdup(xdg_config_dirs);
305     char *tok = strtok(buf, ":");
306     while (tok != NULL) {
307         tok = resolve_tilde(tok);
308         sasprintf(&config_path, "%s/i3/config", tok);
309         free(tok);
310         if (path_exists(config_path)) {
311             free(buf);
312             return config_path;
313         }
314         free(config_path);
315         tok = strtok(NULL, ":");
316     }
317     free(buf);
318
319     die("Unable to find the configuration file (looked at "
320             "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
321             SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
322 }
323
324 /*
325  * Finds the configuration file to use (either the one specified by
326  * override_configpath), the user’s one or the system default) and calls
327  * parse_file().
328  *
329  */
330 static void parse_configuration(const char *override_configpath) {
331     char *path = get_config_path(override_configpath);
332     LOG("Parsing configfile %s\n", path);
333     FREE(current_configpath);
334     current_configpath = path;
335     parse_file(path);
336 }
337
338 /*
339  * (Re-)loads the configuration file (sets useful defaults before).
340  *
341  */
342 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
343     if (reload) {
344         /* First ungrab the keys */
345         ungrab_all_keys(conn);
346
347         struct Mode *mode;
348         Binding *bind;
349         while (!SLIST_EMPTY(&modes)) {
350             mode = SLIST_FIRST(&modes);
351             FREE(mode->name);
352
353             /* Clear the old binding list */
354             bindings = mode->bindings;
355             while (!TAILQ_EMPTY(bindings)) {
356                 bind = TAILQ_FIRST(bindings);
357                 TAILQ_REMOVE(bindings, bind, bindings);
358                 FREE(bind->translated_to);
359                 FREE(bind->command);
360                 FREE(bind);
361             }
362             FREE(bindings);
363             SLIST_REMOVE(&modes, mode, Mode, modes);
364         }
365
366         struct Assignment *assign;
367         while (!TAILQ_EMPTY(&assignments)) {
368             assign = TAILQ_FIRST(&assignments);
369             if (assign->type == A_TO_WORKSPACE)
370                 FREE(assign->dest.workspace);
371             else if (assign->type == A_TO_OUTPUT)
372                 FREE(assign->dest.output);
373             else if (assign->type == A_COMMAND)
374                 FREE(assign->dest.command);
375             match_free(&(assign->match));
376             TAILQ_REMOVE(&assignments, assign, assignments);
377             FREE(assign);
378         }
379
380         /* Clear bar configs */
381         Barconfig *barconfig;
382         while (!TAILQ_EMPTY(&barconfigs)) {
383             barconfig = TAILQ_FIRST(&barconfigs);
384             FREE(barconfig->id);
385             for (int c = 0; c < barconfig->num_outputs; c++)
386                 free(barconfig->outputs[c]);
387             FREE(barconfig->outputs);
388             FREE(barconfig->tray_output);
389             FREE(barconfig->socket_path);
390             FREE(barconfig->status_command);
391             FREE(barconfig->i3bar_command);
392             FREE(barconfig->font);
393             FREE(barconfig->colors.background);
394             FREE(barconfig->colors.statusline);
395             FREE(barconfig->colors.focused_workspace_border);
396             FREE(barconfig->colors.focused_workspace_bg);
397             FREE(barconfig->colors.focused_workspace_text);
398             FREE(barconfig->colors.active_workspace_border);
399             FREE(barconfig->colors.active_workspace_bg);
400             FREE(barconfig->colors.active_workspace_text);
401             FREE(barconfig->colors.inactive_workspace_border);
402             FREE(barconfig->colors.inactive_workspace_bg);
403             FREE(barconfig->colors.inactive_workspace_text);
404             FREE(barconfig->colors.urgent_workspace_border);
405             FREE(barconfig->colors.urgent_workspace_bg);
406             FREE(barconfig->colors.urgent_workspace_text);
407             TAILQ_REMOVE(&barconfigs, barconfig, configs);
408             FREE(barconfig);
409         }
410
411         /* Clear workspace names */
412 #if 0
413         Workspace *ws;
414         TAILQ_FOREACH(ws, workspaces, workspaces)
415             workspace_set_name(ws, NULL);
416 #endif
417
418         /* Invalidate pixmap caches in case font or colors changed */
419         Con *con;
420         TAILQ_FOREACH(con, &all_cons, all_cons)
421             FREE(con->deco_render_params);
422
423         /* Get rid of the current font */
424         free_font();
425     }
426
427     SLIST_INIT(&modes);
428
429     struct Mode *default_mode = scalloc(sizeof(struct Mode));
430     default_mode->name = sstrdup("default");
431     default_mode->bindings = scalloc(sizeof(struct bindings_head));
432     TAILQ_INIT(default_mode->bindings);
433     SLIST_INSERT_HEAD(&modes, default_mode, modes);
434
435     bindings = default_mode->bindings;
436
437 #define REQUIRED_OPTION(name) \
438     if (config.name == NULL) \
439         die("You did not specify required configuration option " #name "\n");
440
441     /* Clear the old config or initialize the data structure */
442     memset(&config, 0, sizeof(config));
443
444     /* Initialize default colors */
445 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
446     do { \
447         x.border = get_colorpixel(cborder); \
448         x.background = get_colorpixel(cbackground); \
449         x.text = get_colorpixel(ctext); \
450         x.indicator = get_colorpixel(cindicator); \
451     } while (0)
452
453     config.client.background = get_colorpixel("#000000");
454     INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
455     INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
456     INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
457     INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
458
459     /* border and indicator color are ignored for placeholder contents */
460     INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
461
462     /* the last argument (indicator color) is ignored for bar colors */
463     INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
464     INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
465     INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
466
467     config.default_border = BS_NORMAL;
468     config.default_floating_border = BS_NORMAL;
469     config.default_border_width = 2;
470     /* Set default_orientation to NO_ORIENTATION for auto orientation. */
471     config.default_orientation = NO_ORIENTATION;
472
473     /* Set default urgency reset delay to 500ms */
474     if (config.workspace_urgency_timer == 0)
475         config.workspace_urgency_timer = 0.5;
476
477     parse_configuration(override_configpath);
478
479     if (reload) {
480         translate_keysyms();
481         grab_all_keys(conn, false);
482     }
483
484     if (config.font.type == FONT_TYPE_NONE) {
485         ELOG("You did not specify required configuration option \"font\"\n");
486         config.font = load_font("fixed", true);
487         set_font(&config.font);
488     }
489
490     /* Redraw the currently visible decorations on reload, so that
491      * the possibly new drawing parameters changed. */
492     if (reload) {
493         x_deco_recurse(croot);
494         xcb_flush(conn);
495     }
496
497 #if 0
498     /* Set an empty name for every workspace which got no name */
499     Workspace *ws;
500     TAILQ_FOREACH(ws, workspaces, workspaces) {
501             if (ws->name != NULL) {
502                     /* If the font was not specified when the workspace name
503                      * was loaded, we need to predict the text width now */
504                     if (ws->text_width == 0)
505                             ws->text_width = predict_text_width(global_conn,
506                                             config.font, ws->name, ws->name_len);
507                     continue;
508             }
509
510             workspace_set_name(ws, NULL);
511     }
512 #endif
513 }