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