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;
17 struct modes_head modes;
18 struct barconfig_head barconfigs = TAILQ_HEAD_INITIALIZER(barconfigs);
19 
25 void ungrab_all_keys(xcb_connection_t *conn) {
26  DLOG("Ungrabbing all keys\n");
27  xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
28 }
29 
30 /*
31  * Sends the current bar configuration as an event to all barconfig_update listeners.
32  *
33  */
35  Barconfig *current;
36  TAILQ_FOREACH(current, &barconfigs, configs) {
38  }
39 }
40 
41 /*
42  * Finds the configuration file to use (either the one specified by
43  * override_configpath), the user’s one or the system default) and calls
44  * parse_file().
45  *
46  */
47 bool parse_configuration(const char *override_configpath, bool use_nagbar) {
48  char *path = get_config_path(override_configpath, true);
49  if (path == NULL) {
50  die("Unable to find the configuration file (looked at "
51  "~/.i3/config, $XDG_CONFIG_HOME/i3/config, " SYSCONFDIR "/i3/config and $XDG_CONFIG_DIRS/i3/config)");
52  }
53 
54  LOG("Parsing configfile %s\n", path);
56  current_configpath = path;
57 
58  /* initialize default bindings if we're just validating the config file */
59  if (!use_nagbar && bindings == NULL) {
60  bindings = scalloc(1, sizeof(struct bindings_head));
62  }
63 
64  return parse_file(path, use_nagbar);
65 }
66 
67 /*
68  * (Re-)loads the configuration file (sets useful defaults before).
69  *
70  */
71 void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload) {
72  if (reload) {
73  /* If we are currently in a binding mode, we first revert to the
74  * default since we have no guarantee that the current mode will even
75  * still exist after parsing the config again. See #2228. */
76  switch_mode("default");
77 
78  /* First ungrab the keys */
79  ungrab_all_keys(conn);
80 
81  struct Mode *mode;
82  while (!SLIST_EMPTY(&modes)) {
83  mode = SLIST_FIRST(&modes);
84  FREE(mode->name);
85 
86  /* Clear the old binding list */
87  while (!TAILQ_EMPTY(mode->bindings)) {
88  Binding *bind = TAILQ_FIRST(mode->bindings);
89  TAILQ_REMOVE(mode->bindings, bind, bindings);
90  binding_free(bind);
91  }
92  FREE(mode->bindings);
93 
94  SLIST_REMOVE(&modes, mode, Mode, modes);
95  FREE(mode);
96  }
97 
98  struct Assignment *assign;
99  while (!TAILQ_EMPTY(&assignments)) {
100  assign = TAILQ_FIRST(&assignments);
101  if (assign->type == A_TO_WORKSPACE)
102  FREE(assign->dest.workspace);
103  else if (assign->type == A_COMMAND)
104  FREE(assign->dest.command);
105  match_free(&(assign->match));
107  FREE(assign);
108  }
109 
110  /* Clear bar configs */
111  Barconfig *barconfig;
112  while (!TAILQ_EMPTY(&barconfigs)) {
113  barconfig = TAILQ_FIRST(&barconfigs);
114  FREE(barconfig->id);
115  for (int c = 0; c < barconfig->num_outputs; c++)
116  free(barconfig->outputs[c]);
117 
118  while (!TAILQ_EMPTY(&(barconfig->bar_bindings))) {
119  struct Barbinding *binding = TAILQ_FIRST(&(barconfig->bar_bindings));
120  FREE(binding->command);
121  TAILQ_REMOVE(&(barconfig->bar_bindings), binding, bindings);
122  FREE(binding);
123  }
124 
125  while (!TAILQ_EMPTY(&(barconfig->tray_outputs))) {
126  struct tray_output_t *tray_output = TAILQ_FIRST(&(barconfig->tray_outputs));
127  FREE(tray_output->output);
128  TAILQ_REMOVE(&(barconfig->tray_outputs), tray_output, tray_outputs);
129  FREE(tray_output);
130  }
131 
132  FREE(barconfig->outputs);
133  FREE(barconfig->socket_path);
134  FREE(barconfig->status_command);
135  FREE(barconfig->i3bar_command);
136  FREE(barconfig->font);
137  FREE(barconfig->colors.background);
138  FREE(barconfig->colors.statusline);
139  FREE(barconfig->colors.separator);
140  FREE(barconfig->colors.focused_background);
141  FREE(barconfig->colors.focused_statusline);
142  FREE(barconfig->colors.focused_separator);
144  FREE(barconfig->colors.focused_workspace_bg);
145  FREE(barconfig->colors.focused_workspace_text);
146  FREE(barconfig->colors.active_workspace_border);
147  FREE(barconfig->colors.active_workspace_bg);
148  FREE(barconfig->colors.active_workspace_text);
150  FREE(barconfig->colors.inactive_workspace_bg);
151  FREE(barconfig->colors.inactive_workspace_text);
152  FREE(barconfig->colors.urgent_workspace_border);
153  FREE(barconfig->colors.urgent_workspace_bg);
154  FREE(barconfig->colors.urgent_workspace_text);
155  FREE(barconfig->colors.binding_mode_border);
156  FREE(barconfig->colors.binding_mode_bg);
157  FREE(barconfig->colors.binding_mode_text);
158  TAILQ_REMOVE(&barconfigs, barconfig, configs);
159  FREE(barconfig);
160  }
161 
162  /* Invalidate pixmap caches in case font or colors changed */
163  Con *con;
165  FREE(con->deco_render_params);
166 
167  /* Get rid of the current font */
168  free_font();
169 
170  free(config.ipc_socket_path);
171  free(config.restart_state_path);
172  free(config.fake_outputs);
173  }
174 
175  SLIST_INIT(&modes);
176 
177  struct Mode *default_mode = scalloc(1, sizeof(struct Mode));
178  default_mode->name = sstrdup("default");
179  default_mode->bindings = scalloc(1, sizeof(struct bindings_head));
180  TAILQ_INIT(default_mode->bindings);
181  SLIST_INSERT_HEAD(&modes, default_mode, modes);
182 
183  bindings = default_mode->bindings;
184 
185 #define REQUIRED_OPTION(name) \
186  if (config.name == NULL) \
187  die("You did not specify required configuration option " #name "\n");
188 
189  /* Clear the old config or initialize the data structure */
190  memset(&config, 0, sizeof(config));
191 
192 /* Initialize default colors */
193 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
194  do { \
195  x.border = draw_util_hex_to_color(cborder); \
196  x.background = draw_util_hex_to_color(cbackground); \
197  x.text = draw_util_hex_to_color(ctext); \
198  x.indicator = draw_util_hex_to_color(cindicator); \
199  x.child_border = draw_util_hex_to_color(cbackground); \
200  } while (0)
201 
202  config.client.background = draw_util_hex_to_color("#000000");
203  INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
204  INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
205  INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
206  INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
207 
208  /* border and indicator color are ignored for placeholder contents */
209  INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
210 
211  /* the last argument (indicator color) is ignored for bar colors */
212  INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
213  INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
214  INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
215 
216  config.show_marks = true;
217 
218  config.default_border = BS_NORMAL;
220  config.default_border_width = logical_px(2);
222  /* Set default_orientation to NO_ORIENTATION for auto orientation. */
224 
225  /* Set default urgency reset delay to 500ms */
226  if (config.workspace_urgency_timer == 0)
227  config.workspace_urgency_timer = 0.5;
228 
229  parse_configuration(override_configpath, true);
230 
231  if (reload) {
233  grab_all_keys(conn);
234  regrab_all_buttons(conn);
235  }
236 
237  if (config.font.type == FONT_TYPE_NONE) {
238  ELOG("You did not specify required configuration option \"font\"\n");
239  config.font = load_font("fixed", true);
240  set_font(&config.font);
241  }
242 
243  /* Redraw the currently visible decorations on reload, so that
244  * the possibly new drawing parameters changed. */
245  if (reload) {
247  xcb_flush(conn);
248  }
249 }
int default_border_width
struct Colortriple unfocused
#define FREE(pointer)
Definition: util.h:50
struct bindings_head * bindings
Definition: configuration.h:83
#define ELOG(fmt,...)
Definition: libi3.h:89
struct assignments_head assignments
Definition: main.c:81
i3Font font
Definition: configuration.h:95
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:268
char * command
The command which is to be executed for this button.
struct barconfig_head barconfigs
Definition: config.c:18
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
Definition: ipc.c:1306
struct Con * croot
Definition: tree.c:12
char * font
Font specification for all text rendered on the bar.
#define SLIST_EMPTY(head)
Definition: queue.h:111
struct Config::config_client client
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:512
enum Assignment::@18 type
type of this assignment:
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition: bindings.c:591
struct bindings_head * bindings
Definition: main.c:72
char ** outputs
Outputs on which this bar should show up on.
struct Config::config_bar bar
void set_font(i3Font *font)
Defines the font to be used for the forthcoming calls.
xcb_window_t root
Definition: main.c:55
#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator)
void match_free(Match *match)
Frees the given match.
Definition: match.c:267
char * id
Automatically generated ID for this bar config.
#define LOG(fmt,...)
Definition: libi3.h:84
char * i3bar_command
Command that should be run to execute i3bar, give a full path if i3bar is not in your $PATH...
struct modes_head modes
Definition: config.c:17
#define SLIST_REMOVE(head, elm, type, field)
Definition: queue.h:154
i3Font load_font(const char *pattern, const bool fallback)
Loads a font for usage, also getting its height.
#define SLIST_INIT(head)
Definition: queue.h:127
char * socket_path
Path to the i3 IPC socket.
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:144
char * get_config_path(const char *override_configpath, bool use_system_paths)
Get the path of the first configuration file found.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
Match match
the criteria to check if a window matches
Definition: data.h:532
Holds the status bar configuration (i3bar).
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:42
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
char * name
Definition: configuration.h:81
struct Barconfig::bar_colors colors
#define TAILQ_FIRST(head)
Definition: queue.h:336
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...
#define SLIST_FIRST(head)
Definition: queue.h:109
color_t draw_util_hex_to_color(const char *color)
Parses the given color in hex format to an internal color representation.
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
char * current_configpath
Definition: config.c:15
bool show_marks
Specifies whether or not marks should be displayed in the window decoration.
Definition: data.h:62
struct Colortriple focused_inactive
struct Colortriple placeholder
int default_orientation
Default orientation for new containers.
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:71
char * restart_state_path
Definition: configuration.h:98
void update_barconfig()
Sends the current bar configuration as an event to all barconfig_update listeners.
Definition: config.c:34
void binding_free(Binding *bind)
Frees the binding.
Definition: bindings.c:757
#define TAILQ_INIT(head)
Definition: queue.h:360
struct Colortriple unfocused
Holds part of the configuration (the part which is not already in dedicated structures in include/dat...
Definition: configuration.h:93
border_style_t default_border
The default border style for new windows.
struct Colortriple urgent
struct all_cons_head all_cons
Definition: tree.c:15
int default_floating_border_width
char * command
Definition: data.h:536
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
char * workspace
Definition: data.h:537
void x_deco_recurse(Con *con)
Recursively calls x_draw_decoration.
Definition: x.c:623
int num_outputs
Number of outputs in the outputs array.
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:558
The configuration file can contain multiple sets of bindings.
Definition: configuration.h:80
enum Font::@23 type
The type of font.
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
#define DLOG(fmt,...)
Definition: libi3.h:94
struct Colortriple focused
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:174
char * status_command
Command that should be run to get a statusline, for example &#39;i3status&#39;.
char * ipc_socket_path
Definition: configuration.h:97
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:47
struct Colortriple focused
union Assignment::@19 dest
destination workspace/command, depending on the type
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
#define TAILQ_EMPTY(head)
Definition: queue.h:344
Config config
Definition: config.c:16
struct Colortriple urgent
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:631
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:25
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
char * fake_outputs
Overwrites output detection (for testing), see src/fake_outputs.c.
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
border_style_t default_floating_border
The default border style for new floating windows.
#define die(...)
Definition: util.h:19
void free_font(void)
Frees the resources taken by the current font.