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