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 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
183 if (strcmp(bindtype, "bindsym") == 0) {
184 new_binding->symbol = sstrdup(key);
186 // TODO: strtol with proper error handling
187 new_binding->keycode = atoi(key);
189 new_binding->mods = modifiers_from_str(modifiers);
190 new_binding->command = sstrdup(command);
191 TAILQ_INSERT_TAIL(bindings, new_binding, bindings);
195 /*******************************************************************************
197 ******************************************************************************/
199 static struct bindings_head *current_bindings;
201 CFGFUN(mode_binding, const char *bindtype, const char *modifiers, const char *key, const char *release, const char *command) {
202 Binding *new_binding = scalloc(sizeof(Binding));
203 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
204 if (strcmp(bindtype, "bindsym") == 0) {
205 new_binding->symbol = sstrdup(key);
207 // TODO: strtol with proper error handling
208 new_binding->keycode = atoi(key);
210 new_binding->mods = modifiers_from_str(modifiers);
211 new_binding->command = sstrdup(command);
212 TAILQ_INSERT_TAIL(current_bindings, new_binding, bindings);
215 CFGFUN(enter_mode, const char *modename) {
216 if (strcasecmp(modename, "default") == 0) {
217 ELOG("You cannot use the name \"default\" for your mode\n");
220 DLOG("\t now in mode %s\n", modename);
221 struct Mode *mode = scalloc(sizeof(struct Mode));
222 mode->name = sstrdup(modename);
223 mode->bindings = scalloc(sizeof(struct bindings_head));
224 TAILQ_INIT(mode->bindings);
225 current_bindings = mode->bindings;
226 SLIST_INSERT_HEAD(&modes, mode, modes);
229 CFGFUN(exec, const char *exectype, const char *no_startup_id, const char *command) {
230 struct Autostart *new = smalloc(sizeof(struct Autostart));
231 new->command = sstrdup(command);
232 new->no_startup_id = (no_startup_id != NULL);
233 if (strcmp(exectype, "exec") == 0) {
234 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
236 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
240 CFGFUN(for_window, const char *command) {
241 if (match_is_empty(current_match)) {
242 ELOG("Match is empty, ignoring this for_window statement\n");
245 DLOG("\t should execute command %s for the criteria mentioned above\n", command);
246 Assignment *assignment = scalloc(sizeof(Assignment));
247 assignment->type = A_COMMAND;
248 match_copy(&(assignment->match), current_match);
249 assignment->dest.command = sstrdup(command);
250 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
253 CFGFUN(floating_minimum_size, const long width, const long height) {
254 config.floating_minimum_width = width;
255 config.floating_minimum_height = height;
258 CFGFUN(floating_maximum_size, const long width, const long height) {
259 config.floating_maximum_width = width;
260 config.floating_maximum_height = height;
263 CFGFUN(floating_modifier, const char *modifiers) {
264 config.floating_modifier = modifiers_from_str(modifiers);
267 CFGFUN(default_orientation, const char *orientation) {
268 if (strcmp(orientation, "horizontal") == 0)
269 config.default_orientation = HORIZ;
270 else if (strcmp(orientation, "vertical") == 0)
271 config.default_orientation = VERT;
272 else config.default_orientation = NO_ORIENTATION;
275 CFGFUN(workspace_layout, const char *layout) {
276 if (strcmp(layout, "default") == 0)
277 config.default_layout = L_DEFAULT;
278 else if (strcmp(layout, "stacking") == 0 ||
279 strcmp(layout, "stacked") == 0)
280 config.default_layout = L_STACKED;
281 else config.default_layout = L_TABBED;
284 CFGFUN(new_window, const char *windowtype, const char *border, const long width) {
285 // FIXME: when using new_float *and* new_window with different border
286 // types, this breaks because default_border_width gets overwritten.
291 if (strcmp(border, "1pixel") == 0) {
292 border_style = BS_PIXEL;
294 } else if (strcmp(border, "none") == 0) {
295 border_style = BS_NONE;
297 } else if (strcmp(border, "pixel") == 0) {
298 border_style = BS_PIXEL;
299 border_width = width;
301 border_style = BS_NORMAL;
302 border_width = width;
305 if (strcmp(windowtype, "new_window") == 0) {
306 config.default_border = border_style;
308 config.default_floating_border = border_style;
312 CFGFUN(hide_edge_borders, const char *borders) {
313 if (strcmp(borders, "vertical") == 0)
314 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
315 else if (strcmp(borders, "horizontal") == 0)
316 config.hide_edge_borders = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
317 else if (strcmp(borders, "both") == 0)
318 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE;
319 else if (strcmp(borders, "none") == 0)
320 config.hide_edge_borders = ADJ_NONE;
321 else if (eval_boolstr(borders))
322 config.hide_edge_borders = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE;
323 else config.hide_edge_borders = ADJ_NONE;
326 CFGFUN(focus_follows_mouse, const char *value) {
327 config.disable_focus_follows_mouse = !eval_boolstr(value);
330 CFGFUN(force_xinerama, const char *value) {
331 config.force_xinerama = eval_boolstr(value);
334 CFGFUN(force_focus_wrapping, const char *value) {
335 config.force_focus_wrapping = eval_boolstr(value);
338 CFGFUN(workspace_back_and_forth, const char *value) {
339 config.workspace_auto_back_and_forth = eval_boolstr(value);
342 CFGFUN(fake_outputs, const char *outputs) {
343 config.fake_outputs = sstrdup(outputs);
346 CFGFUN(force_display_urgency_hint, const long duration_ms) {
347 config.workspace_urgency_timer = duration_ms / 1000.0;
350 CFGFUN(workspace, const char *workspace, const char *output) {
351 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", workspace, output);
352 /* Check for earlier assignments of the same workspace so that we
353 * don’t have assignments of a single workspace to different
355 struct Workspace_Assignment *assignment;
356 bool duplicate = false;
357 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
358 if (strcasecmp(assignment->name, workspace) == 0) {
359 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
361 assignment->output = sstrdup(output);
366 assignment = scalloc(sizeof(struct Workspace_Assignment));
367 assignment->name = sstrdup(workspace);
368 assignment->output = sstrdup(output);
369 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
373 CFGFUN(ipc_socket, const char *path) {
374 config.ipc_socket_path = sstrdup(path);
377 CFGFUN(restart_state, const char *path) {
378 config.restart_state_path = sstrdup(path);
381 CFGFUN(popup_during_fullscreen, const char *value) {
382 config.popup_during_fullscreen =
383 (strcmp(value, "ignore") == 0 ? PDF_IGNORE : PDF_LEAVE_FULLSCREEN);
386 CFGFUN(color_single, const char *colorclass, const char *color) {
387 /* used for client.background only currently */
388 config.client.background = get_colorpixel(color);
391 CFGFUN(color, const char *colorclass, const char *border, const char *background, const char *text, const char *indicator) {
392 #define APPLY_COLORS(classname) \
394 if (strcmp(colorclass, "client." #classname) == 0) { \
395 config.client.classname.border = get_colorpixel(border); \
396 config.client.classname.background = get_colorpixel(background); \
397 config.client.classname.text = get_colorpixel(text); \
398 if (indicator != NULL) { \
399 config.client. classname .indicator = get_colorpixel(indicator); \
404 APPLY_COLORS(focused_inactive);
405 APPLY_COLORS(focused);
406 APPLY_COLORS(unfocused);
407 APPLY_COLORS(urgent);
412 CFGFUN(assign, const char *workspace) {
413 if (match_is_empty(current_match)) {
414 ELOG("Match is empty, ignoring this assignment\n");
417 DLOG("new assignment, using above criteria, to workspace %s\n", workspace);
418 Assignment *assignment = scalloc(sizeof(Assignment));
419 match_copy(&(assignment->match), current_match);
420 assignment->type = A_TO_WORKSPACE;
421 assignment->dest.workspace = sstrdup(workspace);
422 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
425 /*******************************************************************************
426 * Bar configuration (i3bar)
427 ******************************************************************************/
429 static Barconfig current_bar;
431 CFGFUN(bar_font, const char *font) {
432 FREE(current_bar.font);
433 current_bar.font = sstrdup(font);
436 CFGFUN(bar_mode, const char *mode) {
437 current_bar.mode = (strcmp(mode, "hide") == 0 ? M_HIDE : M_DOCK);
440 CFGFUN(bar_output, const char *output) {
441 int new_outputs = current_bar.num_outputs + 1;
442 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
443 current_bar.outputs[current_bar.num_outputs] = sstrdup(output);
444 current_bar.num_outputs = new_outputs;
447 CFGFUN(bar_verbose, const char *verbose) {
448 current_bar.verbose = eval_boolstr(verbose);
451 CFGFUN(bar_modifier, const char *modifier) {
452 if (strcmp(modifier, "Mod1") == 0)
453 current_bar.modifier = M_MOD1;
454 else if (strcmp(modifier, "Mod2") == 0)
455 current_bar.modifier = M_MOD2;
456 else if (strcmp(modifier, "Mod3") == 0)
457 current_bar.modifier = M_MOD3;
458 else if (strcmp(modifier, "Mod4") == 0)
459 current_bar.modifier = M_MOD4;
460 else if (strcmp(modifier, "Mod5") == 0)
461 current_bar.modifier = M_MOD5;
462 else if (strcmp(modifier, "Control") == 0)
463 current_bar.modifier = M_CONTROL;
464 else if (strcmp(modifier, "Shift") == 0)
465 current_bar.modifier = M_SHIFT;
468 CFGFUN(bar_position, const char *position) {
469 current_bar.position = (strcmp(position, "top") == 0 ? P_TOP : P_BOTTOM);
472 CFGFUN(bar_i3bar_command, const char *i3bar_command) {
473 FREE(current_bar.i3bar_command);
474 current_bar.i3bar_command = sstrdup(i3bar_command);
477 CFGFUN(bar_color, const char *colorclass, const char *border, const char *background, const char *text) {
478 #define APPLY_COLORS(classname) \
480 if (strcmp(colorclass, #classname) == 0) { \
481 if (text != NULL) { \
482 /* New syntax: border, background, text */ \
483 current_bar.colors. classname ## _border = sstrdup(border); \
484 current_bar.colors. classname ## _bg = sstrdup(background); \
485 current_bar.colors. classname ## _text = sstrdup(text); \
487 /* Old syntax: text, background */ \
488 current_bar.colors. classname ## _bg = sstrdup(background); \
489 current_bar.colors. classname ## _text = sstrdup(border); \
494 APPLY_COLORS(focused_workspace);
495 APPLY_COLORS(active_workspace);
496 APPLY_COLORS(inactive_workspace);
497 APPLY_COLORS(urgent_workspace);
502 CFGFUN(bar_socket_path, const char *socket_path) {
503 FREE(current_bar.socket_path);
504 current_bar.socket_path = sstrdup(socket_path);
507 CFGFUN(bar_tray_output, const char *output) {
508 FREE(current_bar.tray_output);
509 current_bar.tray_output = sstrdup(output);
512 CFGFUN(bar_color_single, const char *colorclass, const char *color) {
513 if (strcmp(colorclass, "background") == 0)
514 current_bar.colors.background = sstrdup(color);
515 else current_bar.colors.statusline = sstrdup(color);
518 CFGFUN(bar_status_command, const char *command) {
519 FREE(current_bar.status_command);
520 current_bar.status_command = sstrdup(command);
523 CFGFUN(bar_workspace_buttons, const char *value) {
524 current_bar.hide_workspace_buttons = !eval_boolstr(value);
528 DLOG("\t new bar configuration finished, saving.\n");
529 /* Generate a unique ID for this bar */
530 current_bar.id = sstrdup("bar-XXXXXX");
531 /* This works similar to mktemp in that it replaces the last six X with
532 * random letters, but without the restriction that the given buffer
533 * has to contain a valid path name. */
534 char *x = current_bar.id + strlen("bar-");
536 *(x++) = (rand() % 26) + 'a';
539 /* If no font was explicitly set, we use the i3 font as default */
540 if (!current_bar.font && font_pattern)
541 current_bar.font = sstrdup(font_pattern);
543 /* Copy the current (static) structure into a dynamically allocated
544 * one, then cleanup our static one. */
545 Barconfig *bar_config = scalloc(sizeof(Barconfig));
546 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
547 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
549 memset(¤t_bar, '\0', sizeof(Barconfig));