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