3 * vim:ts=4:sw=4:expandtab
5 * i3 - an improved dynamic tiling window manager
6 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8 * cmdparse.y: the parser for commands you send to i3 (or bind on keys)
11 #include <sys/types.h>
19 /** When the command did not include match criteria (!), we use the currently
20 * focused command. Do not confuse this case with a command which included
21 * criteria but which did not match any windows. This macro has to be called in
24 #define HANDLE_EMPTY_MATCH do { \
25 if (match_is_empty(¤t_match)) { \
26 owindow *ow = smalloc(sizeof(owindow)); \
28 TAILQ_INIT(&owindows); \
29 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
33 typedef struct yy_buffer_state *YY_BUFFER_STATE;
34 extern int cmdyylex(struct context *context);
35 extern int cmdyyparse(void);
36 extern int cmdyylex_destroy(void);
38 YY_BUFFER_STATE cmdyy_scan_string(const char *);
40 static struct context *context;
41 static Match current_match;
44 * Helper data structure for an operation window (window on which the operation
45 * will be performed). Used to build the TAILQ owindows.
48 typedef struct owindow {
50 TAILQ_ENTRY(owindow) owindows;
52 static TAILQ_HEAD(owindows_head, owindow) owindows;
54 /* Holds the JSON which will be returned via IPC or NULL for the default return
56 static char *json_output;
58 /* We don’t need yydebug for now, as we got decent error messages using
59 * yyerror(). Should you ever want to extend the parser, it might be handy
60 * to just comment it in again, so it stays here. */
63 void cmdyyerror(const char *error_message) {
65 ELOG("CMD: %s\n", error_message);
66 ELOG("CMD: in command:\n");
67 ELOG("CMD: %s\n", context->line_copy);
69 for (int c = 1; c <= context->last_column; c++)
70 if (c >= context->first_column)
75 context->compact_error = sstrdup(error_message);
82 char *parse_cmd(const char *new) {
84 LOG("COMMAND: *%s*\n", new);
85 cmdyy_scan_string(new);
87 match_init(¤t_match);
88 context = scalloc(sizeof(struct context));
89 context->filename = "cmd";
90 if (cmdyyparse() != 0) {
91 fprintf(stderr, "Could not parse command\n");
92 sasprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
93 context->compact_error, context->first_column);
94 FREE(context->line_copy);
95 FREE(context->compact_error);
99 printf("done, json output = %s\n", json_output);
102 FREE(context->line_copy);
103 FREE(context->compact_error);
109 * Returns true if a is definitely greater than b (using the given epsilon)
112 bool definitelyGreaterThan(float a, float b, float epsilon) {
113 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
119 %lex-param { struct context *context }
127 %token TOK_EXEC "exec"
128 %token TOK_EXIT "exit"
129 %token TOK_RELOAD "reload"
130 %token TOK_RESTART "restart"
131 %token TOK_KILL "kill"
132 %token TOK_WINDOW "window"
133 %token TOK_CLIENT "client"
134 %token TOK_FULLSCREEN "fullscreen"
135 %token TOK_GLOBAL "global"
136 %token TOK_LAYOUT "layout"
137 %token TOK_DEFAULT "default"
138 %token TOK_STACKED "stacked"
139 %token TOK_TABBED "tabbed"
140 %token TOK_BORDER "border"
141 %token TOK_NORMAL "normal"
142 %token TOK_NONE "none"
143 %token TOK_1PIXEL "1pixel"
144 %token TOK_MODE "mode"
145 %token TOK_TILING "tiling"
146 %token TOK_FLOATING "floating"
147 %token TOK_MODE_TOGGLE "mode_toggle"
148 %token TOK_ENABLE "enable"
149 %token TOK_DISABLE "disable"
150 %token TOK_WORKSPACE "workspace"
151 %token TOK_OUTPUT "output"
152 %token TOK_TOGGLE "toggle"
153 %token TOK_FOCUS "focus"
154 %token TOK_MOVE "move"
155 %token TOK_OPEN "open"
156 %token TOK_NEXT "next"
157 %token TOK_PREV "prev"
158 %token TOK_SPLIT "split"
159 %token TOK_HORIZONTAL "horizontal"
160 %token TOK_VERTICAL "vertical"
162 %token TOK_DOWN "down"
163 %token TOK_LEFT "left"
164 %token TOK_RIGHT "right"
165 %token TOK_PARENT "parent"
166 %token TOK_CHILD "child"
167 %token TOK_APPEND_LAYOUT "append_layout"
168 %token TOK_MARK "mark"
169 %token TOK_RESIZE "resize"
170 %token TOK_GROW "grow"
171 %token TOK_SHRINK "shrink"
176 %token TOK_BACK_AND_FORTH "back_and_forth"
177 %token TOK_NO_STARTUP_ID "--no-startup-id"
179 %token TOK_CLASS "class"
180 %token TOK_INSTANCE "instance"
181 %token TOK_WINDOW_ROLE "window_role"
183 %token TOK_CON_ID "con_id"
184 %token TOK_TITLE "title"
186 %token <string> STR "<string>"
187 %token <number> NUMBER "<number>"
189 %type <number> direction
190 %type <number> split_direction
191 %type <number> fullscreen_mode
193 %type <number> window_mode
194 %type <number> boolean
195 %type <number> border_style
196 %type <number> layout_mode
197 %type <number> resize_px
198 %type <number> resize_way
199 %type <number> resize_tiling
200 %type <number> optional_kill_mode
201 %type <number> optional_no_startup_id
211 printf("single command completely parsed, dropping state...\n");
212 while (!TAILQ_EMPTY(&owindows)) {
213 current = TAILQ_FIRST(&owindows);
214 TAILQ_REMOVE(&owindows, current, owindows);
217 match_init(¤t_match);
226 | matchstart criteria matchend
228 printf("match parsed\n");
236 match_init(¤t_match);
237 TAILQ_INIT(&owindows);
240 TAILQ_FOREACH(con, &all_cons, all_cons) {
241 owindow *ow = smalloc(sizeof(owindow));
243 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
251 owindow *next, *current;
253 printf("match specification finished, matching...\n");
254 /* copy the old list head to iterate through it and start with a fresh
255 * list which will contain only matching windows */
256 struct owindows_head old = owindows;
257 TAILQ_INIT(&owindows);
258 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
259 /* make a copy of the next pointer and advance the pointer to the
260 * next element as we are going to invalidate the element’s
261 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
263 next = TAILQ_NEXT(next, owindows);
265 printf("checking if con %p / %s matches\n", current->con, current->con->name);
266 if (current_match.con_id != NULL) {
267 if (current_match.con_id == current->con) {
268 printf("matches container!\n");
269 TAILQ_INSERT_TAIL(&owindows, current, owindows);
272 } else if (current_match.mark != NULL && current->con->mark != NULL &&
273 regex_matches(current_match.mark, current->con->mark)) {
274 printf("match by mark\n");
275 TAILQ_INSERT_TAIL(&owindows, current, owindows);
277 if (current->con->window == NULL)
279 if (match_matches_window(¤t_match, current->con->window)) {
280 printf("matches window!\n");
281 TAILQ_INSERT_TAIL(&owindows, current, owindows);
283 printf("doesnt match\n");
289 TAILQ_FOREACH(current, &owindows, owindows) {
290 printf("matching: %p / %s\n", current->con, current->con->name);
304 printf("criteria: class = %s\n", $3);
305 current_match.class = regex_new($3);
308 | TOK_INSTANCE '=' STR
310 printf("criteria: instance = %s\n", $3);
311 current_match.instance = regex_new($3);
314 | TOK_WINDOW_ROLE '=' STR
316 printf("criteria: window_role = %s\n", $3);
317 current_match.role = regex_new($3);
322 printf("criteria: id = %s\n", $3);
324 long parsed = strtol($3, &end, 10);
325 if (parsed == LONG_MIN ||
326 parsed == LONG_MAX ||
328 (end && *end != '\0')) {
329 ELOG("Could not parse con id \"%s\"\n", $3);
331 current_match.con_id = (Con*)parsed;
332 printf("id as int = %p\n", current_match.con_id);
337 printf("criteria: window id = %s\n", $3);
339 long parsed = strtol($3, &end, 10);
340 if (parsed == LONG_MIN ||
341 parsed == LONG_MAX ||
343 (end && *end != '\0')) {
344 ELOG("Could not parse window id \"%s\"\n", $3);
346 current_match.id = parsed;
347 printf("window id as int = %d\n", current_match.id);
352 printf("criteria: mark = %s\n", $3);
353 current_match.mark = regex_new($3);
358 printf("criteria: title = %s\n", $3);
359 current_match.title = regex_new($3);
366 | operations ',' operation
392 TOK_EXEC optional_no_startup_id STR
395 bool no_startup_id = $2;
397 printf("should execute %s, no_startup_id = %d\n", command, no_startup_id);
398 start_application(command, no_startup_id);
403 optional_no_startup_id:
404 /* empty */ { $$ = false; }
405 | TOK_NO_STARTUP_ID { $$ = true; }
411 printf("exit, bye bye\n");
419 printf("reloading\n");
420 kill_configerror_nagbar(false);
421 load_configuration(conn, NULL, true);
423 /* Send an IPC event just in case the ws names have changed */
424 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
431 printf("restarting i3\n");
441 if (match_is_empty(¤t_match)) {
442 ELOG("You have to specify which window/container should be focused.\n");
443 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
445 sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
446 "specify which window/container should be focused\"}");
451 TAILQ_FOREACH(current, &owindows, owindows) {
452 Con *ws = con_get_workspace(current->con);
453 /* If no workspace could be found, this was a dock window.
454 * Just skip it, you cannot focus dock windows. */
458 /* If the container is not on the current workspace,
459 * workspace_show() will switch to a different workspace and (if
460 * enabled) trigger a mouse pointer warp to the currently focused
461 * container (!) on the target workspace.
463 * Therefore, before calling workspace_show(), we make sure that
464 * 'current' will be focused on the workspace. However, we cannot
465 * just con_focus(current) because then the pointer will not be
466 * warped at all (the code thinks we are already there).
468 * So we focus 'current' to make it the currently focused window of
469 * the target workspace, then revert focus. */
470 Con *currently_focused = focused;
471 con_focus(current->con);
472 con_focus(currently_focused);
474 /* Now switch to the workspace, then focus */
476 LOG("focusing %p / %s\n", current->con, current->con->name);
477 con_focus(current->con);
482 LOG("WARNING: Your criteria for the focus command matches %d containers, "
483 "while only exactly one container can be focused at a time.\n", count);
487 | TOK_FOCUS direction
492 LOG("Focusing left\n");
493 tree_next('p', HORIZ);
496 LOG("Focusing right\n");
497 tree_next('n', HORIZ);
500 LOG("Focusing up\n");
501 tree_next('p', VERT);
504 LOG("Focusing down\n");
505 tree_next('n', VERT);
508 ELOG("Invalid focus direction (%d)\n", direction);
514 | TOK_FOCUS window_mode
516 printf("should focus: ");
518 if ($2 == TOK_TILING)
520 else if ($2 == TOK_FLOATING)
521 printf("floating\n");
522 else printf("mode toggle\n");
524 Con *ws = con_get_workspace(focused);
528 if ($2 == TOK_MODE_TOGGLE) {
529 current = TAILQ_FIRST(&(ws->focus_head));
530 if (current != NULL && current->type == CT_FLOATING_CON)
531 to_focus = TOK_TILING;
532 else to_focus = TOK_FLOATING;
534 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
535 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
536 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
539 con_focus(con_descend_focused(current));
548 if ($2 == TOK_PARENT)
557 TOK_TILING { $$ = TOK_TILING; }
558 | TOK_FLOATING { $$ = TOK_FLOATING; }
559 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
563 TOK_PARENT { $$ = TOK_PARENT; }
564 | TOK_CHILD { $$ = TOK_CHILD; }
568 TOK_KILL optional_kill_mode
572 printf("killing!\n");
573 /* check if the match is empty, not if the result is empty */
574 if (match_is_empty(¤t_match))
577 TAILQ_FOREACH(current, &owindows, owindows) {
578 printf("matching: %p / %s\n", current->con, current->con->name);
579 tree_close(current->con, $2, false, false);
588 /* empty */ { $$ = KILL_WINDOW; }
589 | TOK_WINDOW { $$ = KILL_WINDOW; }
590 | TOK_CLIENT { $$ = KILL_CLIENT; }
594 TOK_WORKSPACE TOK_NEXT
596 workspace_show(workspace_next());
599 | TOK_WORKSPACE TOK_PREV
601 workspace_show(workspace_prev());
604 | TOK_WORKSPACE TOK_BACK_AND_FORTH
606 workspace_back_and_forth();
611 printf("should switch to workspace %s\n", $2);
613 Con *ws = con_get_workspace(focused);
615 /* Check if the command wants to switch to the current workspace */
616 if (strcmp(ws->name, $2) == 0) {
617 printf("This workspace is already focused.\n");
618 if (config.workspace_auto_back_and_forth) {
619 workspace_back_and_forth();
626 workspace_show_by_name($2);
636 printf("opening new container\n");
637 Con *con = tree_open_con(NULL, NULL);
639 sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
646 TOK_FULLSCREEN fullscreen_mode
648 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
653 TAILQ_FOREACH(current, &owindows, owindows) {
654 printf("matching: %p / %s\n", current->con, current->con->name);
655 con_toggle_fullscreen(current->con, $2);
663 /* empty */ { $$ = CF_OUTPUT; }
664 | TOK_GLOBAL { $$ = CF_GLOBAL; }
668 TOK_SPLIT split_direction
670 /* TODO: use matches */
671 printf("splitting in direction %c\n", $2);
672 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
679 TOK_HORIZONTAL { $$ = 'h'; }
681 | TOK_VERTICAL { $$ = 'v'; }
691 TAILQ_FOREACH(current, &owindows, owindows) {
692 printf("matching: %p / %s\n", current->con, current->con->name);
693 if ($2 == TOK_TOGGLE) {
694 printf("should toggle mode\n");
695 toggle_floating_mode(current->con, false);
697 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
698 if ($2 == TOK_ENABLE) {
699 floating_enable(current->con, false);
701 floating_disable(current->con, false);
711 TOK_ENABLE { $$ = TOK_ENABLE; }
712 | TOK_DISABLE { $$ = TOK_DISABLE; }
713 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
717 TOK_BORDER border_style
719 printf("border style should be changed to %d\n", $2);
724 TAILQ_FOREACH(current, &owindows, owindows) {
725 printf("matching: %p / %s\n", current->con, current->con->name);
726 int border_style = current->con->border_style;
727 if ($2 == TOK_TOGGLE) {
730 } else border_style = $2;
731 con_set_border_style(current->con, border_style);
739 TOK_NORMAL { $$ = BS_NORMAL; }
740 | TOK_NONE { $$ = BS_NONE; }
741 | TOK_1PIXEL { $$ = BS_1PIXEL; }
742 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
746 TOK_MOVE direction resize_px
751 /* TODO: make 'move' work with criteria. */
752 printf("moving in direction %d\n", direction);
753 if (con_is_floating(focused)) {
754 printf("floating move with %d pixels\n", px);
755 if (direction == TOK_LEFT) {
756 focused->parent->rect.x -= px;
757 } else if (direction == TOK_RIGHT) {
758 focused->parent->rect.x += px;
759 } else if (direction == TOK_UP) {
760 focused->parent->rect.y -= px;
761 } else if (direction == TOK_DOWN) {
762 focused->parent->rect.y += px;
765 tree_move(direction);
770 | TOK_MOVE TOK_WORKSPACE STR
774 /* Error out early to not create a non-existing workspace (in
775 * workspace_get()) if we are not actually able to move anything. */
776 if (match_is_empty(¤t_match) && focused->type == CT_WORKSPACE)
779 printf("should move window to workspace %s\n", $3);
780 /* get the workspace */
781 Con *ws = workspace_get($3, NULL);
786 TAILQ_FOREACH(current, &owindows, owindows) {
787 printf("matching: %p / %s\n", current->con, current->con->name);
788 con_move_to_workspace(current->con, ws, true, false);
793 | TOK_MOVE TOK_WORKSPACE TOK_NEXT
797 /* get the workspace */
798 Con *ws = workspace_next();
802 TAILQ_FOREACH(current, &owindows, owindows) {
803 printf("matching: %p / %s\n", current->con, current->con->name);
804 con_move_to_workspace(current->con, ws, true, false);
809 | TOK_MOVE TOK_WORKSPACE TOK_PREV
813 /* get the workspace */
814 Con *ws = workspace_prev();
818 TAILQ_FOREACH(current, &owindows, owindows) {
819 printf("matching: %p / %s\n", current->con, current->con->name);
820 con_move_to_workspace(current->con, ws, true, false);
825 | TOK_MOVE TOK_OUTPUT STR
829 printf("should move window to output %s", $3);
834 Output *current_output = NULL;
837 TAILQ_FOREACH(current, &owindows, owindows)
838 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
840 assert(current_output != NULL);
842 if (strcasecmp($3, "up") == 0)
843 output = get_output_next(D_UP, current_output);
844 else if (strcasecmp($3, "down") == 0)
845 output = get_output_next(D_DOWN, current_output);
846 else if (strcasecmp($3, "left") == 0)
847 output = get_output_next(D_LEFT, current_output);
848 else if (strcasecmp($3, "right") == 0)
849 output = get_output_next(D_RIGHT, current_output);
851 output = get_output_by_name($3);
857 /* get visible workspace on output */
859 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
863 TAILQ_FOREACH(current, &owindows, owindows) {
864 printf("matching: %p / %s\n", current->con, current->con->name);
865 con_move_to_workspace(current->con, ws, true, false);
873 TOK_APPEND_LAYOUT STR
875 printf("restoring \"%s\"\n", $2);
876 tree_append_json($2);
883 TOK_LAYOUT layout_mode
885 printf("changing layout to %d\n", $2);
888 /* check if the match is empty, not if the result is empty */
889 if (match_is_empty(¤t_match))
890 con_set_layout(focused->parent, $2);
892 TAILQ_FOREACH(current, &owindows, owindows) {
893 printf("matching: %p / %s\n", current->con, current->con->name);
894 con_set_layout(current->con, $2);
903 TOK_DEFAULT { $$ = L_DEFAULT; }
904 | TOK_STACKED { $$ = L_STACKED; }
905 | TOK_TABBED { $$ = L_TABBED; }
911 printf("marking window with str %s\n", $2);
916 TAILQ_FOREACH(current, &owindows, owindows) {
917 printf("matching: %p / %s\n", current->con, current->con->name);
918 current->con->mark = sstrdup($2);
930 printf("-------------------------------------------------\n");
931 printf(" NOP: %s\n", $2);
932 printf("-------------------------------------------------\n");
938 TOK_RESIZE resize_way direction resize_px resize_tiling
940 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
941 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
945 if ($2 == TOK_SHRINK) {
950 if (con_is_floating(focused)) {
951 printf("floating resize\n");
952 if (direction == TOK_UP) {
953 focused->parent->rect.y -= px;
954 focused->parent->rect.height += px;
955 } else if (direction == TOK_DOWN) {
956 focused->parent->rect.height += px;
957 } else if (direction == TOK_LEFT) {
958 focused->parent->rect.x -= px;
959 focused->parent->rect.width += px;
961 focused->parent->rect.width += px;
964 LOG("tiling resize\n");
965 /* get the appropriate current container (skip stacked/tabbed cons) */
966 Con *current = focused;
967 while (current->parent->layout == L_STACKED ||
968 current->parent->layout == L_TABBED)
969 current = current->parent;
971 /* Then further go up until we find one with the matching orientation. */
972 orientation_t search_orientation =
973 (direction == TOK_LEFT || direction == TOK_RIGHT ? HORIZ : VERT);
975 while (current->type != CT_WORKSPACE &&
976 current->type != CT_FLOATING_CON &&
977 current->parent->orientation != search_orientation)
978 current = current->parent;
980 /* get the default percentage */
981 int children = con_num_children(current->parent);
983 LOG("ins. %d children\n", children);
984 double percentage = 1.0 / children;
985 LOG("default percentage = %f\n", percentage);
987 orientation_t orientation = current->parent->orientation;
989 if ((orientation == HORIZ &&
990 (direction == TOK_UP || direction == TOK_DOWN)) ||
991 (orientation == VERT &&
992 (direction == TOK_LEFT || direction == TOK_RIGHT))) {
993 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
994 (orientation == HORIZ ? "horizontal" : "vertical"));
998 if (direction == TOK_UP || direction == TOK_LEFT) {
999 other = TAILQ_PREV(current, nodes_head, nodes);
1001 other = TAILQ_NEXT(current, nodes);
1003 if (other == TAILQ_END(workspaces)) {
1004 LOG("No other container in this direction found, cannot resize.\n");
1007 LOG("other->percent = %f\n", other->percent);
1008 LOG("current->percent before = %f\n", current->percent);
1009 if (current->percent == 0.0)
1010 current->percent = percentage;
1011 if (other->percent == 0.0)
1012 other->percent = percentage;
1013 double new_current_percent = current->percent + ((double)ppt / 100.0);
1014 double new_other_percent = other->percent - ((double)ppt / 100.0);
1015 LOG("new_current_percent = %f\n", new_current_percent);
1016 LOG("new_other_percent = %f\n", new_other_percent);
1017 /* Ensure that the new percentages are positive and greater than
1018 * 0.05 to have a reasonable minimum size. */
1019 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
1020 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
1021 current->percent += ((double)ppt / 100.0);
1022 other->percent -= ((double)ppt / 100.0);
1023 LOG("current->percent after = %f\n", current->percent);
1024 LOG("other->percent after = %f\n", other->percent);
1026 LOG("Not resizing, already at minimum size\n");
1050 | TOK_OR NUMBER TOK_PPT
1057 TOK_GROW { $$ = TOK_GROW; }
1058 | TOK_SHRINK { $$ = TOK_SHRINK; }
1062 TOK_UP { $$ = TOK_UP; }
1063 | TOK_DOWN { $$ = TOK_DOWN; }
1064 | TOK_LEFT { $$ = TOK_LEFT; }
1065 | TOK_RIGHT { $$ = TOK_RIGHT; }