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 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  * 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  "~/.i3/config, $XDG_CONFIG_HOME/i3/config, " SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
53  }
54 
55  LOG("Parsing configfile %s\n", path);
57  current_configpath = path;
58 
59  /* initialize default bindings if we're just validating the config file */
60  if (!use_nagbar && bindings == NULL) {
61  bindings = scalloc(1, sizeof(struct bindings_head));
63  }
64 
65  return parse_file(path, use_nagbar);
66 }
67 
68 /*
69  * (Re-)loads the configuration file (sets useful defaults before).
70  *
71  */
72 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
73  if (reload) {
74  /* If we are currently in a binding mode, we first revert to the
75  * default since we have no guarantee that the current mode will even
76  * still exist after parsing the config again. See #2228. */
77  switch_mode("default");
78 
79  /* First ungrab the keys */
80  ungrab_all_keys(conn);
81 
82  struct Mode *mode;
83  while (!SLIST_EMPTY(&modes)) {
84  mode = SLIST_FIRST(&modes);
85  FREE(mode->name);
86 
87  /* Clear the old binding list */
88  while (!TAILQ_EMPTY(mode->bindings)) {
89  Binding *bind = TAILQ_FIRST(mode->bindings);
90  TAILQ_REMOVE(mode->bindings, bind, bindings);
91  binding_free(bind);
92  }
93  FREE(mode->bindings);
94 
95  SLIST_REMOVE(&modes, mode, Mode, modes);
96  FREE(mode);
97  }
98 
99  struct Assignment *assign;
100  while (!TAILQ_EMPTY(&assignments)) {
101  assign = TAILQ_FIRST(&assignments);
102  if (assign->type == A_TO_WORKSPACE)
103  FREE(assign->dest.workspace);
104  else if (assign->type == A_COMMAND)
105  FREE(assign->dest.command);
106  match_free(&(assign->match));
108  FREE(assign);
109  }
110 
111  /* Clear bar configs */
112  Barconfig *barconfig;
113  while (!TAILQ_EMPTY(&barconfigs)) {
114  barconfig = TAILQ_FIRST(&barconfigs);
115  FREE(barconfig->id);
116  for (int c = 0; c < barconfig->num_outputs; c++)
117  free(barconfig->outputs[c]);
118 
119  while (!TAILQ_EMPTY(&(barconfig->bar_bindings))) {
120  struct Barbinding *binding = TAILQ_FIRST(&(barconfig->bar_bindings));
121  FREE(binding->command);
122  TAILQ_REMOVE(&(barconfig->bar_bindings), binding, bindings);
123  FREE(binding);
124  }
125 
126  while (!TAILQ_EMPTY(&(barconfig->tray_outputs))) {
127  struct tray_output_t *tray_output = TAILQ_FIRST(&(barconfig->tray_outputs));
128  FREE(tray_output->output);
129  TAILQ_REMOVE(&(barconfig->tray_outputs), tray_output, tray_outputs);
130  FREE(tray_output);
131  }
132 
133  FREE(barconfig->outputs);
134  FREE(barconfig->socket_path);
135  FREE(barconfig->status_command);
136  FREE(barconfig->i3bar_command);
137  FREE(barconfig->font);
138  FREE(barconfig->colors.background);
139  FREE(barconfig->colors.statusline);
140  FREE(barconfig->colors.separator);
141  FREE(barconfig->colors.focused_background);
142  FREE(barconfig->colors.focused_statusline);
143  FREE(barconfig->colors.focused_separator);
145  FREE(barconfig->colors.focused_workspace_bg);
146  FREE(barconfig->colors.focused_workspace_text);
147  FREE(barconfig->colors.active_workspace_border);
148  FREE(barconfig->colors.active_workspace_bg);
149  FREE(barconfig->colors.active_workspace_text);
151  FREE(barconfig->colors.inactive_workspace_bg);
152  FREE(barconfig->colors.inactive_workspace_text);
153  FREE(barconfig->colors.urgent_workspace_border);
154  FREE(barconfig->colors.urgent_workspace_bg);
155  FREE(barconfig->colors.urgent_workspace_text);
156  FREE(barconfig->colors.binding_mode_border);
157  FREE(barconfig->colors.binding_mode_bg);
158  FREE(barconfig->colors.binding_mode_text);
159  TAILQ_REMOVE(&barconfigs, barconfig, configs);
160  FREE(barconfig);
161  }
162 
163 /* Clear workspace names */
164 #if 0
165  Workspace *ws;
166  TAILQ_FOREACH(ws, workspaces, workspaces)
167  workspace_set_name(ws, NULL);
168 #endif
169 
170  /* Invalidate pixmap caches in case font or colors changed */
171  Con *con;
173  FREE(con->deco_render_params);
174 
175  /* Get rid of the current font */
176  free_font();
177 
178  free(config.ipc_socket_path);
179  free(config.restart_state_path);
180  free(config.fake_outputs);
181  }
182 
183  SLIST_INIT(&modes);
184 
185  struct Mode *default_mode = scalloc(1, sizeof(struct Mode));
186  default_mode->name = sstrdup("default");
187  default_mode->bindings = scalloc(1, sizeof(struct bindings_head));
188  TAILQ_INIT(default_mode->bindings);
189  SLIST_INSERT_HEAD(&modes, default_mode, modes);
190 
191  bindings = default_mode->bindings;
192 
193 #define REQUIRED_OPTION(name) \
194  if (config.name == NULL) \
195  die("You did not specify required configuration option " #name "\n");
196 
197  /* Clear the old config or initialize the data structure */
198  memset(&config, 0, sizeof(config));
199 
200 /* Initialize default colors */
201 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
202  do { \
203  x.border = draw_util_hex_to_color(cborder); \
204  x.background = draw_util_hex_to_color(cbackground); \
205  x.text = draw_util_hex_to_color(ctext); \
206  x.indicator = draw_util_hex_to_color(cindicator); \
207  x.child_border = draw_util_hex_to_color(cbackground); \
208  } while (0)
209 
210  config.client.background = draw_util_hex_to_color("#000000");
211  INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
212  INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
213  INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
214  INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
215 
216  /* border and indicator color are ignored for placeholder contents */
217  INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
218 
219  /* the last argument (indicator color) is ignored for bar colors */
220  INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
221  INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
222  INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
223 
224  config.show_marks = true;
225 
226  config.default_border = BS_NORMAL;
228  config.default_border_width = logical_px(2);
230  /* Set default_orientation to NO_ORIENTATION for auto orientation. */
232 
233  /* Set default urgency reset delay to 500ms */
234  if (config.workspace_urgency_timer == 0)
235  config.workspace_urgency_timer = 0.5;
236 
237  parse_configuration(override_configpath, true);
238 
239  if (reload) {
241  grab_all_keys(conn);
242  regrab_all_buttons(conn);
243  }
244 
245  if (config.font.type == FONT_TYPE_NONE) {
246  ELOG("You did not specify required configuration option \"font\"\n");
247  config.font = load_font("fixed", true);
248  set_font(&config.font);
249  }
250 
251  /* Redraw the currently visible decorations on reload, so that
252  * the possibly new drawing parameters changed. */
253  if (reload) {
255  xcb_flush(conn);
256  }
257 
258 #if 0
259  /* Set an empty name for every workspace which got no name */
260  Workspace *ws;
261  TAILQ_FOREACH(ws, workspaces, workspaces) {
262  if (ws->name != NULL) {
263  /* If the font was not specified when the workspace name
264  * was loaded, we need to predict the text width now */
265  if (ws->text_width == 0)
266  ws->text_width = predict_text_width(global_conn,
267  config.font, ws->name, ws->name_len);
268  continue;
269  }
270 
271  workspace_set_name(ws, NULL);
272  }
273 #endif
274 }
struct Colortriple focused_inactive
Definition: config.h:208
struct modes_head modes
Definition: config.c:18
#define SLIST_REMOVE(head, elm, type, field)
Definition: queue.h:154
char * status_command
Command that should be run to get a statusline, for example &#39;i3status&#39;.
Definition: config.h:299
Definition: data.h:61
color_t background
Definition: config.h:206
union Assignment::@19 dest
destination workspace/command, depending on the type
char * focused_statusline
Definition: config.h:329
char * command
The command which is to be executed for this button.
Definition: config.h:366
struct Colortriple focused
Definition: config.h:214
Holds part of the configuration (the part which is not already in dedicated structures in include/dat...
Definition: config.h:92
#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator)
void free_font(void)
Frees the resources taken by the current font.
struct Config::config_client client
char ** outputs
Outputs on which this bar should show up on.
Definition: config.h:250
char * inactive_workspace_bg
Definition: config.h:341
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:498
char * focused_background
Definition: config.h:328
#define TAILQ_EMPTY(head)
Definition: queue.h:344
xcb_window_t root
Definition: main.c:56
char * socket_path
Path to the i3 IPC socket.
Definition: config.h:263
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
Definition: ipc.c:1278
struct bindings_head * bindings
Definition: main.c:73
enum Assignment::@18 type
type of this assignment:
char * focused_workspace_bg
Definition: config.h:333
char * urgent_workspace_border
Definition: config.h:344
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
char * urgent_workspace_text
Definition: config.h:346
char * urgent_workspace_bg
Definition: config.h:345
void match_free(Match *match)
Frees the given match.
Definition: match.c:254
struct barconfig_head barconfigs
Definition: config.c:19
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...
void x_deco_recurse(Con *con)
Recursively calls x_draw_decoration.
Definition: x.c:626
int default_floating_border_width
Definition: config.h:103
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:43
Config config
Definition: config.c:17
i3Font font
Definition: config.h:94
void set_font(i3Font *font)
Defines the font to be used for the forthcoming calls.
struct Con * croot
Definition: tree.c:14
#define ELOG(fmt,...)
Definition: libi3.h:93
char * active_workspace_border
Definition: config.h:336
char * fake_outputs
Overwrites output detection (for testing), see src/fake_outputs.c.
Definition: config.h:157
color_t draw_util_hex_to_color(const char *color)
Parses the given color in hex format to an internal color representation.
#define LOG(fmt,...)
Definition: libi3.h:88
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
enum Font::@23 type
The type of font.
struct assignments_head assignments
Definition: main.c:82
struct Colortriple unfocused
Definition: config.h:215
struct Colortriple placeholder
Definition: config.h:211
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
Definition: config.h:170
char * binding_mode_text
Definition: config.h:350
void binding_free(Binding *bind)
Frees the binding.
Definition: bindings.c:628
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
#define TAILQ_INIT(head)
Definition: queue.h:360
struct Colortriple unfocused
Definition: config.h:209
struct Colortriple urgent
Definition: config.h:210
The configuration file can contain multiple sets of bindings.
Definition: config.h:79
char * get_config_path(const char *override_configpath, bool use_system_paths)
Get the path of the first configuration file found.
char * focused_separator
Definition: config.h:330
bool show_marks
Specifies whether or not marks should be displayed in the window decoration.
Definition: config.h:186
int default_orientation
Default orientation for new containers.
Definition: config.h:106
Holds the status bar configuration (i3bar).
Definition: config.h:241
char * output
Definition: config.h:372
char * focused_workspace_text
Definition: config.h:334
struct Colortriple urgent
Definition: config.h:216
char * focused_workspace_border
Definition: config.h:332
char * active_workspace_text
Definition: config.h:338
struct bindings_head * bindings
Definition: config.h:82
#define FREE(pointer)
Definition: util.h:48
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition: bindings.c:466
i3Font load_font(const char *pattern, const bool fallback)
Loads a font for usage, also getting its height.
#define DLOG(fmt,...)
Definition: libi3.h:98
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:154
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:544
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:617
#define TAILQ_FIRST(head)
Definition: queue.h:336
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 * workspace
Definition: data.h:523
char * ipc_socket_path
Definition: config.h:96
char * font
Font specification for all text rendered on the bar.
Definition: config.h:302
#define die(...)
Definition: util.h:17
int predict_text_width(i3String *text)
Predict the text width in pixels for the given text.
struct Colortriple focused
Definition: config.h:207
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
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 grab_all_keys(xcb_connection_t *conn)
Grab the bound keys (tell X to send us keypress events for those keycodes)
Definition: bindings.c:132
int num_outputs
Number of outputs in the outputs array.
Definition: config.h:247
char * current_configpath
Definition: config.c:16
char * binding_mode_border
Definition: config.h:348
#define SLIST_EMPTY(head)
Definition: queue.h:111
Match match
the criteria to check if a window matches
Definition: data.h:518
border_style_t default_border
The default border style for new windows.
Definition: config.h:189
struct all_cons_head all_cons
Definition: tree.c:17
#define SLIST_FIRST(head)
Definition: queue.h:109
char * active_workspace_bg
Definition: config.h:337
char * inactive_workspace_text
Definition: config.h:342
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:72
void update_barconfig()
Sends the current bar configuration as an event to all barconfig_update listeners.
Definition: config.c:35
char * command
Definition: data.h:522
int default_border_width
Definition: config.h:102
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
Definition: config.h:361
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition: bindings.c:349
struct Barconfig::bar_colors colors
char * inactive_workspace_border
Definition: config.h:340
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:295
border_style_t default_floating_border
The default border style for new floating windows.
Definition: config.h:192
#define SLIST_INIT(head)
Definition: queue.h:127
char * id
Automatically generated ID for this bar config.
Definition: config.h:244
char * restart_state_path
Definition: config.h:97
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
char * name
Definition: config.h:80
struct Config::config_bar bar
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:250
char * binding_mode_bg
Definition: config.h:349