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 result |= BIND_CONTROL;
161 if (strstr(str, "Shift") != NULL)
162 result |= BIND_SHIFT;
163 if (strstr(str, "Mode_switch") != NULL)
164 result |= BIND_MODE_SWITCH;
168 static char *font_pattern;
170 CFGFUN(font, const char *font) {
171 config.font = load_font(font, true);
172 set_font(&config.font);
174 /* Save the font pattern for using it as bar font later on */
176 font_pattern = sstrdup(font);
179 // TODO: refactor with mode_binding
180 CFGFUN(binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
181 Binding *new_binding = scalloc(sizeof(Binding));
182 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
183 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
184 if (strcmp(bindtype, "bindsym") == 0) {
185 new_binding->symbol = sstrdup(key);
187 // TODO: strtol with proper error handling
188 new_binding->keycode = atoi(key);
190 new_binding->mods = modifiers_from_str(modifiers);
191 new_binding->command = sstrdup(command);
192 TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
196 /*******************************************************************************
198 ******************************************************************************/
200 static struct bindings_head *current_bindings;
202 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
203 Binding *new_binding = scalloc(sizeof(Binding));
204 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
205 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
206 if (strcmp(bindtype, "bindsym") == 0) {
207 new_binding->symbol = sstrdup(key);
209 // TODO: strtol with proper error handling
210 new_binding->keycode = atoi(key);
212 new_binding->mods = modifiers_from_str(modifiers);
213 new_binding->command = sstrdup(command);
214 TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
217 CFGFUN(enter_mode, const char *modename) {
218 if (strcasecmp(modename, "default") == 0) {
219 ELOG("You cannot use the name \"default\" for your mode\n");
222 DLOG("\t now in mode %s\n", modename);
223 struct Mode *mode = scalloc(sizeof(struct Mode));
224 mode->name = sstrdup(modename);
225 mode->bindings = scalloc(sizeof(struct bindings_head));
226 TAILQ_INIT(mode->bindings);
227 current_bindings = mode->bindings;
228 SLIST_INSERT_HEAD(&modes, mode, modes);
231 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
232 struct Autostart *new = smalloc(sizeof(struct Autostart));
233 new->command = sstrdup(command);
234 new->no_startup_id = (no_startup_id != NULL);
235 if (strcmp(exectype, "exec") == 0) {
236 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
238 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
242 CFGFUN(for_window, const char *command) {
243 if (match_is_empty(current_match)) {
244 ELOG("Match is empty, ignoring this for_window statement\n");
247 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
248 Assignment *assignment = scalloc(sizeof(Assignment));
249 assignment->type = A_COMMAND;
250 match_copy(&(assignment->match), current_match);
251 assignment->dest.command = sstrdup(command);
252 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
255 CFGFUN(floating_minimum_size, const long width, const long height) {
256 config.floating_minimum_width = width;
257 config.floating_minimum_height = height;
260 CFGFUN(floating_maximum_size, const long width, const long height) {
261 config.floating_maximum_width = width;
262 config.floating_maximum_height = height;
265 CFGFUN(floating_modifier, const char *modifiers) {
266 config.floating_modifier = modifiers_from_str(modifiers);
269 CFGFUN(default_orientation, const char *orientation) {
270 if (strcmp(orientation, "horizontal") == 0)
271 config.default_orientation = HORIZ;
272 else if (strcmp(orientation, "vertical") == 0)
273 config.default_orientation = VERT;
274 else config.default_orientation = NO_ORIENTATION;
277 CFGFUN(workspace_layout, const char *layout) {
278 if (strcmp(layout, "default") == 0)
279 config.default_layout = L_DEFAULT;
280 else if (strcmp(layout, "stacking") == 0 ||
281 strcmp(layout, "stacked") == 0)
282 config.default_layout = L_STACKED;
283 else config.default_layout = L_TABBED;
286 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
287 // FIXME: when using new_float *and* new_window with different border
288 // types, this breaks because default_border_width gets overwritten.
293 if (strcmp(border, "1pixel") == 0) {
294 border_style = BS_PIXEL;
296 } else if (strcmp(border, "none") == 0) {
297 border_style = BS_NONE;
299 } else if (strcmp(border, "pixel") == 0) {
300 border_style = BS_PIXEL;
301 border_width = width;
303 border_style = BS_NORMAL;
304 border_width = width;
307 if (strcmp(windowtype, "new_window") == 0) {
308 config.default_border = border_style;
310 config.default_floating_border = border_style;
314 CFGFUN(hide_edge_borders, const char *borders) {
315 if (strcmp(borders, "vertical") == 0)
316 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
317 else if (strcmp(borders, "horizontal") == 0)
318 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
319 else if (strcmp(borders, "both") == 0)
320 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
321 else if (strcmp(borders, "none") == 0)
322 config.hide_edge_borders = ADJ_NONE;
323 else if (eval_boolstr(borders))
324 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
325 else config.hide_edge_borders = ADJ_NONE;
328 CFGFUN(focus_follows_mouse, const char *value) {
329 config.disable_focus_follows_mouse = !eval_boolstr(value);
332 CFGFUN(force_xinerama, const char *value) {
333 config.force_xinerama = eval_boolstr(value);
336 CFGFUN(force_focus_wrapping, const char *value) {
337 config.force_focus_wrapping = eval_boolstr(value);
340 CFGFUN(workspace_back_and_forth, const char *value) {
341 config.workspace_auto_back_and_forth = eval_boolstr(value);
344 CFGFUN(fake_outputs, const char *outputs) {
345 config.fake_outputs = sstrdup(outputs);
348 CFGFUN(force_display_urgency_hint, const long duration_ms) {
349 config.workspace_urgency_timer = duration_ms / 1000.0;
352 CFGFUN(workspace, const char *workspace, const char *output) {
353 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
354 /* Check for earlier assignments of the same workspace so that we
355 * don’t have assignments of a single workspace to different
357 struct Workspace_Assignment *assignment;
358 bool duplicate = false;
359 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
360 if (strcasecmp(assignment->name, workspace) == 0) {
361 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
363 assignment->output = sstrdup(output);
368 assignment = scalloc(sizeof(struct Workspace_Assignment));
369 assignment->name = sstrdup(workspace);
370 assignment->output = sstrdup(output);
371 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
375 CFGFUN(ipc_socket, const char *path) {
376 config.ipc_socket_path = sstrdup(path);
379 CFGFUN(restart_state, const char *path) {
380 config.restart_state_path = sstrdup(path);
383 CFGFUN(popup_during_fullscreen, const char *value) {
384 config.popup_during_fullscreen =
385 (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
388 CFGFUN(color_single, const char *colorclass, const char *color) {
389 /* used for client.background only currently */
390 config.client.background = get_colorpixel(color);
393 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
394 #define APPLY_COLORS(classname) \
396 if (strcmp(colorclass, "client." #classname) == 0) { \
397 config.client.classname.border = get_colorpixel(border); \
398 config.client.classname.background = get_colorpixel(background); \
399 config.client.classname.text = get_colorpixel(text); \
400 if (indicator != NULL) { \
401 config.client. classname .indicator = get_colorpixel(indicator); \
406 APPLY_COLORS(focused_inactive);
407 APPLY_COLORS(focused);
408 APPLY_COLORS(unfocused);
409 APPLY_COLORS(urgent);
414 CFGFUN(assign, const char *workspace) {
415 if (match_is_empty(current_match)) {
416 ELOG("Match is empty, ignoring this assignment\n");
419 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
420 Assignment *assignment = scalloc(sizeof(Assignment));
421 match_copy(&(assignment->match), current_match);
422 assignment->type = A_TO_WORKSPACE;
423 assignment->dest.workspace = sstrdup(workspace);
424 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
427 /*******************************************************************************
428 * Bar configuration (i3bar)
429 ******************************************************************************/
431 static Barconfig current_bar;
433 CFGFUN(bar_font, const char *font) {
434 FREE(current_bar.font);
435 current_bar.font = sstrdup(font);
438 CFGFUN(bar_mode, const char *mode) {
439 current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
442 CFGFUN(bar_output, const char *output) {
443 int new_outputs = current_bar.num_outputs + 1;
444 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
445 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
446 current_bar.num_outputs = new_outputs;
449 CFGFUN(bar_verbose, const char *verbose) {
450 current_bar.verbose = eval_boolstr(verbose);
453 CFGFUN(bar_modifier, const char *modifier) {
454 if (strcmp(modifier, "Mod1") == 0)
455 current_bar.modifier = M_MOD1;
456 else if (strcmp(modifier, "Mod2") == 0)
457 current_bar.modifier = M_MOD2;
458 else if (strcmp(modifier, "Mod3") == 0)
459 current_bar.modifier = M_MOD3;
460 else if (strcmp(modifier, "Mod4") == 0)
461 current_bar.modifier = M_MOD4;
462 else if (strcmp(modifier, "Mod5") == 0)
463 current_bar.modifier = M_MOD5;
464 else if (strcmp(modifier, "Control") == 0)
465 current_bar.modifier = M_CONTROL;
466 else if (strcmp(modifier, "Shift") == 0)
467 current_bar.modifier = M_SHIFT;
470 CFGFUN(bar_position, const char *position) {
471 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
474 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
475 FREE(current_bar.i3bar_command);
476 current_bar.i3bar_command = sstrdup(i3bar_command);
479 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
480 #define APPLY_COLORS(classname) \
482 if (strcmp(colorclass, #classname) == 0) { \
483 if (text != NULL) { \
484 /* New syntax: border, background, text */ \
485 current_bar.colors. classname ## _border = sstrdup(border); \
486 current_bar.colors. classname ## _bg = sstrdup(background); \
487 current_bar.colors. classname ## _text = sstrdup(text); \
489 /* Old syntax: text, background */ \
490 current_bar.colors. classname ## _bg = sstrdup(background); \
491 current_bar.colors. classname ## _text = sstrdup(border); \
496 APPLY_COLORS(focused_workspace);
497 APPLY_COLORS(active_workspace);
498 APPLY_COLORS(inactive_workspace);
499 APPLY_COLORS(urgent_workspace);
504 CFGFUN(bar_socket_path, const char *socket_path) {
505 FREE(current_bar.socket_path);
506 current_bar.socket_path = sstrdup(socket_path);
509 CFGFUN(bar_tray_output, const char *output) {
510 FREE(current_bar.tray_output);
511 current_bar.tray_output = sstrdup(output);
514 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
515 if (strcmp(colorclass, "background") == 0)
516 current_bar.colors.background = sstrdup(color);
517 else current_bar.colors.statusline = sstrdup(color);
520 CFGFUN(bar_status_command, const char *command) {
521 FREE(current_bar.status_command);
522 current_bar.status_command = sstrdup(command);
525 CFGFUN(bar_workspace_buttons, const char *value) {
526 current_bar.hide_workspace_buttons = !eval_boolstr(value);
530 DLOG("\t new bar configuration finished, saving.\n");
531 /* Generate a unique ID for this bar */
532 current_bar.id = sstrdup("bar-XXXXXX");
533 /* This works similar to mktemp in that it replaces the last six X with
534 * random letters, but without the restriction that the given buffer
535 * has to contain a valid path name. */
536 char *x = current_bar.id + strlen("bar-");
538 *(x++) = (rand() % 26) + 'a';
541 /* If no font was explicitly set, we use the i3 font as default */
542 if (!current_bar.font && font_pattern)
543 current_bar.font = sstrdup(font_pattern);
545 /* Copy the current (static) structure into a dynamically allocated
546 * one, then cleanup our static one. */
547 Barconfig *bar_config = scalloc(sizeof(Barconfig));
548 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
549 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
551 memset(¤t_bar, '\0', sizeof(Barconfig));