]> git.sur5r.net Git - i3/i3/blob - src/config.c
Merge branch 'fix-var-tabs'
[i3/i3] / src / config.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * config.c: Configuration file (calling the parser (src/cfgparse.y) with the
8  *           correct path, switching key bindings mode).
9  *
10  */
11 #include "all.h"
12
13 /* We need Xlib for XStringToKeysym */
14 #include <X11/Xlib.h>
15
16 char *current_configpath = NULL;
17 Config config;
18 struct modes_head modes;
19 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
20
21 /**
22  * Ungrabs all keys, to be called before re-grabbing the keys because of a
23  * mapping_notify event or a configuration file reload
24  *
25  */
26 void ungrab_all_keys(xcb_connection_t *conn) {
27         DLOG("Ungrabbing all keys\n");
28         xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
29 }
30
31 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
32         DLOG("Grabbing %d\n", keycode);
33         /* Grab the key in all combinations */
34         #define GRAB_KEY(modifier) \
35                 do { \
36                         xcb_grab_key(conn, 0, root, modifier, keycode, \
37                                      XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
38                 } while (0)
39         int mods = bind->mods;
40         if ((bind->mods & BIND_MODE_SWITCH) != 0) {
41                 mods &= ~BIND_MODE_SWITCH;
42                 if (mods == 0)
43                         mods = XCB_MOD_MASK_ANY;
44         }
45         GRAB_KEY(mods);
46         GRAB_KEY(mods | xcb_numlock_mask);
47         GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
48 }
49
50 /*
51  * Returns a pointer to the Binding with the specified modifiers and keycode
52  * or NULL if no such binding exists.
53  *
54  */
55 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
56         Binding *bind;
57
58         TAILQ_FOREACH(bind, bindings, bindings) {
59                 /* First compare the modifiers */
60                 if (bind->mods != modifiers)
61                         continue;
62
63                 /* If a symbol was specified by the user, we need to look in
64                  * the array of translated keycodes for the event’s keycode */
65                 if (bind->symbol != NULL) {
66                         if (memmem(bind->translated_to,
67                                    bind->number_keycodes * sizeof(xcb_keycode_t),
68                                    &keycode, sizeof(xcb_keycode_t)) != NULL)
69                                 break;
70                 } else {
71                         /* This case is easier: The user specified a keycode */
72                         if (bind->keycode == keycode)
73                                 break;
74                 }
75         }
76
77         return (bind == TAILQ_END(bindings) ? NULL : bind);
78 }
79
80 /*
81  * Translates keysymbols to keycodes for all bindings which use keysyms.
82  *
83  */
84 void translate_keysyms() {
85         Binding *bind;
86         TAILQ_FOREACH(bind, bindings, bindings) {
87                 if (bind->keycode > 0)
88                         continue;
89
90                 /* We need to translate the symbol to a keycode */
91                 xcb_keysym_t keysym = XStringToKeysym(bind->symbol);
92                 if (keysym == NoSymbol) {
93                         ELOG("Could not translate string to key symbol: \"%s\"\n", bind->symbol);
94                         continue;
95                 }
96
97                 uint32_t last_keycode = 0;
98                 xcb_keycode_t *keycodes = xcb_key_symbols_get_keycode(keysyms, keysym);
99                 if (keycodes == NULL) {
100                         DLOG("Could not translate symbol \"%s\"\n", bind->symbol);
101                         continue;
102                 }
103
104                 bind->number_keycodes = 0;
105
106                 for (xcb_keycode_t *walk = keycodes; *walk != 0; walk++) {
107                         /* We hope duplicate keycodes will be returned in order
108                          * and skip them */
109                         if (last_keycode == *walk)
110                                 continue;
111                         last_keycode = *walk;
112                         bind->number_keycodes++;
113                 }
114                 DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol, bind->number_keycodes);
115                 bind->translated_to = smalloc(bind->number_keycodes * sizeof(xcb_keycode_t));
116                 memcpy(bind->translated_to, keycodes, bind->number_keycodes * sizeof(xcb_keycode_t));
117                 free(keycodes);
118         }
119 }
120
121 /*
122  * Grab the bound keys (tell X to send us keypress events for those keycodes)
123  *
124  */
125 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
126         Binding *bind;
127         TAILQ_FOREACH(bind, bindings, bindings) {
128                 if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
129                     (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
130                         continue;
131
132                 /* The easy case: the user specified a keycode directly. */
133                 if (bind->keycode > 0) {
134                         grab_keycode_for_binding(conn, bind, bind->keycode);
135                         continue;
136                 }
137
138                 xcb_keycode_t *walk = bind->translated_to;
139                 for (int i = 0; i < bind->number_keycodes; i++)
140                         grab_keycode_for_binding(conn, bind, *walk++);
141         }
142 }
143
144 /*
145  * Switches the key bindings to the given mode, if the mode exists
146  *
147  */
148 void switch_mode(const char *new_mode) {
149         struct Mode *mode;
150
151         LOG("Switching to mode %s\n", new_mode);
152
153         SLIST_FOREACH(mode, &modes, modes) {
154                 if (strcasecmp(mode->name, new_mode) != 0)
155                         continue;
156
157                 ungrab_all_keys(conn);
158                 bindings = mode->bindings;
159                 translate_keysyms();
160                 grab_all_keys(conn, false);
161                 return;
162         }
163
164         ELOG("ERROR: Mode not found\n");
165 }
166
167 /*
168  * Get the path of the first configuration file found. If override_configpath
169  * is specified, that path is returned and saved for further calls. Otherwise,
170  * checks the home directory first, then the system directory first, always
171  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
172  * $XDG_CONFIG_DIRS)
173  *
174  */
175 static char *get_config_path(const char *override_configpath) {
176     char *xdg_config_home, *xdg_config_dirs, *config_path;
177
178     static const char *saved_configpath = NULL;
179
180     if (override_configpath != NULL) {
181         saved_configpath = override_configpath;
182         return sstrdup(saved_configpath);
183     }
184
185     if (saved_configpath != NULL)
186         return sstrdup(saved_configpath);
187
188     /* 1: check the traditional path under the home directory */
189     config_path = resolve_tilde("~/.i3/config");
190     if (path_exists(config_path))
191         return config_path;
192     free(config_path);
193
194     /* 2: check for $XDG_CONFIG_HOME/i3/config */
195     if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
196         xdg_config_home = "~/.config";
197
198     xdg_config_home = resolve_tilde(xdg_config_home);
199     sasprintf(&config_path, "%s/i3/config", xdg_config_home);
200     free(xdg_config_home);
201
202     if (path_exists(config_path))
203         return config_path;
204     free(config_path);
205
206     /* 3: check the traditional path under /etc */
207     config_path = SYSCONFDIR "/i3/config";
208     if (path_exists(config_path))
209         return sstrdup(config_path);
210
211     /* 4: check for $XDG_CONFIG_DIRS/i3/config */
212     if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
213         xdg_config_dirs = "/etc/xdg";
214
215     char *buf = sstrdup(xdg_config_dirs);
216     char *tok = strtok(buf, ":");
217     while (tok != NULL) {
218         tok = resolve_tilde(tok);
219         sasprintf(&config_path, "%s/i3/config", tok);
220         free(tok);
221         if (path_exists(config_path)) {
222             free(buf);
223             return config_path;
224         }
225         free(config_path);
226         tok = strtok(NULL, ":");
227     }
228     free(buf);
229
230     die("Unable to find the configuration file (looked at "
231             "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
232             SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
233 }
234
235 /*
236  * Finds the configuration file to use (either the one specified by
237  * override_configpath), the user’s one or the system default) and calls
238  * parse_file().
239  *
240  */
241 static void parse_configuration(const char *override_configpath) {
242     char *path = get_config_path(override_configpath);
243     DLOG("Parsing configfile %s\n", path);
244     FREE(current_configpath);
245     current_configpath = path;
246     parse_file(path);
247 }
248
249 /*
250  * (Re-)loads the configuration file (sets useful defaults before).
251  *
252  */
253 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
254     if (reload) {
255         /* First ungrab the keys */
256         ungrab_all_keys(conn);
257
258         struct Mode *mode;
259         Binding *bind;
260         while (!SLIST_EMPTY(&modes)) {
261             mode = SLIST_FIRST(&modes);
262             FREE(mode->name);
263
264             /* Clear the old binding list */
265             bindings = mode->bindings;
266             while (!TAILQ_EMPTY(bindings)) {
267                 bind = TAILQ_FIRST(bindings);
268                 TAILQ_REMOVE(bindings, bind, bindings);
269                 FREE(bind->translated_to);
270                 FREE(bind->command);
271                 FREE(bind);
272             }
273             FREE(bindings);
274             SLIST_REMOVE(&modes, mode, Mode, modes);
275         }
276
277         struct Assignment *assign;
278         while (!TAILQ_EMPTY(&assignments)) {
279             assign = TAILQ_FIRST(&assignments);
280             if (assign->type == A_TO_WORKSPACE)
281                 FREE(assign->dest.workspace);
282             else if (assign->type == A_TO_OUTPUT)
283                 FREE(assign->dest.output);
284             else if (assign->type == A_COMMAND)
285                 FREE(assign->dest.command);
286             match_free(&(assign->match));
287             TAILQ_REMOVE(&assignments, assign, assignments);
288             FREE(assign);
289         }
290
291         /* Clear bar configs */
292         Barconfig *barconfig;
293         while (!TAILQ_EMPTY(&barconfigs)) {
294             barconfig = TAILQ_FIRST(&barconfigs);
295             FREE(barconfig->id);
296             for (int c = 0; c < barconfig->num_outputs; c++)
297                 free(barconfig->outputs[c]);
298             FREE(barconfig->outputs);
299             FREE(barconfig->tray_output);
300             FREE(barconfig->socket_path);
301             FREE(barconfig->status_command);
302             FREE(barconfig->font);
303             FREE(barconfig->colors.background);
304             FREE(barconfig->colors.statusline);
305             FREE(barconfig->colors.focused_workspace_text);
306             FREE(barconfig->colors.focused_workspace_bg);
307             FREE(barconfig->colors.active_workspace_text);
308             FREE(barconfig->colors.active_workspace_bg);
309             FREE(barconfig->colors.inactive_workspace_text);
310             FREE(barconfig->colors.inactive_workspace_bg);
311             FREE(barconfig->colors.urgent_workspace_text);
312             FREE(barconfig->colors.urgent_workspace_bg);
313             TAILQ_REMOVE(&barconfigs, barconfig, configs);
314             FREE(barconfig);
315         }
316
317         /* Clear workspace names */
318 #if 0
319         Workspace *ws;
320         TAILQ_FOREACH(ws, workspaces, workspaces)
321             workspace_set_name(ws, NULL);
322 #endif
323     }
324
325     SLIST_INIT(&modes);
326
327     struct Mode *default_mode = scalloc(sizeof(struct Mode));
328     default_mode->name = sstrdup("default");
329     default_mode->bindings = scalloc(sizeof(struct bindings_head));
330     TAILQ_INIT(default_mode->bindings);
331     SLIST_INSERT_HEAD(&modes, default_mode, modes);
332
333     bindings = default_mode->bindings;
334
335 #define REQUIRED_OPTION(name) \
336     if (config.name == NULL) \
337         die("You did not specify required configuration option " #name "\n");
338
339     /* Clear the old config or initialize the data structure */
340     memset(&config, 0, sizeof(config));
341
342     /* Initialize default colors */
343 #define INIT_COLOR(x, cborder, cbackground, ctext) \
344     do { \
345         x.border = get_colorpixel(cborder); \
346         x.background = get_colorpixel(cbackground); \
347         x.text = get_colorpixel(ctext); \
348     } while (0)
349
350     config.client.background = get_colorpixel("#000000");
351     INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff");
352     INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff");
353     INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888");
354     INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff");
355     INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff");
356     INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
357     INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
358
359     config.default_border = BS_NORMAL;
360     config.default_floating_border = BS_NORMAL;
361     /* Set default_orientation to NO_ORIENTATION for auto orientation. */
362     config.default_orientation = NO_ORIENTATION;
363
364     parse_configuration(override_configpath);
365
366     if (reload) {
367         translate_keysyms();
368         grab_all_keys(conn, false);
369     }
370
371     if (config.font.id == 0) {
372         ELOG("You did not specify required configuration option \"font\"\n");
373         config.font = load_font("fixed", true);
374     }
375
376 #if 0
377     /* Set an empty name for every workspace which got no name */
378     Workspace *ws;
379     TAILQ_FOREACH(ws, workspaces, workspaces) {
380             if (ws->name != NULL) {
381                     /* If the font was not specified when the workspace name
382                      * was loaded, we need to predict the text width now */
383                     if (ws->text_width == 0)
384                             ws->text_width = predict_text_width(global_conn,
385                                             config.font, ws->name, ws->name_len);
386                     continue;
387             }
388
389             workspace_set_name(ws, NULL);
390     }
391 #endif
392 }