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