2 #define I3__FILE__ "config_directives.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * config_directives.c: all config storing functions (see config_parser.c)
17 /*******************************************************************************
19 ******************************************************************************/
21 static int criteria_next_state;
24 * Initializes the specified 'Match' data structure and the initial state of
25 * commands.c for matching target windows of a command.
28 CFGFUN(criteria_init, int _state) {
29 criteria_next_state = _state;
31 DLOG("Initializing criteria, current_match = %p, state = %d\n", current_match, _state);
32 match_init(current_match);
35 CFGFUN(criteria_pop_state) {
36 result->next_state = criteria_next_state;
40 * Interprets a ctype=cvalue pair and adds it to the current match
44 CFGFUN(criteria_add, const char *ctype, const char *cvalue) {
45 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
47 if (strcmp(ctype, "class") == 0) {
48 current_match->class = regex_new(cvalue);
52 if (strcmp(ctype, "instance") == 0) {
53 current_match->instance = regex_new(cvalue);
57 if (strcmp(ctype, "window_role") == 0) {
58 current_match->window_role = regex_new(cvalue);
62 if (strcmp(ctype, "con_id") == 0) {
64 long parsed = strtol(cvalue, &end, 10);
65 if (parsed == LONG_MIN ||
68 (end && *end != '\0')) {
69 ELOG("Could not parse con id \"%s\"\n", cvalue);
71 current_match->con_id = (Con *)parsed;
72 DLOG("id as int = %p\n", current_match->con_id);
77 if (strcmp(ctype, "id") == 0) {
79 long parsed = strtol(cvalue, &end, 10);
80 if (parsed == LONG_MIN ||
83 (end && *end != '\0')) {
84 ELOG("Could not parse window id \"%s\"\n", cvalue);
86 current_match->id = parsed;
87 DLOG("window id as int = %d\n", current_match->id);
92 if (strcmp(ctype, "window_type") == 0) {
93 if (strcasecmp(cvalue, "normal") == 0)
94 current_match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
95 else if (strcasecmp(cvalue, "dialog") == 0)
96 current_match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
97 else if (strcasecmp(cvalue, "utility") == 0)
98 current_match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
99 else if (strcasecmp(cvalue, "toolbar") == 0)
100 current_match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
101 else if (strcasecmp(cvalue, "splash") == 0)
102 current_match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
103 else if (strcasecmp(cvalue, "menu") == 0)
104 current_match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
105 else if (strcasecmp(cvalue, "dropdown_menu") == 0)
106 current_match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
107 else if (strcasecmp(cvalue, "popup_menu") == 0)
108 current_match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
109 else if (strcasecmp(cvalue, "tooltip") == 0)
110 current_match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
112 ELOG("unknown window_type value \"%s\"\n", cvalue);
117 if (strcmp(ctype, "con_mark") == 0) {
118 current_match->mark = regex_new(cvalue);
122 if (strcmp(ctype, "title") == 0) {
123 current_match->title = regex_new(cvalue);
127 if (strcmp(ctype, "urgent") == 0) {
128 if (strcasecmp(cvalue, "latest") == 0 ||
129 strcasecmp(cvalue, "newest") == 0 ||
130 strcasecmp(cvalue, "recent") == 0 ||
131 strcasecmp(cvalue, "last") == 0) {
132 current_match->urgent = U_LATEST;
133 } else if (strcasecmp(cvalue, "oldest") == 0 ||
134 strcasecmp(cvalue, "first") == 0) {
135 current_match->urgent = U_OLDEST;
140 ELOG("Unknown criterion: %s\n", ctype);
143 /* TODO: refactor the above criteria code into a single file (with src/commands.c). */
145 /*******************************************************************************
147 ******************************************************************************/
149 static bool eval_boolstr(const char *str) {
150 return (strcasecmp(str, "1") == 0 ||
151 strcasecmp(str, "yes") == 0 ||
152 strcasecmp(str, "true") == 0 ||
153 strcasecmp(str, "on") == 0 ||
154 strcasecmp(str, "enable") == 0 ||
155 strcasecmp(str, "active") == 0);
159 * A utility function to convert a string of modifiers to the corresponding bit
162 uint32_t modifiers_from_str(const char *str) {
163 /* It might be better to use strtok() here, but the simpler strstr() should
168 if (strstr(str, "Mod1") != NULL)
170 if (strstr(str, "Mod2") != NULL)
172 if (strstr(str, "Mod3") != NULL)
174 if (strstr(str, "Mod4") != NULL)
176 if (strstr(str, "Mod5") != NULL)
178 if (strstr(str, "Control") != NULL ||
179 strstr(str, "Ctrl") != NULL)
180 result |= BIND_CONTROL;
181 if (strstr(str, "Shift") != NULL)
182 result |= BIND_SHIFT;
183 if (strstr(str, "Mode_switch") != NULL)
184 result |= BIND_MODE_SWITCH;
188 static char *font_pattern;
190 CFGFUN(font, const char *font) {
191 config.font = load_font(font, true);
192 set_font(&config.font);
194 /* Save the font pattern for using it as bar font later on */
196 font_pattern = sstrdup(font);
199 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command) {
200 configure_binding(bindtype, modifiers, key, release, border, whole_window, command, DEFAULT_BINDING_MODE);
203 /*******************************************************************************
205 ******************************************************************************/
207 static char *current_mode;
209 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *border, const char *whole_window, const char *command) {
210 configure_binding(bindtype, modifiers, key, release, border, whole_window, command, current_mode);
213 CFGFUN(enter_mode, const char *modename) {
214 if (strcasecmp(modename, DEFAULT_BINDING_MODE) == 0) {
215 ELOG("You cannot use the name %s for your mode\n", DEFAULT_BINDING_MODE);
218 DLOG("\t now in mode %s\n", modename);
220 current_mode = sstrdup(modename);
223 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
224 struct Autostart *new = smalloc(sizeof(struct Autostart));
225 new->command = sstrdup(command);
226 new->no_startup_id = (no_startup_id != NULL);
227 if (strcmp(exectype, "exec") == 0) {
228 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
230 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
234 CFGFUN(for_window, const char *command) {
235 if (match_is_empty(current_match)) {
236 ELOG("Match is empty, ignoring this for_window statement\n");
239 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
240 Assignment *assignment = scalloc(sizeof(Assignment));
241 assignment->type = A_COMMAND;
242 match_copy(&(assignment->match), current_match);
243 assignment->dest.command = sstrdup(command);
244 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
247 CFGFUN(floating_minimum_size, const long width, const long height) {
248 config.floating_minimum_width = width;
249 config.floating_minimum_height = height;
252 CFGFUN(floating_maximum_size, const long width, const long height) {
253 config.floating_maximum_width = width;
254 config.floating_maximum_height = height;
257 CFGFUN(floating_modifier, const char *modifiers) {
258 config.floating_modifier = modifiers_from_str(modifiers);
261 CFGFUN(default_orientation, const char *orientation) {
262 if (strcmp(orientation, "horizontal") == 0)
263 config.default_orientation = HORIZ;
264 else if (strcmp(orientation, "vertical") == 0)
265 config.default_orientation = VERT;
267 config.default_orientation = NO_ORIENTATION;
270 CFGFUN(workspace_layout, const char *layout) {
271 if (strcmp(layout, "default") == 0)
272 config.default_layout = L_DEFAULT;
273 else if (strcmp(layout, "stacking") == 0 ||
274 strcmp(layout, "stacked") == 0)
275 config.default_layout = L_STACKED;
277 config.default_layout = L_TABBED;
280 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
284 if (strcmp(border, "1pixel") == 0) {
285 border_style = BS_PIXEL;
287 } else if (strcmp(border, "none") == 0) {
288 border_style = BS_NONE;
290 } else if (strcmp(border, "pixel") == 0) {
291 border_style = BS_PIXEL;
292 border_width = width;
294 border_style = BS_NORMAL;
295 border_width = width;
298 if (strcmp(windowtype, "new_window") == 0) {
299 DLOG("default tiled border style = %d and border width = %d (%d physical px)\n",
300 border_style, border_width, logical_px(border_width));
301 config.default_border = border_style;
302 config.default_border_width = logical_px(border_width);
304 DLOG("default floating border style = %d and border width = %d (%d physical px)\n",
305 border_style, border_width, logical_px(border_width));
306 config.default_floating_border = border_style;
307 config.default_floating_border_width = logical_px(border_width);
311 CFGFUN(hide_edge_borders, const char *borders) {
312 if (strcmp(borders, "vertical") == 0)
313 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
314 else if (strcmp(borders, "horizontal") == 0)
315 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
316 else if (strcmp(borders, "both") == 0)
317 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
318 else if (strcmp(borders, "none") == 0)
319 config.hide_edge_borders = ADJ_NONE;
320 else if (eval_boolstr(borders))
321 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
323 config.hide_edge_borders = ADJ_NONE;
326 CFGFUN(focus_follows_mouse, const char *value) {
327 config.disable_focus_follows_mouse = !eval_boolstr(value);
330 CFGFUN(mouse_warping, const char *value) {
331 if (strcmp(value, "none") == 0)
332 config.mouse_warping = POINTER_WARPING_NONE;
333 else if (strcmp(value, "output") == 0)
334 config.mouse_warping = POINTER_WARPING_OUTPUT;
337 CFGFUN(force_xinerama, const char *value) {
338 config.force_xinerama = eval_boolstr(value);
341 CFGFUN(force_focus_wrapping, const char *value) {
342 config.force_focus_wrapping = eval_boolstr(value);
345 CFGFUN(workspace_back_and_forth, const char *value) {
346 config.workspace_auto_back_and_forth = eval_boolstr(value);
349 CFGFUN(fake_outputs, const char *outputs) {
350 config.fake_outputs = sstrdup(outputs);
353 CFGFUN(force_display_urgency_hint, const long duration_ms) {
354 config.workspace_urgency_timer = duration_ms / 1000.0;
357 CFGFUN(delay_exit_on_zero_displays, const long duration_ms) {
358 config.zero_disp_exit_timer_ms = duration_ms;
361 CFGFUN(focus_on_window_activation, const char *mode) {
362 if (strcmp(mode, "smart") == 0)
363 config.focus_on_window_activation = FOWA_SMART;
364 else if (strcmp(mode, "urgent") == 0)
365 config.focus_on_window_activation = FOWA_URGENT;
366 else if (strcmp(mode, "focus") == 0)
367 config.focus_on_window_activation = FOWA_FOCUS;
368 else if (strcmp(mode, "none") == 0)
369 config.focus_on_window_activation = FOWA_NONE;
371 ELOG("Unknown focus_on_window_activation mode \"%s\", ignoring it.\n", mode);
375 DLOG("Set new focus_on_window_activation mode = %i", config.focus_on_window_activation);
378 CFGFUN(show_marks, const char *value) {
379 config.show_marks = eval_boolstr(value);
382 CFGFUN(workspace, const char *workspace, const char *output) {
383 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
384 /* Check for earlier assignments of the same workspace so that we
385 * don’t have assignments of a single workspace to different
387 struct Workspace_Assignment *assignment;
388 bool duplicate = false;
389 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
390 if (strcasecmp(assignment->name, workspace) == 0) {
391 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
393 assignment->output = sstrdup(output);
398 assignment = scalloc(sizeof(struct Workspace_Assignment));
399 assignment->name = sstrdup(workspace);
400 assignment->output = sstrdup(output);
401 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
405 CFGFUN(ipc_socket, const char *path) {
406 config.ipc_socket_path = sstrdup(path);
409 CFGFUN(restart_state, const char *path) {
410 config.restart_state_path = sstrdup(path);
413 CFGFUN(popup_during_fullscreen, const char *value) {
414 if (strcmp(value, "ignore") == 0) {
415 config.popup_during_fullscreen = PDF_IGNORE;
416 } else if (strcmp(value, "leave_fullscreen") == 0) {
417 config.popup_during_fullscreen = PDF_LEAVE_FULLSCREEN;
419 config.popup_during_fullscreen = PDF_SMART;
423 CFGFUN(color_single, const char *colorclass, const char *color) {
424 /* used for client.background only currently */
425 config.client.background = get_colorpixel(color);
428 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
429 #define APPLY_COLORS(classname) \
431 if (strcmp(colorclass, "client." #classname) == 0) { \
432 config.client.classname.border = get_colorpixel(border); \
433 config.client.classname.background = get_colorpixel(background); \
434 config.client.classname.text = get_colorpixel(text); \
435 if (indicator != NULL) { \
436 config.client.classname.indicator = get_colorpixel(indicator); \
441 APPLY_COLORS(focused_inactive);
442 APPLY_COLORS(focused);
443 APPLY_COLORS(unfocused);
444 APPLY_COLORS(urgent);
445 APPLY_COLORS(placeholder);
450 CFGFUN(assign, const char *workspace) {
451 if (match_is_empty(current_match)) {
452 ELOG("Match is empty, ignoring this assignment\n");
455 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
456 Assignment *assignment = scalloc(sizeof(Assignment));
457 match_copy(&(assignment->match), current_match);
458 assignment->type = A_TO_WORKSPACE;
459 assignment->dest.workspace = sstrdup(workspace);
460 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
464 if (match_is_empty(current_match)) {
465 ELOG("Match is empty, ignoring this assignment\n");
469 DLOG("new assignment, using above criteria, to ignore focus on manage");
470 Assignment *assignment = scalloc(sizeof(Assignment));
471 match_copy(&(assignment->match), current_match);
472 assignment->type = A_NO_FOCUS;
473 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
476 /*******************************************************************************
477 * Bar configuration (i3bar)
478 ******************************************************************************/
480 static Barconfig current_bar;
482 CFGFUN(bar_font, const char *font) {
483 FREE(current_bar.font);
484 current_bar.font = sstrdup(font);
487 CFGFUN(bar_separator_symbol, const char *separator) {
488 FREE(current_bar.separator_symbol);
489 current_bar.separator_symbol = sstrdup(separator);
492 CFGFUN(bar_mode, const char *mode) {
493 current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
496 CFGFUN(bar_hidden_state, const char *hidden_state) {
497 current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
500 CFGFUN(bar_id, const char *bar_id) {
501 current_bar.id = sstrdup(bar_id);
504 CFGFUN(bar_output, const char *output) {
505 int new_outputs = current_bar.num_outputs + 1;
506 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char *) * new_outputs);
507 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
508 current_bar.num_outputs = new_outputs;
511 CFGFUN(bar_verbose, const char *verbose) {
512 current_bar.verbose = eval_boolstr(verbose);
515 CFGFUN(bar_modifier, const char *modifier) {
516 if (strcmp(modifier, "Mod1") == 0)
517 current_bar.modifier = M_MOD1;
518 else if (strcmp(modifier, "Mod2") == 0)
519 current_bar.modifier = M_MOD2;
520 else if (strcmp(modifier, "Mod3") == 0)
521 current_bar.modifier = M_MOD3;
522 else if (strcmp(modifier, "Mod4") == 0)
523 current_bar.modifier = M_MOD4;
524 else if (strcmp(modifier, "Mod5") == 0)
525 current_bar.modifier = M_MOD5;
526 else if (strcmp(modifier, "Control") == 0 ||
527 strcmp(modifier, "Ctrl") == 0)
528 current_bar.modifier = M_CONTROL;
529 else if (strcmp(modifier, "Shift") == 0)
530 current_bar.modifier = M_SHIFT;
533 CFGFUN(bar_wheel_up_cmd, const char *command) {
534 FREE(current_bar.wheel_up_cmd);
535 current_bar.wheel_up_cmd = sstrdup(command);
538 CFGFUN(bar_wheel_down_cmd, const char *command) {
539 FREE(current_bar.wheel_down_cmd);
540 current_bar.wheel_down_cmd = sstrdup(command);
543 CFGFUN(bar_position, const char *position) {
544 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
547 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
548 FREE(current_bar.i3bar_command);
549 current_bar.i3bar_command = sstrdup(i3bar_command);
552 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
553 #define APPLY_COLORS(classname) \
555 if (strcmp(colorclass, #classname) == 0) { \
556 if (text != NULL) { \
557 /* New syntax: border, background, text */ \
558 current_bar.colors.classname##_border = sstrdup(border); \
559 current_bar.colors.classname##_bg = sstrdup(background); \
560 current_bar.colors.classname##_text = sstrdup(text); \
562 /* Old syntax: text, background */ \
563 current_bar.colors.classname##_bg = sstrdup(background); \
564 current_bar.colors.classname##_text = sstrdup(border); \
569 APPLY_COLORS(focused_workspace);
570 APPLY_COLORS(active_workspace);
571 APPLY_COLORS(inactive_workspace);
572 APPLY_COLORS(urgent_workspace);
573 APPLY_COLORS(binding_mode);
578 CFGFUN(bar_socket_path, const char *socket_path) {
579 FREE(current_bar.socket_path);
580 current_bar.socket_path = sstrdup(socket_path);
583 CFGFUN(bar_tray_output, const char *output) {
584 FREE(current_bar.tray_output);
585 current_bar.tray_output = sstrdup(output);
588 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
589 if (strcmp(colorclass, "background") == 0)
590 current_bar.colors.background = sstrdup(color);
591 else if (strcmp(colorclass, "separator") == 0)
592 current_bar.colors.separator = sstrdup(color);
594 current_bar.colors.statusline = sstrdup(color);
597 CFGFUN(bar_status_command, const char *command) {
598 FREE(current_bar.status_command);
599 current_bar.status_command = sstrdup(command);
602 CFGFUN(bar_binding_mode_indicator, const char *value) {
603 current_bar.hide_binding_mode_indicator = !eval_boolstr(value);
606 CFGFUN(bar_workspace_buttons, const char *value) {
607 current_bar.hide_workspace_buttons = !eval_boolstr(value);
610 CFGFUN(bar_strip_workspace_numbers, const char *value) {
611 current_bar.strip_workspace_numbers = eval_boolstr(value);
615 DLOG("\t new bar configuration finished, saving.\n");
616 /* Generate a unique ID for this bar if not already configured */
618 sasprintf(¤t_bar.id, "bar-%d", config.number_barconfigs);
620 config.number_barconfigs++;
622 /* If no font was explicitly set, we use the i3 font as default */
623 if (!current_bar.font && font_pattern)
624 current_bar.font = sstrdup(font_pattern);
626 /* Copy the current (static) structure into a dynamically allocated
627 * one, then cleanup our static one. */
628 Barconfig *bar_config = scalloc(sizeof(Barconfig));
629 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
630 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
632 memset(¤t_bar, '\0', sizeof(Barconfig));