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 static void free_configuration(void) {
32  assert(conn != NULL);
33 
34  /* If we are currently in a binding mode, we first revert to the default
35  * since we have no guarantee that the current mode will even still exist
36  * after parsing the config again. See #2228. */
37  switch_mode("default");
38 
39  /* First ungrab the keys */
41 
42  struct Mode *mode;
43  while (!SLIST_EMPTY(&modes)) {
44  mode = SLIST_FIRST(&modes);
45  FREE(mode->name);
46 
47  /* Clear the old binding list */
48  while (!TAILQ_EMPTY(mode->bindings)) {
49  Binding *bind = TAILQ_FIRST(mode->bindings);
50  TAILQ_REMOVE(mode->bindings, bind, bindings);
51  binding_free(bind);
52  }
53  FREE(mode->bindings);
54 
55  SLIST_REMOVE(&modes, mode, Mode, modes);
56  FREE(mode);
57  }
58 
59  while (!TAILQ_EMPTY(&assignments)) {
60  struct Assignment *assign = TAILQ_FIRST(&assignments);
61  if (assign->type == A_TO_WORKSPACE || assign->type == A_TO_WORKSPACE_NUMBER)
62  FREE(assign->dest.workspace);
63  else if (assign->type == A_COMMAND)
64  FREE(assign->dest.command);
65  else if (assign->type == A_TO_OUTPUT)
66  FREE(assign->dest.output);
67  match_free(&(assign->match));
69  FREE(assign);
70  }
71 
72  while (!TAILQ_EMPTY(&ws_assignments)) {
74  FREE(assign->name);
75  FREE(assign->output);
77  FREE(assign);
78  }
79 
80  /* Clear bar configs */
81  Barconfig *barconfig;
82  while (!TAILQ_EMPTY(&barconfigs)) {
83  barconfig = TAILQ_FIRST(&barconfigs);
84  FREE(barconfig->id);
85  for (int c = 0; c < barconfig->num_outputs; c++)
86  free(barconfig->outputs[c]);
87 
88  while (!TAILQ_EMPTY(&(barconfig->bar_bindings))) {
89  struct Barbinding *binding = TAILQ_FIRST(&(barconfig->bar_bindings));
90  FREE(binding->command);
91  TAILQ_REMOVE(&(barconfig->bar_bindings), binding, bindings);
92  FREE(binding);
93  }
94 
95  while (!TAILQ_EMPTY(&(barconfig->tray_outputs))) {
96  struct tray_output_t *tray_output = TAILQ_FIRST(&(barconfig->tray_outputs));
97  FREE(tray_output->output);
98  TAILQ_REMOVE(&(barconfig->tray_outputs), tray_output, tray_outputs);
99  FREE(tray_output);
100  }
101 
102  FREE(barconfig->outputs);
103  FREE(barconfig->socket_path);
104  FREE(barconfig->status_command);
105  FREE(barconfig->i3bar_command);
106  FREE(barconfig->font);
107  FREE(barconfig->colors.background);
108  FREE(barconfig->colors.statusline);
109  FREE(barconfig->colors.separator);
110  FREE(barconfig->colors.focused_background);
111  FREE(barconfig->colors.focused_statusline);
112  FREE(barconfig->colors.focused_separator);
114  FREE(barconfig->colors.focused_workspace_bg);
115  FREE(barconfig->colors.focused_workspace_text);
116  FREE(barconfig->colors.active_workspace_border);
117  FREE(barconfig->colors.active_workspace_bg);
118  FREE(barconfig->colors.active_workspace_text);
120  FREE(barconfig->colors.inactive_workspace_bg);
121  FREE(barconfig->colors.inactive_workspace_text);
122  FREE(barconfig->colors.urgent_workspace_border);
123  FREE(barconfig->colors.urgent_workspace_bg);
124  FREE(barconfig->colors.urgent_workspace_text);
125  FREE(barconfig->colors.binding_mode_border);
126  FREE(barconfig->colors.binding_mode_bg);
127  FREE(barconfig->colors.binding_mode_text);
128  TAILQ_REMOVE(&barconfigs, barconfig, configs);
129  FREE(barconfig);
130  }
131 
132  Con *con;
133  TAILQ_FOREACH (con, &all_cons, all_cons) {
134  /* Assignments changed, previously ran assignments are invalid. */
135  if (con->window) {
136  con->window->nr_assignments = 0;
137  FREE(con->window->ran_assignments);
138  }
139  /* Invalidate pixmap caches in case font or colors changed. */
140  FREE(con->deco_render_params);
141  }
142 
143  /* Get rid of the current font */
144  free_font();
145 
146  free(config.ipc_socket_path);
148  free(config.fake_outputs);
149 }
150 
151 /*
152  * (Re-)loads the configuration file (sets useful defaults before).
153  *
154  * If you specify override_configpath, only this path is used to look for a
155  * configuration file.
156  *
157  * load_type specifies the type of loading: C_VALIDATE is used to only verify
158  * the correctness of the config file (used with the flag -C). C_LOAD will load
159  * the config for normal use and display errors in the nagbar. C_RELOAD will
160  * also clear the previous config.
161  *
162  */
163 bool load_configuration(const char *override_configpath, config_load_t load_type) {
164  if (load_type == C_RELOAD) {
166  }
167 
168  SLIST_INIT(&modes);
169 
170  struct Mode *default_mode = scalloc(1, sizeof(struct Mode));
171  default_mode->name = sstrdup("default");
172  default_mode->bindings = scalloc(1, sizeof(struct bindings_head));
173  TAILQ_INIT(default_mode->bindings);
174  SLIST_INSERT_HEAD(&modes, default_mode, modes);
175 
176  bindings = default_mode->bindings;
177  current_binding_mode = default_mode->name;
178 
179  /* Clear the old config or initialize the data structure */
180  memset(&config, 0, sizeof(config));
181 
182  /* Initialize default colors */
183 #define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
184  do { \
185  x.border = draw_util_hex_to_color(cborder); \
186  x.background = draw_util_hex_to_color(cbackground); \
187  x.text = draw_util_hex_to_color(ctext); \
188  x.indicator = draw_util_hex_to_color(cindicator); \
189  x.child_border = draw_util_hex_to_color(cbackground); \
190  } while (0)
191 
193  INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
194  INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
195  INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
196  INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
197 
198  /* border and indicator color are ignored for placeholder contents */
199  INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
200 
201  /* the last argument (indicator color) is ignored for bar colors */
202  INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
203  INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
204  INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
205 
206  config.show_marks = true;
207 
212  /* Set default_orientation to NO_ORIENTATION for auto orientation. */
214 
215  /* Set default urgency reset delay to 500ms */
218 
220 
222  current_configpath = get_config_path(override_configpath, true);
223  if (current_configpath == NULL) {
224  die("Unable to find the configuration file (looked at "
225  "$XDG_CONFIG_HOME/i3/config, ~/.i3/config, $XDG_CONFIG_DIRS/i3/config "
226  "and " SYSCONFDIR "/i3/config)");
227  }
228  LOG("Parsing configfile %s\n", current_configpath);
229  const bool result = parse_file(current_configpath, load_type != C_VALIDATE);
230 
231  if (config.font.type == FONT_TYPE_NONE && load_type != C_VALIDATE) {
232  ELOG("You did not specify required configuration option \"font\"\n");
233  config.font = load_font("fixed", true);
234  set_font(&config.font);
235  }
236 
237  if (load_type == C_RELOAD) {
241 
242  /* Redraw the currently visible decorations on reload, so that the
243  * possibly new drawing parameters changed. */
245  xcb_flush(conn);
246  }
247 
248  return result;
249 }
void binding_free(Binding *bind)
Frees the binding.
Definition: bindings.c:795
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:149
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:179
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition: bindings.c:620
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition: bindings.c:434
static void free_configuration(void)
Definition: config.c:31
Config config
Definition: config.c:17
bool load_configuration(const char *override_configpath, config_load_t load_type)
(Re-)loads the configuration file (sets useful defaults before).
Definition: config.c:163
struct barconfig_head barconfigs
Definition: config.c:19
struct modes_head modes
Definition: config.c:18
#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator)
char * current_configpath
Definition: config.c:15
char * current_config
Definition: config.c:16
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...
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:51
xcb_window_t root
Definition: main.c:64
const char * current_binding_mode
Definition: main.c:82
struct assignments_head assignments
Definition: main.c:91
struct ws_assignments_head ws_assignments
Definition: main.c:95
struct bindings_head * bindings
Definition: main.c:81
void match_free(Match *match)
Frees the given match.
Definition: match.c:267
struct Con * croot
Definition: tree.c:12
struct all_cons_head all_cons
Definition: tree.c:15
void x_deco_recurse(Con *con)
Recursively calls x_draw_decoration.
Definition: x.c:718
config_load_t
@ C_VALIDATE
@ C_RELOAD
@ FOCUS_WRAPPING_ON
Definition: data.h:140
@ NO_ORIENTATION
Definition: data.h:56
@ BS_NORMAL
Definition: data.h:61
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...
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
#define DLOG(fmt,...)
Definition: libi3.h:104
#define LOG(fmt,...)
Definition: libi3.h:94
#define ELOG(fmt,...)
Definition: libi3.h:99
color_t draw_util_hex_to_color(const char *color)
Parses the given color in hex format to an internal color representation.
void set_font(i3Font *font)
Defines the font to be used for the forthcoming calls.
i3Font load_font(const char *pattern, const bool fallback)
Loads a font for usage, also getting its height.
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 free_font(void)
Frees the resources taken by the current font.
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define SLIST_INIT(head)
Definition: queue.h:127
#define TAILQ_INIT(head)
Definition: queue.h:360
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
#define SLIST_EMPTY(head)
Definition: queue.h:111
#define TAILQ_FIRST(head)
Definition: queue.h:336
#define SLIST_FIRST(head)
Definition: queue.h:109
#define SLIST_REMOVE(head, elm, type, field)
Definition: queue.h:154
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define TAILQ_EMPTY(head)
Definition: queue.h:344
#define die(...)
Definition: util.h:19
#define FREE(pointer)
Definition: util.h:47
The configuration file can contain multiple sets of bindings.
Definition: configuration.h:78
char * name
Definition: configuration.h:79
struct bindings_head * bindings
Definition: configuration.h:81
Holds part of the configuration (the part which is not already in dedicated structures in include/dat...
Definition: configuration.h:91
focus_wrapping_t focus_wrapping
When focus wrapping is enabled (the default), attempting to move focus past the edge of the screen (i...
char * restart_state_path
Definition: configuration.h:96
int default_border_width
i3Font font
Definition: configuration.h:93
struct Config::config_client client
char * fake_outputs
Overwrites output detection (for testing), see src/fake_outputs.c.
int default_floating_border_width
int default_orientation
Default orientation for new containers.
char * ipc_socket_path
Definition: configuration.h:95
bool show_marks
Specifies whether or not marks should be displayed in the window decoration.
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
struct Config::config_bar bar
border_style_t default_border
The default border style for new windows.
border_style_t default_floating_border
The default border style for new floating windows.
struct Colortriple focused
struct Colortriple placeholder
struct Colortriple unfocused
struct Colortriple urgent
struct Colortriple focused_inactive
struct Colortriple focused
struct Colortriple unfocused
struct Colortriple urgent
Holds the status bar configuration (i3bar).
struct Barconfig::bar_colors colors
char * i3bar_command
Command that should be run to execute i3bar, give a full path if i3bar is not in your $PATH.
int num_outputs
Number of outputs in the outputs array.
char * font
Font specification for all text rendered on the bar.
char * id
Automatically generated ID for this bar config.
char * status_command
Command that should be run to get a statusline, for example 'i3status'.
char ** outputs
Outputs on which this bar should show up on.
char * socket_path
Path to the i3 IPC socket.
Defines a mouse command to be executed instead of the default behavior when clicking on the non-statu...
char * command
The command which is to be executed for this button.
Stores which workspace (by name or number) goes to which output.
Definition: data.h:205
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:275
Assignment ** ran_assignments
Definition: data.h:404
uint32_t nr_assignments
Pointers to the Assignments which were already ran for this Window (assignments run only once)
Definition: data.h:403
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:555
Match match
the criteria to check if a window matches
Definition: data.h:577
@ A_COMMAND
Definition: data.h:569
@ A_TO_WORKSPACE
Definition: data.h:570
@ A_TO_WORKSPACE_NUMBER
Definition: data.h:572
@ A_TO_OUTPUT
Definition: data.h:573
char * output
Definition: data.h:583
union Assignment::@19 dest
destination workspace/command/output, depending on the type
char * command
Definition: data.h:581
char * workspace
Definition: data.h:582
enum Assignment::@18 type
type of this assignment:
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:604
struct Window * window
Definition: data.h:671
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition: data.h:677
enum Font::@23 type
The type of font.