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->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);
133 static uint32_t modifiers_from_str(const char *str) {
134 /* It might be better to use strtok() here, but the simpler strstr() should
139 if (strstr(str, "Mod1") != NULL)
141 if (strstr(str, "Mod2") != NULL)
143 if (strstr(str, "Mod3") != NULL)
145 if (strstr(str, "Mod4") != NULL)
147 if (strstr(str, "Mod5") != NULL)
149 if (strstr(str, "Control") != NULL ||
150 strstr(str, "Ctrl") != NULL)
151 result |= BIND_CONTROL;
152 if (strstr(str, "Shift") != NULL)
153 result |= BIND_SHIFT;
154 if (strstr(str, "Mode_switch") != NULL)
155 result |= BIND_MODE_SWITCH;
159 static char *font_pattern;
161 CFGFUN(font, const char *font) {
162 config.font = load_font(font, true);
163 set_font(&config.font);
165 /* Save the font pattern for using it as bar font later on */
167 font_pattern = sstrdup(font);
170 // TODO: refactor with mode_binding
171 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
172 Binding *new_binding = scalloc(sizeof(Binding));
173 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
174 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
175 if (strcmp(bindtype, "bindsym") == 0) {
176 new_binding->symbol = sstrdup(key);
178 // TODO: strtol with proper error handling
179 new_binding->keycode = atoi(key);
180 if (new_binding->keycode == 0) {
181 ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
185 new_binding->mods = modifiers_from_str(modifiers);
186 new_binding->command = sstrdup(command);
187 TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
191 /*******************************************************************************
193 ******************************************************************************/
195 static struct bindings_head *current_bindings;
197 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
198 Binding *new_binding = scalloc(sizeof(Binding));
199 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
200 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
201 if (strcmp(bindtype, "bindsym") == 0) {
202 new_binding->symbol = sstrdup(key);
204 // TODO: strtol with proper error handling
205 new_binding->keycode = atoi(key);
206 if (new_binding->keycode == 0) {
207 ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
211 new_binding->mods = modifiers_from_str(modifiers);
212 new_binding->command = sstrdup(command);
213 TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
216 CFGFUN(enter_mode, const char *modename) {
217 if (strcasecmp(modename, "default") == 0) {
218 ELOG("You cannot use the name \"default\" for your mode\n");
221 DLOG("\t now in mode %s\n", modename);
222 struct Mode *mode = scalloc(sizeof(struct Mode));
223 mode->name = sstrdup(modename);
224 mode->bindings = scalloc(sizeof(struct bindings_head));
225 TAILQ_INIT(mode->bindings);
226 current_bindings = mode->bindings;
227 SLIST_INSERT_HEAD(&modes, mode, modes);
230 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
231 struct Autostart *new = smalloc(sizeof(struct Autostart));
232 new->command = sstrdup(command);
233 new->no_startup_id = (no_startup_id != NULL);
234 if (strcmp(exectype, "exec") == 0) {
235 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
237 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
241 CFGFUN(for_window, const char *command) {
242 if (match_is_empty(current_match)) {
243 ELOG("Match is empty, ignoring this for_window statement\n");
246 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
247 Assignment *assignment = scalloc(sizeof(Assignment));
248 assignment->type = A_COMMAND;
249 match_copy(&(assignment->match), current_match);
250 assignment->dest.command = sstrdup(command);
251 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
254 CFGFUN(floating_minimum_size, const long width, const long height) {
255 config.floating_minimum_width = width;
256 config.floating_minimum_height = height;
259 CFGFUN(floating_maximum_size, const long width, const long height) {
260 config.floating_maximum_width = width;
261 config.floating_maximum_height = height;
264 CFGFUN(floating_modifier, const char *modifiers) {
265 config.floating_modifier = modifiers_from_str(modifiers);
268 CFGFUN(default_orientation, const char *orientation) {
269 if (strcmp(orientation, "horizontal") == 0)
270 config.default_orientation = HORIZ;
271 else if (strcmp(orientation, "vertical") == 0)
272 config.default_orientation = VERT;
273 else config.default_orientation = NO_ORIENTATION;
276 CFGFUN(workspace_layout, const char *layout) {
277 if (strcmp(layout, "default") == 0)
278 config.default_layout = L_DEFAULT;
279 else if (strcmp(layout, "stacking") == 0 ||
280 strcmp(layout, "stacked") == 0)
281 config.default_layout = L_STACKED;
282 else config.default_layout = L_TABBED;
285 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
286 // FIXME: when using new_float *and* new_window with different border
287 // types, this breaks because default_border_width gets overwritten.
292 if (strcmp(border, "1pixel") == 0) {
293 border_style = BS_PIXEL;
295 } else if (strcmp(border, "none") == 0) {
296 border_style = BS_NONE;
298 } else if (strcmp(border, "pixel") == 0) {
299 border_style = BS_PIXEL;
300 border_width = width;
302 border_style = BS_NORMAL;
303 border_width = width;
306 if (strcmp(windowtype, "new_window") == 0) {
307 config.default_border = border_style;
309 config.default_floating_border = border_style;
312 config.default_border_width = border_width;
315 CFGFUN(hide_edge_borders, const char *borders) {
316 if (strcmp(borders, "vertical") == 0)
317 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
318 else if (strcmp(borders, "horizontal") == 0)
319 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
320 else if (strcmp(borders, "both") == 0)
321 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
322 else if (strcmp(borders, "none") == 0)
323 config.hide_edge_borders = ADJ_NONE;
324 else if (eval_boolstr(borders))
325 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
326 else config.hide_edge_borders = ADJ_NONE;
329 CFGFUN(focus_follows_mouse, const char *value) {
330 config.disable_focus_follows_mouse = !eval_boolstr(value);
333 CFGFUN(force_xinerama, const char *value) {
334 config.force_xinerama = eval_boolstr(value);
337 CFGFUN(force_focus_wrapping, const char *value) {
338 config.force_focus_wrapping = eval_boolstr(value);
341 CFGFUN(workspace_back_and_forth, const char *value) {
342 config.workspace_auto_back_and_forth = eval_boolstr(value);
345 CFGFUN(fake_outputs, const char *outputs) {
346 config.fake_outputs = sstrdup(outputs);
349 CFGFUN(force_display_urgency_hint, const long duration_ms) {
350 config.workspace_urgency_timer = duration_ms / 1000.0;
353 CFGFUN(workspace, const char *workspace, const char *output) {
354 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
355 /* Check for earlier assignments of the same workspace so that we
356 * don’t have assignments of a single workspace to different
358 struct Workspace_Assignment *assignment;
359 bool duplicate = false;
360 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
361 if (strcasecmp(assignment->name, workspace) == 0) {
362 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
364 assignment->output = sstrdup(output);
369 assignment = scalloc(sizeof(struct Workspace_Assignment));
370 assignment->name = sstrdup(workspace);
371 assignment->output = sstrdup(output);
372 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
376 CFGFUN(ipc_socket, const char *path) {
377 config.ipc_socket_path = sstrdup(path);
380 CFGFUN(restart_state, const char *path) {
381 config.restart_state_path = sstrdup(path);
384 CFGFUN(popup_during_fullscreen, const char *value) {
385 if (strcmp(value, "ignore") == 0) {
386 config.popup_during_fullscreen = PDF_IGNORE;
387 } else if (strcmp(value, "leave_fullscreen") == 0) {
388 config.popup_during_fullscreen = PDF_LEAVE_FULLSCREEN;
390 config.popup_during_fullscreen = PDF_SMART;
394 CFGFUN(color_single, const char *colorclass, const char *color) {
395 /* used for client.background only currently */
396 config.client.background = get_colorpixel(color);
399 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
400 #define APPLY_COLORS(classname) \
402 if (strcmp(colorclass, "client." #classname) == 0) { \
403 config.client.classname.border = get_colorpixel(border); \
404 config.client.classname.background = get_colorpixel(background); \
405 config.client.classname.text = get_colorpixel(text); \
406 if (indicator != NULL) { \
407 config.client. classname .indicator = get_colorpixel(indicator); \
412 APPLY_COLORS(focused_inactive);
413 APPLY_COLORS(focused);
414 APPLY_COLORS(unfocused);
415 APPLY_COLORS(urgent);
420 CFGFUN(assign, const char *workspace) {
421 if (match_is_empty(current_match)) {
422 ELOG("Match is empty, ignoring this assignment\n");
425 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
426 Assignment *assignment = scalloc(sizeof(Assignment));
427 match_copy(&(assignment->match), current_match);
428 assignment->type = A_TO_WORKSPACE;
429 assignment->dest.workspace = sstrdup(workspace);
430 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
433 /*******************************************************************************
434 * Bar configuration (i3bar)
435 ******************************************************************************/
437 static Barconfig current_bar;
439 CFGFUN(bar_font, const char *font) {
440 FREE(current_bar.font);
441 current_bar.font = sstrdup(font);
444 CFGFUN(bar_mode, const char *mode) {
445 current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
448 CFGFUN(bar_hidden_state, const char *hidden_state) {
449 current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
452 CFGFUN(bar_id, const char *bar_id) {
453 current_bar.id = sstrdup(bar_id);
456 CFGFUN(bar_output, const char *output) {
457 int new_outputs = current_bar.num_outputs + 1;
458 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
459 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
460 current_bar.num_outputs = new_outputs;
463 CFGFUN(bar_verbose, const char *verbose) {
464 current_bar.verbose = eval_boolstr(verbose);
467 CFGFUN(bar_modifier, const char *modifier) {
468 if (strcmp(modifier, "Mod1") == 0)
469 current_bar.modifier = M_MOD1;
470 else if (strcmp(modifier, "Mod2") == 0)
471 current_bar.modifier = M_MOD2;
472 else if (strcmp(modifier, "Mod3") == 0)
473 current_bar.modifier = M_MOD3;
474 else if (strcmp(modifier, "Mod4") == 0)
475 current_bar.modifier = M_MOD4;
476 else if (strcmp(modifier, "Mod5") == 0)
477 current_bar.modifier = M_MOD5;
478 else if (strcmp(modifier, "Control") == 0 ||
479 strcmp(modifier, "Ctrl") == 0)
480 current_bar.modifier = M_CONTROL;
481 else if (strcmp(modifier, "Shift") == 0)
482 current_bar.modifier = M_SHIFT;
485 CFGFUN(bar_position, const char *position) {
486 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
489 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
490 FREE(current_bar.i3bar_command);
491 current_bar.i3bar_command = sstrdup(i3bar_command);
494 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
495 #define APPLY_COLORS(classname) \
497 if (strcmp(colorclass, #classname) == 0) { \
498 if (text != NULL) { \
499 /* New syntax: border, background, text */ \
500 current_bar.colors. classname ## _border = sstrdup(border); \
501 current_bar.colors. classname ## _bg = sstrdup(background); \
502 current_bar.colors. classname ## _text = sstrdup(text); \
504 /* Old syntax: text, background */ \
505 current_bar.colors. classname ## _bg = sstrdup(background); \
506 current_bar.colors. classname ## _text = sstrdup(border); \
511 APPLY_COLORS(focused_workspace);
512 APPLY_COLORS(active_workspace);
513 APPLY_COLORS(inactive_workspace);
514 APPLY_COLORS(urgent_workspace);
519 CFGFUN(bar_socket_path, const char *socket_path) {
520 FREE(current_bar.socket_path);
521 current_bar.socket_path = sstrdup(socket_path);
524 CFGFUN(bar_tray_output, const char *output) {
525 FREE(current_bar.tray_output);
526 current_bar.tray_output = sstrdup(output);
529 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
530 if (strcmp(colorclass, "background") == 0)
531 current_bar.colors.background = sstrdup(color);
532 else if (strcmp(colorclass, "separator") == 0)
533 current_bar.colors.separator = sstrdup(color);
535 current_bar.colors.statusline = sstrdup(color);
538 CFGFUN(bar_status_command, const char *command) {
539 FREE(current_bar.status_command);
540 current_bar.status_command = sstrdup(command);
543 CFGFUN(bar_binding_mode_indicator, const char *value) {
544 current_bar.hide_binding_mode_indicator = !eval_boolstr(value);
547 CFGFUN(bar_workspace_buttons, const char *value) {
548 current_bar.hide_workspace_buttons = !eval_boolstr(value);
552 DLOG("\t new bar configuration finished, saving.\n");
553 /* Generate a unique ID for this bar if not already configured */
555 sasprintf(¤t_bar.id, "bar-%d", config.number_barconfigs);
557 config.number_barconfigs++;
559 /* If no font was explicitly set, we use the i3 font as default */
560 if (!current_bar.font && font_pattern)
561 current_bar.font = sstrdup(font_pattern);
563 /* Copy the current (static) structure into a dynamically allocated
564 * one, then cleanup our static one. */
565 Barconfig *bar_config = scalloc(sizeof(Barconfig));
566 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
567 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
569 memset(¤t_bar, '\0', sizeof(Barconfig));