]> git.sur5r.net Git - i3/i3/blob - src/config.c
Allow to validate the config file without X.
[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 #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  * Get the path of the first configuration file found. If override_configpath
44  * is specified, that path is returned and saved for further calls. Otherwise,
45  * checks the home directory first, then the system directory first, always
46  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
47  * $XDG_CONFIG_DIRS)
48  *
49  */
50 static char *get_config_path(const char *override_configpath) {
51     char *xdg_config_home, *xdg_config_dirs, *config_path;
52
53     static const char *saved_configpath = NULL;
54
55     if (override_configpath != NULL) {
56         saved_configpath = override_configpath;
57         return sstrdup(saved_configpath);
58     }
59
60     if (saved_configpath != NULL)
61         return sstrdup(saved_configpath);
62
63     /* 1: check the traditional path under the home directory */
64     config_path = resolve_tilde("~/.i3/config");
65     if (path_exists(config_path))
66         return config_path;
67     free(config_path);
68
69     /* 2: check for $XDG_CONFIG_HOME/i3/config */
70     if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
71         xdg_config_home = "~/.config";
72
73     xdg_config_home = resolve_tilde(xdg_config_home);
74     sasprintf(&config_path, "%s/i3/config", xdg_config_home);
75     free(xdg_config_home);
76
77     if (path_exists(config_path))
78         return config_path;
79     free(config_path);
80
81     /* 3: check the traditional path under /etc */
82     config_path = SYSCONFDIR "/i3/config";
83     if (path_exists(config_path))
84         return sstrdup(config_path);
85
86     /* 4: check for $XDG_CONFIG_DIRS/i3/config */
87     if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
88         xdg_config_dirs = "/etc/xdg";
89
90     char *buf = sstrdup(xdg_config_dirs);
91     char *tok = strtok(buf, ":");
92     while (tok != NULL) {
93         tok = resolve_tilde(tok);
94         sasprintf(&config_path, "%s/i3/config", tok);
95         free(tok);
96         if (path_exists(config_path)) {
97             free(buf);
98             return config_path;
99         }
100         free(config_path);
101         tok = strtok(NULL, ":");
102     }
103     free(buf);
104
105     die("Unable to find the configuration file (looked at "
106         "~/.i3/config, $XDG_CONFIG_HOME/i3/config, " SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
107 }
108
109 /*
110  * Finds the configuration file to use (either the one specified by
111  * override_configpath), the user’s one or the system default) and calls
112  * parse_file().
113  *
114  */
115 bool parse_configuration(const char *override_configpath, bool use_nagbar) {
116     char *path = get_config_path(override_configpath);
117     LOG("Parsing configfile %s\n", path);
118     FREE(current_configpath);
119     current_configpath = path;
120
121     /* initialize default bindings if we're just validating the config file */
122     if (!use_nagbar && bindings == NULL) {
123         bindings = scalloc(sizeof(struct bindings_head));
124         TAILQ_INIT(bindings);
125     }
126
127     return parse_file(path, use_nagbar);
128 }
129
130 /*
131  * (Re-)loads the configuration file (sets useful defaults before).
132  *
133  */
134 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
135     if (reload) {
136         /* First ungrab the keys */
137         ungrab_all_keys(conn);
138
139         struct Mode *mode;
140         Binding *bind;
141         while (!SLIST_EMPTY(&modes)) {
142             mode = SLIST_FIRST(&modes);
143             FREE(mode->name);
144
145             /* Clear the old binding list */
146             bindings = mode->bindings;
147             while (!TAILQ_EMPTY(bindings)) {
148                 bind = TAILQ_FIRST(bindings);
149                 TAILQ_REMOVE(bindings, bind, bindings);
150                 FREE(bind->translated_to);
151                 FREE(bind->command);
152                 FREE(bind);
153             }
154             FREE(bindings);
155             SLIST_REMOVE(&modes, mode, Mode, modes);
156         }
157
158         struct Assignment *assign;
159         while (!TAILQ_EMPTY(&assignments)) {
160             assign = TAILQ_FIRST(&assignments);
161             if (assign->type == A_TO_WORKSPACE)
162                 FREE(assign->dest.workspace);
163             else if (assign->type == A_TO_OUTPUT)
164                 FREE(assign->dest.output);
165             else if (assign->type == A_COMMAND)
166                 FREE(assign->dest.command);
167             match_free(&(assign->match));
168             TAILQ_REMOVE(&assignments, assign, assignments);
169             FREE(assign);
170         }
171
172         /* Clear bar configs */
173         Barconfig *barconfig;
174         while (!TAILQ_EMPTY(&barconfigs)) {
175             barconfig = TAILQ_FIRST(&barconfigs);
176             FREE(barconfig->id);
177             for (int c = 0; c < barconfig->num_outputs; c++)
178                 free(barconfig->outputs[c]);
179             FREE(barconfig->outputs);
180             FREE(barconfig->tray_output);
181             FREE(barconfig->socket_path);
182             FREE(barconfig->status_command);
183             FREE(barconfig->i3bar_command);
184             FREE(barconfig->font);
185             FREE(barconfig->colors.background);
186             FREE(barconfig->colors.statusline);
187             FREE(barconfig->colors.focused_workspace_border);
188             FREE(barconfig->colors.focused_workspace_bg);
189             FREE(barconfig->colors.focused_workspace_text);
190             FREE(barconfig->colors.active_workspace_border);
191             FREE(barconfig->colors.active_workspace_bg);
192             FREE(barconfig->colors.active_workspace_text);
193             FREE(barconfig->colors.inactive_workspace_border);
194             FREE(barconfig->colors.inactive_workspace_bg);
195             FREE(barconfig->colors.inactive_workspace_text);
196             FREE(barconfig->colors.urgent_workspace_border);
197             FREE(barconfig->colors.urgent_workspace_bg);
198             FREE(barconfig->colors.urgent_workspace_text);
199             TAILQ_REMOVE(&barconfigs, barconfig, configs);
200             FREE(barconfig);
201         }
202
203 /* Clear workspace names */
204 #if 0
205         Workspace *ws;
206         TAILQ_FOREACH(ws, workspaces, workspaces)
207             workspace_set_name(ws, NULL);
208 #endif
209
210         /* Invalidate pixmap caches in case font or colors changed */
211         Con *con;
212         TAILQ_FOREACH(con, &all_cons, all_cons)
213         FREE(con->deco_render_params);
214
215         /* Get rid of the current font */
216         free_font();
217     }
218
219     SLIST_INIT(&modes);
220
221     struct Mode *default_mode = scalloc(sizeof(struct Mode));
222     default_mode->name = sstrdup("default");
223     default_mode->bindings = scalloc(sizeof(struct bindings_head));
224     TAILQ_INIT(default_mode->bindings);
225     SLIST_INSERT_HEAD(&modes, default_mode, modes);
226
227     bindings = default_mode->bindings;
228
229 #define REQUIRED_OPTION(name) \
230     if (config.name == NULL)  \
231         die("You did not specify required configuration option " #name "\n");
232
233     /* Clear the old config or initialize the data structure */
234     memset(&config, 0, sizeof(config));
235
236 /* Initialize default colors */
237 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
238     do {                                                       \
239         x.border = get_colorpixel(cborder);                    \
240         x.background = get_colorpixel(cbackground);            \
241         x.text = get_colorpixel(ctext);                        \
242         x.indicator = get_colorpixel(cindicator);              \
243     } while (0)
244
245     config.client.background = get_colorpixel("#000000");
246     INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
247     INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
248     INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
249     INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
250
251     /* border and indicator color are ignored for placeholder contents */
252     INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
253
254     /* the last argument (indicator color) is ignored for bar colors */
255     INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
256     INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
257     INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
258
259     config.default_border = BS_NORMAL;
260     config.default_floating_border = BS_NORMAL;
261     config.default_border_width = logical_px(2);
262     config.default_floating_border_width = logical_px(2);
263     /* Set default_orientation to NO_ORIENTATION for auto orientation. */
264     config.default_orientation = NO_ORIENTATION;
265
266     /* Set default urgency reset delay to 500ms */
267     if (config.workspace_urgency_timer == 0)
268         config.workspace_urgency_timer = 0.5;
269
270     parse_configuration(override_configpath, true);
271
272     if (reload) {
273         translate_keysyms();
274         grab_all_keys(conn, false);
275     }
276
277     if (config.font.type == FONT_TYPE_NONE) {
278         ELOG("You did not specify required configuration option \"font\"\n");
279         config.font = load_font("fixed", true);
280         set_font(&config.font);
281     }
282
283     /* Redraw the currently visible decorations on reload, so that
284      * the possibly new drawing parameters changed. */
285     if (reload) {
286         x_deco_recurse(croot);
287         xcb_flush(conn);
288     }
289
290 #if 0
291     /* Set an empty name for every workspace which got no name */
292     Workspace *ws;
293     TAILQ_FOREACH(ws, workspaces, workspaces) {
294             if (ws->name != NULL) {
295                     /* If the font was not specified when the workspace name
296                      * was loaded, we need to predict the text width now */
297                     if (ws->text_width == 0)
298                             ws->text_width = predict_text_width(global_conn,
299                                             config.font, ws->name, ws->name_len);
300                     continue;
301             }
302
303             workspace_set_name(ws, NULL);
304     }
305 #endif
306 }