i3
config.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * config.c: Configuration file (calling the parser (src/cfgparse.y) with the
8  * correct path, switching key bindings mode).
9  *
10  */
11 #include "all.h"
12 
13 /* We need Xlib for XStringToKeysym */
14 #include <X11/Xlib.h>
15 
16 char *current_configpath = NULL;
18 struct modes_head modes;
19 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
20 
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 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
32  DLOG("Grabbing %d\n", keycode);
33  /* Grab the key in all combinations */
34  #define GRAB_KEY(modifier) \
35  do { \
36  xcb_grab_key(conn, 0, root, modifier, keycode, \
37  XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
38  } while (0)
39  int mods = bind->mods;
40  if ((bind->mods & BIND_MODE_SWITCH) != 0) {
41  mods &= ~BIND_MODE_SWITCH;
42  if (mods == 0)
43  mods = XCB_MOD_MASK_ANY;
44  }
45  GRAB_KEY(mods);
46  GRAB_KEY(mods | xcb_numlock_mask);
47  GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
48 }
49 
50 /*
51  * Returns a pointer to the Binding with the specified modifiers and keycode
52  * or NULL if no such binding exists.
53  *
54  */
55 Binding *get_binding(uint16_t modifiers, xcb_keycode_t keycode) {
56  Binding *bind;
57 
59  /* First compare the modifiers */
60  if (bind->mods != modifiers)
61  continue;
62 
63  /* If a symbol was specified by the user, we need to look in
64  * the array of translated keycodes for the event’s keycode */
65  if (bind->symbol != NULL) {
66  if (memmem(bind->translated_to,
67  bind->number_keycodes * sizeof(xcb_keycode_t),
68  &keycode, sizeof(xcb_keycode_t)) != NULL)
69  break;
70  } else {
71  /* This case is easier: The user specified a keycode */
72  if (bind->keycode == keycode)
73  break;
74  }
75  }
76 
77  return (bind == TAILQ_END(bindings) ? NULL : bind);
78 }
79 
80 /*
81  * Translates keysymbols to keycodes for all bindings which use keysyms.
82  *
83  */
84 void translate_keysyms(void) {
85  Binding *bind;
86  xcb_keysym_t keysym;
87  int col;
88  xcb_keycode_t i,
89  min_keycode = xcb_get_setup(conn)->min_keycode,
90  max_keycode = xcb_get_setup(conn)->max_keycode;
91 
93  if (bind->keycode > 0)
94  continue;
95 
96  /* We need to translate the symbol to a keycode */
97  keysym = XStringToKeysym(bind->symbol);
98  if (keysym == NoSymbol) {
99  ELOG("Could not translate string to key symbol: \"%s\"\n",
100  bind->symbol);
101  continue;
102  }
103 
104  /* Base column we use for looking up key symbols. We always consider
105  * the base column and the corresponding shift column, so without
106  * mode_switch, we look in 0 and 1, with mode_switch we look in 2 and
107  * 3. */
108  col = (bind->mods & BIND_MODE_SWITCH ? 2 : 0);
109 
110  FREE(bind->translated_to);
111  bind->number_keycodes = 0;
112 
113  for (i = min_keycode; i && i <= max_keycode; i++) {
114  if ((xcb_key_symbols_get_keysym(keysyms, i, col) != keysym) &&
115  (xcb_key_symbols_get_keysym(keysyms, i, col+1) != keysym))
116  continue;
117  bind->number_keycodes++;
118  bind->translated_to = srealloc(bind->translated_to,
119  (sizeof(xcb_keycode_t) *
120  bind->number_keycodes));
121  bind->translated_to[bind->number_keycodes-1] = i;
122  }
123 
124  DLOG("Translated symbol \"%s\" to %d keycode\n", bind->symbol,
125  bind->number_keycodes);
126  }
127 }
128 
129 /*
130  * Grab the bound keys (tell X to send us keypress events for those keycodes)
131  *
132  */
133 void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
134  Binding *bind;
136  if ((bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) == 0) ||
137  (!bind_mode_switch && (bind->mods & BIND_MODE_SWITCH) != 0))
138  continue;
139 
140  /* The easy case: the user specified a keycode directly. */
141  if (bind->keycode > 0) {
142  grab_keycode_for_binding(conn, bind, bind->keycode);
143  continue;
144  }
145 
146  xcb_keycode_t *walk = bind->translated_to;
147  for (int i = 0; i < bind->number_keycodes; i++)
148  grab_keycode_for_binding(conn, bind, *walk++);
149  }
150 }
151 
152 /*
153  * Switches the key bindings to the given mode, if the mode exists
154  *
155  */
156 void switch_mode(const char *new_mode) {
157  struct Mode *mode;
158 
159  LOG("Switching to mode %s\n", new_mode);
160 
161  SLIST_FOREACH(mode, &modes, modes) {
162  if (strcasecmp(mode->name, new_mode) != 0)
163  continue;
164 
166  bindings = mode->bindings;
168  grab_all_keys(conn, false);
169  return;
170  }
171 
172  ELOG("ERROR: Mode not found\n");
173 }
174 
175 /*
176  * Get the path of the first configuration file found. If override_configpath
177  * is specified, that path is returned and saved for further calls. Otherwise,
178  * checks the home directory first, then the system directory first, always
179  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
180  * $XDG_CONFIG_DIRS)
181  *
182  */
183 static char *get_config_path(const char *override_configpath) {
184  char *xdg_config_home, *xdg_config_dirs, *config_path;
185 
186  static const char *saved_configpath = NULL;
187 
188  if (override_configpath != NULL) {
189  saved_configpath = override_configpath;
190  return sstrdup(saved_configpath);
191  }
192 
193  if (saved_configpath != NULL)
194  return sstrdup(saved_configpath);
195 
196  /* 1: check the traditional path under the home directory */
197  config_path = resolve_tilde("~/.i3/config");
198  if (path_exists(config_path))
199  return config_path;
200  free(config_path);
201 
202  /* 2: check for $XDG_CONFIG_HOME/i3/config */
203  if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
204  xdg_config_home = "~/.config";
205 
206  xdg_config_home = resolve_tilde(xdg_config_home);
207  sasprintf(&config_path, "%s/i3/config", xdg_config_home);
208  free(xdg_config_home);
209 
210  if (path_exists(config_path))
211  return config_path;
212  free(config_path);
213 
214  /* 3: check the traditional path under /etc */
215  config_path = SYSCONFDIR "/i3/config";
216  if (path_exists(config_path))
217  return sstrdup(config_path);
218 
219  /* 4: check for $XDG_CONFIG_DIRS/i3/config */
220  if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
221  xdg_config_dirs = "/etc/xdg";
222 
223  char *buf = sstrdup(xdg_config_dirs);
224  char *tok = strtok(buf, ":");
225  while (tok != NULL) {
226  tok = resolve_tilde(tok);
227  sasprintf(&config_path, "%s/i3/config", tok);
228  free(tok);
229  if (path_exists(config_path)) {
230  free(buf);
231  return config_path;
232  }
233  free(config_path);
234  tok = strtok(NULL, ":");
235  }
236  free(buf);
237 
238  die("Unable to find the configuration file (looked at "
239  "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
240  SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
241 }
242 
243 /*
244  * Finds the configuration file to use (either the one specified by
245  * override_configpath), the user’s one or the system default) and calls
246  * parse_file().
247  *
248  */
249 static void parse_configuration(const char *override_configpath) {
250  char *path = get_config_path(override_configpath);
251  LOG("Parsing configfile %s\n", path);
253  current_configpath = path;
254  parse_file(path);
255 }
256 
257 /*
258  * (Re-)loads the configuration file (sets useful defaults before).
259  *
260  */
261 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
262  if (reload) {
263  /* First ungrab the keys */
264  ungrab_all_keys(conn);
265 
266  struct Mode *mode;
267  Binding *bind;
268  while (!SLIST_EMPTY(&modes)) {
269  mode = SLIST_FIRST(&modes);
270  FREE(mode->name);
271 
272  /* Clear the old binding list */
273  bindings = mode->bindings;
274  while (!TAILQ_EMPTY(bindings)) {
275  bind = TAILQ_FIRST(bindings);
277  FREE(bind->translated_to);
278  FREE(bind->command);
279  FREE(bind);
280  }
281  FREE(bindings);
282  SLIST_REMOVE(&modes, mode, Mode, modes);
283  }
284 
285  struct Assignment *assign;
286  while (!TAILQ_EMPTY(&assignments)) {
287  assign = TAILQ_FIRST(&assignments);
288  if (assign->type == A_TO_WORKSPACE)
289  FREE(assign->dest.workspace);
290  else if (assign->type == A_TO_OUTPUT)
291  FREE(assign->dest.output);
292  else if (assign->type == A_COMMAND)
293  FREE(assign->dest.command);
294  match_free(&(assign->match));
296  FREE(assign);
297  }
298 
299  /* Clear bar configs */
300  Barconfig *barconfig;
301  while (!TAILQ_EMPTY(&barconfigs)) {
302  barconfig = TAILQ_FIRST(&barconfigs);
303  FREE(barconfig->id);
304  for (int c = 0; c < barconfig->num_outputs; c++)
305  free(barconfig->outputs[c]);
306  FREE(barconfig->outputs);
307  FREE(barconfig->tray_output);
308  FREE(barconfig->socket_path);
309  FREE(barconfig->status_command);
310  FREE(barconfig->i3bar_command);
311  FREE(barconfig->font);
312  FREE(barconfig->colors.background);
313  FREE(barconfig->colors.statusline);
315  FREE(barconfig->colors.focused_workspace_bg);
316  FREE(barconfig->colors.focused_workspace_text);
317  FREE(barconfig->colors.active_workspace_border);
318  FREE(barconfig->colors.active_workspace_bg);
319  FREE(barconfig->colors.active_workspace_text);
321  FREE(barconfig->colors.inactive_workspace_bg);
322  FREE(barconfig->colors.inactive_workspace_text);
323  FREE(barconfig->colors.urgent_workspace_border);
324  FREE(barconfig->colors.urgent_workspace_bg);
325  FREE(barconfig->colors.urgent_workspace_text);
326  TAILQ_REMOVE(&barconfigs, barconfig, configs);
327  FREE(barconfig);
328  }
329 
330  /* Clear workspace names */
331 #if 0
332  Workspace *ws;
333  TAILQ_FOREACH(ws, workspaces, workspaces)
334  workspace_set_name(ws, NULL);
335 #endif
336 
337  /* Invalidate pixmap caches in case font or colors changed */
338  Con *con;
340  FREE(con->deco_render_params);
341 
342  /* Get rid of the current font */
343  free_font();
344  }
345 
346  SLIST_INIT(&modes);
347 
348  struct Mode *default_mode = scalloc(sizeof(struct Mode));
349  default_mode->name = sstrdup("default");
350  default_mode->bindings = scalloc(sizeof(struct bindings_head));
351  TAILQ_INIT(default_mode->bindings);
352  SLIST_INSERT_HEAD(&modes, default_mode, modes);
353 
354  bindings = default_mode->bindings;
355 
356 #define REQUIRED_OPTION(name) \
357  if (config.name == NULL) \
358  die("You did not specify required configuration option " #name "\n");
359 
360  /* Clear the old config or initialize the data structure */
361  memset(&config, 0, sizeof(config));
362 
363  /* Initialize default colors */
364 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
365  do { \
366  x.border = get_colorpixel(cborder); \
367  x.background = get_colorpixel(cbackground); \
368  x.text = get_colorpixel(ctext); \
369  x.indicator = get_colorpixel(cindicator); \
370  } while (0)
371 
372  config.client.background = get_colorpixel("#000000");
373  INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
374  INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
375  INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
376  INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
377 
378  /* the last argument (indicator color) is ignored for bar colors */
379  INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
380  INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
381  INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
382 
383  config.default_border = BS_NORMAL;
385  /* Set default_orientation to NO_ORIENTATION for auto orientation. */
387 
388  parse_configuration(override_configpath);
389 
390  if (reload) {
392  grab_all_keys(conn, false);
393  }
394 
395  if (config.font.id == 0) {
396  ELOG("You did not specify required configuration option \"font\"\n");
397  config.font = load_font("fixed", true);
398  set_font(&config.font);
399  }
400 
401  /* Redraw the currently visible decorations on reload, so that
402  * the possibly new drawing parameters changed. */
403  if (reload) {
405  xcb_flush(conn);
406  }
407 
408 #if 0
409  /* Set an empty name for every workspace which got no name */
410  Workspace *ws;
411  TAILQ_FOREACH(ws, workspaces, workspaces) {
412  if (ws->name != NULL) {
413  /* If the font was not specified when the workspace name
414  * was loaded, we need to predict the text width now */
415  if (ws->text_width == 0)
416  ws->text_width = predict_text_width(global_conn,
417  config.font, ws->name, ws->name_len);
418  continue;
419  }
420 
421  workspace_set_name(ws, NULL);
422  }
423 #endif
424 }