3 * vim:ts=4:sw=4:expandtab
14 static pid_t configerror_pid = -1;
16 static Match current_match;
17 static Barconfig current_bar;
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int yylex(struct context *context);
21 extern int yyparse(void);
22 extern int yylex_destroy(void);
24 YY_BUFFER_STATE yy_scan_string(const char *);
26 static struct bindings_head *current_bindings;
27 static struct context *context;
29 /* We don’t need yydebug for now, as we got decent error messages using
30 * yyerror(). Should you ever want to extend the parser, it might be handy
31 * to just comment it in again, so it stays here. */
34 void yyerror(const char *error_message) {
35 context->has_errors = true;
38 ELOG("CONFIG: %s\n", error_message);
39 ELOG("CONFIG: in file \"%s\", line %d:\n",
40 context->filename, context->line_number);
41 ELOG("CONFIG: %s\n", context->line_copy);
42 char buffer[context->last_column+1];
43 buffer[context->last_column] = '\0';
44 for (int c = 1; c <= context->last_column; c++)
45 buffer[c-1] = (c >= context->first_column ? '^' : ' ');
46 ELOG("CONFIG: %s\n", buffer);
55 * Goes through each line of buf (separated by \n) and checks for statements /
56 * commands which only occur in i3 v4 configuration files. If it finds any, it
57 * returns version 4, otherwise it returns version 3.
60 static int detect_version(char *buf) {
63 while (*walk != '\0') {
69 /* check for some v4-only statements */
70 if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
71 strncasecmp(line, "force_focus_wrapping", strlen("force_focus_wrapping")) == 0 ||
72 strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
73 strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
74 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
78 /* if this is a bind statement, we can check the command */
79 if (strncasecmp(line, "bind", strlen("bind")) == 0) {
80 char *bind = strchr(line, ' ');
83 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
87 if ((bind = strchr(bind, ' ')) == NULL)
89 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
93 if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
94 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
95 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
96 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
97 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
98 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
99 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
100 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
101 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
102 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0) {
103 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
109 /* advance to the next line */
118 * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
121 * Returns the converted config file or NULL if there was an error (for
122 * example the script could not be found in $PATH or the i3 executable’s
126 static char *migrate_config(char *input, off_t size) {
130 if (pipe(writepipe) != 0 ||
131 pipe(readpipe) != 0) {
132 warn("migrate_config: Could not create pipes");
138 warn("Could not fork()");
144 /* close writing end of writepipe, connect reading side to stdin */
146 dup2(writepipe[0], 0);
148 /* close reading end of readpipe, connect writing side to stdout */
150 dup2(readpipe[1], 1);
152 static char *argv[] = {
153 NULL, /* will be replaced by the executable path */
156 exec_i3_utility("i3-migrate-config-to-v4", argv);
161 /* close reading end of the writepipe (connected to the script’s stdin) */
164 /* write the whole config file to the pipe, the script will read everything
168 while (written < size) {
169 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
170 warn("Could not write to pipe");
177 /* close writing end of the readpipe (connected to the script’s stdout) */
180 /* read the script’s output */
181 int conv_size = 65535;
182 char *converted = malloc(conv_size);
185 if (read_bytes == conv_size) {
187 converted = realloc(converted, conv_size);
189 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
191 warn("Cannot read from pipe");
198 /* get the returncode */
201 if (!WIFEXITED(status)) {
202 fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
206 int returncode = WEXITSTATUS(status);
207 if (returncode != 0) {
208 fprintf(stderr, "Migration process exit code was != 0\n");
209 if (returncode == 2) {
210 fprintf(stderr, "could not start the migration script\n");
211 /* TODO: script was not found. tell the user to fix his system or create a v4 config */
212 } else if (returncode == 1) {
213 fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
214 fprintf(stderr, "# i3 config file (v4)\n");
215 /* TODO: nag the user with a message to include a hint for i3 in his config file */
224 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
225 * it exited (or could not be started, depending on the exit code).
228 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
229 ev_child_stop(EV_A_ watcher);
230 if (!WIFEXITED(watcher->rstatus)) {
231 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
235 int exitcode = WEXITSTATUS(watcher->rstatus);
236 printf("i3-nagbar process exited with status %d\n", exitcode);
238 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
241 configerror_pid = -1;
244 /* We need ev >= 4 for the following code. Since it is not *that* important (it
245 * only makes sure that there are no i3-nagbar instances left behind) we still
246 * support old systems with libev 3. */
247 #if EV_VERSION_MAJOR >= 4
249 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
250 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
253 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
254 if (configerror_pid != -1) {
255 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", configerror_pid);
256 kill(configerror_pid, SIGKILL);
262 * Starts an i3-nagbar process which alerts the user that his configuration
263 * file contains one or more errors. Also offers two buttons: One to launch an
264 * $EDITOR on the config file and another one to launch a $PAGER on the error
268 static void start_configerror_nagbar(const char *config_path) {
269 if (only_check_config)
272 fprintf(stderr, "Starting i3-nagbar due to configuration errors\n");
273 configerror_pid = fork();
274 if (configerror_pid == -1) {
275 warn("Could not fork()");
280 if (configerror_pid == 0) {
283 if (asprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path) == -1)
285 if (asprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename) == -1)
288 NULL, /* will be replaced by the executable path */
290 "You have an error in your i3 config file!",
294 (errorfilename ? "-b" : NULL),
299 exec_i3_utility("i3-nagbar", argv);
303 /* install a child watcher */
304 ev_child *child = smalloc(sizeof(ev_child));
305 ev_child_init(child, &nagbar_exited, configerror_pid, 0);
306 ev_child_start(main_loop, child);
308 /* We need ev >= 4 for the following code. Since it is not *that* important (it
309 * only makes sure that there are no i3-nagbar instances left behind) we still
310 * support old systems with libev 3. */
311 #if EV_VERSION_MAJOR >= 4
312 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
314 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
315 ev_cleanup_init(cleanup, nagbar_cleanup);
316 ev_cleanup_start(main_loop, cleanup);
321 * Kills the configerror i3-nagbar process, if any.
323 * Called when reloading/restarting.
325 * If wait_for_it is set (restarting), this function will waitpid(), otherwise,
326 * ev is assumed to handle it (reloading).
329 void kill_configerror_nagbar(bool wait_for_it) {
330 if (configerror_pid == -1)
333 if (kill(configerror_pid, SIGTERM) == -1)
334 warn("kill(configerror_nagbar) failed");
339 /* When restarting, we don’t enter the ev main loop anymore and after the
340 * exec(), our old pid is no longer watched. So, ev won’t handle SIGCHLD
341 * for us and we would end up with a <defunct> process. Therefore we
343 waitpid(configerror_pid, NULL, 0);
347 * Checks for duplicate key bindings (the same keycode or keysym is configured
348 * more than once). If a duplicate binding is found, a message is printed to
349 * stderr and the has_errors variable is set to true, which will start
353 static void check_for_duplicate_bindings(struct context *context) {
354 Binding *bind, *current;
355 TAILQ_FOREACH(current, bindings, bindings) {
356 TAILQ_FOREACH(bind, bindings, bindings) {
357 /* Abort when we reach the current keybinding, only check the
362 /* Check if one is using keysym while the other is using bindsym.
364 /* XXX: It should be checked at a later place (when translating the
365 * keysym to keycodes) if there are any duplicates */
366 if ((bind->symbol == NULL && current->symbol != NULL) ||
367 (bind->symbol != NULL && current->symbol == NULL))
370 /* If bind is NULL, current has to be NULL, too (see above).
371 * If the keycodes differ, it can't be a duplicate. */
372 if (bind->symbol != NULL &&
373 strcasecmp(bind->symbol, current->symbol) != 0)
376 /* Check if the keycodes or modifiers are different. If so, they
377 * can't be duplicate */
378 if (bind->keycode != current->keycode ||
379 bind->mods != current->mods)
381 context->has_errors = true;
382 if (current->keycode != 0) {
383 ELOG("Duplicate keybinding in config file:\n modmask %d with keycode %d, command \"%s\"\n",
384 current->mods, current->keycode, current->command);
386 ELOG("Duplicate keybinding in config file:\n modmask %d with keysym %s, command \"%s\"\n",
387 current->mods, current->symbol, current->command);
393 void parse_file(const char *f) {
394 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
395 int fd, ret, read_bytes = 0;
399 char buffer[1026], key[512], value[512];
401 if ((fd = open(f, O_RDONLY)) == -1)
402 die("Could not open configuration file: %s\n", strerror(errno));
404 if (fstat(fd, &stbuf) == -1)
405 die("Could not fstat file: %s\n", strerror(errno));
407 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
408 while (read_bytes < stbuf.st_size) {
409 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
410 die("Could not read(): %s\n", strerror(errno));
414 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
415 die("Could not lseek: %s\n", strerror(errno));
417 if ((fstr = fdopen(fd, "r")) == NULL)
418 die("Could not fdopen: %s\n", strerror(errno));
420 while (!feof(fstr)) {
421 if (fgets(buffer, 1024, fstr) == NULL) {
424 die("Could not read configuration file\n");
427 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
428 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
429 key[0] == '#' || strlen(key) < 3)
432 if (strcasecmp(key, "set") == 0) {
433 if (value[0] != '$') {
434 ELOG("Malformed variable assignment, name has to start with $\n");
438 /* get key/value for this variable */
439 char *v_key = value, *v_value;
440 if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
441 ELOG("Malformed variable assignment, need a value\n");
445 if (!(v_value = strstr(value, " ")))
446 v_value = strstr(value, "\t");
450 struct Variable *new = scalloc(sizeof(struct Variable));
451 new->key = sstrdup(v_key);
452 new->value = sstrdup(v_value);
453 SLIST_INSERT_HEAD(&variables, new, variables);
454 DLOG("Got new variable %s = %s\n", v_key, v_value);
460 /* For every custom variable, see how often it occurs in the file and
461 * how much extra bytes it requires when replaced. */
462 struct Variable *current, *nearest;
464 /* We need to copy the buffer because we need to invalidate the
465 * variables (otherwise we will count them twice, which is bad when
466 * 'extra' is negative) */
467 char *bufcopy = sstrdup(buf);
468 SLIST_FOREACH(current, &variables, variables) {
469 int extra = (strlen(current->value) - strlen(current->key));
472 next < (bufcopy + stbuf.st_size) &&
473 (next = strcasestr(next, current->key)) != NULL;
474 next += strlen(current->key)) {
476 extra_bytes += extra;
481 /* Then, allocate a new buffer and copy the file over to the new one,
482 * but replace occurences of our variables */
483 char *walk = buf, *destwalk;
484 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
486 while (walk < (buf + stbuf.st_size)) {
487 /* Find the next variable */
488 SLIST_FOREACH(current, &variables, variables)
489 current->next_match = strcasestr(walk, current->key);
491 int distance = stbuf.st_size;
492 SLIST_FOREACH(current, &variables, variables) {
493 if (current->next_match == NULL)
495 if ((current->next_match - walk) < distance) {
496 distance = (current->next_match - walk);
500 if (nearest == NULL) {
501 /* If there are no more variables, we just copy the rest */
502 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
503 destwalk += (buf + stbuf.st_size) - walk;
507 /* Copy until the next variable, then copy its value */
508 strncpy(destwalk, walk, distance);
509 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
510 walk += distance + strlen(nearest->key);
511 destwalk += distance + strlen(nearest->value);
515 /* analyze the string to find out whether this is an old config file (3.x)
516 * or a new config file (4.x). If it’s old, we run the converter script. */
517 int version = detect_version(buf);
519 /* We need to convert this v3 configuration */
520 char *converted = migrate_config(new, stbuf.st_size);
521 if (converted != NULL) {
523 printf("****************************************************************\n");
524 printf("NOTE: Automatically converted configuration file from v3 to v4.\n");
526 printf("Please convert your config file to v4. You can use this command:\n");
527 printf(" mv %s %s.O\n", f, f);
528 printf(" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
529 printf("****************************************************************\n");
535 printf("**********************************************************************\n");
536 printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
537 printf("was not correctly installed on your system?\n");
538 printf("**********************************************************************\n");
543 /* now lex/parse it */
546 context = scalloc(sizeof(struct context));
547 context->filename = f;
549 if (yyparse() != 0) {
550 fprintf(stderr, "Could not parse configfile\n");
554 check_for_duplicate_bindings(context);
556 if (context->has_errors) {
557 start_configerror_nagbar(f);
561 FREE(context->line_copy);
566 while (!SLIST_EMPTY(&variables)) {
567 current = SLIST_FIRST(&variables);
569 FREE(current->value);
570 SLIST_REMOVE_HEAD(&variables, variables);
578 %lex-param { struct context *context }
583 uint32_t *single_color;
584 struct Colortriple *color;
586 struct Binding *binding;
589 %token <number> NUMBER "<number>"
590 %token <string> WORD "<word>"
591 %token <string> STR "<string>"
592 %token <string> STR_NG "<string (non-greedy)>"
593 %token <string> HEXCOLOR "#<hex>"
594 %token <string> OUTPUT "<RandR output>"
597 %token TOKCOMMENT "<comment>"
598 %token TOKFONT "font"
599 %token TOKBINDSYM "bindsym"
600 %token <number> MODIFIER "<modifier>"
601 %token TOKCONTROL "control"
602 %token TOKSHIFT "shift"
603 %token TOKFLOATING_MODIFIER "floating_modifier"
604 %token <string> QUOTEDSTRING "<quoted string>"
605 %token TOKWORKSPACE "workspace"
606 %token TOKOUTPUT "output"
607 %token TOKASSIGN "assign"
609 %token TOKIPCSOCKET "ipc_socket"
610 %token TOKRESTARTSTATE "restart_state"
611 %token TOKEXEC "exec"
612 %token TOKEXEC_ALWAYS "exec_always"
613 %token <single_color> TOKSINGLECOLOR
614 %token <color> TOKCOLOR
616 %token TOKMODE "mode"
618 %token TOK_ORIENTATION "default_orientation"
619 %token TOK_HORIZ "horizontal"
620 %token TOK_VERT "vertical"
621 %token TOK_AUTO "auto"
622 %token TOK_WORKSPACE_LAYOUT "workspace_layout"
623 %token TOKNEWWINDOW "new_window"
624 %token TOKNEWFLOAT "new_float"
625 %token TOK_NORMAL "normal"
626 %token TOK_NONE "none"
627 %token TOK_1PIXEL "1pixel"
628 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
629 %token TOK_FORCE_FOCUS_WRAPPING "force_focus_wrapping"
630 %token TOK_FORCE_XINERAMA "force_xinerama"
631 %token TOK_WORKSPACE_AUTO_BAF "workspace_auto_back_and_forth"
632 %token TOKWORKSPACEBAR "workspace_bar"
633 %token TOK_DEFAULT "default"
634 %token TOK_STACKING "stacking"
635 %token TOK_TABBED "tabbed"
636 %token <number> TOKSTACKLIMIT "stack-limit"
637 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
638 %token TOK_IGNORE "ignore"
639 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
640 %token TOK_FOR_WINDOW "for_window"
642 %token TOK_BAR_OUTPUT "output (bar)"
643 %token TOK_BAR_TRAY_OUTPUT "tray_output"
644 %token TOK_BAR_SOCKET_PATH "socket_path"
645 %token TOK_BAR_MODE "mode"
646 %token TOK_BAR_HIDE "hide"
647 %token TOK_BAR_DOCK "dock"
648 %token TOK_BAR_POSITION "position"
649 %token TOK_BAR_BOTTOM "bottom"
650 %token TOK_BAR_TOP "top"
651 %token TOK_BAR_STATUS_COMMAND "status_command"
652 %token TOK_BAR_FONT "font"
653 %token TOK_BAR_WORKSPACE_BUTTONS "workspace_buttons"
654 %token TOK_BAR_VERBOSE "verbose"
655 %token TOK_BAR_COLORS "colors"
656 %token TOK_BAR_COLOR_BACKGROUND "background"
657 %token TOK_BAR_COLOR_STATUSLINE "statusline"
658 %token TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
659 %token TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
660 %token TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
661 %token TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
663 %token TOK_MARK "mark"
664 %token TOK_CLASS "class"
665 %token TOK_INSTANCE "instance"
666 %token TOK_WINDOW_ROLE "window_role"
668 %token TOK_CON_ID "con_id"
669 %token TOK_TITLE "title"
671 %type <binding> binding
672 %type <binding> bindcode
673 %type <binding> bindsym
674 %type <number> binding_modifiers
675 %type <number> binding_modifier
676 %type <number> direction
677 %type <number> layout_mode
678 %type <number> border_style
679 %type <number> new_window
680 %type <number> new_float
681 %type <number> colorpixel
683 %type <number> popup_setting
684 %type <number> bar_position_position
685 %type <number> bar_mode_mode
686 %type <string> command
687 %type <string> word_or_number
688 %type <string> optional_workspace_name
689 %type <string> workspace_name
690 %type <string> window_class
709 | focus_follows_mouse
710 | force_focus_wrapping
712 | workspace_back_and_forth
725 | popup_during_fullscreen
739 TAILQ_INSERT_TAIL(bindings, $1, bindings);
744 TOKBINDCODE bindcode { $$ = $2; }
745 | TOKBINDSYM bindsym { $$ = $2; }
749 binding_modifiers NUMBER command
751 printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
752 Binding *new = scalloc(sizeof(Binding));
763 binding_modifiers word_or_number command
765 printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
766 Binding *new = scalloc(sizeof(Binding));
777 TOK_FOR_WINDOW match command
779 if (match_is_empty(¤t_match)) {
780 ELOG("Match is empty, ignoring this for_window statement\n");
783 printf("\t should execute command %s for the criteria mentioned above\n", $3);
784 Assignment *assignment = scalloc(sizeof(Assignment));
785 assignment->type = A_COMMAND;
786 assignment->match = current_match;
787 assignment->dest.command = $3;
788 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
793 | matchstart criteria matchend
795 printf("match parsed\n");
803 match_init(¤t_match);
810 printf("match specification finished\n");
822 printf("criteria: class = %s\n", $3);
823 current_match.class = regex_new($3);
826 | TOK_INSTANCE '=' STR
828 printf("criteria: instance = %s\n", $3);
829 current_match.instance = regex_new($3);
832 | TOK_WINDOW_ROLE '=' STR
834 printf("criteria: window_role = %s\n", $3);
835 current_match.role = regex_new($3);
840 printf("criteria: id = %s\n", $3);
842 long parsed = strtol($3, &end, 10);
843 if (parsed == LONG_MIN ||
844 parsed == LONG_MAX ||
846 (end && *end != '\0')) {
847 ELOG("Could not parse con id \"%s\"\n", $3);
849 current_match.con_id = (Con*)parsed;
850 printf("id as int = %p\n", current_match.con_id);
855 printf("criteria: window id = %s\n", $3);
857 long parsed = strtol($3, &end, 10);
858 if (parsed == LONG_MIN ||
859 parsed == LONG_MAX ||
861 (end && *end != '\0')) {
862 ELOG("Could not parse window id \"%s\"\n", $3);
864 current_match.id = parsed;
865 printf("window id as int = %d\n", current_match.id);
870 printf("criteria: mark = %s\n", $3);
871 current_match.mark = regex_new($3);
876 printf("criteria: title = %s\n", $3);
877 current_match.title = regex_new($3);
888 asprintf(&$$, "%d", $1);
893 TOKMODE QUOTEDSTRING '{' modelines '}'
895 if (strcasecmp($2, "default") == 0) {
896 printf("You cannot use the name \"default\" for your mode\n");
899 printf("\t now in mode %s\n", $2);
900 printf("\t current bindings = %p\n", current_bindings);
902 TAILQ_FOREACH(binding, current_bindings, bindings) {
903 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
904 binding->mods, binding->keycode, binding->symbol, binding->command);
907 struct Mode *mode = scalloc(sizeof(struct Mode));
909 mode->bindings = current_bindings;
910 current_bindings = NULL;
911 SLIST_INSERT_HEAD(&modes, mode, modes);
925 if (current_bindings == NULL) {
926 current_bindings = scalloc(sizeof(struct bindings_head));
927 TAILQ_INIT(current_bindings);
930 TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
935 TOK_BAR '{' barlines '}'
937 printf("\t new bar configuration finished, saving.\n");
938 /* Generate a unique ID for this bar */
939 current_bar.id = sstrdup("bar-XXXXXX");
940 /* This works similar to mktemp in that it replaces the last six X with
941 * random letters, but without the restriction that the given buffer
942 * has to contain a valid path name. */
943 char *x = current_bar.id + strlen("bar-");
945 *(x++) = (rand() % 26) + 'a';
948 /* Copy the current (static) structure into a dynamically allocated
949 * one, then cleanup our static one. */
950 Barconfig *bar_config = scalloc(sizeof(Barconfig));
951 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
952 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
954 memset(¤t_bar, '\0', sizeof(Barconfig));
971 | bar_workspace_buttons
975 | bar_color_background
976 | bar_color_statusline
977 | bar_color_focused_workspace
978 | bar_color_active_workspace
979 | bar_color_inactive_workspace
980 | bar_color_urgent_workspace
984 TOK_BAR_STATUS_COMMAND STR
986 DLOG("should add status command %s\n", $2);
987 FREE(current_bar.status_command);
988 current_bar.status_command = $2;
995 DLOG("bar output %s\n", $2);
996 int new_outputs = current_bar.num_outputs + 1;
997 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
998 current_bar.outputs[current_bar.num_outputs] = $2;
999 current_bar.num_outputs = new_outputs;
1004 TOK_BAR_TRAY_OUTPUT STR
1006 DLOG("tray %s\n", $2);
1007 FREE(current_bar.tray_output);
1008 current_bar.tray_output = $2;
1013 TOK_BAR_POSITION bar_position_position
1015 DLOG("position %d\n", $2);
1016 current_bar.position = $2;
1020 bar_position_position:
1021 TOK_BAR_TOP { $$ = P_TOP; }
1022 | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1026 TOK_BAR_MODE bar_mode_mode
1028 DLOG("mode %d\n", $2);
1029 current_bar.mode = $2;
1034 TOK_BAR_HIDE { $$ = M_HIDE; }
1035 | TOK_BAR_DOCK { $$ = M_DOCK; }
1041 DLOG("font %s\n", $2);
1042 FREE(current_bar.font);
1043 current_bar.font = $2;
1047 bar_workspace_buttons:
1048 TOK_BAR_WORKSPACE_BUTTONS bool
1050 DLOG("workspace_buttons = %d\n", $2);
1051 /* We store this inverted to make the default setting right when
1052 * initializing the struct with zero. */
1053 current_bar.hide_workspace_buttons = !($2);
1058 TOK_BAR_VERBOSE bool
1060 DLOG("verbose = %d\n", $2);
1061 current_bar.verbose = $2;
1066 TOK_BAR_SOCKET_PATH STR
1068 DLOG("socket_path = %s\n", $2);
1069 FREE(current_bar.socket_path);
1070 current_bar.socket_path = $2;
1075 TOK_BAR_COLORS '{' barlines '}'
1077 /* At the moment, the TOK_BAR_COLORS token is only to make the config
1078 * friendlier for humans. We might change this in the future if it gets
1083 bar_color_background:
1084 TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1086 DLOG("background = %s\n", $2);
1087 current_bar.colors.background = $2;
1091 bar_color_statusline:
1092 TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1094 DLOG("statusline = %s\n", $2);
1095 current_bar.colors.statusline = $2;
1099 bar_color_focused_workspace:
1100 TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1102 DLOG("focused_ws = %s and %s\n", $2, $3);
1103 current_bar.colors.focused_workspace_text = $2;
1104 current_bar.colors.focused_workspace_bg = $3;
1108 bar_color_active_workspace:
1109 TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1111 DLOG("active_ws = %s and %s\n", $2, $3);
1112 current_bar.colors.active_workspace_text = $2;
1113 current_bar.colors.active_workspace_bg = $3;
1117 bar_color_inactive_workspace:
1118 TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1120 DLOG("inactive_ws = %s and %s\n", $2, $3);
1121 current_bar.colors.inactive_workspace_text = $2;
1122 current_bar.colors.inactive_workspace_bg = $3;
1126 bar_color_urgent_workspace:
1127 TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1129 DLOG("urgent_ws = %s and %s\n", $2, $3);
1130 current_bar.colors.urgent_workspace_text = $2;
1131 current_bar.colors.urgent_workspace_bg = $3;
1136 TOKFLOATING_MODIFIER binding_modifiers
1138 DLOG("floating modifier = %d\n", $2);
1139 config.floating_modifier = $2;
1144 TOK_ORIENTATION direction
1146 DLOG("New containers should start with split direction %d\n", $2);
1147 config.default_orientation = $2;
1152 TOK_HORIZ { $$ = HORIZ; }
1153 | TOK_VERT { $$ = VERT; }
1154 | TOK_AUTO { $$ = NO_ORIENTATION; }
1158 TOK_WORKSPACE_LAYOUT layout_mode
1160 DLOG("new containers will be in mode %d\n", $2);
1161 config.default_layout = $2;
1164 /* We also need to change the layout of the already existing
1165 * workspaces here. Workspaces may exist at this point because
1166 * of the other directives which are modifying workspaces
1167 * (setting the preferred screen or name). While the workspace
1168 * objects are already created, they have never been used.
1169 * Thus, the user very likely awaits the default container mode
1170 * to trigger in this case, regardless of where it is inside
1171 * his configuration file. */
1173 TAILQ_FOREACH(ws, workspaces, workspaces) {
1174 if (ws->table == NULL)
1176 switch_layout_mode(global_conn,
1178 config.container_mode);
1182 | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1184 DLOG("stack-limit %d with val %d\n", $3, $4);
1185 config.container_stack_limit = $3;
1186 config.container_stack_limit_value = $4;
1189 /* See the comment above */
1191 TAILQ_FOREACH(ws, workspaces, workspaces) {
1192 if (ws->table == NULL)
1194 Container *con = ws->table[0][0];
1195 con->stack_limit = config.container_stack_limit;
1196 con->stack_limit_value = config.container_stack_limit_value;
1203 TOK_DEFAULT { $$ = L_DEFAULT; }
1204 | TOK_STACKING { $$ = L_STACKED; }
1205 | TOK_TABBED { $$ = L_TABBED; }
1209 TOKNEWWINDOW border_style
1211 DLOG("new windows should start with border style %d\n", $2);
1212 config.default_border = $2;
1217 TOKNEWFLOAT border_style
1219 DLOG("new floating windows should start with border style %d\n", $2);
1220 config.default_floating_border = $2;
1225 TOK_NORMAL { $$ = BS_NORMAL; }
1226 | TOK_NONE { $$ = BS_NONE; }
1227 | TOK_1PIXEL { $$ = BS_1PIXEL; }
1237 DLOG("checking word \"%s\"\n", $1);
1238 $$ = (strcasecmp($1, "yes") == 0 ||
1239 strcasecmp($1, "true") == 0 ||
1240 strcasecmp($1, "on") == 0 ||
1241 strcasecmp($1, "enable") == 0 ||
1242 strcasecmp($1, "active") == 0);
1246 focus_follows_mouse:
1247 TOKFOCUSFOLLOWSMOUSE bool
1249 DLOG("focus follows mouse = %d\n", $2);
1250 config.disable_focus_follows_mouse = !($2);
1254 force_focus_wrapping:
1255 TOK_FORCE_FOCUS_WRAPPING bool
1257 DLOG("force focus wrapping = %d\n", $2);
1258 config.force_focus_wrapping = $2;
1263 TOK_FORCE_XINERAMA bool
1265 DLOG("force xinerama = %d\n", $2);
1266 config.force_xinerama = $2;
1270 workspace_back_and_forth:
1271 TOK_WORKSPACE_AUTO_BAF bool
1273 DLOG("automatic workspace back-and-forth = %d\n", $2);
1274 config.workspace_auto_back_and_forth = $2;
1279 TOKWORKSPACEBAR bool
1281 DLOG("workspace bar = %d\n", $2);
1282 config.disable_workspace_bar = !($2);
1287 TOKWORKSPACE NUMBER TOKOUTPUT OUTPUT optional_workspace_name
1291 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1293 char *ws_name = NULL;
1295 asprintf(&ws_name, "%d", ws_num);
1300 DLOG("Should assign workspace %s to output %s\n", ws_name, $4);
1301 /* Check for earlier assignments of the same workspace so that we
1302 * don’t have assignments of a single workspace to different
1304 struct Workspace_Assignment *assignment;
1305 bool duplicate = false;
1306 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1307 if (strcasecmp(assignment->name, ws_name) == 0) {
1308 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1310 assignment->output = $4;
1315 assignment = scalloc(sizeof(struct Workspace_Assignment));
1316 assignment->name = ws_name;
1317 assignment->output = $4;
1318 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1322 | TOKWORKSPACE NUMBER workspace_name
1326 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1328 DLOG("workspace name to: %s\n", $3);
1330 if ($<string>3 != NULL) {
1331 workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1339 optional_workspace_name:
1340 /* empty */ { $$ = NULL; }
1341 | workspace_name { $$ = $1; }
1345 QUOTEDSTRING { $$ = $1; }
1351 TOKASSIGN window_class STR
1353 /* This is the old, deprecated form of assignments. It’s provided for
1354 * compatibility in version (4.1, 4.2, 4.3) and will be removed
1355 * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1356 ELOG("You are using the old assign syntax (without criteria). "
1357 "Please see the User's Guide for the new syntax and fix "
1358 "your config file.\n");
1359 context->has_errors = true;
1360 printf("assignment of %s to *%s*\n", $2, $3);
1361 char *workspace = $3;
1362 char *criteria = $2;
1364 Assignment *assignment = scalloc(sizeof(Assignment));
1365 Match *match = &(assignment->match);
1368 char *separator = NULL;
1369 if ((separator = strchr(criteria, '/')) != NULL) {
1370 *(separator++) = '\0';
1372 if (asprintf(&pattern, "(?i)%s", separator) == -1) {
1373 ELOG("asprintf failed\n");
1376 match->title = regex_new(pattern);
1378 printf(" title = %s\n", separator);
1380 if (*criteria != '\0') {
1382 if (asprintf(&pattern, "(?i)%s", criteria) == -1) {
1383 ELOG("asprintf failed\n");
1386 match->class = regex_new(pattern);
1388 printf(" class = %s\n", criteria);
1392 /* Compatibility with older versions: If the assignment target starts
1393 * with ~, we create the equivalent of:
1395 * for_window [class="foo"] floating enable
1397 if (*workspace == '~') {
1399 if (*workspace == '\0') {
1400 /* This assignment was *only* for floating */
1401 assignment->type = A_COMMAND;
1402 assignment->dest.command = sstrdup("floating enable");
1403 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1406 /* Create a new assignment and continue afterwards */
1407 Assignment *floating = scalloc(sizeof(Assignment));
1408 match_copy(&(floating->match), match);
1409 floating->type = A_COMMAND;
1410 floating->dest.command = sstrdup("floating enable");
1411 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1415 assignment->type = A_TO_WORKSPACE;
1416 assignment->dest.workspace = workspace;
1417 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1419 | TOKASSIGN match STR
1421 if (match_is_empty(¤t_match)) {
1422 ELOG("Match is empty, ignoring this assignment\n");
1425 printf("new assignment, using above criteria, to workspace %s\n", $3);
1426 Assignment *assignment = scalloc(sizeof(Assignment));
1427 assignment->match = current_match;
1428 assignment->type = A_TO_WORKSPACE;
1429 assignment->dest.workspace = $3;
1430 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1442 config.ipc_socket_path = $2;
1449 config.restart_state_path = $2;
1456 struct Autostart *new = smalloc(sizeof(struct Autostart));
1458 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1465 struct Autostart *new = smalloc(sizeof(struct Autostart));
1467 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1474 ELOG("The terminal option is DEPRECATED and has no effect. "
1475 "Please remove it from your configuration file.\n");
1482 config.font = load_font($2, true);
1483 printf("font %s\n", $2);
1489 TOKSINGLECOLOR colorpixel
1491 uint32_t *dest = $1;
1497 TOKCOLOR colorpixel colorpixel colorpixel
1499 struct Colortriple *dest = $1;
1502 dest->background = $3;
1510 $$ = get_colorpixel($1);
1517 /* NULL */ { $$ = 0; }
1519 | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1520 | binding_modifiers '+' { $$ = $1; }
1524 MODIFIER { $$ = $1; }
1525 | TOKCONTROL { $$ = BIND_CONTROL; }
1526 | TOKSHIFT { $$ = BIND_SHIFT; }
1529 popup_during_fullscreen:
1530 TOK_POPUP_DURING_FULLSCREEN popup_setting
1532 DLOG("popup_during_fullscreen setting: %d\n", $2);
1533 config.popup_during_fullscreen = $2;
1538 TOK_IGNORE { $$ = PDF_IGNORE; }
1539 | TOK_LEAVE_FULLSCREEN { $$ = PDF_LEAVE_FULLSCREEN; }