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