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)
12 #include <sys/types.h>
20 /** When the command did not include match criteria (!), we use the currently
21 * focused command. Do not confuse this case with a command which included
22 * criteria but which did not match any windows. This macro has to be called in
25 #define HANDLE_EMPTY_MATCH do { \
26 if (match_is_empty(¤t_match)) { \
27 owindow *ow = smalloc(sizeof(owindow)); \
29 TAILQ_INIT(&owindows); \
30 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
34 typedef struct yy_buffer_state *YY_BUFFER_STATE;
35 extern int cmdyylex(struct context *context);
36 extern int cmdyyparse(void);
37 extern int cmdyylex_destroy(void);
39 YY_BUFFER_STATE cmdyy_scan_string(const char *);
41 static struct context *context;
42 static Match current_match;
45 * Helper data structure for an operation window (window on which the operation
46 * will be performed). Used to build the TAILQ owindows.
49 typedef struct owindow {
51 TAILQ_ENTRY(owindow) owindows;
53 static TAILQ_HEAD(owindows_head, owindow) owindows;
55 /* Holds the JSON which will be returned via IPC or NULL for the default return
57 static char *json_output;
59 /* We don’t need yydebug for now, as we got decent error messages using
60 * yyerror(). Should you ever want to extend the parser, it might be handy
61 * to just comment it in again, so it stays here. */
64 void cmdyyerror(const char *error_message) {
66 ELOG("CMD: %s\n", error_message);
67 ELOG("CMD: in command:\n");
68 ELOG("CMD: %s\n", context->line_copy);
70 for (int c = 1; c <= context->last_column; c++)
71 if (c >= context->first_column)
76 context->compact_error = sstrdup(error_message);
83 char *parse_cmd(const char *new) {
85 LOG("COMMAND: *%s*\n", new);
86 cmdyy_scan_string(new);
88 match_init(¤t_match);
89 context = scalloc(sizeof(struct context));
90 context->filename = "cmd";
91 if (cmdyyparse() != 0) {
92 fprintf(stderr, "Could not parse command\n");
93 asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
94 context->compact_error, context->first_column);
95 FREE(context->line_copy);
96 FREE(context->compact_error);
100 printf("done, json output = %s\n", json_output);
103 FREE(context->line_copy);
104 FREE(context->compact_error);
110 * Returns true if a is definitely greater than b (using the given epsilon)
113 bool definitelyGreaterThan(float a, float b, float epsilon) {
114 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
120 %lex-param { struct context *context }
128 %token TOK_EXEC "exec"
129 %token TOK_EXIT "exit"
130 %token TOK_RELOAD "reload"
131 %token TOK_RESTART "restart"
132 %token TOK_KILL "kill"
133 %token TOK_WINDOW "window"
134 %token TOK_CLIENT "client"
135 %token TOK_FULLSCREEN "fullscreen"
136 %token TOK_GLOBAL "global"
137 %token TOK_LAYOUT "layout"
138 %token TOK_DEFAULT "default"
139 %token TOK_STACKED "stacked"
140 %token TOK_TABBED "tabbed"
141 %token TOK_BORDER "border"
142 %token TOK_NORMAL "normal"
143 %token TOK_NONE "none"
144 %token TOK_1PIXEL "1pixel"
145 %token TOK_MODE "mode"
146 %token TOK_TILING "tiling"
147 %token TOK_FLOATING "floating"
148 %token TOK_MODE_TOGGLE "mode_toggle"
149 %token TOK_ENABLE "enable"
150 %token TOK_DISABLE "disable"
151 %token TOK_WORKSPACE "workspace"
152 %token TOK_OUTPUT "output"
153 %token TOK_TOGGLE "toggle"
154 %token TOK_FOCUS "focus"
155 %token TOK_MOVE "move"
156 %token TOK_OPEN "open"
157 %token TOK_NEXT "next"
158 %token TOK_PREV "prev"
159 %token TOK_SPLIT "split"
160 %token TOK_HORIZONTAL "horizontal"
161 %token TOK_VERTICAL "vertical"
163 %token TOK_DOWN "down"
164 %token TOK_LEFT "left"
165 %token TOK_RIGHT "right"
166 %token TOK_PARENT "parent"
167 %token TOK_CHILD "child"
168 %token TOK_APPEND_LAYOUT "append_layout"
169 %token TOK_MARK "mark"
170 %token TOK_RESIZE "resize"
171 %token TOK_GROW "grow"
172 %token TOK_SHRINK "shrink"
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 asprintf(&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());
592 printf("should switch to workspace %s\n", $2);
593 workspace_show_by_name($2);
603 printf("opening new container\n");
604 Con *con = tree_open_con(NULL, NULL);
606 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
613 TOK_FULLSCREEN fullscreen_mode
615 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
620 TAILQ_FOREACH(current, &owindows, owindows) {
621 printf("matching: %p / %s\n", current->con, current->con->name);
622 con_toggle_fullscreen(current->con, $2);
630 /* empty */ { $$ = CF_OUTPUT; }
631 | TOK_GLOBAL { $$ = CF_GLOBAL; }
635 TOK_SPLIT split_direction
637 /* TODO: use matches */
638 printf("splitting in direction %c\n", $2);
639 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
646 TOK_HORIZONTAL { $$ = 'h'; }
648 | TOK_VERTICAL { $$ = 'v'; }
658 TAILQ_FOREACH(current, &owindows, owindows) {
659 printf("matching: %p / %s\n", current->con, current->con->name);
660 if ($2 == TOK_TOGGLE) {
661 printf("should toggle mode\n");
662 toggle_floating_mode(current->con, false);
664 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
665 if ($2 == TOK_ENABLE) {
666 floating_enable(current->con, false);
668 floating_disable(current->con, false);
678 TOK_ENABLE { $$ = TOK_ENABLE; }
679 | TOK_DISABLE { $$ = TOK_DISABLE; }
680 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
684 TOK_BORDER border_style
686 printf("border style should be changed to %d\n", $2);
691 TAILQ_FOREACH(current, &owindows, owindows) {
692 printf("matching: %p / %s\n", current->con, current->con->name);
693 if ($2 == TOK_TOGGLE) {
694 current->con->border_style++;
695 current->con->border_style %= 3;
696 } else current->con->border_style = $2;
704 TOK_NORMAL { $$ = BS_NORMAL; }
705 | TOK_NONE { $$ = BS_NONE; }
706 | TOK_1PIXEL { $$ = BS_1PIXEL; }
707 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
713 printf("moving in direction %d\n", $2);
718 | TOK_MOVE TOK_WORKSPACE STR
722 printf("should move window to workspace %s\n", $3);
723 /* get the workspace */
724 Con *ws = workspace_get($3, NULL);
729 TAILQ_FOREACH(current, &owindows, owindows) {
730 printf("matching: %p / %s\n", current->con, current->con->name);
731 con_move_to_workspace(current->con, ws, true, false);
736 | TOK_MOVE TOK_WORKSPACE TOK_NEXT
740 /* get the workspace */
741 Con *ws = workspace_next();
745 TAILQ_FOREACH(current, &owindows, owindows) {
746 printf("matching: %p / %s\n", current->con, current->con->name);
747 con_move_to_workspace(current->con, ws, true, false);
752 | TOK_MOVE TOK_WORKSPACE TOK_PREV
756 /* get the workspace */
757 Con *ws = workspace_prev();
761 TAILQ_FOREACH(current, &owindows, owindows) {
762 printf("matching: %p / %s\n", current->con, current->con->name);
763 con_move_to_workspace(current->con, ws, true, false);
768 | TOK_MOVE TOK_OUTPUT STR
772 printf("should move window to output %s", $3);
777 Output *current_output = NULL;
780 TAILQ_FOREACH(current, &owindows, owindows)
781 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
783 assert(current_output != NULL);
785 if (strcasecmp($3, "up") == 0)
786 output = get_output_next(D_UP, current_output);
787 else if (strcasecmp($3, "down") == 0)
788 output = get_output_next(D_DOWN, current_output);
789 else if (strcasecmp($3, "left") == 0)
790 output = get_output_next(D_LEFT, current_output);
791 else if (strcasecmp($3, "right") == 0)
792 output = get_output_next(D_RIGHT, current_output);
794 output = get_output_by_name($3);
800 /* get visible workspace on output */
802 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
806 TAILQ_FOREACH(current, &owindows, owindows) {
807 printf("matching: %p / %s\n", current->con, current->con->name);
808 con_move_to_workspace(current->con, ws, true, false);
816 TOK_APPEND_LAYOUT STR
818 printf("restoring \"%s\"\n", $2);
819 tree_append_json($2);
826 TOK_LAYOUT layout_mode
828 printf("changing layout to %d\n", $2);
831 /* check if the match is empty, not if the result is empty */
832 if (match_is_empty(¤t_match))
833 con_set_layout(focused->parent, $2);
835 TAILQ_FOREACH(current, &owindows, owindows) {
836 printf("matching: %p / %s\n", current->con, current->con->name);
837 con_set_layout(current->con, $2);
846 TOK_DEFAULT { $$ = L_DEFAULT; }
847 | TOK_STACKED { $$ = L_STACKED; }
848 | TOK_TABBED { $$ = L_TABBED; }
854 printf("marking window with str %s\n", $2);
859 TAILQ_FOREACH(current, &owindows, owindows) {
860 printf("matching: %p / %s\n", current->con, current->con->name);
861 current->con->mark = sstrdup($2);
873 printf("-------------------------------------------------\n");
874 printf(" NOP: %s\n", $2);
875 printf("-------------------------------------------------\n");
881 TOK_RESIZE resize_way direction resize_px resize_tiling
883 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
884 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
888 if ($2 == TOK_SHRINK) {
893 if (con_is_floating(focused)) {
894 printf("floating resize\n");
895 if (direction == TOK_UP) {
896 focused->parent->rect.y -= px;
897 focused->parent->rect.height += px;
898 } else if (direction == TOK_DOWN) {
899 focused->parent->rect.height += px;
900 } else if (direction == TOK_LEFT) {
901 focused->parent->rect.x -= px;
902 focused->parent->rect.width += px;
904 focused->parent->rect.width += px;
907 LOG("tiling resize\n");
908 /* get the appropriate current container (skip stacked/tabbed cons) */
909 Con *current = focused;
910 while (current->parent->layout == L_STACKED ||
911 current->parent->layout == L_TABBED)
912 current = current->parent;
913 /* get the default percentage */
914 int children = con_num_children(current->parent);
916 LOG("ins. %d children\n", children);
917 double percentage = 1.0 / children;
918 LOG("default percentage = %f\n", percentage);
920 orientation_t orientation = current->parent->orientation;
922 if ((orientation == HORIZ &&
923 (direction == TOK_UP || direction == TOK_DOWN)) ||
924 (orientation == VERT &&
925 (direction == TOK_LEFT || direction == TOK_RIGHT))) {
926 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
927 (orientation == HORIZ ? "horizontal" : "vertical"));
931 if (direction == TOK_UP || direction == TOK_LEFT) {
932 other = TAILQ_PREV(current, nodes_head, nodes);
934 other = TAILQ_NEXT(current, nodes);
936 if (other == TAILQ_END(workspaces)) {
937 LOG("No other container in this direction found, cannot resize.\n");
940 LOG("other->percent = %f\n", other->percent);
941 LOG("current->percent before = %f\n", current->percent);
942 if (current->percent == 0.0)
943 current->percent = percentage;
944 if (other->percent == 0.0)
945 other->percent = percentage;
946 double new_current_percent = current->percent + ((double)ppt / 100.0);
947 double new_other_percent = other->percent - ((double)ppt / 100.0);
948 LOG("new_current_percent = %f\n", new_current_percent);
949 LOG("new_other_percent = %f\n", new_other_percent);
950 /* Ensure that the new percentages are positive and greater than
951 * 0.05 to have a reasonable minimum size. */
952 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
953 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
954 current->percent += ((double)ppt / 100.0);
955 other->percent -= ((double)ppt / 100.0);
956 LOG("current->percent after = %f\n", current->percent);
957 LOG("other->percent after = %f\n", other->percent);
959 LOG("Not resizing, already at minimum size\n");
983 | TOK_OR NUMBER TOK_PPT
990 TOK_GROW { $$ = TOK_GROW; }
991 | TOK_SHRINK { $$ = TOK_SHRINK; }
995 TOK_UP { $$ = TOK_UP; }
996 | TOK_DOWN { $$ = TOK_DOWN; }
997 | TOK_LEFT { $$ = TOK_LEFT; }
998 | TOK_RIGHT { $$ = TOK_RIGHT; }