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 // Macros to make the YAJL API a bit easier to use.
18 #define y(x, ...) yajl_gen_ ## x (cmd_output->json_gen, ##__VA_ARGS__)
19 #define ystr(str) yajl_gen_string(cmd_output->json_gen, (unsigned char*)str, strlen(str))
20 #define ysuccess(success) do { \
27 /*******************************************************************************
29 ******************************************************************************/
31 static int criteria_next_state;
34 * Initializes the specified 'Match' data structure and the initial state of
35 * commands.c for matching target windows of a command.
38 CFGFUN(criteria_init, int _state) {
39 criteria_next_state = _state;
41 DLOG("Initializing criteria, current_match = %p, state = %d\n", current_match, _state);
42 match_init(current_match);
45 CFGFUN(criteria_pop_state) {
46 result->next_state = criteria_next_state;
50 * Interprets a ctype=cvalue pair and adds it to the current match
54 CFGFUN(criteria_add, const char *ctype, const char *cvalue) {
55 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
57 if (strcmp(ctype, "class") == 0) {
58 current_match->class = regex_new(cvalue);
62 if (strcmp(ctype, "instance") == 0) {
63 current_match->instance = regex_new(cvalue);
67 if (strcmp(ctype, "window_role") == 0) {
68 current_match->role = regex_new(cvalue);
72 if (strcmp(ctype, "con_id") == 0) {
74 long parsed = strtol(cvalue, &end, 10);
75 if (parsed == LONG_MIN ||
78 (end && *end != '\0')) {
79 ELOG("Could not parse con id \"%s\"\n", cvalue);
81 current_match->con_id = (Con*)parsed;
82 printf("id as int = %p\n", current_match->con_id);
87 if (strcmp(ctype, "id") == 0) {
89 long parsed = strtol(cvalue, &end, 10);
90 if (parsed == LONG_MIN ||
93 (end && *end != '\0')) {
94 ELOG("Could not parse window id \"%s\"\n", cvalue);
96 current_match->id = parsed;
97 printf("window id as int = %d\n", current_match->id);
102 if (strcmp(ctype, "con_mark") == 0) {
103 current_match->mark = regex_new(cvalue);
107 if (strcmp(ctype, "title") == 0) {
108 current_match->title = regex_new(cvalue);
112 if (strcmp(ctype, "urgent") == 0) {
113 if (strcasecmp(cvalue, "latest") == 0 ||
114 strcasecmp(cvalue, "newest") == 0 ||
115 strcasecmp(cvalue, "recent") == 0 ||
116 strcasecmp(cvalue, "last") == 0) {
117 current_match->urgent = U_LATEST;
118 } else if (strcasecmp(cvalue, "oldest") == 0 ||
119 strcasecmp(cvalue, "first") == 0) {
120 current_match->urgent = U_OLDEST;
125 ELOG("Unknown criterion: %s\n", ctype);
128 /* TODO: refactor the above criteria code into a single file (with src/commands.c). */
130 /*******************************************************************************
132 ******************************************************************************/
134 static bool eval_boolstr(const char *str) {
135 return (strcasecmp(str, "1") == 0 ||
136 strcasecmp(str, "yes") == 0 ||
137 strcasecmp(str, "true") == 0 ||
138 strcasecmp(str, "on") == 0 ||
139 strcasecmp(str, "enable") == 0 ||
140 strcasecmp(str, "active") == 0);
143 static uint32_t modifiers_from_str(const char *str) {
144 /* It might be better to use strtok() here, but the simpler strstr() should
149 if (strstr(str, "Mod1") != NULL)
151 if (strstr(str, "Mod2") != NULL)
153 if (strstr(str, "Mod3") != NULL)
155 if (strstr(str, "Mod4") != NULL)
157 if (strstr(str, "Mod5") != NULL)
159 if (strstr(str, "Control") != NULL ||
160 strstr(str, "Ctrl") != NULL)
161 result |= BIND_CONTROL;
162 if (strstr(str, "Shift") != NULL)
163 result |= BIND_SHIFT;
164 if (strstr(str, "Mode_switch") != NULL)
165 result |= BIND_MODE_SWITCH;
169 static char *font_pattern;
171 CFGFUN(font, const char *font) {
172 config.font = load_font(font, true);
173 set_font(&config.font);
175 /* Save the font pattern for using it as bar font later on */
177 font_pattern = sstrdup(font);
180 // TODO: refactor with mode_binding
181 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
182 Binding *new_binding = scalloc(sizeof(Binding));
183 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
184 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
185 if (strcmp(bindtype, "bindsym") == 0) {
186 new_binding->symbol = sstrdup(key);
188 // TODO: strtol with proper error handling
189 new_binding->keycode = atoi(key);
190 if (new_binding->keycode == 0) {
191 ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
195 new_binding->mods = modifiers_from_str(modifiers);
196 new_binding->command = sstrdup(command);
197 TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
201 /*******************************************************************************
203 ******************************************************************************/
205 static struct bindings_head *current_bindings;
207 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
208 Binding *new_binding = scalloc(sizeof(Binding));
209 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
210 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
211 if (strcmp(bindtype, "bindsym") == 0) {
212 new_binding->symbol = sstrdup(key);
214 // TODO: strtol with proper error handling
215 new_binding->keycode = atoi(key);
216 if (new_binding->keycode == 0) {
217 ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
221 new_binding->mods = modifiers_from_str(modifiers);
222 new_binding->command = sstrdup(command);
223 TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
226 CFGFUN(enter_mode, const char *modename) {
227 if (strcasecmp(modename, "default") == 0) {
228 ELOG("You cannot use the name \"default\" for your mode\n");
231 DLOG("\t now in mode %s\n", modename);
232 struct Mode *mode = scalloc(sizeof(struct Mode));
233 mode->name = sstrdup(modename);
234 mode->bindings = scalloc(sizeof(struct bindings_head));
235 TAILQ_INIT(mode->bindings);
236 current_bindings = mode->bindings;
237 SLIST_INSERT_HEAD(&modes, mode, modes);
240 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
241 struct Autostart *new = smalloc(sizeof(struct Autostart));
242 new->command = sstrdup(command);
243 new->no_startup_id = (no_startup_id != NULL);
244 if (strcmp(exectype, "exec") == 0) {
245 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
247 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
251 CFGFUN(for_window, const char *command) {
252 if (match_is_empty(current_match)) {
253 ELOG("Match is empty, ignoring this for_window statement\n");
256 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
257 Assignment *assignment = scalloc(sizeof(Assignment));
258 assignment->type = A_COMMAND;
259 match_copy(&(assignment->match), current_match);
260 assignment->dest.command = sstrdup(command);
261 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
264 CFGFUN(floating_minimum_size, const long width, const long height) {
265 config.floating_minimum_width = width;
266 config.floating_minimum_height = height;
269 CFGFUN(floating_maximum_size, const long width, const long height) {
270 config.floating_maximum_width = width;
271 config.floating_maximum_height = height;
274 CFGFUN(floating_modifier, const char *modifiers) {
275 config.floating_modifier = modifiers_from_str(modifiers);
278 CFGFUN(default_orientation, const char *orientation) {
279 if (strcmp(orientation, "horizontal") == 0)
280 config.default_orientation = HORIZ;
281 else if (strcmp(orientation, "vertical") == 0)
282 config.default_orientation = VERT;
283 else config.default_orientation = NO_ORIENTATION;
286 CFGFUN(workspace_layout, const char *layout) {
287 if (strcmp(layout, "default") == 0)
288 config.default_layout = L_DEFAULT;
289 else if (strcmp(layout, "stacking") == 0 ||
290 strcmp(layout, "stacked") == 0)
291 config.default_layout = L_STACKED;
292 else config.default_layout = L_TABBED;
295 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
296 // FIXME: when using new_float *and* new_window with different border
297 // types, this breaks because default_border_width gets overwritten.
302 if (strcmp(border, "1pixel") == 0) {
303 border_style = BS_PIXEL;
305 } else if (strcmp(border, "none") == 0) {
306 border_style = BS_NONE;
308 } else if (strcmp(border, "pixel") == 0) {
309 border_style = BS_PIXEL;
310 border_width = width;
312 border_style = BS_NORMAL;
313 border_width = width;
316 if (strcmp(windowtype, "new_window") == 0) {
317 config.default_border = border_style;
319 config.default_floating_border = border_style;
322 config.default_border_width = border_width;
325 CFGFUN(hide_edge_borders, const char *borders) {
326 if (strcmp(borders, "vertical") == 0)
327 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
328 else if (strcmp(borders, "horizontal") == 0)
329 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
330 else if (strcmp(borders, "both") == 0)
331 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
332 else if (strcmp(borders, "none") == 0)
333 config.hide_edge_borders = ADJ_NONE;
334 else if (eval_boolstr(borders))
335 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
336 else config.hide_edge_borders = ADJ_NONE;
339 CFGFUN(focus_follows_mouse, const char *value) {
340 config.disable_focus_follows_mouse = !eval_boolstr(value);
343 CFGFUN(force_xinerama, const char *value) {
344 config.force_xinerama = eval_boolstr(value);
347 CFGFUN(force_focus_wrapping, const char *value) {
348 config.force_focus_wrapping = eval_boolstr(value);
351 CFGFUN(workspace_back_and_forth, const char *value) {
352 config.workspace_auto_back_and_forth = eval_boolstr(value);
355 CFGFUN(fake_outputs, const char *outputs) {
356 config.fake_outputs = sstrdup(outputs);
359 CFGFUN(force_display_urgency_hint, const long duration_ms) {
360 config.workspace_urgency_timer = duration_ms / 1000.0;
363 CFGFUN(workspace, const char *workspace, const char *output) {
364 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
365 /* Check for earlier assignments of the same workspace so that we
366 * don’t have assignments of a single workspace to different
368 struct Workspace_Assignment *assignment;
369 bool duplicate = false;
370 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
371 if (strcasecmp(assignment->name, workspace) == 0) {
372 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
374 assignment->output = sstrdup(output);
379 assignment = scalloc(sizeof(struct Workspace_Assignment));
380 assignment->name = sstrdup(workspace);
381 assignment->output = sstrdup(output);
382 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
386 CFGFUN(ipc_socket, const char *path) {
387 config.ipc_socket_path = sstrdup(path);
390 CFGFUN(restart_state, const char *path) {
391 config.restart_state_path = sstrdup(path);
394 CFGFUN(popup_during_fullscreen, const char *value) {
395 if (strcmp(value, "ignore") == 0) {
396 config.popup_during_fullscreen = PDF_IGNORE;
397 } else if (strcmp(value, "leave_fullscreen") == 0) {
398 config.popup_during_fullscreen = PDF_LEAVE_FULLSCREEN;
400 config.popup_during_fullscreen = PDF_SMART;
404 CFGFUN(color_single, const char *colorclass, const char *color) {
405 /* used for client.background only currently */
406 config.client.background = get_colorpixel(color);
409 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
410 #define APPLY_COLORS(classname) \
412 if (strcmp(colorclass, "client." #classname) == 0) { \
413 config.client.classname.border = get_colorpixel(border); \
414 config.client.classname.background = get_colorpixel(background); \
415 config.client.classname.text = get_colorpixel(text); \
416 if (indicator != NULL) { \
417 config.client. classname .indicator = get_colorpixel(indicator); \
422 APPLY_COLORS(focused_inactive);
423 APPLY_COLORS(focused);
424 APPLY_COLORS(unfocused);
425 APPLY_COLORS(urgent);
430 CFGFUN(assign, const char *workspace) {
431 if (match_is_empty(current_match)) {
432 ELOG("Match is empty, ignoring this assignment\n");
435 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
436 Assignment *assignment = scalloc(sizeof(Assignment));
437 match_copy(&(assignment->match), current_match);
438 assignment->type = A_TO_WORKSPACE;
439 assignment->dest.workspace = sstrdup(workspace);
440 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
443 /*******************************************************************************
444 * Bar configuration (i3bar)
445 ******************************************************************************/
447 static Barconfig current_bar;
449 CFGFUN(bar_font, const char *font) {
450 FREE(current_bar.font);
451 current_bar.font = sstrdup(font);
454 CFGFUN(bar_mode, const char *mode) {
455 current_bar.mode = (strcmp(mode, "dock") == 0 ? M_DOCK : (strcmp(mode, "hide") == 0 ? M_HIDE : M_INVISIBLE));
458 CFGFUN(bar_hidden_state, const char *hidden_state) {
459 current_bar.hidden_state = (strcmp(hidden_state, "hide") == 0 ? S_HIDE : S_SHOW);
462 CFGFUN(bar_id, const char *bar_id) {
463 current_bar.id = sstrdup(bar_id);
466 CFGFUN(bar_output, const char *output) {
467 int new_outputs = current_bar.num_outputs + 1;
468 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
469 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
470 current_bar.num_outputs = new_outputs;
473 CFGFUN(bar_verbose, const char *verbose) {
474 current_bar.verbose = eval_boolstr(verbose);
477 CFGFUN(bar_modifier, const char *modifier) {
478 if (strcmp(modifier, "Mod1") == 0)
479 current_bar.modifier = M_MOD1;
480 else if (strcmp(modifier, "Mod2") == 0)
481 current_bar.modifier = M_MOD2;
482 else if (strcmp(modifier, "Mod3") == 0)
483 current_bar.modifier = M_MOD3;
484 else if (strcmp(modifier, "Mod4") == 0)
485 current_bar.modifier = M_MOD4;
486 else if (strcmp(modifier, "Mod5") == 0)
487 current_bar.modifier = M_MOD5;
488 else if (strcmp(modifier, "Control") == 0 ||
489 strcmp(modifier, "Ctrl") == 0)
490 current_bar.modifier = M_CONTROL;
491 else if (strcmp(modifier, "Shift") == 0)
492 current_bar.modifier = M_SHIFT;
495 CFGFUN(bar_position, const char *position) {
496 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
499 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
500 FREE(current_bar.i3bar_command);
501 current_bar.i3bar_command = sstrdup(i3bar_command);
504 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
505 #define APPLY_COLORS(classname) \
507 if (strcmp(colorclass, #classname) == 0) { \
508 if (text != NULL) { \
509 /* New syntax: border, background, text */ \
510 current_bar.colors. classname ## _border = sstrdup(border); \
511 current_bar.colors. classname ## _bg = sstrdup(background); \
512 current_bar.colors. classname ## _text = sstrdup(text); \
514 /* Old syntax: text, background */ \
515 current_bar.colors. classname ## _bg = sstrdup(background); \
516 current_bar.colors. classname ## _text = sstrdup(border); \
521 APPLY_COLORS(focused_workspace);
522 APPLY_COLORS(active_workspace);
523 APPLY_COLORS(inactive_workspace);
524 APPLY_COLORS(urgent_workspace);
529 CFGFUN(bar_socket_path, const char *socket_path) {
530 FREE(current_bar.socket_path);
531 current_bar.socket_path = sstrdup(socket_path);
534 CFGFUN(bar_tray_output, const char *output) {
535 FREE(current_bar.tray_output);
536 current_bar.tray_output = sstrdup(output);
539 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
540 if (strcmp(colorclass, "background") == 0)
541 current_bar.colors.background = sstrdup(color);
542 else if (strcmp(colorclass, "separator") == 0)
543 current_bar.colors.separator = sstrdup(color);
545 current_bar.colors.statusline = sstrdup(color);
548 CFGFUN(bar_status_command, const char *command) {
549 FREE(current_bar.status_command);
550 current_bar.status_command = sstrdup(command);
553 CFGFUN(bar_binding_mode_indicator, const char *value) {
554 current_bar.hide_binding_mode_indicator = !eval_boolstr(value);
557 CFGFUN(bar_workspace_buttons, const char *value) {
558 current_bar.hide_workspace_buttons = !eval_boolstr(value);
562 DLOG("\t new bar configuration finished, saving.\n");
563 /* Generate a unique ID for this bar if not already configured */
565 sasprintf(¤t_bar.id, "bar-%d", config.number_barconfigs);
567 config.number_barconfigs++;
569 /* If no font was explicitly set, we use the i3 font as default */
570 if (!current_bar.font && font_pattern)
571 current_bar.font = sstrdup(font_pattern);
573 /* Copy the current (static) structure into a dynamically allocated
574 * one, then cleanup our static one. */
575 Barconfig *bar_config = scalloc(sizeof(Barconfig));
576 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
577 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
579 memset(¤t_bar, '\0', sizeof(Barconfig));