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/config_parser.c) with
10  * the correct path, switching key bindings mode).
11  *
12  */
13 #include "all.h"
14 #include <xkbcommon/xkbcommon.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 /*
32  * Sends the current bar configuration as an event to all barconfig_update listeners.
33  *
34  */
36  Barconfig *current;
37  TAILQ_FOREACH(current, &barconfigs, configs) {
39  }
40 }
41 
42 /*
43  * Get the path of the first configuration file found. If override_configpath
44  * is specified, that path is returned and saved for further calls. Otherwise,
45  * checks the home directory first, then the system directory first, always
46  * taking into account the XDG Base Directory Specification ($XDG_CONFIG_HOME,
47  * $XDG_CONFIG_DIRS)
48  *
49  */
50 static char *get_config_path(const char *override_configpath) {
51  char *xdg_config_home, *xdg_config_dirs, *config_path;
52 
53  static const char *saved_configpath = NULL;
54 
55  if (override_configpath != NULL) {
56  saved_configpath = override_configpath;
57  return sstrdup(saved_configpath);
58  }
59 
60  if (saved_configpath != NULL)
61  return sstrdup(saved_configpath);
62 
63  /* 1: check the traditional path under the home directory */
64  config_path = resolve_tilde("~/.i3/config");
65  if (path_exists(config_path))
66  return config_path;
67  free(config_path);
68 
69  /* 2: check for $XDG_CONFIG_HOME/i3/config */
70  if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
71  xdg_config_home = "~/.config";
72 
73  xdg_config_home = resolve_tilde(xdg_config_home);
74  sasprintf(&config_path, "%s/i3/config", xdg_config_home);
75  free(xdg_config_home);
76 
77  if (path_exists(config_path))
78  return config_path;
79  free(config_path);
80 
81  /* 3: check the traditional path under /etc */
82  config_path = SYSCONFDIR "/i3/config";
83  if (path_exists(config_path))
84  return sstrdup(config_path);
85 
86  /* 4: check for $XDG_CONFIG_DIRS/i3/config */
87  if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
88  xdg_config_dirs = "/etc/xdg";
89 
90  char *buf = sstrdup(xdg_config_dirs);
91  char *tok = strtok(buf, ":");
92  while (tok != NULL) {
93  tok = resolve_tilde(tok);
94  sasprintf(&config_path, "%s/i3/config", tok);
95  free(tok);
96  if (path_exists(config_path)) {
97  free(buf);
98  return config_path;
99  }
100  free(config_path);
101  tok = strtok(NULL, ":");
102  }
103  free(buf);
104 
105  die("Unable to find the configuration file (looked at "
106  "~/.i3/config, $XDG_CONFIG_HOME/i3/config, " SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
107 }
108 
109 /*
110  * Finds the configuration file to use (either the one specified by
111  * override_configpath), the user’s one or the system default) and calls
112  * parse_file().
113  *
114  */
115 bool parse_configuration(const char *override_configpath, bool use_nagbar) {
116  char *path = get_config_path(override_configpath);
117  LOG("Parsing configfile %s\n", path);
119  current_configpath = path;
120 
121  /* initialize default bindings if we're just validating the config file */
122  if (!use_nagbar && bindings == NULL) {
123  bindings = scalloc(sizeof(struct bindings_head));
125  }
126 
127  return parse_file(path, use_nagbar);
128 }
129 
130 /*
131  * (Re-)loads the configuration file (sets useful defaults before).
132  *
133  */
134 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
135  if (reload) {
136  /* First ungrab the keys */
137  ungrab_all_keys(conn);
138 
139  struct Mode *mode;
140  Binding *bind;
141  while (!SLIST_EMPTY(&modes)) {
142  mode = SLIST_FIRST(&modes);
143  FREE(mode->name);
144 
145  /* Clear the old binding list */
146  bindings = mode->bindings;
147  while (!TAILQ_EMPTY(bindings)) {
148  bind = TAILQ_FIRST(bindings);
150  binding_free(bind);
151  }
152  FREE(bindings);
153  SLIST_REMOVE(&modes, mode, Mode, modes);
154  }
155 
156  struct Assignment *assign;
157  while (!TAILQ_EMPTY(&assignments)) {
158  assign = TAILQ_FIRST(&assignments);
159  if (assign->type == A_TO_WORKSPACE)
160  FREE(assign->dest.workspace);
161  else if (assign->type == A_COMMAND)
162  FREE(assign->dest.command);
163  match_free(&(assign->match));
165  FREE(assign);
166  }
167 
168  /* Clear bar configs */
169  Barconfig *barconfig;
170  while (!TAILQ_EMPTY(&barconfigs)) {
171  barconfig = TAILQ_FIRST(&barconfigs);
172  FREE(barconfig->id);
173  for (int c = 0; c < barconfig->num_outputs; c++)
174  free(barconfig->outputs[c]);
175  FREE(barconfig->outputs);
176  FREE(barconfig->tray_output);
177  FREE(barconfig->socket_path);
178  FREE(barconfig->status_command);
179  FREE(barconfig->i3bar_command);
180  FREE(barconfig->font);
181  FREE(barconfig->colors.background);
182  FREE(barconfig->colors.statusline);
184  FREE(barconfig->colors.focused_workspace_bg);
185  FREE(barconfig->colors.focused_workspace_text);
186  FREE(barconfig->colors.active_workspace_border);
187  FREE(barconfig->colors.active_workspace_bg);
188  FREE(barconfig->colors.active_workspace_text);
190  FREE(barconfig->colors.inactive_workspace_bg);
191  FREE(barconfig->colors.inactive_workspace_text);
192  FREE(barconfig->colors.urgent_workspace_border);
193  FREE(barconfig->colors.urgent_workspace_bg);
194  FREE(barconfig->colors.urgent_workspace_text);
195  TAILQ_REMOVE(&barconfigs, barconfig, configs);
196  FREE(barconfig);
197  }
198 
199 /* Clear workspace names */
200 #if 0
201  Workspace *ws;
202  TAILQ_FOREACH(ws, workspaces, workspaces)
203  workspace_set_name(ws, NULL);
204 #endif
205 
206  /* Invalidate pixmap caches in case font or colors changed */
207  Con *con;
209  FREE(con->deco_render_params);
210 
211  /* Get rid of the current font */
212  free_font();
213  }
214 
215  SLIST_INIT(&modes);
216 
217  struct Mode *default_mode = scalloc(sizeof(struct Mode));
218  default_mode->name = sstrdup("default");
219  default_mode->bindings = scalloc(sizeof(struct bindings_head));
220  TAILQ_INIT(default_mode->bindings);
221  SLIST_INSERT_HEAD(&modes, default_mode, modes);
222 
223  bindings = default_mode->bindings;
224 
225 #define REQUIRED_OPTION(name) \
226  if (config.name == NULL) \
227  die("You did not specify required configuration option " #name "\n");
228 
229  /* Clear the old config or initialize the data structure */
230  memset(&config, 0, sizeof(config));
231 
232 /* Initialize default colors */
233 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
234  do { \
235  x.border = get_colorpixel(cborder); \
236  x.background = get_colorpixel(cbackground); \
237  x.text = get_colorpixel(ctext); \
238  x.indicator = get_colorpixel(cindicator); \
239  } while (0)
240 
241  config.client.background = get_colorpixel("#000000");
242  INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
243  INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
244  INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
245  INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
246 
247  /* border and indicator color are ignored for placeholder contents */
248  INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
249 
250  /* the last argument (indicator color) is ignored for bar colors */
251  INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
252  INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
253  INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
254 
255  config.default_border = BS_NORMAL;
257  config.default_border_width = logical_px(2);
259  /* Set default_orientation to NO_ORIENTATION for auto orientation. */
261 
262  /* Set default urgency reset delay to 500ms */
263  if (config.workspace_urgency_timer == 0)
264  config.workspace_urgency_timer = 0.5;
265 
266  parse_configuration(override_configpath, true);
267 
268  if (reload) {
270  grab_all_keys(conn, false);
271  }
272 
273  if (config.font.type == FONT_TYPE_NONE) {
274  ELOG("You did not specify required configuration option \"font\"\n");
275  config.font = load_font("fixed", true);
276  set_font(&config.font);
277  }
278 
279  /* Redraw the currently visible decorations on reload, so that
280  * the possibly new drawing parameters changed. */
281  if (reload) {
283  xcb_flush(conn);
284  }
285 
286 #if 0
287  /* Set an empty name for every workspace which got no name */
288  Workspace *ws;
289  TAILQ_FOREACH(ws, workspaces, workspaces) {
290  if (ws->name != NULL) {
291  /* If the font was not specified when the workspace name
292  * was loaded, we need to predict the text width now */
293  if (ws->text_width == 0)
294  ws->text_width = predict_text_width(global_conn,
295  config.font, ws->name, ws->name_len);
296  continue;
297  }
298 
299  workspace_set_name(ws, NULL);
300  }
301 #endif
302 }
char * font
Font specification for all text rendered on the bar.
Definition: config.h:286
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
char * name
Definition: config.h:79
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
struct Colortriple unfocused
Definition: config.h:197
void set_font(i3Font *font)
Defines the font to be used for the forthcoming calls.
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
Config config
Definition: config.c:17
xcb_connection_t * conn
Definition: main.c:43
int default_floating_border_width
Definition: config.h:101
i3Font font
Definition: config.h:92
#define LOG(fmt,...)
Definition: libi3.h:76
char * inactive_workspace_bg
Definition: config.h:321
void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload)
Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
Definition: config.c:134
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:458
static char * get_config_path(const char *override_configpath)
Definition: config.c:50
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition: bindings.c:246
struct all_cons_head all_cons
Definition: tree.c:17
char * focused_workspace_bg
Definition: config.h:313
char * urgent_workspace_border
Definition: config.h:324
struct Barconfig::bar_colors colors
bool parse_configuration(const char *override_configpath, bool use_nagbar)
Finds the configuration file to use (either the one specified by override_configpath), the user’s one or the system default) and calls parse_file().
Definition: config.c:115
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
char * urgent_workspace_text
Definition: config.h:326
union Assignment::@17 dest
destination workspace/command, depending on the type
char * urgent_workspace_bg
Definition: config.h:325
#define TAILQ_FIRST(head)
Definition: queue.h:336
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
Definition: config.h:168
void x_deco_recurse(Con *con)
Recursively calls x_draw_decoration.
Definition: x.c:575
struct Config::config_client client
struct Colortriple unfocused
Definition: config.h:191
void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch)
Grab the bound keys (tell X to send us keypress events for those keycodes)
Definition: bindings.c:108
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:239
void update_barconfig()
Sends the current bar configuration as an event to all barconfig_update listeners.
Definition: config.c:35
bool parse_file(const char *f, bool use_nagbar)
Parses the given file by first replacing the variables, then calling parse_config and launching i3-na...
int default_orientation
Default orientation for new containers.
Definition: config.h:104
char * active_workspace_border
Definition: config.h:316
#define DLOG(fmt,...)
Definition: libi3.h:86
void free_font(void)
Frees the resources taken by the current font.
struct bindings_head * bindings
Definition: config.h:80
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
The configuration file can contain multiple sets of bindings.
Definition: config.h:78
char * tray_output
Output on which the tray should be shown.
Definition: config.h:236
struct Colortriple focused
Definition: config.h:196
uint32_t get_colorpixel(const char *hex) __attribute__((const ))
Returns the colorpixel to use for the given hex color (think of HTML).
#define SLIST_EMPTY(head)
Definition: queue.h:111
#define die(...)
Definition: util.h:17
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:570
char * id
Automatically generated ID for this bar config.
Definition: config.h:226
uint32_t background
Definition: config.h:188
enum Font::@21 type
The type of font.
#define TAILQ_INIT(head)
Definition: queue.h:360
struct modes_head modes
Definition: config.c:18
char * focused_workspace_text
Definition: config.h:314
#define TAILQ_EMPTY(head)
Definition: queue.h:344
char * focused_workspace_border
Definition: config.h:312
struct assignments_head assignments
Definition: main.c:82
struct Colortriple focused
Definition: config.h:189
char * active_workspace_text
Definition: config.h:318
void ungrab_all_keys(xcb_connection_t *conn)
Ungrabs all keys, to be called before re-grabbing the keys because of a mapping_notify event or a con...
Definition: config.c:26
char * i3bar_command
Command that should be run to execute i3bar, give a full path if i3bar is not in your $PATH...
Definition: config.h:279
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
Definition: util.c:168
#define ELOG(fmt,...)
Definition: libi3.h:81
char * current_configpath
Definition: config.c:16
Holds part of the configuration (the part which is not already in dedicated structures in include/dat...
Definition: config.h:90
struct Colortriple urgent
Definition: config.h:192
border_style_t default_border
The default border style for new windows.
Definition: config.h:171
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:496
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
struct bindings_head * bindings
Definition: main.c:73
int default_border_width
Definition: config.h:100
void binding_free(Binding *bind)
Frees the binding.
Definition: bindings.c:405
#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator)
char * workspace
Definition: data.h:481
#define SLIST_REMOVE(head, elm, type, field)
Definition: queue.h:154
border_style_t default_floating_border
The default border style for new floating windows.
Definition: config.h:174
struct Con * croot
Definition: tree.c:14
xcb_window_t root
Definition: main.c:56
struct Config::config_bar bar
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
struct Colortriple focused_inactive
Definition: config.h:190
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
Definition: util.c:198
struct barconfig_head barconfigs
Definition: config.c:19
char ** outputs
Outputs on which this bar should show up on.
Definition: config.h:232
enum Assignment::@16 type
type of this assignment:
struct Colortriple urgent
Definition: config.h:198
Match match
the criteria to check if a window matches
Definition: data.h:476
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define SLIST_INIT(head)
Definition: queue.h:127
char * active_workspace_bg
Definition: config.h:317
char * inactive_workspace_text
Definition: config.h:322
char * command
Definition: data.h:480
char * status_command
Command that should be run to get a statusline, for example 'i3status'.
Definition: config.h:283
Holds the status bar configuration (i3bar).
Definition: config.h:223
char * socket_path
Path to the i3 IPC socket.
Definition: config.h:241
char * inactive_workspace_border
Definition: config.h:320
#define FREE(pointer)
Definition: util.h:48
i3Font load_font(const char *pattern, const bool fallback)
Loads a font for usage, also getting its height.
Definition: data.h:60
#define SLIST_FIRST(head)
Definition: queue.h:109
void match_free(Match *match)
Frees the given match.
Definition: match.c:191
struct Colortriple placeholder
Definition: config.h:193
int num_outputs
Number of outputs in the outputs array.
Definition: config.h:229
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
Definition: ipc.c:1184