]> git.sur5r.net Git - i3/i3/blob - src/config.c
naive implementation of 'bindsym --release' (and bindcode)
[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/cfgparse.y) with the
10  *           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\n", keycode);
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_numlock_mask | XCB_MOD_MASK_LOCK);
50 }
51
52 /*
53  * Returns a pointer to the Binding with the specified modifiers and keycode
54  * or NULL if no such binding exists.
55  *
56  */
57 Binding *get_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode) {
58     Binding *bind;
59
60     TAILQ_FOREACH(bind, bindings, bindings) {
61         /* First compare the modifiers */
62         if (bind->mods != modifiers)
63             continue;
64
65         /* Check if the binding is for a KeyPress or a KeyRelease event */
66         if (bind->release != key_release)
67             continue;
68
69         /* If a symbol was specified by the user, we need to look in
70          * the array of translated keycodes for the event’s keycode */
71         if (bind->symbol != NULL) {
72             if (memmem(bind->translated_to,
73                        bind->number_keycodes * sizeof(xcb_keycode_t),
74                        &keycode, sizeof(xcb_keycode_t)) != NULL)
75                 break;
76         } else {
77             /* This case is easier: The user specified a keycode */
78             if (bind->keycode == keycode)
79                 break;
80         }
81     }
82
83     return (bind == TAILQ_END(bindings) ? NULL : bind);
84 }
85
86 /*
87  * Translates keysymbols to keycodes for all bindings which use keysyms.
88  *
89  */
90 void translate_keysyms(void) {
91     Binding *bind;
92     xcb_keysym_t keysym;
93     int col;
94     xcb_keycode_t i,
95                   min_keycode = xcb_get_setup(conn)->min_keycode,
96                   max_keycode = xcb_get_setup(conn)->max_keycode;
97
98     TAILQ_FOREACH(bind, bindings, bindings) {
99         if (bind->keycode > 0)
100             continue;
101
102         /* We need to translate the symbol to a keycode */
103         keysym = XStringToKeysym(bind->symbol);
104         if (keysym == NoSymbol) {
105             ELOG("Could not translate string to key symbol: \"%s\"\n",
106                  bind->symbol);
107             continue;
108         }
109
110         /* Base column we use for looking up key symbols. We always consider
111          * the base column and the corresponding shift column, so without
112          * mode_switch, we look in 0 and 1, with mode_switch we look in 2 and
113          * 3. */
114         col = (bind->mods & BIND_MODE_SWITCH ? 2 : 0);
115
116         FREE(bind->translated_to);
117         bind->number_keycodes = 0;
118
119         for (i = min_keycode; i && i <= max_keycode; i++) {
120             if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
121                 (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
122                 continue;
123             bind->number_keycodes++;
124             bind->translated_to = srealloc(bind->translated_to,
125                                            (sizeof(xcb_keycode_t) *
126                                             bind->number_keycodes));
127             bind->translated_to[bind->number_keycodes-1] = i;
128         }
129
130         DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
131              bind->number_keycodes);
132     }
133 }
134
135 /*
136  * Grab the bound keys (tell X to send us keypress events for those keycodes)
137  *
138  */
139 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
140     Binding *bind;
141     TAILQ_FOREACH(bind, bindings, bindings) {
142         if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
143             (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
144             continue;
145
146         /* The easy case: the user specified a keycode directly. */
147         if (bind->keycode > 0) {
148             grab_keycode_for_binding(conn, bind, bind->keycode);
149             continue;
150         }
151
152         xcb_keycode_t *walk = bind->translated_to;
153         for (int i = 0; i < bind->number_keycodes; i++)
154             grab_keycode_for_binding(conn, bind, *walk++);
155     }
156 }
157
158 /*
159  * Switches the key bindings to the given mode, if the mode exists
160  *
161  */
162 void switch_mode(const char *new_mode) {
163     struct Mode *mode;
164
165     LOG("Switching to mode %s\n", new_mode);
166
167     SLIST_FOREACH(mode, &modes, modes) {
168         if (strcasecmp(mode->name, new_mode) != 0)
169             continue;
170
171         ungrab_all_keys(conn);
172         bindings = mode->bindings;
173         translate_keysyms();
174         grab_all_keys(conn, false);
175         return;
176     }
177
178     ELOG("ERROR: Mode not found\n");
179 }
180
181 /*
182  * Get the path of the first configuration file found. If override_configpath
183  * is specified, that path is returned and saved for further calls. Otherwise,
184  * checks the home directory first, then the system directory first, always
185  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
186  * $XDG_CONFIG_DIRS)
187  *
188  */
189 static char *get_config_path(const char *override_configpath) {
190     char *xdg_config_home, *xdg_config_dirs, *config_path;
191
192     static const char *saved_configpath = NULL;
193
194     if (override_configpath != NULL) {
195         saved_configpath = override_configpath;
196         return sstrdup(saved_configpath);
197     }
198
199     if (saved_configpath != NULL)
200         return sstrdup(saved_configpath);
201
202     /* 1: check the traditional path under the home directory */
203     config_path = resolve_tilde("~/.i3/config");
204     if (path_exists(config_path))
205         return config_path;
206     free(config_path);
207
208     /* 2: check for $XDG_CONFIG_HOME/i3/config */
209     if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
210         xdg_config_home = "~/.config";
211
212     xdg_config_home = resolve_tilde(xdg_config_home);
213     sasprintf(&config_path, "%s/i3/config", xdg_config_home);
214     free(xdg_config_home);
215
216     if (path_exists(config_path))
217         return config_path;
218     free(config_path);
219
220     /* 3: check the traditional path under /etc */
221     config_path = SYSCONFDIR "/i3/config";
222     if (path_exists(config_path))
223         return sstrdup(config_path);
224
225     /* 4: check for $XDG_CONFIG_DIRS/i3/config */
226     if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
227         xdg_config_dirs = "/etc/xdg";
228
229     char *buf = sstrdup(xdg_config_dirs);
230     char *tok = strtok(buf, ":");
231     while (tok != NULL) {
232         tok = resolve_tilde(tok);
233         sasprintf(&config_path, "%s/i3/config", tok);
234         free(tok);
235         if (path_exists(config_path)) {
236             free(buf);
237             return config_path;
238         }
239         free(config_path);
240         tok = strtok(NULL, ":");
241     }
242     free(buf);
243
244     die("Unable to find the configuration file (looked at "
245             "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
246             SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
247 }
248
249 /*
250  * Finds the configuration file to use (either the one specified by
251  * override_configpath), the user’s one or the system default) and calls
252  * parse_file().
253  *
254  */
255 static void parse_configuration(const char *override_configpath) {
256     char *path = get_config_path(override_configpath);
257     LOG("Parsing configfile %s\n", path);
258     FREE(current_configpath);
259     current_configpath = path;
260     parse_file(path);
261 }
262
263 /*
264  * (Re-)loads the configuration file (sets useful defaults before).
265  *
266  */
267 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
268     if (reload) {
269         /* First ungrab the keys */
270         ungrab_all_keys(conn);
271
272         struct Mode *mode;
273         Binding *bind;
274         while (!SLIST_EMPTY(&modes)) {
275             mode = SLIST_FIRST(&modes);
276             FREE(mode->name);
277
278             /* Clear the old binding list */
279             bindings = mode->bindings;
280             while (!TAILQ_EMPTY(bindings)) {
281                 bind = TAILQ_FIRST(bindings);
282                 TAILQ_REMOVE(bindings, bind, bindings);
283                 FREE(bind->translated_to);
284                 FREE(bind->command);
285                 FREE(bind);
286             }
287             FREE(bindings);
288             SLIST_REMOVE(&modes, mode, Mode, modes);
289         }
290
291         struct Assignment *assign;
292         while (!TAILQ_EMPTY(&assignments)) {
293             assign = TAILQ_FIRST(&assignments);
294             if (assign->type == A_TO_WORKSPACE)
295                 FREE(assign->dest.workspace);
296             else if (assign->type == A_TO_OUTPUT)
297                 FREE(assign->dest.output);
298             else if (assign->type == A_COMMAND)
299                 FREE(assign->dest.command);
300             match_free(&(assign->match));
301             TAILQ_REMOVE(&assignments, assign, assignments);
302             FREE(assign);
303         }
304
305         /* Clear bar configs */
306         Barconfig *barconfig;
307         while (!TAILQ_EMPTY(&barconfigs)) {
308             barconfig = TAILQ_FIRST(&barconfigs);
309             FREE(barconfig->id);
310             for (int c = 0; c < barconfig->num_outputs; c++)
311                 free(barconfig->outputs[c]);
312             FREE(barconfig->outputs);
313             FREE(barconfig->tray_output);
314             FREE(barconfig->socket_path);
315             FREE(barconfig->status_command);
316             FREE(barconfig->i3bar_command);
317             FREE(barconfig->font);
318             FREE(barconfig->colors.background);
319             FREE(barconfig->colors.statusline);
320             FREE(barconfig->colors.focused_workspace_border);
321             FREE(barconfig->colors.focused_workspace_bg);
322             FREE(barconfig->colors.focused_workspace_text);
323             FREE(barconfig->colors.active_workspace_border);
324             FREE(barconfig->colors.active_workspace_bg);
325             FREE(barconfig->colors.active_workspace_text);
326             FREE(barconfig->colors.inactive_workspace_border);
327             FREE(barconfig->colors.inactive_workspace_bg);
328             FREE(barconfig->colors.inactive_workspace_text);
329             FREE(barconfig->colors.urgent_workspace_border);
330             FREE(barconfig->colors.urgent_workspace_bg);
331             FREE(barconfig->colors.urgent_workspace_text);
332             TAILQ_REMOVE(&barconfigs, barconfig, configs);
333             FREE(barconfig);
334         }
335
336         /* Clear workspace names */
337 #if 0
338         Workspace *ws;
339         TAILQ_FOREACH(ws, workspaces, workspaces)
340             workspace_set_name(ws, NULL);
341 #endif
342
343         /* Invalidate pixmap caches in case font or colors changed */
344         Con *con;
345         TAILQ_FOREACH(con, &all_cons, all_cons)
346             FREE(con->deco_render_params);
347
348         /* Get rid of the current font */
349         free_font();
350     }
351
352     SLIST_INIT(&modes);
353
354     struct Mode *default_mode = scalloc(sizeof(struct Mode));
355     default_mode->name = sstrdup("default");
356     default_mode->bindings = scalloc(sizeof(struct bindings_head));
357     TAILQ_INIT(default_mode->bindings);
358     SLIST_INSERT_HEAD(&modes, default_mode, modes);
359
360     bindings = default_mode->bindings;
361
362 #define REQUIRED_OPTION(name) \
363     if (config.name == NULL) \
364         die("You did not specify required configuration option " #name "\n");
365
366     /* Clear the old config or initialize the data structure */
367     memset(&config, 0, sizeof(config));
368
369     /* Initialize default colors */
370 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
371     do { \
372         x.border = get_colorpixel(cborder); \
373         x.background = get_colorpixel(cbackground); \
374         x.text = get_colorpixel(ctext); \
375         x.indicator = get_colorpixel(cindicator); \
376     } while (0)
377
378     config.client.background = get_colorpixel("#000000");
379     INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
380     INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
381     INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
382     INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
383
384     /* the last argument (indicator color) is ignored for bar colors */
385     INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
386     INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
387     INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
388
389     config.default_border = BS_NORMAL;
390     config.default_floating_border = BS_NORMAL;
391     /* Set default_orientation to NO_ORIENTATION for auto orientation. */
392     config.default_orientation = NO_ORIENTATION;
393
394     parse_configuration(override_configpath);
395
396     if (reload) {
397         translate_keysyms();
398         grab_all_keys(conn, false);
399     }
400
401     if (config.font.type == FONT_TYPE_NONE) {
402         ELOG("You did not specify required configuration option \"font\"\n");
403         config.font = load_font("fixed", true);
404         set_font(&config.font);
405     }
406
407     /* Redraw the currently visible decorations on reload, so that
408      * the possibly new drawing parameters changed. */
409     if (reload) {
410         x_deco_recurse(croot);
411         xcb_flush(conn);
412     }
413
414 #if 0
415     /* Set an empty name for every workspace which got no name */
416     Workspace *ws;
417     TAILQ_FOREACH(ws, workspaces, workspaces) {
418             if (ws->name != NULL) {
419                     /* If the font was not specified when the workspace name
420                      * was loaded, we need to predict the text width now */
421                     if (ws->text_width == 0)
422                             ws->text_width = predict_text_width(global_conn,
423                                             config.font, ws->name, ws->name_len);
424                     continue;
425             }
426
427             workspace_set_name(ws, NULL);
428     }
429 #endif
430 }