i3
config.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved 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 <libgen.h>
14#include <unistd.h>
15
16#include <xkbcommon/xkbcommon.h>
17
18char *current_configpath = NULL;
20struct modes_head modes;
23
24/*
25 * Ungrabs all keys, to be called before re-grabbing the keys because of a
26 * mapping_notify event or a configuration file reload
27 *
28 */
29void ungrab_all_keys(xcb_connection_t *conn) {
30 DLOG("Ungrabbing all keys\n");
31 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
32}
33
34static void free_configuration(void) {
35 assert(conn != NULL);
36
37 /* If we are currently in a binding mode, we first revert to the default
38 * since we have no guarantee that the current mode will even still exist
39 * after parsing the config again. See #2228. */
40 switch_mode("default");
41
42 /* First ungrab the keys */
44
45 struct Mode *mode;
46 while (!SLIST_EMPTY(&modes)) {
47 mode = SLIST_FIRST(&modes);
48 FREE(mode->name);
49
50 /* Clear the old binding list */
51 while (!TAILQ_EMPTY(mode->bindings)) {
52 Binding *bind = TAILQ_FIRST(mode->bindings);
53 TAILQ_REMOVE(mode->bindings, bind, bindings);
54 binding_free(bind);
55 }
56 FREE(mode->bindings);
57
58 SLIST_REMOVE(&modes, mode, Mode, modes);
59 FREE(mode);
60 }
61
62 while (!TAILQ_EMPTY(&assignments)) {
63 struct Assignment *assign = TAILQ_FIRST(&assignments);
64 if (assign->type == A_TO_WORKSPACE || assign->type == A_TO_WORKSPACE_NUMBER) {
65 FREE(assign->dest.workspace);
66 } else if (assign->type == A_COMMAND) {
67 FREE(assign->dest.command);
68 } else if (assign->type == A_TO_OUTPUT) {
69 FREE(assign->dest.output);
70 }
71 match_free(&(assign->match));
73 FREE(assign);
74 }
75
76 while (!TAILQ_EMPTY(&ws_assignments)) {
78 FREE(assign->name);
79 FREE(assign->output);
81 FREE(assign);
82 }
83
84 /* Clear bar configs */
85 Barconfig *barconfig;
86 while (!TAILQ_EMPTY(&barconfigs)) {
87 barconfig = TAILQ_FIRST(&barconfigs);
88 FREE(barconfig->id);
89 for (int c = 0; c < barconfig->num_outputs; c++) {
90 free(barconfig->outputs[c]);
91 }
92
93 while (!TAILQ_EMPTY(&(barconfig->bar_bindings))) {
94 struct Barbinding *binding = TAILQ_FIRST(&(barconfig->bar_bindings));
95 FREE(binding->command);
96 TAILQ_REMOVE(&(barconfig->bar_bindings), binding, bindings);
97 FREE(binding);
98 }
99
100 while (!TAILQ_EMPTY(&(barconfig->tray_outputs))) {
101 struct tray_output_t *tray_output = TAILQ_FIRST(&(barconfig->tray_outputs));
102 FREE(tray_output->output);
103 TAILQ_REMOVE(&(barconfig->tray_outputs), tray_output, tray_outputs);
104 FREE(tray_output);
105 }
106
107 FREE(barconfig->outputs);
108 FREE(barconfig->socket_path);
109 FREE(barconfig->status_command);
110 FREE(barconfig->workspace_command);
111 FREE(barconfig->i3bar_command);
112 FREE(barconfig->font);
113 FREE(barconfig->colors.background);
114 FREE(barconfig->colors.statusline);
115 FREE(barconfig->colors.separator);
116 FREE(barconfig->colors.focused_background);
117 FREE(barconfig->colors.focused_statusline);
118 FREE(barconfig->colors.focused_separator);
120 FREE(barconfig->colors.focused_workspace_bg);
123 FREE(barconfig->colors.active_workspace_bg);
129 FREE(barconfig->colors.urgent_workspace_bg);
131 FREE(barconfig->colors.binding_mode_border);
132 FREE(barconfig->colors.binding_mode_bg);
133 FREE(barconfig->colors.binding_mode_text);
134 TAILQ_REMOVE(&barconfigs, barconfig, configs);
135 FREE(barconfig);
136 }
137
138 Con *con;
140 /* Assignments changed, previously ran assignments are invalid. */
141 if (con->window) {
142 con->window->nr_assignments = 0;
144 }
145 /* Invalidate pixmap caches in case font or colors changed. */
147 }
148
149 /* Get rid of the current font */
150 free_font();
151
154 free(config.fake_outputs);
155}
156
157/*
158 * (Re-)loads the configuration file (sets useful defaults before).
159 *
160 * If you specify override_configpath, only this path is used to look for a
161 * configuration file.
162 *
163 * load_type specifies the type of loading: C_VALIDATE is used to only verify
164 * the correctness of the config file (used with the flag -C). C_LOAD will load
165 * the config for normal use and display errors in the nagbar. C_RELOAD will
166 * also clear the previous config.
167 *
168 */
169bool load_configuration(const char *override_configpath, config_load_t load_type) {
170 if (load_type == C_RELOAD) {
172 }
173
175
176 struct Mode *default_mode = scalloc(1, sizeof(struct Mode));
177 default_mode->name = sstrdup("default");
178 default_mode->bindings = scalloc(1, sizeof(struct bindings_head));
179 TAILQ_INIT(default_mode->bindings);
180 SLIST_INSERT_HEAD(&modes, default_mode, modes);
181
182 bindings = default_mode->bindings;
183 current_binding_mode = default_mode->name;
184
185 /* Clear the old config or initialize the data structure */
186 memset(&config, 0, sizeof(config));
187
188 /* Initialize default colors */
189#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator) \
190 do { \
191 x.border = draw_util_hex_to_color(cborder); \
192 x.background = draw_util_hex_to_color(cbackground); \
193 x.text = draw_util_hex_to_color(ctext); \
194 x.indicator = draw_util_hex_to_color(cindicator); \
195 x.child_border = draw_util_hex_to_color(cbackground); \
196 } while (0)
197
199 INIT_COLOR(config.client.focused, "#4c7899", "#285577", "#ffffff", "#2e9ef4");
200 INIT_COLOR(config.client.focused_inactive, "#333333", "#5f676a", "#ffffff", "#484e50");
201 INIT_COLOR(config.client.unfocused, "#333333", "#222222", "#888888", "#292d2e");
202 INIT_COLOR(config.client.urgent, "#2f343a", "#900000", "#ffffff", "#900000");
204
205 /* border and indicator color are ignored for placeholder contents */
206 INIT_COLOR(config.client.placeholder, "#000000", "#0c0c0c", "#ffffff", "#000000");
207
208 /* the last argument (indicator color) is ignored for bar colors */
209 INIT_COLOR(config.bar.focused, "#4c7899", "#285577", "#ffffff", "#000000");
210 INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888", "#000000");
211 INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff", "#000000");
212
213 config.show_marks = true;
214
219 /* Set default_orientation to NO_ORIENTATION for auto orientation. */
221
222 config.gaps.inner = 0;
223 config.gaps.top = 0;
224 config.gaps.right = 0;
225 config.gaps.bottom = 0;
226 config.gaps.left = 0;
227
228 /* Set default urgency reset delay to 500ms */
231 }
232
234
236 config.swap_modifier = XCB_KEY_BUT_MASK_SHIFT;
237
239 current_configpath = get_config_path(override_configpath, true);
240 if (current_configpath == NULL) {
241 die("Unable to find the configuration file (looked at "
242 "$XDG_CONFIG_HOME/i3/config, ~/.i3/config, $XDG_CONFIG_DIRS/i3/config "
243 "and " SYSCONFDIR "/i3/config)");
244 }
245
246 IncludedFile *file;
247 while (!TAILQ_EMPTY(&included_files)) {
249 FREE(file->path);
250 FREE(file->raw_contents);
252 TAILQ_REMOVE(&included_files, file, files);
253 FREE(file);
254 }
255
256 char resolved_path[PATH_MAX] = {'\0'};
257 if (realpath(current_configpath, resolved_path) == NULL) {
258 die("realpath(%s): %s", current_configpath, strerror(errno));
259 }
260
261 file = scalloc(1, sizeof(IncludedFile));
262 file->path = sstrdup(resolved_path);
263 TAILQ_INSERT_TAIL(&included_files, file, files);
264
265 LOG("Parsing configfile %s\n", resolved_path);
266 struct stack stack;
267 memset(&stack, '\0', sizeof(struct stack));
268 struct parser_ctx ctx = {
269 .use_nagbar = (load_type != C_VALIDATE),
270 .stack = &stack,
271 };
272 SLIST_INIT(&(ctx.variables));
273 const int result = parse_file(&ctx, resolved_path, file);
275 if (result == -1) {
276 die("Could not open configuration file: %s\n", strerror(errno));
277 }
278
281
282 if (config.font.type == FONT_TYPE_NONE && load_type != C_VALIDATE) {
283 ELOG("You did not specify required configuration option \"font\"\n");
284 config.font = load_font("fixed", true);
286 }
287
288 /* Make bar config blocks without a configured font use the i3-wide font. */
289 Barconfig *current;
290 if (load_type != C_VALIDATE) {
291 TAILQ_FOREACH (current, &barconfigs, configs) {
292 if (current->font != NULL) {
293 continue;
294 }
295 current->font = sstrdup(config.font.pattern);
296 }
297 }
298
299 if (load_type == C_RELOAD) {
304
305 /* Redraw the currently visible decorations on reload, so that the
306 * possibly new drawing parameters changed. */
307 tree_render();
308 }
309
310 return result == 0;
311}
void binding_free(Binding *bind)
Frees the binding.
Definition bindings.c:836
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:155
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:187
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition bindings.c:639
void reorder_bindings(void)
Reorders bindings by event_state_mask descendingly so that get_binding() correctly matches more speci...
Definition bindings.c:732
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition bindings.c:449
static void free_configuration(void)
Definition config.c:34
struct includedfiles_head included_files
Definition config.c:22
Config config
Definition config.c:19
bool load_configuration(const char *override_configpath, config_load_t load_type)
(Re-)loads the configuration file (sets useful defaults before).
Definition config.c:169
struct barconfig_head barconfigs
Definition config.c:21
struct modes_head modes
Definition config.c:20
#define INIT_COLOR(x, cborder, cbackground, ctext, cindicator)
char * current_configpath
Definition config.c:18
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:29
parse_file_result_t parse_file(struct parser_ctx *ctx, const char *f, IncludedFile *included_file)
Parses the given file by first replacing the variables, then calling parse_config and launching i3-na...
void free_variables(struct parser_ctx *ctx)
Releases the memory of all variables in ctx.
void gaps_reapply_workspace_assignments(void)
Re-applies all workspace gap assignments to existing workspaces after reloading the configuration fil...
Definition gaps.c:166
void match_free(Match *match)
Frees the given match.
Definition match.c:279
struct all_cons_head all_cons
Definition tree.c:15
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition tree.c:455
void extract_workspace_names_from_bindings(void)
Extracts workspace names from keybindings (e.g.
Definition workspace.c:181
static xcb_cursor_context_t * ctx
Definition xcursor.c:19
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
xcb_window_t root
Definition main.c:67
const char * current_binding_mode
Definition main.c:88
struct assignments_head assignments
Definition main.c:97
struct ws_assignments_head ws_assignments
Definition main.c:101
struct bindings_head * bindings
Definition main.c:87
config_load_t
@ C_VALIDATE
@ C_RELOAD
@ FOCUS_WRAPPING_ON
Definition data.h:170
@ NO_ORIENTATION
Definition data.h:60
@ BS_NORMAL
Definition data.h:68
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define ELOG(fmt,...)
Definition libi3.h:100
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...
color_t draw_util_hex_to_color(const char *color)
Parses the given color in hex format to an internal color representation.
char * get_config_path(const char *override_configpath, bool use_system_paths)
Get the path of the first configuration file found.
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 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 TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:376
#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
@ TILING_DRAG_MODIFIER
Definition tiling_drag.h:19
#define die(...)
Definition util.h:19
#define FREE(pointer)
Definition util.h:47
List entry struct for an included file.
char * variable_replaced_contents
char * raw_contents
The configuration file can contain multiple sets of bindings.
char * name
struct bindings_head * bindings
Holds part of the configuration (the part which is not already in dedicated structures in include/dat...
focus_wrapping_t focus_wrapping
When focus wrapping is enabled (the default), attempting to move focus past the edge of the screen (i...
gaps_t gaps
char * restart_state_path
tiling_drag_t tiling_drag
uint32_t swap_modifier
The modifier which needs to be pressed in combination with the floating modifier and your mouse butto...
int default_border_width
i3Font font
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
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 * workspace_command
Command that should be run to get the workspace buttons.
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.
int inner
Definition data.h:147
int left
Definition data.h:151
int right
Definition data.h:149
int top
Definition data.h:148
int bottom
Definition data.h:150
Stores which workspace (by name or number) goes to which output and its gaps config.
Definition data.h:235
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition data.h:306
Assignment ** ran_assignments
Definition data.h:435
uint32_t nr_assignments
Pointers to the Assignments which were already ran for this Window (assignments run only once)
Definition data.h:434
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition data.h:594
Match match
the criteria to check if a window matches
Definition data.h:616
union Assignment::@17 dest
destination workspace/command/output, depending on the type
char * output
Definition data.h:622
char * command
Definition data.h:620
char * workspace
Definition data.h:621
enum Assignment::@16 type
type of this assignment:
@ A_COMMAND
Definition data.h:608
@ A_TO_WORKSPACE
Definition data.h:609
@ A_TO_WORKSPACE_NUMBER
Definition data.h:611
@ A_TO_OUTPUT
Definition data.h:612
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Window * window
Definition data.h:718
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition data.h:724
char * pattern
The pattern/name used to load the font.
Definition libi3.h:71
enum Font::@21 type
The type of font.