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