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(focus_on_window_activation, const char *mode) {
358 if (strcmp(mode, "smart") == 0)
359 config.focus_on_window_activation = FOWA_SMART;
360 else if (strcmp(mode, "urgent") == 0)
361 config.focus_on_window_activation = FOWA_URGENT;
362 else if (strcmp(mode, "focus") == 0)
363 config.focus_on_window_activation = FOWA_FOCUS;
364 else if (strcmp(mode, "none") == 0)
365 config.focus_on_window_activation = FOWA_NONE;
367 ELOG("Unknown focus_on_window_activation mode \"%s\", ignoring it.\n", mode);
371 DLOG("Set new focus_on_window_activation mode = %i", config.focus_on_window_activation);
374 CFGFUN(show_marks, const char *value) {
375 config.show_marks = eval_boolstr(value);
378 CFGFUN(workspace, const char *workspace, const char *output) {
379 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
380 /* Check for earlier assignments of the same workspace so that we
381 * don’t have assignments of a single workspace to different
383 struct Workspace_Assignment *assignment;
384 bool duplicate = false;
385 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
386 if (strcasecmp(assignment->name, workspace) == 0) {
387 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
389 assignment->output = sstrdup(output);
394 assignment = scalloc(sizeof(struct Workspace_Assignment));
395 assignment->name = sstrdup(workspace);
396 assignment->output = sstrdup(output);
397 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
401 CFGFUN(ipc_socket, const char *path) {
402 config.ipc_socket_path = sstrdup(path);
405 CFGFUN(restart_state, const char *path) {
406 config.restart_state_path = sstrdup(path);
409 CFGFUN(popup_during_fullscreen, const char *value) {
410 if (strcmp(value, "ignore") == 0) {
411 config.popup_during_fullscreen = PDF_IGNORE;
412 } else if (strcmp(value, "leave_fullscreen") == 0) {
413 config.popup_during_fullscreen = PDF_LEAVE_FULLSCREEN;
415 config.popup_during_fullscreen = PDF_SMART;
419 CFGFUN(color_single, const char *colorclass, const char *color) {
420 /* used for client.background only currently */
421 config.client.background = get_colorpixel(color);
424 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
425 #define APPLY_COLORS(classname) \
427 if (strcmp(colorclass, "client." #classname) == 0) { \
428 config.client.classname.border = get_colorpixel(border); \
429 config.client.classname.background = get_colorpixel(background); \
430 config.client.classname.text = get_colorpixel(text); \
431 if (indicator != NULL) { \
432 config.client.classname.indicator = get_colorpixel(indicator); \
437 APPLY_COLORS(focused_inactive);
438 APPLY_COLORS(focused);
439 APPLY_COLORS(unfocused);
440 APPLY_COLORS(urgent);
441 APPLY_COLORS(placeholder);
446 CFGFUN(assign, const char *workspace) {
447 if (match_is_empty(current_match)) {
448 ELOG("Match is empty, ignoring this assignment\n");
451 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
452 Assignment *assignment = scalloc(sizeof(Assignment));
453 match_copy(&(assignment->match), current_match);
454 assignment->type = A_TO_WORKSPACE;
455 assignment->dest.workspace = sstrdup(workspace);
456 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
460 if (match_is_empty(current_match)) {
461 ELOG("Match is empty, ignoring this assignment\n");
465 DLOG("new assignment, using above criteria, to ignore focus on manage");
466 Assignment *assignment = scalloc(sizeof(Assignment));
467 match_copy(&(assignment->match), current_match);
468 assignment->type = A_NO_FOCUS;
469 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
472 /*******************************************************************************
473 * Bar configuration (i3bar)
474 ******************************************************************************/
476 static Barconfig current_bar;
478 CFGFUN(bar_font, const char *font) {
479 FREE(current_bar.font);
480 current_bar.font = sstrdup(font);
483 CFGFUN(bar_separator_symbol, const char *separator) {
484 FREE(current_bar.separator_symbol);
485 current_bar.separator_symbol = sstrdup(separator);
488 CFGFUN(bar_mode, const char *mode) {
489 current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
492 CFGFUN(bar_hidden_state, const char *hidden_state) {
493 current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
496 CFGFUN(bar_id, const char *bar_id) {
497 current_bar.id = sstrdup(bar_id);
500 CFGFUN(bar_output, const char *output) {
501 int new_outputs = current_bar.num_outputs + 1;
502 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char *) * new_outputs);
503 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
504 current_bar.num_outputs = new_outputs;
507 CFGFUN(bar_verbose, const char *verbose) {
508 current_bar.verbose = eval_boolstr(verbose);
511 CFGFUN(bar_modifier, const char *modifier) {
512 if (strcmp(modifier, "Mod1") == 0)
513 current_bar.modifier = M_MOD1;
514 else if (strcmp(modifier, "Mod2") == 0)
515 current_bar.modifier = M_MOD2;
516 else if (strcmp(modifier, "Mod3") == 0)
517 current_bar.modifier = M_MOD3;
518 else if (strcmp(modifier, "Mod4") == 0)
519 current_bar.modifier = M_MOD4;
520 else if (strcmp(modifier, "Mod5") == 0)
521 current_bar.modifier = M_MOD5;
522 else if (strcmp(modifier, "Control") == 0 ||
523 strcmp(modifier, "Ctrl") == 0)
524 current_bar.modifier = M_CONTROL;
525 else if (strcmp(modifier, "Shift") == 0)
526 current_bar.modifier = M_SHIFT;
529 CFGFUN(bar_wheel_up_cmd, const char *command) {
530 FREE(current_bar.wheel_up_cmd);
531 current_bar.wheel_up_cmd = sstrdup(command);
534 CFGFUN(bar_wheel_down_cmd, const char *command) {
535 FREE(current_bar.wheel_down_cmd);
536 current_bar.wheel_down_cmd = sstrdup(command);
539 CFGFUN(bar_position, const char *position) {
540 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
543 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
544 FREE(current_bar.i3bar_command);
545 current_bar.i3bar_command = sstrdup(i3bar_command);
548 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
549 #define APPLY_COLORS(classname) \
551 if (strcmp(colorclass, #classname) == 0) { \
552 if (text != NULL) { \
553 /* New syntax: border, background, text */ \
554 current_bar.colors.classname##_border = sstrdup(border); \
555 current_bar.colors.classname##_bg = sstrdup(background); \
556 current_bar.colors.classname##_text = sstrdup(text); \
558 /* Old syntax: text, background */ \
559 current_bar.colors.classname##_bg = sstrdup(background); \
560 current_bar.colors.classname##_text = sstrdup(border); \
565 APPLY_COLORS(focused_workspace);
566 APPLY_COLORS(active_workspace);
567 APPLY_COLORS(inactive_workspace);
568 APPLY_COLORS(urgent_workspace);
573 CFGFUN(bar_socket_path, const char *socket_path) {
574 FREE(current_bar.socket_path);
575 current_bar.socket_path = sstrdup(socket_path);
578 CFGFUN(bar_tray_output, const char *output) {
579 FREE(current_bar.tray_output);
580 current_bar.tray_output = sstrdup(output);
583 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
584 if (strcmp(colorclass, "background") == 0)
585 current_bar.colors.background = sstrdup(color);
586 else if (strcmp(colorclass, "separator") == 0)
587 current_bar.colors.separator = sstrdup(color);
589 current_bar.colors.statusline = sstrdup(color);
592 CFGFUN(bar_status_command, const char *command) {
593 FREE(current_bar.status_command);
594 current_bar.status_command = sstrdup(command);
597 CFGFUN(bar_binding_mode_indicator, const char *value) {
598 current_bar.hide_binding_mode_indicator = !eval_boolstr(value);
601 CFGFUN(bar_workspace_buttons, const char *value) {
602 current_bar.hide_workspace_buttons = !eval_boolstr(value);
605 CFGFUN(bar_strip_workspace_numbers, const char *value) {
606 current_bar.strip_workspace_numbers = eval_boolstr(value);
610 DLOG("\t new bar configuration finished, saving.\n");
611 /* Generate a unique ID for this bar if not already configured */
613 sasprintf(¤t_bar.id, "bar-%d", config.number_barconfigs);
615 config.number_barconfigs++;
617 /* If no font was explicitly set, we use the i3 font as default */
618 if (!current_bar.font && font_pattern)
619 current_bar.font = sstrdup(font_pattern);
621 /* Copy the current (static) structure into a dynamically allocated
622 * one, then cleanup our static one. */
623 Barconfig *bar_config = scalloc(sizeof(Barconfig));
624 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
625 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
627 memset(¤t_bar, '\0', sizeof(Barconfig));