]> git.sur5r.net Git - i3/i3/blob - src/config.c
Merge branch 'next' into master
[i3/i3] / src / config.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * config.c: Configuration file (calling the parser (src/config_parser.c) with
8  *           the correct path, switching key bindings mode).
9  *
10  */
11 #include "all.h"
12
13 #include <xkbcommon/xkbcommon.h>
14
15 char *current_configpath = NULL;
16 Config config;
17 struct modes_head modes;
18 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
19
20 /**
21  * Ungrabs all keys, to be called before re-grabbing the keys because of a
22  * mapping_notify event or a configuration file reload
23  *
24  */
25 void ungrab_all_keys(xcb_connection_t *conn) {
26     DLOG("Ungrabbing all keys\n");
27     xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
28 }
29
30 /*
31  * Sends the current bar configuration as an event to all barconfig_update listeners.
32  *
33  */
34 void update_barconfig() {
35     Barconfig *current;
36     TAILQ_FOREACH(current, &barconfigs, configs) {
37         ipc_send_barconfig_update_event(current);
38     }
39 }
40
41 /*
42  * Finds the configuration file to use (either the one specified by
43  * override_configpath), the user’s one or the system default) and calls
44  * parse_file().
45  *
46  */
47 bool parse_configuration(const char *override_configpath, bool use_nagbar) {
48     char *path = get_config_path(override_configpath, true);
49     if (path == NULL) {
50         die("Unable to find the configuration file (looked at "
51             "~/.i3/config, $XDG_CONFIG_HOME/i3/config, " SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
52     }
53
54     LOG("Parsing configfile %s\n", path);
55     FREE(current_configpath);
56     current_configpath = path;
57
58     /* initialize default bindings if we're just validating the config file */
59     if (!use_nagbar && bindings == NULL) {
60         bindings = scalloc(1, sizeof(struct bindings_head));
61         TAILQ_INIT(bindings);
62     }
63
64     return parse_file(path, use_nagbar);
65 }
66
67 /*
68  * (Re-)loads the configuration file (sets useful defaults before).
69  *
70  */
71 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
72     if (reload) {
73         /* If we are currently in a binding mode, we first revert to the
74          * default since we have no guarantee that the current mode will even
75          * still exist after parsing the config again. See #2228. */
76         switch_mode("default");
77
78         /* First ungrab the keys */
79         ungrab_all_keys(conn);
80
81         struct Mode *mode;
82         while (!SLIST_EMPTY(&modes)) {
83             mode = SLIST_FIRST(&modes);
84             FREE(mode->name);
85
86             /* Clear the old binding list */
87             while (!TAILQ_EMPTY(mode->bindings)) {
88                 Binding *bind = TAILQ_FIRST(mode->bindings);
89                 TAILQ_REMOVE(mode->bindings, bind, bindings);
90                 binding_free(bind);
91             }
92             FREE(mode->bindings);
93
94             SLIST_REMOVE(&modes, mode, Mode, modes);
95             FREE(mode);
96         }
97
98         struct Assignment *assign;
99         while (!TAILQ_EMPTY(&assignments)) {
100             assign = TAILQ_FIRST(&assignments);
101             if (assign->type == A_TO_WORKSPACE)
102                 FREE(assign->dest.workspace);
103             else if (assign->type == A_COMMAND)
104                 FREE(assign->dest.command);
105             match_free(&(assign->match));
106             TAILQ_REMOVE(&assignments, assign, assignments);
107             FREE(assign);
108         }
109
110         /* Clear bar configs */
111         Barconfig *barconfig;
112         while (!TAILQ_EMPTY(&barconfigs)) {
113             barconfig = TAILQ_FIRST(&barconfigs);
114             FREE(barconfig->id);
115             for (int c = 0; c < barconfig->num_outputs; c++)
116                 free(barconfig->outputs[c]);
117
118             while (!TAILQ_EMPTY(&(barconfig->bar_bindings))) {
119                 struct Barbinding *binding = TAILQ_FIRST(&(barconfig->bar_bindings));
120                 FREE(binding->command);
121                 TAILQ_REMOVE(&(barconfig->bar_bindings), binding, bindings);
122                 FREE(binding);
123             }
124
125             while (!TAILQ_EMPTY(&(barconfig->tray_outputs))) {
126                 struct tray_output_t *tray_output = TAILQ_FIRST(&(barconfig->tray_outputs));
127                 FREE(tray_output->output);
128                 TAILQ_REMOVE(&(barconfig->tray_outputs), tray_output, tray_outputs);
129                 FREE(tray_output);
130             }
131
132             FREE(barconfig->outputs);
133             FREE(barconfig->socket_path);
134             FREE(barconfig->status_command);
135             FREE(barconfig->i3bar_command);
136             FREE(barconfig->font);
137             FREE(barconfig->colors.background);
138             FREE(barconfig->colors.statusline);
139             FREE(barconfig->colors.separator);
140             FREE(barconfig->colors.focused_background);
141             FREE(barconfig->colors.focused_statusline);
142             FREE(barconfig->colors.focused_separator);
143             FREE(barconfig->colors.focused_workspace_border);
144             FREE(barconfig->colors.focused_workspace_bg);
145             FREE(barconfig->colors.focused_workspace_text);
146             FREE(barconfig->colors.active_workspace_border);
147             FREE(barconfig->colors.active_workspace_bg);
148             FREE(barconfig->colors.active_workspace_text);
149             FREE(barconfig->colors.inactive_workspace_border);
150             FREE(barconfig->colors.inactive_workspace_bg);
151             FREE(barconfig->colors.inactive_workspace_text);
152             FREE(barconfig->colors.urgent_workspace_border);
153             FREE(barconfig->colors.urgent_workspace_bg);
154             FREE(barconfig->colors.urgent_workspace_text);
155             FREE(barconfig->colors.binding_mode_border);
156             FREE(barconfig->colors.binding_mode_bg);
157             FREE(barconfig->colors.binding_mode_text);
158             TAILQ_REMOVE(&barconfigs, barconfig, configs);
159             FREE(barconfig);
160         }
161
162         /* Invalidate pixmap caches in case font or colors changed */
163         Con *con;
164         TAILQ_FOREACH(con, &all_cons, all_cons)
165         FREE(con->deco_render_params);
166
167         /* Get rid of the current font */
168         free_font();
169
170         free(config.ipc_socket_path);
171         free(config.restart_state_path);
172         free(config.fake_outputs);
173     }
174
175     SLIST_INIT(&modes);
176
177     struct Mode *default_mode = scalloc(1, sizeof(struct Mode));
178     default_mode->name = sstrdup("default");
179     default_mode->bindings = scalloc(1, sizeof(struct bindings_head));
180     TAILQ_INIT(default_mode->bindings);
181     SLIST_INSERT_HEAD(&modes, default_mode, modes);
182
183     bindings = default_mode->bindings;
184
185 #define REQUIRED_OPTION(name) \
186     if (config.name == NULL)  \
187         die("You did not specify required configuration option " #name "\n");
188
189     /* Clear the old config or initialize the data structure */
190     memset(&config, 0, sizeof(config));
191
192 /* Initialize default colors */
193 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
194     do {                                                       \
195         x.border = draw_util_hex_to_color(cborder);            \
196         x.background = draw_util_hex_to_color(cbackground);    \
197         x.text = draw_util_hex_to_color(ctext);                \
198         x.indicator = draw_util_hex_to_color(cindicator);      \
199         x.child_border = draw_util_hex_to_color(cbackground);  \
200     } while (0)
201
202     config.client.background = draw_util_hex_to_color("#000000");
203     INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
204     INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
205     INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
206     INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
207
208     /* border and indicator color are ignored for placeholder contents */
209     INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
210
211     /* the last argument (indicator color) is ignored for bar colors */
212     INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
213     INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
214     INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
215
216     config.show_marks = true;
217
218     config.default_border = BS_NORMAL;
219     config.default_floating_border = BS_NORMAL;
220     config.default_border_width = logical_px(2);
221     config.default_floating_border_width = logical_px(2);
222     /* Set default_orientation to NO_ORIENTATION for auto orientation. */
223     config.default_orientation = NO_ORIENTATION;
224
225     /* Set default urgency reset delay to 500ms */
226     if (config.workspace_urgency_timer == 0)
227         config.workspace_urgency_timer = 0.5;
228
229     parse_configuration(override_configpath, true);
230
231     if (reload) {
232         translate_keysyms();
233         grab_all_keys(conn);
234         regrab_all_buttons(conn);
235     }
236
237     if (config.font.type == FONT_TYPE_NONE) {
238         ELOG("You did not specify required configuration option \"font\"\n");
239         config.font = load_font("fixed", true);
240         set_font(&config.font);
241     }
242
243     /* Redraw the currently visible decorations on reload, so that
244      * the possibly new drawing parameters changed. */
245     if (reload) {
246         x_deco_recurse(croot);
247         xcb_flush(conn);
248     }
249 }