2 #define I3__FILE__ "config_directives.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2012 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 printf("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 printf("window id as int = %d\n", current_match->id);
92 if (strcmp(ctype, "con_mark") == 0) {
93 current_match->mark = regex_new(cvalue);
97 if (strcmp(ctype, "title") == 0) {
98 current_match->title = regex_new(cvalue);
102 if (strcmp(ctype, "urgent") == 0) {
103 if (strcasecmp(cvalue, "latest") == 0 ||
104 strcasecmp(cvalue, "newest") == 0 ||
105 strcasecmp(cvalue, "recent") == 0 ||
106 strcasecmp(cvalue, "last") == 0) {
107 current_match->urgent = U_LATEST;
108 } else if (strcasecmp(cvalue, "oldest") == 0 ||
109 strcasecmp(cvalue, "first") == 0) {
110 current_match->urgent = U_OLDEST;
115 ELOG("Unknown criterion: %s\n", ctype);
118 /* TODO: refactor the above criteria code into a single file (with src/commands.c). */
120 /*******************************************************************************
122 ******************************************************************************/
124 static bool eval_boolstr(const char *str) {
125 return (strcasecmp(str, "1") == 0 ||
126 strcasecmp(str, "yes") == 0 ||
127 strcasecmp(str, "true") == 0 ||
128 strcasecmp(str, "on") == 0 ||
129 strcasecmp(str, "enable") == 0 ||
130 strcasecmp(str, "active") == 0);
134 * A utility function to convert a string of modifiers to the corresponding bit
137 uint32_t modifiers_from_str(const char *str) {
138 /* It might be better to use strtok() here, but the simpler strstr() should
143 if (strstr(str, "Mod1") != NULL)
145 if (strstr(str, "Mod2") != NULL)
147 if (strstr(str, "Mod3") != NULL)
149 if (strstr(str, "Mod4") != NULL)
151 if (strstr(str, "Mod5") != NULL)
153 if (strstr(str, "Control") != NULL ||
154 strstr(str, "Ctrl") != NULL)
155 result |= BIND_CONTROL;
156 if (strstr(str, "Shift") != NULL)
157 result |= BIND_SHIFT;
158 if (strstr(str, "Mode_switch") != NULL)
159 result |= BIND_MODE_SWITCH;
163 static char *font_pattern;
165 CFGFUN(font, const char *font) {
166 config.font = load_font(font, true);
167 set_font(&config.font);
169 /* Save the font pattern for using it as bar font later on */
171 font_pattern = sstrdup(font);
174 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
175 configure_binding(bindtype, modifiers, key, release, command, DEFAULT_BINDING_MODE);
179 /*******************************************************************************
181 ******************************************************************************/
183 static char *current_mode;
185 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
186 configure_binding(bindtype, modifiers, key, release, command, current_mode);
189 CFGFUN(enter_mode, const char *modename) {
190 if (strcasecmp(modename, DEFAULT_BINDING_MODE) == 0) {
191 ELOG("You cannot use the name %s for your mode\n", DEFAULT_BINDING_MODE);
194 DLOG("\t now in mode %s\n", modename);
196 current_mode = sstrdup(modename);
199 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
200 struct Autostart *new = smalloc(sizeof(struct Autostart));
201 new->command = sstrdup(command);
202 new->no_startup_id = (no_startup_id != NULL);
203 if (strcmp(exectype, "exec") == 0) {
204 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
206 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
210 CFGFUN(for_window, const char *command) {
211 if (match_is_empty(current_match)) {
212 ELOG("Match is empty, ignoring this for_window statement\n");
215 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
216 Assignment *assignment = scalloc(sizeof(Assignment));
217 assignment->type = A_COMMAND;
218 match_copy(&(assignment->match), current_match);
219 assignment->dest.command = sstrdup(command);
220 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
223 CFGFUN(floating_minimum_size, const long width, const long height) {
224 config.floating_minimum_width = width;
225 config.floating_minimum_height = height;
228 CFGFUN(floating_maximum_size, const long width, const long height) {
229 config.floating_maximum_width = width;
230 config.floating_maximum_height = height;
233 CFGFUN(floating_modifier, const char *modifiers) {
234 config.floating_modifier = modifiers_from_str(modifiers);
237 CFGFUN(default_orientation, const char *orientation) {
238 if (strcmp(orientation, "horizontal") == 0)
239 config.default_orientation = HORIZ;
240 else if (strcmp(orientation, "vertical") == 0)
241 config.default_orientation = VERT;
242 else config.default_orientation = NO_ORIENTATION;
245 CFGFUN(workspace_layout, const char *layout) {
246 if (strcmp(layout, "default") == 0)
247 config.default_layout = L_DEFAULT;
248 else if (strcmp(layout, "stacking") == 0 ||
249 strcmp(layout, "stacked") == 0)
250 config.default_layout = L_STACKED;
251 else config.default_layout = L_TABBED;
254 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
255 // FIXME: when using new_float *and* new_window with different border
256 // types, this breaks because default_border_width gets overwritten.
261 if (strcmp(border, "1pixel") == 0) {
262 border_style = BS_PIXEL;
264 } else if (strcmp(border, "none") == 0) {
265 border_style = BS_NONE;
267 } else if (strcmp(border, "pixel") == 0) {
268 border_style = BS_PIXEL;
269 border_width = width;
271 border_style = BS_NORMAL;
272 border_width = width;
275 if (strcmp(windowtype, "new_window") == 0) {
276 config.default_border = border_style;
278 config.default_floating_border = border_style;
281 config.default_border_width = border_width;
284 CFGFUN(hide_edge_borders, const char *borders) {
285 if (strcmp(borders, "vertical") == 0)
286 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
287 else if (strcmp(borders, "horizontal") == 0)
288 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
289 else if (strcmp(borders, "both") == 0)
290 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
291 else if (strcmp(borders, "none") == 0)
292 config.hide_edge_borders = ADJ_NONE;
293 else if (eval_boolstr(borders))
294 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
295 else config.hide_edge_borders = ADJ_NONE;
298 CFGFUN(focus_follows_mouse, const char *value) {
299 config.disable_focus_follows_mouse = !eval_boolstr(value);
302 CFGFUN(force_xinerama, const char *value) {
303 config.force_xinerama = eval_boolstr(value);
306 CFGFUN(force_focus_wrapping, const char *value) {
307 config.force_focus_wrapping = eval_boolstr(value);
310 CFGFUN(workspace_back_and_forth, const char *value) {
311 config.workspace_auto_back_and_forth = eval_boolstr(value);
314 CFGFUN(fake_outputs, const char *outputs) {
315 config.fake_outputs = sstrdup(outputs);
318 CFGFUN(force_display_urgency_hint, const long duration_ms) {
319 config.workspace_urgency_timer = duration_ms / 1000.0;
322 CFGFUN(workspace, const char *workspace, const char *output) {
323 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
324 /* Check for earlier assignments of the same workspace so that we
325 * don’t have assignments of a single workspace to different
327 struct Workspace_Assignment *assignment;
328 bool duplicate = false;
329 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
330 if (strcasecmp(assignment->name, workspace) == 0) {
331 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
333 assignment->output = sstrdup(output);
338 assignment = scalloc(sizeof(struct Workspace_Assignment));
339 assignment->name = sstrdup(workspace);
340 assignment->output = sstrdup(output);
341 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
345 CFGFUN(ipc_socket, const char *path) {
346 config.ipc_socket_path = sstrdup(path);
349 CFGFUN(restart_state, const char *path) {
350 config.restart_state_path = sstrdup(path);
353 CFGFUN(popup_during_fullscreen, const char *value) {
354 if (strcmp(value, "ignore") == 0) {
355 config.popup_during_fullscreen = PDF_IGNORE;
356 } else if (strcmp(value, "leave_fullscreen") == 0) {
357 config.popup_during_fullscreen = PDF_LEAVE_FULLSCREEN;
359 config.popup_during_fullscreen = PDF_SMART;
363 CFGFUN(color_single, const char *colorclass, const char *color) {
364 /* used for client.background only currently */
365 config.client.background = get_colorpixel(color);
368 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
369 #define APPLY_COLORS(classname) \
371 if (strcmp(colorclass, "client." #classname) == 0) { \
372 config.client.classname.border = get_colorpixel(border); \
373 config.client.classname.background = get_colorpixel(background); \
374 config.client.classname.text = get_colorpixel(text); \
375 if (indicator != NULL) { \
376 config.client. classname .indicator = get_colorpixel(indicator); \
381 APPLY_COLORS(focused_inactive);
382 APPLY_COLORS(focused);
383 APPLY_COLORS(unfocused);
384 APPLY_COLORS(urgent);
389 CFGFUN(assign, const char *workspace) {
390 if (match_is_empty(current_match)) {
391 ELOG("Match is empty, ignoring this assignment\n");
394 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
395 Assignment *assignment = scalloc(sizeof(Assignment));
396 match_copy(&(assignment->match), current_match);
397 assignment->type = A_TO_WORKSPACE;
398 assignment->dest.workspace = sstrdup(workspace);
399 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
402 /*******************************************************************************
403 * Bar configuration (i3bar)
404 ******************************************************************************/
406 static Barconfig current_bar;
408 CFGFUN(bar_font, const char *font) {
409 FREE(current_bar.font);
410 current_bar.font = sstrdup(font);
413 CFGFUN(bar_mode, const char *mode) {
414 current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
417 CFGFUN(bar_hidden_state, const char *hidden_state) {
418 current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
421 CFGFUN(bar_id, const char *bar_id) {
422 current_bar.id = sstrdup(bar_id);
425 CFGFUN(bar_output, const char *output) {
426 int new_outputs = current_bar.num_outputs + 1;
427 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
428 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
429 current_bar.num_outputs = new_outputs;
432 CFGFUN(bar_verbose, const char *verbose) {
433 current_bar.verbose = eval_boolstr(verbose);
436 CFGFUN(bar_modifier, const char *modifier) {
437 if (strcmp(modifier, "Mod1") == 0)
438 current_bar.modifier = M_MOD1;
439 else if (strcmp(modifier, "Mod2") == 0)
440 current_bar.modifier = M_MOD2;
441 else if (strcmp(modifier, "Mod3") == 0)
442 current_bar.modifier = M_MOD3;
443 else if (strcmp(modifier, "Mod4") == 0)
444 current_bar.modifier = M_MOD4;
445 else if (strcmp(modifier, "Mod5") == 0)
446 current_bar.modifier = M_MOD5;
447 else if (strcmp(modifier, "Control") == 0 ||
448 strcmp(modifier, "Ctrl") == 0)
449 current_bar.modifier = M_CONTROL;
450 else if (strcmp(modifier, "Shift") == 0)
451 current_bar.modifier = M_SHIFT;
454 CFGFUN(bar_position, const char *position) {
455 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
458 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
459 FREE(current_bar.i3bar_command);
460 current_bar.i3bar_command = sstrdup(i3bar_command);
463 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
464 #define APPLY_COLORS(classname) \
466 if (strcmp(colorclass, #classname) == 0) { \
467 if (text != NULL) { \
468 /* New syntax: border, background, text */ \
469 current_bar.colors. classname ## _border = sstrdup(border); \
470 current_bar.colors. classname ## _bg = sstrdup(background); \
471 current_bar.colors. classname ## _text = sstrdup(text); \
473 /* Old syntax: text, background */ \
474 current_bar.colors. classname ## _bg = sstrdup(background); \
475 current_bar.colors. classname ## _text = sstrdup(border); \
480 APPLY_COLORS(focused_workspace);
481 APPLY_COLORS(active_workspace);
482 APPLY_COLORS(inactive_workspace);
483 APPLY_COLORS(urgent_workspace);
488 CFGFUN(bar_socket_path, const char *socket_path) {
489 FREE(current_bar.socket_path);
490 current_bar.socket_path = sstrdup(socket_path);
493 CFGFUN(bar_tray_output, const char *output) {
494 FREE(current_bar.tray_output);
495 current_bar.tray_output = sstrdup(output);
498 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
499 if (strcmp(colorclass, "background") == 0)
500 current_bar.colors.background = sstrdup(color);
501 else if (strcmp(colorclass, "separator") == 0)
502 current_bar.colors.separator = sstrdup(color);
504 current_bar.colors.statusline = sstrdup(color);
507 CFGFUN(bar_status_command, const char *command) {
508 FREE(current_bar.status_command);
509 current_bar.status_command = sstrdup(command);
512 CFGFUN(bar_binding_mode_indicator, const char *value) {
513 current_bar.hide_binding_mode_indicator = !eval_boolstr(value);
516 CFGFUN(bar_workspace_buttons, const char *value) {
517 current_bar.hide_workspace_buttons = !eval_boolstr(value);
521 DLOG("\t new bar configuration finished, saving.\n");
522 /* Generate a unique ID for this bar if not already configured */
524 sasprintf(¤t_bar.id, "bar-%d", config.number_barconfigs);
526 config.number_barconfigs++;
528 /* If no font was explicitly set, we use the i3 font as default */
529 if (!current_bar.font && font_pattern)
530 current_bar.font = sstrdup(font_pattern);
532 /* Copy the current (static) structure into a dynamically allocated
533 * one, then cleanup our static one. */
534 Barconfig *bar_config = scalloc(sizeof(Barconfig));
535 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
536 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
538 memset(¤t_bar, '\0', sizeof(Barconfig));