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"
178 %token TOK_CLASS "class"
179 %token TOK_INSTANCE "instance"
180 %token TOK_WINDOW_ROLE "window_role"
182 %token TOK_CON_ID "con_id"
183 %token TOK_TITLE "title"
185 %token <string> STR "<string>"
186 %token <number> NUMBER "<number>"
188 %type <number> direction
189 %type <number> split_direction
190 %type <number> fullscreen_mode
192 %type <number> window_mode
193 %type <number> boolean
194 %type <number> border_style
195 %type <number> layout_mode
196 %type <number> resize_px
197 %type <number> resize_way
198 %type <number> resize_tiling
199 %type <number> optional_kill_mode
209 printf("single command completely parsed, dropping state...\n");
210 while (!TAILQ_EMPTY(&owindows)) {
211 current = TAILQ_FIRST(&owindows);
212 TAILQ_REMOVE(&owindows, current, owindows);
215 match_init(¤t_match);
224 | matchstart criteria matchend
226 printf("match parsed\n");
234 match_init(¤t_match);
235 TAILQ_INIT(&owindows);
238 TAILQ_FOREACH(con, &all_cons, all_cons) {
239 owindow *ow = smalloc(sizeof(owindow));
241 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
249 owindow *next, *current;
251 printf("match specification finished, matching...\n");
252 /* copy the old list head to iterate through it and start with a fresh
253 * list which will contain only matching windows */
254 struct owindows_head old = owindows;
255 TAILQ_INIT(&owindows);
256 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
257 /* make a copy of the next pointer and advance the pointer to the
258 * next element as we are going to invalidate the element’s
259 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
261 next = TAILQ_NEXT(next, owindows);
263 printf("checking if con %p / %s matches\n", current->con, current->con->name);
264 if (current_match.con_id != NULL) {
265 if (current_match.con_id == current->con) {
266 printf("matches container!\n");
267 TAILQ_INSERT_TAIL(&owindows, current, owindows);
270 } else if (current_match.mark != NULL && current->con->mark != NULL &&
271 regex_matches(current_match.mark, current->con->mark)) {
272 printf("match by mark\n");
273 TAILQ_INSERT_TAIL(&owindows, current, owindows);
275 if (current->con->window == NULL)
277 if (match_matches_window(¤t_match, current->con->window)) {
278 printf("matches window!\n");
279 TAILQ_INSERT_TAIL(&owindows, current, owindows);
281 printf("doesnt match\n");
287 TAILQ_FOREACH(current, &owindows, owindows) {
288 printf("matching: %p / %s\n", current->con, current->con->name);
302 printf("criteria: class = %s\n", $3);
303 current_match.class = regex_new($3);
306 | TOK_INSTANCE '=' STR
308 printf("criteria: instance = %s\n", $3);
309 current_match.instance = regex_new($3);
312 | TOK_WINDOW_ROLE '=' STR
314 printf("criteria: window_role = %s\n", $3);
315 current_match.role = regex_new($3);
320 printf("criteria: id = %s\n", $3);
322 long parsed = strtol($3, &end, 10);
323 if (parsed == LONG_MIN ||
324 parsed == LONG_MAX ||
326 (end && *end != '\0')) {
327 ELOG("Could not parse con id \"%s\"\n", $3);
329 current_match.con_id = (Con*)parsed;
330 printf("id as int = %p\n", current_match.con_id);
335 printf("criteria: window id = %s\n", $3);
337 long parsed = strtol($3, &end, 10);
338 if (parsed == LONG_MIN ||
339 parsed == LONG_MAX ||
341 (end && *end != '\0')) {
342 ELOG("Could not parse window id \"%s\"\n", $3);
344 current_match.id = parsed;
345 printf("window id as int = %d\n", current_match.id);
350 printf("criteria: mark = %s\n", $3);
351 current_match.mark = regex_new($3);
356 printf("criteria: title = %s\n", $3);
357 current_match.title = regex_new($3);
364 | operations ',' operation
392 printf("should execute %s\n", $2);
393 start_application($2);
401 printf("exit, bye bye\n");
409 printf("reloading\n");
410 kill_configerror_nagbar(false);
411 load_configuration(conn, NULL, true);
413 /* Send an IPC event just in case the ws names have changed */
414 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
421 printf("restarting i3\n");
431 if (match_is_empty(¤t_match)) {
432 ELOG("You have to specify which window/container should be focused.\n");
433 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
435 sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
436 "specify which window/container should be focused\"}");
441 TAILQ_FOREACH(current, &owindows, owindows) {
442 Con *ws = con_get_workspace(current->con);
444 /* If the container is not on the current workspace,
445 * workspace_show() will switch to a different workspace and (if
446 * enabled) trigger a mouse pointer warp to the currently focused
447 * container (!) on the target workspace.
449 * Therefore, before calling workspace_show(), we make sure that
450 * 'current' will be focused on the workspace. However, we cannot
451 * just con_focus(current) because then the pointer will not be
452 * warped at all (the code thinks we are already there).
454 * So we focus 'current' to make it the currently focused window of
455 * the target workspace, then revert focus. */
456 Con *currently_focused = focused;
457 con_focus(current->con);
458 con_focus(currently_focused);
460 /* Now switch to the workspace, then focus */
462 LOG("focusing %p / %s\n", current->con, current->con->name);
463 con_focus(current->con);
468 LOG("WARNING: Your criteria for the focus command matches %d containers, "
469 "while only exactly one container can be focused at a time.\n", count);
473 | TOK_FOCUS direction
478 LOG("Focusing left\n");
479 tree_next('p', HORIZ);
482 LOG("Focusing right\n");
483 tree_next('n', HORIZ);
486 LOG("Focusing up\n");
487 tree_next('p', VERT);
490 LOG("Focusing down\n");
491 tree_next('n', VERT);
494 ELOG("Invalid focus direction (%d)\n", direction);
500 | TOK_FOCUS window_mode
502 printf("should focus: ");
504 if ($2 == TOK_TILING)
506 else if ($2 == TOK_FLOATING)
507 printf("floating\n");
508 else printf("mode toggle\n");
510 Con *ws = con_get_workspace(focused);
514 if ($2 == TOK_MODE_TOGGLE) {
515 current = TAILQ_FIRST(&(ws->focus_head));
516 if (current != NULL && current->type == CT_FLOATING_CON)
517 to_focus = TOK_TILING;
518 else to_focus = TOK_FLOATING;
520 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
521 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
522 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
525 con_focus(con_descend_focused(current));
534 if ($2 == TOK_PARENT)
543 TOK_TILING { $$ = TOK_TILING; }
544 | TOK_FLOATING { $$ = TOK_FLOATING; }
545 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
549 TOK_PARENT { $$ = TOK_PARENT; }
550 | TOK_CHILD { $$ = TOK_CHILD; }
554 TOK_KILL optional_kill_mode
558 printf("killing!\n");
559 /* check if the match is empty, not if the result is empty */
560 if (match_is_empty(¤t_match))
563 TAILQ_FOREACH(current, &owindows, owindows) {
564 printf("matching: %p / %s\n", current->con, current->con->name);
565 tree_close(current->con, $2, false, false);
574 /* empty */ { $$ = KILL_WINDOW; }
575 | TOK_WINDOW { $$ = KILL_WINDOW; }
576 | TOK_CLIENT { $$ = KILL_CLIENT; }
580 TOK_WORKSPACE TOK_NEXT
582 workspace_show(workspace_next());
585 | TOK_WORKSPACE TOK_PREV
587 workspace_show(workspace_prev());
590 | TOK_WORKSPACE TOK_BACK_AND_FORTH
592 workspace_back_and_forth();
597 printf("should switch to workspace %s\n", $2);
599 Con *ws = con_get_workspace(focused);
601 /* Check if the command wants to switch to the current workspace */
602 if (strcmp(ws->name, $2) == 0) {
603 printf("This workspace is already focused.\n");
604 if (config.workspace_auto_back_and_forth) {
605 workspace_back_and_forth();
612 workspace_show_by_name($2);
622 printf("opening new container\n");
623 Con *con = tree_open_con(NULL, NULL);
625 sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
632 TOK_FULLSCREEN fullscreen_mode
634 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
639 TAILQ_FOREACH(current, &owindows, owindows) {
640 printf("matching: %p / %s\n", current->con, current->con->name);
641 con_toggle_fullscreen(current->con, $2);
649 /* empty */ { $$ = CF_OUTPUT; }
650 | TOK_GLOBAL { $$ = CF_GLOBAL; }
654 TOK_SPLIT split_direction
656 /* TODO: use matches */
657 printf("splitting in direction %c\n", $2);
658 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
665 TOK_HORIZONTAL { $$ = 'h'; }
667 | TOK_VERTICAL { $$ = 'v'; }
677 TAILQ_FOREACH(current, &owindows, owindows) {
678 printf("matching: %p / %s\n", current->con, current->con->name);
679 if ($2 == TOK_TOGGLE) {
680 printf("should toggle mode\n");
681 toggle_floating_mode(current->con, false);
683 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
684 if ($2 == TOK_ENABLE) {
685 floating_enable(current->con, false);
687 floating_disable(current->con, false);
697 TOK_ENABLE { $$ = TOK_ENABLE; }
698 | TOK_DISABLE { $$ = TOK_DISABLE; }
699 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
703 TOK_BORDER border_style
705 printf("border style should be changed to %d\n", $2);
710 TAILQ_FOREACH(current, &owindows, owindows) {
711 printf("matching: %p / %s\n", current->con, current->con->name);
712 if ($2 == TOK_TOGGLE) {
713 current->con->border_style++;
714 current->con->border_style %= 3;
715 } else current->con->border_style = $2;
723 TOK_NORMAL { $$ = BS_NORMAL; }
724 | TOK_NONE { $$ = BS_NONE; }
725 | TOK_1PIXEL { $$ = BS_1PIXEL; }
726 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
732 printf("moving in direction %d\n", $2);
737 | TOK_MOVE TOK_WORKSPACE STR
741 printf("should move window to workspace %s\n", $3);
742 /* get the workspace */
743 Con *ws = workspace_get($3, NULL);
748 TAILQ_FOREACH(current, &owindows, owindows) {
749 printf("matching: %p / %s\n", current->con, current->con->name);
750 con_move_to_workspace(current->con, ws, true, false);
755 | TOK_MOVE TOK_WORKSPACE TOK_NEXT
759 /* get the workspace */
760 Con *ws = workspace_next();
764 TAILQ_FOREACH(current, &owindows, owindows) {
765 printf("matching: %p / %s\n", current->con, current->con->name);
766 con_move_to_workspace(current->con, ws, true, false);
771 | TOK_MOVE TOK_WORKSPACE TOK_PREV
775 /* get the workspace */
776 Con *ws = workspace_prev();
780 TAILQ_FOREACH(current, &owindows, owindows) {
781 printf("matching: %p / %s\n", current->con, current->con->name);
782 con_move_to_workspace(current->con, ws, true, false);
787 | TOK_MOVE TOK_OUTPUT STR
791 printf("should move window to output %s", $3);
796 Output *current_output = NULL;
799 TAILQ_FOREACH(current, &owindows, owindows)
800 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
802 assert(current_output != NULL);
804 if (strcasecmp($3, "up") == 0)
805 output = get_output_next(D_UP, current_output);
806 else if (strcasecmp($3, "down") == 0)
807 output = get_output_next(D_DOWN, current_output);
808 else if (strcasecmp($3, "left") == 0)
809 output = get_output_next(D_LEFT, current_output);
810 else if (strcasecmp($3, "right") == 0)
811 output = get_output_next(D_RIGHT, current_output);
813 output = get_output_by_name($3);
819 /* get visible workspace on output */
821 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
825 TAILQ_FOREACH(current, &owindows, owindows) {
826 printf("matching: %p / %s\n", current->con, current->con->name);
827 con_move_to_workspace(current->con, ws, true, false);
835 TOK_APPEND_LAYOUT STR
837 printf("restoring \"%s\"\n", $2);
838 tree_append_json($2);
845 TOK_LAYOUT layout_mode
847 printf("changing layout to %d\n", $2);
850 /* check if the match is empty, not if the result is empty */
851 if (match_is_empty(¤t_match))
852 con_set_layout(focused->parent, $2);
854 TAILQ_FOREACH(current, &owindows, owindows) {
855 printf("matching: %p / %s\n", current->con, current->con->name);
856 con_set_layout(current->con, $2);
865 TOK_DEFAULT { $$ = L_DEFAULT; }
866 | TOK_STACKED { $$ = L_STACKED; }
867 | TOK_TABBED { $$ = L_TABBED; }
873 printf("marking window with str %s\n", $2);
878 TAILQ_FOREACH(current, &owindows, owindows) {
879 printf("matching: %p / %s\n", current->con, current->con->name);
880 current->con->mark = sstrdup($2);
892 printf("-------------------------------------------------\n");
893 printf(" NOP: %s\n", $2);
894 printf("-------------------------------------------------\n");
900 TOK_RESIZE resize_way direction resize_px resize_tiling
902 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
903 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
907 if ($2 == TOK_SHRINK) {
912 if (con_is_floating(focused)) {
913 printf("floating resize\n");
914 if (direction == TOK_UP) {
915 focused->parent->rect.y -= px;
916 focused->parent->rect.height += px;
917 } else if (direction == TOK_DOWN) {
918 focused->parent->rect.height += px;
919 } else if (direction == TOK_LEFT) {
920 focused->parent->rect.x -= px;
921 focused->parent->rect.width += px;
923 focused->parent->rect.width += px;
926 LOG("tiling resize\n");
927 /* get the appropriate current container (skip stacked/tabbed cons) */
928 Con *current = focused;
929 while (current->parent->layout == L_STACKED ||
930 current->parent->layout == L_TABBED)
931 current = current->parent;
932 /* get the default percentage */
933 int children = con_num_children(current->parent);
935 LOG("ins. %d children\n", children);
936 double percentage = 1.0 / children;
937 LOG("default percentage = %f\n", percentage);
939 orientation_t orientation = current->parent->orientation;
941 if ((orientation == HORIZ &&
942 (direction == TOK_UP || direction == TOK_DOWN)) ||
943 (orientation == VERT &&
944 (direction == TOK_LEFT || direction == TOK_RIGHT))) {
945 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
946 (orientation == HORIZ ? "horizontal" : "vertical"));
950 if (direction == TOK_UP || direction == TOK_LEFT) {
951 other = TAILQ_PREV(current, nodes_head, nodes);
953 other = TAILQ_NEXT(current, nodes);
955 if (other == TAILQ_END(workspaces)) {
956 LOG("No other container in this direction found, cannot resize.\n");
959 LOG("other->percent = %f\n", other->percent);
960 LOG("current->percent before = %f\n", current->percent);
961 if (current->percent == 0.0)
962 current->percent = percentage;
963 if (other->percent == 0.0)
964 other->percent = percentage;
965 double new_current_percent = current->percent + ((double)ppt / 100.0);
966 double new_other_percent = other->percent - ((double)ppt / 100.0);
967 LOG("new_current_percent = %f\n", new_current_percent);
968 LOG("new_other_percent = %f\n", new_other_percent);
969 /* Ensure that the new percentages are positive and greater than
970 * 0.05 to have a reasonable minimum size. */
971 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
972 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
973 current->percent += ((double)ppt / 100.0);
974 other->percent -= ((double)ppt / 100.0);
975 LOG("current->percent after = %f\n", current->percent);
976 LOG("other->percent after = %f\n", other->percent);
978 LOG("Not resizing, already at minimum size\n");
1002 | TOK_OR NUMBER TOK_PPT
1009 TOK_GROW { $$ = TOK_GROW; }
1010 | TOK_SHRINK { $$ = TOK_SHRINK; }
1014 TOK_UP { $$ = TOK_UP; }
1015 | TOK_DOWN { $$ = TOK_DOWN; }
1016 | TOK_LEFT { $$ = TOK_LEFT; }
1017 | TOK_RIGHT { $$ = TOK_RIGHT; }