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);
189 if (new_binding->keycode == 0) {
190 ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
194 new_binding->mods = modifiers_from_str(modifiers);
195 new_binding->command = sstrdup(command);
196 TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
200 /*******************************************************************************
202 ******************************************************************************/
204 static struct bindings_head *current_bindings;
206 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
207 Binding *new_binding = scalloc(sizeof(Binding));
208 DLOG("bindtype %s, modifiers %s, key %s, release %s\n", bindtype, modifiers, key, release);
209 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
210 if (strcmp(bindtype, "bindsym") == 0) {
211 new_binding->symbol = sstrdup(key);
213 // TODO: strtol with proper error handling
214 new_binding->keycode = atoi(key);
215 if (new_binding->keycode == 0) {
216 ELOG("Could not parse \"%s\" as a keycode, ignoring this binding.\n", key);
220 new_binding->mods = modifiers_from_str(modifiers);
221 new_binding->command = sstrdup(command);
222 TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
225 CFGFUN(enter_mode, const char *modename) {
226 if (strcasecmp(modename, "default") == 0) {
227 ELOG("You cannot use the name \"default\" for your mode\n");
230 DLOG("\t now in mode %s\n", modename);
231 struct Mode *mode = scalloc(sizeof(struct Mode));
232 mode->name = sstrdup(modename);
233 mode->bindings = scalloc(sizeof(struct bindings_head));
234 TAILQ_INIT(mode->bindings);
235 current_bindings = mode->bindings;
236 SLIST_INSERT_HEAD(&modes, mode, modes);
239 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
240 struct Autostart *new = smalloc(sizeof(struct Autostart));
241 new->command = sstrdup(command);
242 new->no_startup_id = (no_startup_id != NULL);
243 if (strcmp(exectype, "exec") == 0) {
244 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
246 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
250 CFGFUN(for_window, const char *command) {
251 if (match_is_empty(current_match)) {
252 ELOG("Match is empty, ignoring this for_window statement\n");
255 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
256 Assignment *assignment = scalloc(sizeof(Assignment));
257 assignment->type = A_COMMAND;
258 match_copy(&(assignment->match), current_match);
259 assignment->dest.command = sstrdup(command);
260 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
263 CFGFUN(floating_minimum_size, const long width, const long height) {
264 config.floating_minimum_width = width;
265 config.floating_minimum_height = height;
268 CFGFUN(floating_maximum_size, const long width, const long height) {
269 config.floating_maximum_width = width;
270 config.floating_maximum_height = height;
273 CFGFUN(floating_modifier, const char *modifiers) {
274 config.floating_modifier = modifiers_from_str(modifiers);
277 CFGFUN(default_orientation, const char *orientation) {
278 if (strcmp(orientation, "horizontal") == 0)
279 config.default_orientation = HORIZ;
280 else if (strcmp(orientation, "vertical") == 0)
281 config.default_orientation = VERT;
282 else config.default_orientation = NO_ORIENTATION;
285 CFGFUN(workspace_layout, const char *layout) {
286 if (strcmp(layout, "default") == 0)
287 config.default_layout = L_DEFAULT;
288 else if (strcmp(layout, "stacking") == 0 ||
289 strcmp(layout, "stacked") == 0)
290 config.default_layout = L_STACKED;
291 else config.default_layout = L_TABBED;
294 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
295 // FIXME: when using new_float *and* new_window with different border
296 // types, this breaks because default_border_width gets overwritten.
301 if (strcmp(border, "1pixel") == 0) {
302 border_style = BS_PIXEL;
304 } else if (strcmp(border, "none") == 0) {
305 border_style = BS_NONE;
307 } else if (strcmp(border, "pixel") == 0) {
308 border_style = BS_PIXEL;
309 border_width = width;
311 border_style = BS_NORMAL;
312 border_width = width;
315 if (strcmp(windowtype, "new_window") == 0) {
316 config.default_border = border_style;
318 config.default_floating_border = border_style;
322 CFGFUN(hide_edge_borders, const char *borders) {
323 if (strcmp(borders, "vertical") == 0)
324 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
325 else if (strcmp(borders, "horizontal") == 0)
326 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
327 else if (strcmp(borders, "both") == 0)
328 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
329 else if (strcmp(borders, "none") == 0)
330 config.hide_edge_borders = ADJ_NONE;
331 else if (eval_boolstr(borders))
332 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
333 else config.hide_edge_borders = ADJ_NONE;
336 CFGFUN(focus_follows_mouse, const char *value) {
337 config.disable_focus_follows_mouse = !eval_boolstr(value);
340 CFGFUN(force_xinerama, const char *value) {
341 config.force_xinerama = eval_boolstr(value);
344 CFGFUN(force_focus_wrapping, const char *value) {
345 config.force_focus_wrapping = eval_boolstr(value);
348 CFGFUN(workspace_back_and_forth, const char *value) {
349 config.workspace_auto_back_and_forth = eval_boolstr(value);
352 CFGFUN(fake_outputs, const char *outputs) {
353 config.fake_outputs = sstrdup(outputs);
356 CFGFUN(force_display_urgency_hint, const long duration_ms) {
357 config.workspace_urgency_timer = duration_ms / 1000.0;
360 CFGFUN(workspace, const char *workspace, const char *output) {
361 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
362 /* Check for earlier assignments of the same workspace so that we
363 * don’t have assignments of a single workspace to different
365 struct Workspace_Assignment *assignment;
366 bool duplicate = false;
367 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
368 if (strcasecmp(assignment->name, workspace) == 0) {
369 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
371 assignment->output = sstrdup(output);
376 assignment = scalloc(sizeof(struct Workspace_Assignment));
377 assignment->name = sstrdup(workspace);
378 assignment->output = sstrdup(output);
379 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
383 CFGFUN(ipc_socket, const char *path) {
384 config.ipc_socket_path = sstrdup(path);
387 CFGFUN(restart_state, const char *path) {
388 config.restart_state_path = sstrdup(path);
391 CFGFUN(popup_during_fullscreen, const char *value) {
392 config.popup_during_fullscreen =
393 (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
396 CFGFUN(color_single, const char *colorclass, const char *color) {
397 /* used for client.background only currently */
398 config.client.background = get_colorpixel(color);
401 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
402 #define APPLY_COLORS(classname) \
404 if (strcmp(colorclass, "client." #classname) == 0) { \
405 config.client.classname.border = get_colorpixel(border); \
406 config.client.classname.background = get_colorpixel(background); \
407 config.client.classname.text = get_colorpixel(text); \
408 if (indicator != NULL) { \
409 config.client. classname .indicator = get_colorpixel(indicator); \
414 APPLY_COLORS(focused_inactive);
415 APPLY_COLORS(focused);
416 APPLY_COLORS(unfocused);
417 APPLY_COLORS(urgent);
422 CFGFUN(assign, const char *workspace) {
423 if (match_is_empty(current_match)) {
424 ELOG("Match is empty, ignoring this assignment\n");
427 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
428 Assignment *assignment = scalloc(sizeof(Assignment));
429 match_copy(&(assignment->match), current_match);
430 assignment->type = A_TO_WORKSPACE;
431 assignment->dest.workspace = sstrdup(workspace);
432 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
435 /*******************************************************************************
436 * Bar configuration (i3bar)
437 ******************************************************************************/
439 static Barconfig current_bar;
441 CFGFUN(bar_font, const char *font) {
442 FREE(current_bar.font);
443 current_bar.font = sstrdup(font);
446 CFGFUN(bar_mode, const char *mode) {
447 current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
450 CFGFUN(bar_output, const char *output) {
451 int new_outputs = current_bar.num_outputs + 1;
452 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
453 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
454 current_bar.num_outputs = new_outputs;
457 CFGFUN(bar_verbose, const char *verbose) {
458 current_bar.verbose = eval_boolstr(verbose);
461 CFGFUN(bar_modifier, const char *modifier) {
462 if (strcmp(modifier, "Mod1") == 0)
463 current_bar.modifier = M_MOD1;
464 else if (strcmp(modifier, "Mod2") == 0)
465 current_bar.modifier = M_MOD2;
466 else if (strcmp(modifier, "Mod3") == 0)
467 current_bar.modifier = M_MOD3;
468 else if (strcmp(modifier, "Mod4") == 0)
469 current_bar.modifier = M_MOD4;
470 else if (strcmp(modifier, "Mod5") == 0)
471 current_bar.modifier = M_MOD5;
472 else if (strcmp(modifier, "Control") == 0)
473 current_bar.modifier = M_CONTROL;
474 else if (strcmp(modifier, "Shift") == 0)
475 current_bar.modifier = M_SHIFT;
478 CFGFUN(bar_position, const char *position) {
479 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
482 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
483 FREE(current_bar.i3bar_command);
484 current_bar.i3bar_command = sstrdup(i3bar_command);
487 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
488 #define APPLY_COLORS(classname) \
490 if (strcmp(colorclass, #classname) == 0) { \
491 if (text != NULL) { \
492 /* New syntax: border, background, text */ \
493 current_bar.colors. classname ## _border = sstrdup(border); \
494 current_bar.colors. classname ## _bg = sstrdup(background); \
495 current_bar.colors. classname ## _text = sstrdup(text); \
497 /* Old syntax: text, background */ \
498 current_bar.colors. classname ## _bg = sstrdup(background); \
499 current_bar.colors. classname ## _text = sstrdup(border); \
504 APPLY_COLORS(focused_workspace);
505 APPLY_COLORS(active_workspace);
506 APPLY_COLORS(inactive_workspace);
507 APPLY_COLORS(urgent_workspace);
512 CFGFUN(bar_socket_path, const char *socket_path) {
513 FREE(current_bar.socket_path);
514 current_bar.socket_path = sstrdup(socket_path);
517 CFGFUN(bar_tray_output, const char *output) {
518 FREE(current_bar.tray_output);
519 current_bar.tray_output = sstrdup(output);
522 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
523 if (strcmp(colorclass, "background") == 0)
524 current_bar.colors.background = sstrdup(color);
525 else current_bar.colors.statusline = sstrdup(color);
528 CFGFUN(bar_status_command, const char *command) {
529 FREE(current_bar.status_command);
530 current_bar.status_command = sstrdup(command);
533 CFGFUN(bar_workspace_buttons, const char *value) {
534 current_bar.hide_workspace_buttons = !eval_boolstr(value);
538 DLOG("\t new bar configuration finished, saving.\n");
539 /* Generate a unique ID for this bar */
540 current_bar.id = sstrdup("bar-XXXXXX");
541 /* This works similar to mktemp in that it replaces the last six X with
542 * random letters, but without the restriction that the given buffer
543 * has to contain a valid path name. */
544 char *x = current_bar.id + strlen("bar-");
546 *(x++) = (rand() % 26) + 'a';
549 /* If no font was explicitly set, we use the i3 font as default */
550 if (!current_bar.font && font_pattern)
551 current_bar.font = sstrdup(font_pattern);
553 /* Copy the current (static) structure into a dynamically allocated
554 * one, then cleanup our static one. */
555 Barconfig *bar_config = scalloc(sizeof(Barconfig));
556 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
557 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
559 memset(¤t_bar, '\0', sizeof(Barconfig));