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