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