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 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * config.c: Configuration file (calling the parser (src/config_parser.c) with
8  * the correct path, switching key bindings mode).
9  *
10  */
11 #include "all.h"
12 
13 #include <xkbcommon/xkbcommon.h>
14 
15 char *current_configpath = NULL;
16 char *current_config = NULL;
18 struct modes_head modes;
19 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
20 
21 /*
22  * Ungrabs all keys, to be called before re-grabbing the keys because of a
23  * mapping_notify event or a configuration file reload
24  *
25  */
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  */
35 void update_barconfig(void) {
36  Barconfig *current;
37  TAILQ_FOREACH(current, &barconfigs, configs) {
39  }
40 }
41 
42 /*
43  * Finds the configuration file to use (either the one specified by
44  * override_configpath), the user’s one or the system default) and calls
45  * parse_file().
46  *
47  */
48 bool parse_configuration(const char *override_configpath, bool use_nagbar) {
49  char *path = get_config_path(override_configpath, true);
50  if (path == NULL) {
51  die("Unable to find the configuration file (looked at "
52  "$XDG_CONFIG_HOME/i3/config, ~/.i3/config, $XDG_CONFIG_DIRS/i3/config "
53  "and " SYSCONFDIR "/i3/config)");
54  }
55 
56  LOG("Parsing configfile %s\n", path);
58  current_configpath = path;
59 
60  /* initialize default bindings if we're just validating the config file */
61  if (!use_nagbar && bindings == NULL) {
62  bindings = scalloc(1, sizeof(struct bindings_head));
64  }
65 
66  return parse_file(path, use_nagbar);
67 }
68 
69 /*
70  * (Re-)loads the configuration file (sets useful defaults before).
71  *
72  */
73 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
74  if (reload) {
75  /* If we are currently in a binding mode, we first revert to the
76  * default since we have no guarantee that the current mode will even
77  * still exist after parsing the config again. See #2228. */
78  switch_mode("default");
79 
80  /* First ungrab the keys */
81  ungrab_all_keys(conn);
82 
83  struct Mode *mode;
84  while (!SLIST_EMPTY(&modes)) {
85  mode = SLIST_FIRST(&modes);
86  FREE(mode->name);
87 
88  /* Clear the old binding list */
89  while (!TAILQ_EMPTY(mode->bindings)) {
90  Binding *bind = TAILQ_FIRST(mode->bindings);
91  TAILQ_REMOVE(mode->bindings, bind, bindings);
92  binding_free(bind);
93  }
94  FREE(mode->bindings);
95 
96  SLIST_REMOVE(&modes, mode, Mode, modes);
97  FREE(mode);
98  }
99 
100  while (!TAILQ_EMPTY(&assignments)) {
101  struct Assignment *assign = TAILQ_FIRST(&assignments);
102  if (assign->type == A_TO_WORKSPACE || assign->type == A_TO_WORKSPACE_NUMBER)
103  FREE(assign->dest.workspace);
104  else if (assign->type == A_COMMAND)
105  FREE(assign->dest.command);
106  else if (assign->type == A_TO_OUTPUT)
107  FREE(assign->dest.output);
108  match_free(&(assign->match));
110  FREE(assign);
111  }
112 
113  while (!TAILQ_EMPTY(&ws_assignments)) {
115  FREE(assign->name);
116  FREE(assign->output);
118  FREE(assign);
119  }
120 
121  /* Clear bar configs */
122  Barconfig *barconfig;
123  while (!TAILQ_EMPTY(&barconfigs)) {
124  barconfig = TAILQ_FIRST(&barconfigs);
125  FREE(barconfig->id);
126  for (int c = 0; c < barconfig->num_outputs; c++)
127  free(barconfig->outputs[c]);
128 
129  while (!TAILQ_EMPTY(&(barconfig->bar_bindings))) {
130  struct Barbinding *binding = TAILQ_FIRST(&(barconfig->bar_bindings));
131  FREE(binding->command);
132  TAILQ_REMOVE(&(barconfig->bar_bindings), binding, bindings);
133  FREE(binding);
134  }
135 
136  while (!TAILQ_EMPTY(&(barconfig->tray_outputs))) {
137  struct tray_output_t *tray_output = TAILQ_FIRST(&(barconfig->tray_outputs));
138  FREE(tray_output->output);
139  TAILQ_REMOVE(&(barconfig->tray_outputs), tray_output, tray_outputs);
140  FREE(tray_output);
141  }
142 
143  FREE(barconfig->outputs);
144  FREE(barconfig->socket_path);
145  FREE(barconfig->status_command);
146  FREE(barconfig->i3bar_command);
147  FREE(barconfig->font);
148  FREE(barconfig->colors.background);
149  FREE(barconfig->colors.statusline);
150  FREE(barconfig->colors.separator);
151  FREE(barconfig->colors.focused_background);
152  FREE(barconfig->colors.focused_statusline);
153  FREE(barconfig->colors.focused_separator);
155  FREE(barconfig->colors.focused_workspace_bg);
156  FREE(barconfig->colors.focused_workspace_text);
157  FREE(barconfig->colors.active_workspace_border);
158  FREE(barconfig->colors.active_workspace_bg);
159  FREE(barconfig->colors.active_workspace_text);
161  FREE(barconfig->colors.inactive_workspace_bg);
162  FREE(barconfig->colors.inactive_workspace_text);
163  FREE(barconfig->colors.urgent_workspace_border);
164  FREE(barconfig->colors.urgent_workspace_bg);
165  FREE(barconfig->colors.urgent_workspace_text);
166  FREE(barconfig->colors.binding_mode_border);
167  FREE(barconfig->colors.binding_mode_bg);
168  FREE(barconfig->colors.binding_mode_text);
169  TAILQ_REMOVE(&barconfigs, barconfig, configs);
170  FREE(barconfig);
171  }
172 
173  Con *con;
175  /* Assignments changed, previously ran assignments are invalid. */
176  if (con->window) {
177  con->window->nr_assignments = 0;
178  FREE(con->window->ran_assignments);
179  }
180  /* Invalidate pixmap caches in case font or colors changed. */
181  FREE(con->deco_render_params);
182  }
183 
184  /* Get rid of the current font */
185  free_font();
186 
187  free(config.ipc_socket_path);
188  free(config.restart_state_path);
189  free(config.fake_outputs);
190  }
191 
192  SLIST_INIT(&modes);
193 
194  struct Mode *default_mode = scalloc(1, sizeof(struct Mode));
195  default_mode->name = sstrdup("default");
196  default_mode->bindings = scalloc(1, sizeof(struct bindings_head));
197  TAILQ_INIT(default_mode->bindings);
198  SLIST_INSERT_HEAD(&modes, default_mode, modes);
199 
200  bindings = default_mode->bindings;
201 
202  /* Clear the old config or initialize the data structure */
203  memset(&config, 0, sizeof(config));
204 
205 /* Initialize default colors */
206 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
207  do { \
208  x.border = draw_util_hex_to_color(cborder); \
209  x.background = draw_util_hex_to_color(cbackground); \
210  x.text = draw_util_hex_to_color(ctext); \
211  x.indicator = draw_util_hex_to_color(cindicator); \
212  x.child_border = draw_util_hex_to_color(cbackground); \
213  } while (0)
214 
215  config.client[QUBE_DOM0].background = draw_util_hex_to_color("#121212");
217  "#522702", "#522702", "#ffffff", "#a6907d");
219  "#522702", "#361a01", "#ffffff", "#a6907d");
221  "#522702", "#361a01", "#999999", "#a6907d");
223  "#666666", "#a6907d", "#ce0000", "#a6907d");
224 
225  config.client[QUBE_RED].background = draw_util_hex_to_color("#121212");
227  "#e53b27", "#e53b27", "#ffffff", "#f19b90");
229  "#e53b27", "#902519", "#ffffff", "#f19b90");
231  "#e53b27", "#902519", "#999999", "#f19b90");
233  "#e53b27", "#f19b90", "#ce0000", "#f19b90");
234 
237  "#d05f03", "#d05f03", "#ffffff", "#daa67e");
239  "#d05f03", "#7b3702", "#ffffff", "#daa67e");
241  "#d05f03", "#7b3702", "#999999", "#daa67e");
243  "#d05f03", "#daa67e", "#ce0000", "#daa67e");
244 
247  "#999b00", "#999b00", "#ffffff", "#cacb7c");
249  "#999b00", "#666700", "#ffffff", "#cacb7c");
251  "#999b00", "#666700", "#999999", "#cacb7c");
253  "#999b00", "#cacb7c", "#ce0000", "#cacb7c");
254 
255  config.client[QUBE_GREEN].background = draw_util_hex_to_color("#121212");
257  "#04af5b", "#04af5b", "#ffffff", "#7dd5aa");
259  "#04af5b", "#02713b", "#ffffff", "#7dd5aa");
261  "#04af5b", "#02713b", "#999999", "#7dd5aa");
263  "#04af5b", "#7dd5aa", "#ce0000", "#7dd5aa");
264 
265  config.client[QUBE_GRAY].background = draw_util_hex_to_color("#121212");
267  "#8c959f", "#8c959f", "#ffffff", "#c3c8cd");
269  "#8c959f", "#676d75", "#ffffff", "#c3c8cd");
271  "#8c959f", "#676d75", "#999999", "#c3c8cd");
273  "#8c959f", "#c3c8cd", "#ce0000", "#c3c8cd");
274 
275  config.client[QUBE_BLUE].background = draw_util_hex_to_color("#121212");
277  "#3384d6", "#3384d6", "#ffffff", "#95bee8");
279  "#3384d6", "#1f5082", "#ffffff", "#95bee8");
281  "#3384d6", "#1f5082", "#999999", "#95bee8");
283  "#3384d6", "#95bee8", "#ce0000", "#95bee8");
284 
287  "#8f5cbe", "#8f5cbe", "#ffffff", "#c6abdd");
289  "#8f5cbe", "#5c3e78", "#ffffff", "#c6abdd");
291  "#8f5cbe", "#5c3e78", "#999999", "#c6abdd");
293  "#8f5cbe", "#c6abdd", "#ce0000", "#c6abdd");
294 
295  config.client[QUBE_BLACK].background = draw_util_hex_to_color("#121212");
297  "#595959", "#595959", "#ffffff", "#a3a3a3");
299  "#595959", "#3a3a3a", "#ffffff", "#a3a3a3");
301  "#595959", "#3a3a3a", "#999999", "#a3a3a3");
303  "#595959", "#a3a3a3", "#ce0000", "#a3a3a3");
304 
305  /* border and indicator color are ignored for placeholder contents */
307  "#000000", "#0c0c0c", "#ffffff", "#000000");
308 
309 
310  /* the last argument (indicator color) is ignored for bar colors */
311  INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
312  INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
313  INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
314 
315  config.show_marks = true;
316 
317  config.default_border = BS_NORMAL;
319  config.default_border_width = logical_px(2);
321  /* Set default_orientation to NO_ORIENTATION for auto orientation. */
323 
324  /* Set default urgency reset delay to 500ms */
325  if (config.workspace_urgency_timer == 0)
326  config.workspace_urgency_timer = 0.5;
327 
329 
330  parse_configuration(override_configpath, true);
331 
332  /* redefine defaults, to overwrite user settings easily */
333  config.default_border = BS_NORMAL;
335  config.default_border_width = logical_px(2);
337 
338  if (reload) {
340  grab_all_keys(conn);
341  regrab_all_buttons(conn);
342  }
343 
344  if (config.font.type == FONT_TYPE_NONE) {
345  ELOG("You did not specify required configuration option \"font\"\n");
346  config.font = load_font("fixed", true);
347  set_font(&config.font);
348  }
349 
350  /* Redraw the currently visible decorations on reload, so that
351  * the possibly new drawing parameters changed. */
352  if (reload) {
354  xcb_flush(conn);
355  }
356 }
border_style_t default_border
The default border style for new windows.
int num_outputs
Number of outputs in the outputs array.
#define FREE(pointer)
Definition: util.h:47
char * i3bar_command
Command that should be run to execute i3bar, give a full path if i3bar is not in your $PATH...
xcb_window_t root
Definition: main.c:57
enum Font::@23 type
The type of font.
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:704
char * ipc_socket_path
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
char * socket_path
Path to the i3 IPC socket.
void update_barconfig(void)
Sends the current bar configuration as an event to all barconfig_update listeners.
Definition: config.c:35
struct barconfig_head barconfigs
Definition: config.c:19
Stores which workspace (by name or number) goes to which output.
Definition: data.h:224
i3Font font
Definition: configuration.h:98
struct Con * croot
Definition: tree.c:12
#define DLOG(fmt,...)
Definition: libi3.h:104
#define SLIST_REMOVE(head, elm, type, field)
Definition: queue.h:154
struct Barconfig::bar_colors colors
#define TAILQ_EMPTY(head)
Definition: queue.h:344
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:44
void match_free(Match *match)
Frees the given match.
Definition: match.c:241
focus_wrapping_t focus_wrapping
When focus wrapping is enabled (the default), attempting to move focus past the edge of the screen (i...
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition: bindings.c:618
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:299
#define SLIST_EMPTY(head)
Definition: queue.h:111
int default_floating_border_width
#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator)
int default_border_width
char * name
Definition: configuration.h:83
char * current_config
Definition: config.c:16
struct bindings_head * bindings
Definition: main.c:74
void free_font(void)
Frees the resources taken by the current font.
struct assignments_head assignments
Definition: main.c:83
struct Config::config_bar bar
struct modes_head modes
Definition: config.c:18
The configuration file can contain multiple sets of bindings.
Definition: configuration.h:82
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
struct Colortriple unfocused
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
Definition: ipc.c:1616
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
struct Window * window
Definition: data.h:698
void grab_all_keys(xcb_connection_t *conn)
Grab the bound keys (tell X to send us keypress events for those keycodes)
Definition: bindings.c:147
#define SLIST_INIT(head)
Definition: queue.h:127
#define TAILQ_FIRST(head)
Definition: queue.h:336
char * font
Font specification for all text rendered on the bar.
#define SLIST_FIRST(head)
Definition: queue.h:109
#define TAILQ_INIT(head)
Definition: queue.h:360
char ** outputs
Outputs on which this bar should show up on.
enum Assignment::@18 type
type of this assignment:
char * fake_outputs
Overwrites output detection (for testing), see src/fake_outputs.c.
char * status_command
Command that should be run to get a statusline, for example &#39;i3status&#39;.
char * current_configpath
Definition: config.c:15
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
int default_orientation
Default orientation for new containers.
Definition: data.h:62
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:73
struct Colortriple urgent
void binding_free(Binding *bind)
Frees the binding.
Definition: bindings.c:792
#define LOG(fmt,...)
Definition: libi3.h:94
uint32_t nr_assignments
Pointers to the Assignments which were already ran for this Window (assignments run only once) ...
Definition: data.h:437
char * workspace
Definition: data.h:606
struct Colortriple focused_inactive
char * get_config_path(const char *override_configpath, bool use_system_paths)
Get the path of the first configuration file found.
color_t draw_util_hex_to_color(const char *color)
Parses the given color in hex format to an internal color representation.
struct all_cons_head all_cons
Definition: tree.c:15
struct Config::config_client client[QUBE_NUM_LABELS]
char * command
The command which is to be executed for this button.
Match match
the criteria to check if a window matches
Definition: data.h:601
Holds the status bar configuration (i3bar).
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:630
void x_deco_recurse(Con *con)
Recursively calls x_draw_decoration.
Definition: x.c:709
Holds part of the configuration (the part which is not already in dedicated structures in include/dat...
Definition: configuration.h:96
i3Font load_font(const char *pattern, const bool fallback)
Loads a font for usage, also getting its height.
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:579
#define ELOG(fmt,...)
Definition: libi3.h:99
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...
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition: bindings.c:432
struct Colortriple placeholder
char * command
Definition: data.h:605
struct Colortriple unfocused
void regrab_all_buttons(xcb_connection_t *conn)
Release the button grabs on all managed windows and regrab them, reevaluating which buttons need to b...
Definition: bindings.c:177
struct Colortriple urgent
struct ws_assignments_head ws_assignments
Definition: main.c:87
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
Definition: data.h:150
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:48
struct Colortriple focused
Assignment ** ran_assignments
Definition: data.h:438
struct bindings_head * bindings
Definition: configuration.h:85
Config config
Definition: config.c:17
char * output
Definition: data.h:607
bool show_marks
Specifies whether or not marks should be displayed in the window decoration.
void set_font(i3Font *font)
Defines the font to be used for the forthcoming calls.
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 * restart_state_path
union Assignment::@19 dest
destination workspace/command/output, depending on the type
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
border_style_t default_floating_border
The default border style for new floating windows.
struct Colortriple focused
#define die(...)
Definition: util.h:19
char * id
Automatically generated ID for this bar config.