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