3 * vim:ts=4:sw=4:expandtab
14 static pid_t configerror_pid = -1;
16 static Match current_match;
17 static Barconfig current_bar;
18 /* The pattern which was specified by the user, for example -misc-fixed-*. We
19 * store this in a separate variable because in the i3 config struct we just
20 * store the i3Font. */
21 static char *font_pattern;
23 typedef struct yy_buffer_state *YY_BUFFER_STATE;
24 extern int yylex(struct context *context);
25 extern int yyparse(void);
26 extern int yylex_destroy(void);
28 YY_BUFFER_STATE yy_scan_string(const char *);
30 static struct bindings_head *current_bindings;
31 static struct context *context;
33 /* We don’t need yydebug for now, as we got decent error messages using
34 * yyerror(). Should you ever want to extend the parser, it might be handy
35 * to just comment it in again, so it stays here. */
38 void yyerror(const char *error_message) {
39 context->has_errors = true;
42 ELOG("CONFIG: %s\n", error_message);
43 ELOG("CONFIG: in file \"%s\", line %d:\n",
44 context->filename, context->line_number);
45 ELOG("CONFIG: %s\n", context->line_copy);
46 char buffer[context->last_column+1];
47 buffer[context->last_column] = '\0';
48 for (int c = 1; c <= context->last_column; c++)
49 buffer[c-1] = (c >= context->first_column ? '^' : ' ');
50 ELOG("CONFIG: %s\n", buffer);
59 * Goes through each line of buf (separated by \n) and checks for statements /
60 * commands which only occur in i3 v4 configuration files. If it finds any, it
61 * returns version 4, otherwise it returns version 3.
64 static int detect_version(char *buf) {
67 while (*walk != '\0') {
73 /* check for some v4-only statements */
74 if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
75 strncasecmp(line, "force_focus_wrapping", strlen("force_focus_wrapping")) == 0 ||
76 strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
77 strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
78 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
82 /* if this is a bind statement, we can check the command */
83 if (strncasecmp(line, "bind", strlen("bind")) == 0) {
84 char *bind = strchr(line, ' ');
87 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
91 if ((bind = strchr(bind, ' ')) == NULL)
93 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
97 if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
98 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
99 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
100 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
101 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
102 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
103 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
104 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
105 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
106 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0) {
107 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
113 /* advance to the next line */
122 * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
125 * Returns the converted config file or NULL if there was an error (for
126 * example the script could not be found in $PATH or the i3 executable’s
130 static char *migrate_config(char *input, off_t size) {
134 if (pipe(writepipe) != 0 ||
135 pipe(readpipe) != 0) {
136 warn("migrate_config: Could not create pipes");
142 warn("Could not fork()");
148 /* close writing end of writepipe, connect reading side to stdin */
150 dup2(writepipe[0], 0);
152 /* close reading end of readpipe, connect writing side to stdout */
154 dup2(readpipe[1], 1);
156 static char *argv[] = {
157 NULL, /* will be replaced by the executable path */
160 exec_i3_utility("i3-migrate-config-to-v4", argv);
165 /* close reading end of the writepipe (connected to the script’s stdin) */
168 /* write the whole config file to the pipe, the script will read everything
172 while (written < size) {
173 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
174 warn("Could not write to pipe");
181 /* close writing end of the readpipe (connected to the script’s stdout) */
184 /* read the script’s output */
185 int conv_size = 65535;
186 char *converted = malloc(conv_size);
189 if (read_bytes == conv_size) {
191 converted = realloc(converted, conv_size);
193 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
195 warn("Cannot read from pipe");
202 /* get the returncode */
205 if (!WIFEXITED(status)) {
206 fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
210 int returncode = WEXITSTATUS(status);
211 if (returncode != 0) {
212 fprintf(stderr, "Migration process exit code was != 0\n");
213 if (returncode == 2) {
214 fprintf(stderr, "could not start the migration script\n");
215 /* TODO: script was not found. tell the user to fix his system or create a v4 config */
216 } else if (returncode == 1) {
217 fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
218 fprintf(stderr, "# i3 config file (v4)\n");
219 /* TODO: nag the user with a message to include a hint for i3 in his config file */
228 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
229 * it exited (or could not be started, depending on the exit code).
232 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
233 ev_child_stop(EV_A_ watcher);
234 if (!WIFEXITED(watcher->rstatus)) {
235 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
239 int exitcode = WEXITSTATUS(watcher->rstatus);
240 printf("i3-nagbar process exited with status %d\n", exitcode);
242 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
245 configerror_pid = -1;
248 /* We need ev >= 4 for the following code. Since it is not *that* important (it
249 * only makes sure that there are no i3-nagbar instances left behind) we still
250 * support old systems with libev 3. */
251 #if EV_VERSION_MAJOR >= 4
253 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
254 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
257 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
258 if (configerror_pid != -1) {
259 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", configerror_pid);
260 kill(configerror_pid, SIGKILL);
266 * Starts an i3-nagbar process which alerts the user that his configuration
267 * file contains one or more errors. Also offers two buttons: One to launch an
268 * $EDITOR on the config file and another one to launch a $PAGER on the error
272 static void start_configerror_nagbar(const char *config_path) {
273 if (only_check_config)
276 fprintf(stderr, "Starting i3-nagbar due to configuration errors\n");
277 configerror_pid = fork();
278 if (configerror_pid == -1) {
279 warn("Could not fork()");
284 if (configerror_pid == 0) {
287 sasprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path);
288 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
290 NULL, /* will be replaced by the executable path */
292 (context->has_errors ? "error" : "warning"),
294 (context->has_errors ?
295 "You have an error in your i3 config file!" :
296 "Your config is outdated. Please fix the warnings to make sure everything works."),
300 (errorfilename ? "-b" : NULL),
301 (context->has_errors ? "show errors" : "show warnings"),
305 exec_i3_utility("i3-nagbar", argv);
309 /* install a child watcher */
310 ev_child *child = smalloc(sizeof(ev_child));
311 ev_child_init(child, &nagbar_exited, configerror_pid, 0);
312 ev_child_start(main_loop, child);
314 /* We need ev >= 4 for the following code. Since it is not *that* important (it
315 * only makes sure that there are no i3-nagbar instances left behind) we still
316 * support old systems with libev 3. */
317 #if EV_VERSION_MAJOR >= 4
318 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
320 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
321 ev_cleanup_init(cleanup, nagbar_cleanup);
322 ev_cleanup_start(main_loop, cleanup);
327 * Kills the configerror i3-nagbar process, if any.
329 * Called when reloading/restarting.
331 * If wait_for_it is set (restarting), this function will waitpid(), otherwise,
332 * ev is assumed to handle it (reloading).
335 void kill_configerror_nagbar(bool wait_for_it) {
336 if (configerror_pid == -1)
339 if (kill(configerror_pid, SIGTERM) == -1)
340 warn("kill(configerror_nagbar) failed");
345 /* When restarting, we don’t enter the ev main loop anymore and after the
346 * exec(), our old pid is no longer watched. So, ev won’t handle SIGCHLD
347 * for us and we would end up with a <defunct> process. Therefore we
349 waitpid(configerror_pid, NULL, 0);
353 * Checks for duplicate key bindings (the same keycode or keysym is configured
354 * more than once). If a duplicate binding is found, a message is printed to
355 * stderr and the has_errors variable is set to true, which will start
359 static void check_for_duplicate_bindings(struct context *context) {
360 Binding *bind, *current;
361 TAILQ_FOREACH(current, bindings, bindings) {
362 TAILQ_FOREACH(bind, bindings, bindings) {
363 /* Abort when we reach the current keybinding, only check the
368 /* Check if one is using keysym while the other is using bindsym.
370 /* XXX: It should be checked at a later place (when translating the
371 * keysym to keycodes) if there are any duplicates */
372 if ((bind->symbol == NULL && current->symbol != NULL) ||
373 (bind->symbol != NULL && current->symbol == NULL))
376 /* If bind is NULL, current has to be NULL, too (see above).
377 * If the keycodes differ, it can't be a duplicate. */
378 if (bind->symbol != NULL &&
379 strcasecmp(bind->symbol, current->symbol) != 0)
382 /* Check if the keycodes or modifiers are different. If so, they
383 * can't be duplicate */
384 if (bind->keycode != current->keycode ||
385 bind->mods != current->mods)
387 context->has_errors = true;
388 if (current->keycode != 0) {
389 ELOG("Duplicate keybinding in config file:\n modmask %d with keycode %d, command \"%s\"\n",
390 current->mods, current->keycode, current->command);
392 ELOG("Duplicate keybinding in config file:\n modmask %d with keysym %s, command \"%s\"\n",
393 current->mods, current->symbol, current->command);
399 static void migrate_i3bar_exec(struct Autostart *exec) {
400 ELOG("**********************************************************************\n");
401 ELOG("IGNORING exec command: %s\n", exec->command);
402 ELOG("It contains \"i3bar\". Since i3 v4.1, i3bar will be automatically started\n");
403 ELOG("for each 'bar' configuration block in your i3 config. Please remove the exec\n");
404 ELOG("line and add the following to your i3 config:\n");
407 ELOG(" status_command i3status\n");
409 ELOG("**********************************************************************\n");
411 /* Generate a dummy bar configuration */
412 Barconfig *bar_config = scalloc(sizeof(Barconfig));
413 /* The hard-coded ID is not a problem. It does not conflict with the
414 * auto-generated bar IDs and having multiple hard-coded IDs is irrelevant
415 * – they all just contain status_command = i3status */
416 bar_config->id = sstrdup("migrate-bar");
417 bar_config->status_command = sstrdup("i3status");
418 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
420 /* Trigger an i3-nagbar */
421 context->has_warnings = true;
424 void parse_file(const char *f) {
425 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
426 int fd, ret, read_bytes = 0;
430 char buffer[1026], key[512], value[512];
432 if ((fd = open(f, O_RDONLY)) == -1)
433 die("Could not open configuration file: %s\n", strerror(errno));
435 if (fstat(fd, &stbuf) == -1)
436 die("Could not fstat file: %s\n", strerror(errno));
438 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
439 while (read_bytes < stbuf.st_size) {
440 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
441 die("Could not read(): %s\n", strerror(errno));
445 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
446 die("Could not lseek: %s\n", strerror(errno));
448 if ((fstr = fdopen(fd, "r")) == NULL)
449 die("Could not fdopen: %s\n", strerror(errno));
451 while (!feof(fstr)) {
452 if (fgets(buffer, 1024, fstr) == NULL) {
455 die("Could not read configuration file\n");
458 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
459 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
460 key[0] == '#' || strlen(key) < 3)
463 if (strcasecmp(key, "set") == 0) {
464 if (value[0] != '$') {
465 ELOG("Malformed variable assignment, name has to start with $\n");
469 /* get key/value for this variable */
470 char *v_key = value, *v_value;
471 if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
472 ELOG("Malformed variable assignment, need a value\n");
476 if (!(v_value = strstr(value, " ")))
477 v_value = strstr(value, "\t");
481 struct Variable *new = scalloc(sizeof(struct Variable));
482 new->key = sstrdup(v_key);
483 new->value = sstrdup(v_value);
484 SLIST_INSERT_HEAD(&variables, new, variables);
485 DLOG("Got new variable %s = %s\n", v_key, v_value);
491 /* For every custom variable, see how often it occurs in the file and
492 * how much extra bytes it requires when replaced. */
493 struct Variable *current, *nearest;
495 /* We need to copy the buffer because we need to invalidate the
496 * variables (otherwise we will count them twice, which is bad when
497 * 'extra' is negative) */
498 char *bufcopy = sstrdup(buf);
499 SLIST_FOREACH(current, &variables, variables) {
500 int extra = (strlen(current->value) - strlen(current->key));
503 next < (bufcopy + stbuf.st_size) &&
504 (next = strcasestr(next, current->key)) != NULL;
505 next += strlen(current->key)) {
507 extra_bytes += extra;
512 /* Then, allocate a new buffer and copy the file over to the new one,
513 * but replace occurences of our variables */
514 char *walk = buf, *destwalk;
515 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
517 while (walk < (buf + stbuf.st_size)) {
518 /* Find the next variable */
519 SLIST_FOREACH(current, &variables, variables)
520 current->next_match = strcasestr(walk, current->key);
522 int distance = stbuf.st_size;
523 SLIST_FOREACH(current, &variables, variables) {
524 if (current->next_match == NULL)
526 if ((current->next_match - walk) < distance) {
527 distance = (current->next_match - walk);
531 if (nearest == NULL) {
532 /* If there are no more variables, we just copy the rest */
533 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
534 destwalk += (buf + stbuf.st_size) - walk;
538 /* Copy until the next variable, then copy its value */
539 strncpy(destwalk, walk, distance);
540 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
541 walk += distance + strlen(nearest->key);
542 destwalk += distance + strlen(nearest->value);
546 /* analyze the string to find out whether this is an old config file (3.x)
547 * or a new config file (4.x). If it’s old, we run the converter script. */
548 int version = detect_version(buf);
550 /* We need to convert this v3 configuration */
551 char *converted = migrate_config(new, stbuf.st_size);
552 if (converted != NULL) {
554 printf("****************************************************************\n");
555 printf("NOTE: Automatically converted configuration file from v3 to v4.\n");
557 printf("Please convert your config file to v4. You can use this command:\n");
558 printf(" mv %s %s.O\n", f, f);
559 printf(" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
560 printf("****************************************************************\n");
566 printf("**********************************************************************\n");
567 printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
568 printf("was not correctly installed on your system?\n");
569 printf("**********************************************************************\n");
574 /* now lex/parse it */
577 context = scalloc(sizeof(struct context));
578 context->filename = f;
580 if (yyparse() != 0) {
581 fprintf(stderr, "Could not parse configfile\n");
585 check_for_duplicate_bindings(context);
587 /* XXX: The following code will be removed in i3 v4.3 (three releases from
588 * now, as of 2011-10-22) */
589 /* Check for any exec or exec_always lines starting i3bar. We remove these
590 * and add a bar block instead. Additionally, a i3-nagbar warning (not an
591 * error) will be displayed so that users update their config file. */
592 struct Autostart *exec, *next;
593 for (exec = TAILQ_FIRST(&autostarts); exec; ) {
594 next = TAILQ_NEXT(exec, autostarts);
595 if (strstr(exec->command, "i3bar") != NULL) {
596 migrate_i3bar_exec(exec);
597 TAILQ_REMOVE(&autostarts, exec, autostarts);
602 for (exec = TAILQ_FIRST(&autostarts_always); exec; ) {
603 next = TAILQ_NEXT(exec, autostarts_always);
604 if (strstr(exec->command, "i3bar") != NULL) {
605 migrate_i3bar_exec(exec);
606 TAILQ_REMOVE(&autostarts_always, exec, autostarts_always);
611 if (context->has_errors || context->has_warnings) {
612 start_configerror_nagbar(f);
616 FREE(context->line_copy);
622 while (!SLIST_EMPTY(&variables)) {
623 current = SLIST_FIRST(&variables);
625 FREE(current->value);
626 SLIST_REMOVE_HEAD(&variables, variables);
634 %lex-param { struct context *context }
639 uint32_t *single_color;
640 struct Colortriple *color;
642 struct Binding *binding;
645 %token <number> NUMBER "<number>"
646 %token <string> WORD "<word>"
647 %token <string> STR "<string>"
648 %token <string> STR_NG "<string (non-greedy)>"
649 %token <string> HEXCOLOR "#<hex>"
650 %token <string> OUTPUT "<RandR output>"
653 %token TOKCOMMENT "<comment>"
654 %token TOKFONT "font"
655 %token TOKBINDSYM "bindsym"
656 %token <number> MODIFIER "<modifier>"
657 %token TOKCONTROL "control"
658 %token TOKSHIFT "shift"
659 %token TOKFLOATING_MODIFIER "floating_modifier"
660 %token <string> QUOTEDSTRING "<quoted string>"
661 %token TOKWORKSPACE "workspace"
662 %token TOKOUTPUT "output"
663 %token TOKASSIGN "assign"
665 %token TOKIPCSOCKET "ipc_socket"
666 %token TOKRESTARTSTATE "restart_state"
667 %token TOKEXEC "exec"
668 %token TOKEXEC_ALWAYS "exec_always"
669 %token <single_color> TOKSINGLECOLOR
670 %token <color> TOKCOLOR
672 %token TOKMODE "mode"
674 %token TOK_ORIENTATION "default_orientation"
675 %token TOK_HORIZ "horizontal"
676 %token TOK_VERT "vertical"
677 %token TOK_AUTO "auto"
678 %token TOK_WORKSPACE_LAYOUT "workspace_layout"
679 %token TOKNEWWINDOW "new_window"
680 %token TOKNEWFLOAT "new_float"
681 %token TOK_NORMAL "normal"
682 %token TOK_NONE "none"
683 %token TOK_1PIXEL "1pixel"
684 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
685 %token TOK_FORCE_FOCUS_WRAPPING "force_focus_wrapping"
686 %token TOK_FORCE_XINERAMA "force_xinerama"
687 %token TOK_WORKSPACE_AUTO_BAF "workspace_auto_back_and_forth"
688 %token TOKWORKSPACEBAR "workspace_bar"
689 %token TOK_DEFAULT "default"
690 %token TOK_STACKING "stacking"
691 %token TOK_TABBED "tabbed"
692 %token <number> TOKSTACKLIMIT "stack-limit"
693 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
694 %token TOK_IGNORE "ignore"
695 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
696 %token TOK_FOR_WINDOW "for_window"
698 %token TOK_BAR_OUTPUT "output (bar)"
699 %token TOK_BAR_TRAY_OUTPUT "tray_output"
700 %token TOK_BAR_SOCKET_PATH "socket_path"
701 %token TOK_BAR_MODE "mode (bar)"
702 %token TOK_BAR_HIDE "hide"
703 %token TOK_BAR_DOCK "dock"
704 %token TOK_BAR_POSITION "position"
705 %token TOK_BAR_BOTTOM "bottom"
706 %token TOK_BAR_TOP "top"
707 %token TOK_BAR_STATUS_COMMAND "status_command"
708 %token TOK_BAR_I3BAR_COMMAND "i3bar_command"
709 %token TOK_BAR_FONT "font (bar)"
710 %token TOK_BAR_WORKSPACE_BUTTONS "workspace_buttons"
711 %token TOK_BAR_VERBOSE "verbose"
712 %token TOK_BAR_COLORS "colors"
713 %token TOK_BAR_COLOR_BACKGROUND "background"
714 %token TOK_BAR_COLOR_STATUSLINE "statusline"
715 %token TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
716 %token TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
717 %token TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
718 %token TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
719 %token TOK_NO_STARTUP_ID "--no-startup-id"
721 %token TOK_MARK "mark"
722 %token TOK_CLASS "class"
723 %token TOK_INSTANCE "instance"
724 %token TOK_WINDOW_ROLE "window_role"
726 %token TOK_CON_ID "con_id"
727 %token TOK_TITLE "title"
729 %type <binding> binding
730 %type <binding> bindcode
731 %type <binding> bindsym
732 %type <number> binding_modifiers
733 %type <number> binding_modifier
734 %type <number> direction
735 %type <number> layout_mode
736 %type <number> border_style
737 %type <number> new_window
738 %type <number> new_float
739 %type <number> colorpixel
741 %type <number> popup_setting
742 %type <number> bar_position_position
743 %type <number> bar_mode_mode
744 %type <number> optional_no_startup_id
745 %type <string> command
746 %type <string> word_or_number
747 %type <string> qstring_or_number
748 %type <string> optional_workspace_name
749 %type <string> workspace_name
750 %type <string> window_class
769 | focus_follows_mouse
770 | force_focus_wrapping
772 | workspace_back_and_forth
785 | popup_during_fullscreen
799 TAILQ_INSERT_TAIL(bindings, $1, bindings);
804 TOKBINDCODE bindcode { $$ = $2; }
805 | TOKBINDSYM bindsym { $$ = $2; }
809 binding_modifiers NUMBER command
811 printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
812 Binding *new = scalloc(sizeof(Binding));
823 binding_modifiers word_or_number command
825 printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
826 Binding *new = scalloc(sizeof(Binding));
837 TOK_FOR_WINDOW match command
839 if (match_is_empty(¤t_match)) {
840 ELOG("Match is empty, ignoring this for_window statement\n");
843 printf("\t should execute command %s for the criteria mentioned above\n", $3);
844 Assignment *assignment = scalloc(sizeof(Assignment));
845 assignment->type = A_COMMAND;
846 assignment->match = current_match;
847 assignment->dest.command = $3;
848 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
853 | matchstart criteria matchend
855 printf("match parsed\n");
863 match_init(¤t_match);
870 printf("match specification finished\n");
882 printf("criteria: class = %s\n", $3);
883 current_match.class = regex_new($3);
886 | TOK_INSTANCE '=' STR
888 printf("criteria: instance = %s\n", $3);
889 current_match.instance = regex_new($3);
892 | TOK_WINDOW_ROLE '=' STR
894 printf("criteria: window_role = %s\n", $3);
895 current_match.role = regex_new($3);
900 printf("criteria: id = %s\n", $3);
902 long parsed = strtol($3, &end, 10);
903 if (parsed == LONG_MIN ||
904 parsed == LONG_MAX ||
906 (end && *end != '\0')) {
907 ELOG("Could not parse con id \"%s\"\n", $3);
909 current_match.con_id = (Con*)parsed;
910 printf("id as int = %p\n", current_match.con_id);
915 printf("criteria: window id = %s\n", $3);
917 long parsed = strtol($3, &end, 10);
918 if (parsed == LONG_MIN ||
919 parsed == LONG_MAX ||
921 (end && *end != '\0')) {
922 ELOG("Could not parse window id \"%s\"\n", $3);
924 current_match.id = parsed;
925 printf("window id as int = %d\n", current_match.id);
930 printf("criteria: mark = %s\n", $3);
931 current_match.mark = regex_new($3);
936 printf("criteria: title = %s\n", $3);
937 current_match.title = regex_new($3);
944 | NUMBER { sasprintf(&$$, "%d", $1); }
951 sasprintf(&$$, "%d", $1);
956 TOKMODE QUOTEDSTRING '{' modelines '}'
958 if (strcasecmp($2, "default") == 0) {
959 printf("You cannot use the name \"default\" for your mode\n");
962 printf("\t now in mode %s\n", $2);
963 printf("\t current bindings = %p\n", current_bindings);
965 TAILQ_FOREACH(binding, current_bindings, bindings) {
966 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
967 binding->mods, binding->keycode, binding->symbol, binding->command);
970 struct Mode *mode = scalloc(sizeof(struct Mode));
972 mode->bindings = current_bindings;
973 current_bindings = NULL;
974 SLIST_INSERT_HEAD(&modes, mode, modes);
988 if (current_bindings == NULL) {
989 current_bindings = scalloc(sizeof(struct bindings_head));
990 TAILQ_INIT(current_bindings);
993 TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
998 TOK_BAR '{' barlines '}'
1000 printf("\t new bar configuration finished, saving.\n");
1001 /* Generate a unique ID for this bar */
1002 current_bar.id = sstrdup("bar-XXXXXX");
1003 /* This works similar to mktemp in that it replaces the last six X with
1004 * random letters, but without the restriction that the given buffer
1005 * has to contain a valid path name. */
1006 char *x = current_bar.id + strlen("bar-");
1007 while (*x != '\0') {
1008 *(x++) = (rand() % 26) + 'a';
1011 /* If no font was explicitly set, we use the i3 font as default */
1012 if (!current_bar.font && font_pattern)
1013 current_bar.font = sstrdup(font_pattern);
1015 /* Copy the current (static) structure into a dynamically allocated
1016 * one, then cleanup our static one. */
1017 Barconfig *bar_config = scalloc(sizeof(Barconfig));
1018 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
1019 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1021 memset(¤t_bar, '\0', sizeof(Barconfig));
1032 | bar_status_command
1039 | bar_workspace_buttons
1043 | bar_color_background
1044 | bar_color_statusline
1045 | bar_color_focused_workspace
1046 | bar_color_active_workspace
1047 | bar_color_inactive_workspace
1048 | bar_color_urgent_workspace
1052 TOK_BAR_STATUS_COMMAND STR
1054 DLOG("should add status command %s\n", $2);
1055 FREE(current_bar.status_command);
1056 current_bar.status_command = $2;
1061 TOK_BAR_I3BAR_COMMAND STR
1063 DLOG("should add i3bar_command %s\n", $2);
1064 FREE(current_bar.i3bar_command);
1065 current_bar.i3bar_command = $2;
1072 DLOG("bar output %s\n", $2);
1073 int new_outputs = current_bar.num_outputs + 1;
1074 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1075 current_bar.outputs[current_bar.num_outputs] = $2;
1076 current_bar.num_outputs = new_outputs;
1081 TOK_BAR_TRAY_OUTPUT STR
1083 DLOG("tray %s\n", $2);
1084 FREE(current_bar.tray_output);
1085 current_bar.tray_output = $2;
1090 TOK_BAR_POSITION bar_position_position
1092 DLOG("position %d\n", $2);
1093 current_bar.position = $2;
1097 bar_position_position:
1098 TOK_BAR_TOP { $$ = P_TOP; }
1099 | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1103 TOK_BAR_MODE bar_mode_mode
1105 DLOG("mode %d\n", $2);
1106 current_bar.mode = $2;
1111 TOK_BAR_HIDE { $$ = M_HIDE; }
1112 | TOK_BAR_DOCK { $$ = M_DOCK; }
1118 DLOG("font %s\n", $2);
1119 FREE(current_bar.font);
1120 current_bar.font = $2;
1124 bar_workspace_buttons:
1125 TOK_BAR_WORKSPACE_BUTTONS bool
1127 DLOG("workspace_buttons = %d\n", $2);
1128 /* We store this inverted to make the default setting right when
1129 * initializing the struct with zero. */
1130 current_bar.hide_workspace_buttons = !($2);
1135 TOK_BAR_VERBOSE bool
1137 DLOG("verbose = %d\n", $2);
1138 current_bar.verbose = $2;
1143 TOK_BAR_SOCKET_PATH STR
1145 DLOG("socket_path = %s\n", $2);
1146 FREE(current_bar.socket_path);
1147 current_bar.socket_path = $2;
1152 TOK_BAR_COLORS '{' barlines '}'
1154 /* At the moment, the TOK_BAR_COLORS token is only to make the config
1155 * friendlier for humans. We might change this in the future if it gets
1160 bar_color_background:
1161 TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1163 DLOG("background = %s\n", $2);
1164 current_bar.colors.background = $2;
1168 bar_color_statusline:
1169 TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1171 DLOG("statusline = %s\n", $2);
1172 current_bar.colors.statusline = $2;
1176 bar_color_focused_workspace:
1177 TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1179 DLOG("focused_ws = %s and %s\n", $2, $3);
1180 current_bar.colors.focused_workspace_text = $2;
1181 current_bar.colors.focused_workspace_bg = $3;
1185 bar_color_active_workspace:
1186 TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1188 DLOG("active_ws = %s and %s\n", $2, $3);
1189 current_bar.colors.active_workspace_text = $2;
1190 current_bar.colors.active_workspace_bg = $3;
1194 bar_color_inactive_workspace:
1195 TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1197 DLOG("inactive_ws = %s and %s\n", $2, $3);
1198 current_bar.colors.inactive_workspace_text = $2;
1199 current_bar.colors.inactive_workspace_bg = $3;
1203 bar_color_urgent_workspace:
1204 TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1206 DLOG("urgent_ws = %s and %s\n", $2, $3);
1207 current_bar.colors.urgent_workspace_text = $2;
1208 current_bar.colors.urgent_workspace_bg = $3;
1213 TOKFLOATING_MODIFIER binding_modifiers
1215 DLOG("floating modifier = %d\n", $2);
1216 config.floating_modifier = $2;
1221 TOK_ORIENTATION direction
1223 DLOG("New containers should start with split direction %d\n", $2);
1224 config.default_orientation = $2;
1229 TOK_HORIZ { $$ = HORIZ; }
1230 | TOK_VERT { $$ = VERT; }
1231 | TOK_AUTO { $$ = NO_ORIENTATION; }
1235 TOK_WORKSPACE_LAYOUT layout_mode
1237 DLOG("new containers will be in mode %d\n", $2);
1238 config.default_layout = $2;
1241 /* We also need to change the layout of the already existing
1242 * workspaces here. Workspaces may exist at this point because
1243 * of the other directives which are modifying workspaces
1244 * (setting the preferred screen or name). While the workspace
1245 * objects are already created, they have never been used.
1246 * Thus, the user very likely awaits the default container mode
1247 * to trigger in this case, regardless of where it is inside
1248 * his configuration file. */
1250 TAILQ_FOREACH(ws, workspaces, workspaces) {
1251 if (ws->table == NULL)
1253 switch_layout_mode(global_conn,
1255 config.container_mode);
1259 | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1261 DLOG("stack-limit %d with val %d\n", $3, $4);
1262 config.container_stack_limit = $3;
1263 config.container_stack_limit_value = $4;
1266 /* See the comment above */
1268 TAILQ_FOREACH(ws, workspaces, workspaces) {
1269 if (ws->table == NULL)
1271 Container *con = ws->table[0][0];
1272 con->stack_limit = config.container_stack_limit;
1273 con->stack_limit_value = config.container_stack_limit_value;
1280 TOK_DEFAULT { $$ = L_DEFAULT; }
1281 | TOK_STACKING { $$ = L_STACKED; }
1282 | TOK_TABBED { $$ = L_TABBED; }
1286 TOKNEWWINDOW border_style
1288 DLOG("new windows should start with border style %d\n", $2);
1289 config.default_border = $2;
1294 TOKNEWFLOAT border_style
1296 DLOG("new floating windows should start with border style %d\n", $2);
1297 config.default_floating_border = $2;
1302 TOK_NORMAL { $$ = BS_NORMAL; }
1303 | TOK_NONE { $$ = BS_NONE; }
1304 | TOK_1PIXEL { $$ = BS_1PIXEL; }
1314 DLOG("checking word \"%s\"\n", $1);
1315 $$ = (strcasecmp($1, "yes") == 0 ||
1316 strcasecmp($1, "true") == 0 ||
1317 strcasecmp($1, "on") == 0 ||
1318 strcasecmp($1, "enable") == 0 ||
1319 strcasecmp($1, "active") == 0);
1323 focus_follows_mouse:
1324 TOKFOCUSFOLLOWSMOUSE bool
1326 DLOG("focus follows mouse = %d\n", $2);
1327 config.disable_focus_follows_mouse = !($2);
1331 force_focus_wrapping:
1332 TOK_FORCE_FOCUS_WRAPPING bool
1334 DLOG("force focus wrapping = %d\n", $2);
1335 config.force_focus_wrapping = $2;
1340 TOK_FORCE_XINERAMA bool
1342 DLOG("force xinerama = %d\n", $2);
1343 config.force_xinerama = $2;
1347 workspace_back_and_forth:
1348 TOK_WORKSPACE_AUTO_BAF bool
1350 DLOG("automatic workspace back-and-forth = %d\n", $2);
1351 config.workspace_auto_back_and_forth = $2;
1356 TOKWORKSPACEBAR bool
1358 DLOG("workspace bar = %d\n", $2);
1359 config.disable_workspace_bar = !($2);
1364 TOKWORKSPACE qstring_or_number TOKOUTPUT OUTPUT optional_workspace_name
1369 ELOG("The old (v3) syntax workspace <number> output <output> <name> is deprecated.\n");
1370 ELOG("Please use the new syntax: workspace \"<workspace>\" output <output>\n");
1371 ELOG("In your case, the following should work:\n");
1372 ELOG(" workspace \"%s\" output %s\n", $5, $4);
1374 context->has_warnings = true;
1377 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", ws_name, $4);
1378 /* Check for earlier assignments of the same workspace so that we
1379 * don’t have assignments of a single workspace to different
1381 struct Workspace_Assignment *assignment;
1382 bool duplicate = false;
1383 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1384 if (strcasecmp(assignment->name, ws_name) == 0) {
1385 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1387 assignment->output = $4;
1392 assignment = scalloc(sizeof(struct Workspace_Assignment));
1393 assignment->name = ws_name;
1394 assignment->output = $4;
1395 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1398 | TOKWORKSPACE NUMBER workspace_name
1402 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1404 DLOG("workspace name to: %s\n", $3);
1406 if ($<string>3 != NULL) {
1407 workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1415 optional_workspace_name:
1416 /* empty */ { $$ = NULL; }
1417 | workspace_name { $$ = $1; }
1421 QUOTEDSTRING { $$ = $1; }
1427 TOKASSIGN window_class STR
1429 /* This is the old, deprecated form of assignments. It’s provided for
1430 * compatibility in version (4.1, 4.2, 4.3) and will be removed
1431 * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1432 ELOG("You are using the old assign syntax (without criteria). "
1433 "Please see the User's Guide for the new syntax and fix "
1434 "your config file.\n");
1435 context->has_warnings = true;
1436 printf("assignment of %s to *%s*\n", $2, $3);
1437 char *workspace = $3;
1438 char *criteria = $2;
1440 Assignment *assignment = scalloc(sizeof(Assignment));
1441 Match *match = &(assignment->match);
1444 char *separator = NULL;
1445 if ((separator = strchr(criteria, '/')) != NULL) {
1446 *(separator++) = '\0';
1448 sasprintf(&pattern, "(?i)%s", separator);
1449 match->title = regex_new(pattern);
1451 printf(" title = %s\n", separator);
1453 if (*criteria != '\0') {
1455 sasprintf(&pattern, "(?i)%s", criteria);
1456 match->class = regex_new(pattern);
1458 printf(" class = %s\n", criteria);
1462 /* Compatibility with older versions: If the assignment target starts
1463 * with ~, we create the equivalent of:
1465 * for_window [class="foo"] floating enable
1467 if (*workspace == '~') {
1469 if (*workspace == '\0') {
1470 /* This assignment was *only* for floating */
1471 assignment->type = A_COMMAND;
1472 assignment->dest.command = sstrdup("floating enable");
1473 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1476 /* Create a new assignment and continue afterwards */
1477 Assignment *floating = scalloc(sizeof(Assignment));
1478 match_copy(&(floating->match), match);
1479 floating->type = A_COMMAND;
1480 floating->dest.command = sstrdup("floating enable");
1481 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1485 assignment->type = A_TO_WORKSPACE;
1486 assignment->dest.workspace = workspace;
1487 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1489 | TOKASSIGN match STR
1491 if (match_is_empty(¤t_match)) {
1492 ELOG("Match is empty, ignoring this assignment\n");
1495 printf("new assignment, using above criteria, to workspace %s\n", $3);
1496 Assignment *assignment = scalloc(sizeof(Assignment));
1497 assignment->match = current_match;
1498 assignment->type = A_TO_WORKSPACE;
1499 assignment->dest.workspace = $3;
1500 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1512 config.ipc_socket_path = $2;
1519 config.restart_state_path = $2;
1524 TOKEXEC optional_no_startup_id STR
1526 struct Autostart *new = smalloc(sizeof(struct Autostart));
1528 new->no_startup_id = $2;
1529 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1534 TOKEXEC_ALWAYS optional_no_startup_id STR
1536 struct Autostart *new = smalloc(sizeof(struct Autostart));
1538 new->no_startup_id = $2;
1539 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1543 optional_no_startup_id:
1544 /* empty */ { $$ = false; }
1545 | TOK_NO_STARTUP_ID { $$ = true; }
1551 ELOG("The terminal option is DEPRECATED and has no effect. "
1552 "Please remove it from your configuration file.\n");
1559 config.font = load_font($2, true);
1560 set_font(&config.font);
1561 printf("font %s\n", $2);
1568 TOKSINGLECOLOR colorpixel
1570 uint32_t *dest = $1;
1576 TOKCOLOR colorpixel colorpixel colorpixel
1578 struct Colortriple *dest = $1;
1581 dest->background = $3;
1589 $$ = get_colorpixel($1);
1596 /* NULL */ { $$ = 0; }
1598 | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1599 | binding_modifiers '+' { $$ = $1; }
1603 MODIFIER { $$ = $1; }
1604 | TOKCONTROL { $$ = BIND_CONTROL; }
1605 | TOKSHIFT { $$ = BIND_SHIFT; }
1608 popup_during_fullscreen:
1609 TOK_POPUP_DURING_FULLSCREEN popup_setting
1611 DLOG("popup_during_fullscreen setting: %d\n", $2);
1612 config.popup_during_fullscreen = $2;
1617 TOK_IGNORE { $$ = PDF_IGNORE; }
1618 | TOK_LEAVE_FULLSCREEN { $$ = PDF_LEAVE_FULLSCREEN; }