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