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