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 sasprintf(&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"
177 %token TOK_BACK_AND_FORTH "back_and_forth"
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
210 printf("single command completely parsed, dropping state...\n");
211 while (!TAILQ_EMPTY(&owindows)) {
212 current = TAILQ_FIRST(&owindows);
213 TAILQ_REMOVE(&owindows, current, owindows);
216 match_init(¤t_match);
225 | matchstart criteria matchend
227 printf("match parsed\n");
235 match_init(¤t_match);
236 TAILQ_INIT(&owindows);
239 TAILQ_FOREACH(con, &all_cons, all_cons) {
240 owindow *ow = smalloc(sizeof(owindow));
242 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
250 owindow *next, *current;
252 printf("match specification finished, matching...\n");
253 /* copy the old list head to iterate through it and start with a fresh
254 * list which will contain only matching windows */
255 struct owindows_head old = owindows;
256 TAILQ_INIT(&owindows);
257 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
258 /* make a copy of the next pointer and advance the pointer to the
259 * next element as we are going to invalidate the element’s
260 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
262 next = TAILQ_NEXT(next, owindows);
264 printf("checking if con %p / %s matches\n", current->con, current->con->name);
265 if (current_match.con_id != NULL) {
266 if (current_match.con_id == current->con) {
267 printf("matches container!\n");
268 TAILQ_INSERT_TAIL(&owindows, current, owindows);
271 } else if (current_match.mark != NULL && current->con->mark != NULL &&
272 regex_matches(current_match.mark, current->con->mark)) {
273 printf("match by mark\n");
274 TAILQ_INSERT_TAIL(&owindows, current, owindows);
276 if (current->con->window == NULL)
278 if (match_matches_window(¤t_match, current->con->window)) {
279 printf("matches window!\n");
280 TAILQ_INSERT_TAIL(&owindows, current, owindows);
282 printf("doesnt match\n");
288 TAILQ_FOREACH(current, &owindows, owindows) {
289 printf("matching: %p / %s\n", current->con, current->con->name);
303 printf("criteria: class = %s\n", $3);
304 current_match.class = regex_new($3);
307 | TOK_INSTANCE '=' STR
309 printf("criteria: instance = %s\n", $3);
310 current_match.instance = regex_new($3);
313 | TOK_WINDOW_ROLE '=' STR
315 printf("criteria: window_role = %s\n", $3);
316 current_match.role = regex_new($3);
321 printf("criteria: id = %s\n", $3);
323 long parsed = strtol($3, &end, 10);
324 if (parsed == LONG_MIN ||
325 parsed == LONG_MAX ||
327 (end && *end != '\0')) {
328 ELOG("Could not parse con id \"%s\"\n", $3);
330 current_match.con_id = (Con*)parsed;
331 printf("id as int = %p\n", current_match.con_id);
336 printf("criteria: window id = %s\n", $3);
338 long parsed = strtol($3, &end, 10);
339 if (parsed == LONG_MIN ||
340 parsed == LONG_MAX ||
342 (end && *end != '\0')) {
343 ELOG("Could not parse window id \"%s\"\n", $3);
345 current_match.id = parsed;
346 printf("window id as int = %d\n", current_match.id);
351 printf("criteria: mark = %s\n", $3);
352 current_match.mark = regex_new($3);
357 printf("criteria: title = %s\n", $3);
358 current_match.title = regex_new($3);
365 | operations ',' operation
393 printf("should execute %s\n", $2);
394 start_application($2);
402 printf("exit, bye bye\n");
410 printf("reloading\n");
411 kill_configerror_nagbar(false);
412 load_configuration(conn, NULL, true);
414 /* Send an IPC event just in case the ws names have changed */
415 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
422 printf("restarting i3\n");
432 if (match_is_empty(¤t_match)) {
433 ELOG("You have to specify which window/container should be focused.\n");
434 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
436 sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
437 "specify which window/container should be focused\"}");
442 TAILQ_FOREACH(current, &owindows, owindows) {
443 Con *ws = con_get_workspace(current->con);
445 /* If the container is not on the current workspace,
446 * workspace_show() will switch to a different workspace and (if
447 * enabled) trigger a mouse pointer warp to the currently focused
448 * container (!) on the target workspace.
450 * Therefore, before calling workspace_show(), we make sure that
451 * 'current' will be focused on the workspace. However, we cannot
452 * just con_focus(current) because then the pointer will not be
453 * warped at all (the code thinks we are already there).
455 * So we focus 'current' to make it the currently focused window of
456 * the target workspace, then revert focus. */
457 Con *currently_focused = focused;
458 con_focus(current->con);
459 con_focus(currently_focused);
461 /* Now switch to the workspace, then focus */
463 LOG("focusing %p / %s\n", current->con, current->con->name);
464 con_focus(current->con);
469 LOG("WARNING: Your criteria for the focus command matches %d containers, "
470 "while only exactly one container can be focused at a time.\n", count);
474 | TOK_FOCUS direction
479 LOG("Focusing left\n");
480 tree_next('p', HORIZ);
483 LOG("Focusing right\n");
484 tree_next('n', HORIZ);
487 LOG("Focusing up\n");
488 tree_next('p', VERT);
491 LOG("Focusing down\n");
492 tree_next('n', VERT);
495 ELOG("Invalid focus direction (%d)\n", direction);
501 | TOK_FOCUS window_mode
503 printf("should focus: ");
505 if ($2 == TOK_TILING)
507 else if ($2 == TOK_FLOATING)
508 printf("floating\n");
509 else printf("mode toggle\n");
511 Con *ws = con_get_workspace(focused);
515 if ($2 == TOK_MODE_TOGGLE) {
516 current = TAILQ_FIRST(&(ws->focus_head));
517 if (current != NULL && current->type == CT_FLOATING_CON)
518 to_focus = TOK_TILING;
519 else to_focus = TOK_FLOATING;
521 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
522 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
523 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
526 con_focus(con_descend_focused(current));
535 if ($2 == TOK_PARENT)
544 TOK_TILING { $$ = TOK_TILING; }
545 | TOK_FLOATING { $$ = TOK_FLOATING; }
546 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
550 TOK_PARENT { $$ = TOK_PARENT; }
551 | TOK_CHILD { $$ = TOK_CHILD; }
555 TOK_KILL optional_kill_mode
559 printf("killing!\n");
560 /* check if the match is empty, not if the result is empty */
561 if (match_is_empty(¤t_match))
564 TAILQ_FOREACH(current, &owindows, owindows) {
565 printf("matching: %p / %s\n", current->con, current->con->name);
566 tree_close(current->con, $2, false, false);
575 /* empty */ { $$ = KILL_WINDOW; }
576 | TOK_WINDOW { $$ = KILL_WINDOW; }
577 | TOK_CLIENT { $$ = KILL_CLIENT; }
581 TOK_WORKSPACE TOK_NEXT
583 workspace_show(workspace_next());
586 | TOK_WORKSPACE TOK_PREV
588 workspace_show(workspace_prev());
591 | TOK_WORKSPACE TOK_BACK_AND_FORTH
593 workspace_back_and_forth();
598 printf("should switch to workspace %s\n", $2);
600 Con *ws = con_get_workspace(focused);
602 /* Check if the command wants to switch to the current workspace */
603 if (strcmp(ws->name, $2) == 0) {
604 printf("This workspace is already focused.\n");
605 if (config.workspace_auto_back_and_forth) {
606 workspace_back_and_forth();
613 workspace_show_by_name($2);
623 printf("opening new container\n");
624 Con *con = tree_open_con(NULL, NULL);
626 sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
633 TOK_FULLSCREEN fullscreen_mode
635 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
640 TAILQ_FOREACH(current, &owindows, owindows) {
641 printf("matching: %p / %s\n", current->con, current->con->name);
642 con_toggle_fullscreen(current->con, $2);
650 /* empty */ { $$ = CF_OUTPUT; }
651 | TOK_GLOBAL { $$ = CF_GLOBAL; }
655 TOK_SPLIT split_direction
657 /* TODO: use matches */
658 printf("splitting in direction %c\n", $2);
659 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
666 TOK_HORIZONTAL { $$ = 'h'; }
668 | TOK_VERTICAL { $$ = 'v'; }
678 TAILQ_FOREACH(current, &owindows, owindows) {
679 printf("matching: %p / %s\n", current->con, current->con->name);
680 if ($2 == TOK_TOGGLE) {
681 printf("should toggle mode\n");
682 toggle_floating_mode(current->con, false);
684 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
685 if ($2 == TOK_ENABLE) {
686 floating_enable(current->con, false);
688 floating_disable(current->con, false);
698 TOK_ENABLE { $$ = TOK_ENABLE; }
699 | TOK_DISABLE { $$ = TOK_DISABLE; }
700 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
704 TOK_BORDER border_style
706 printf("border style should be changed to %d\n", $2);
711 TAILQ_FOREACH(current, &owindows, owindows) {
712 printf("matching: %p / %s\n", current->con, current->con->name);
713 if ($2 == TOK_TOGGLE) {
714 current->con->border_style++;
715 current->con->border_style %= 3;
716 } else current->con->border_style = $2;
724 TOK_NORMAL { $$ = BS_NORMAL; }
725 | TOK_NONE { $$ = BS_NONE; }
726 | TOK_1PIXEL { $$ = BS_1PIXEL; }
727 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
733 printf("moving in direction %d\n", $2);
738 | TOK_MOVE TOK_WORKSPACE STR
742 printf("should move window to workspace %s\n", $3);
743 /* get the workspace */
744 Con *ws = workspace_get($3, NULL);
749 TAILQ_FOREACH(current, &owindows, owindows) {
750 printf("matching: %p / %s\n", current->con, current->con->name);
751 con_move_to_workspace(current->con, ws, true, false);
756 | TOK_MOVE TOK_WORKSPACE TOK_NEXT
760 /* get the workspace */
761 Con *ws = workspace_next();
765 TAILQ_FOREACH(current, &owindows, owindows) {
766 printf("matching: %p / %s\n", current->con, current->con->name);
767 con_move_to_workspace(current->con, ws, true, false);
772 | TOK_MOVE TOK_WORKSPACE TOK_PREV
776 /* get the workspace */
777 Con *ws = workspace_prev();
781 TAILQ_FOREACH(current, &owindows, owindows) {
782 printf("matching: %p / %s\n", current->con, current->con->name);
783 con_move_to_workspace(current->con, ws, true, false);
788 | TOK_MOVE TOK_OUTPUT STR
792 printf("should move window to output %s", $3);
797 Output *current_output = NULL;
800 TAILQ_FOREACH(current, &owindows, owindows)
801 current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
803 assert(current_output != NULL);
805 if (strcasecmp($3, "up") == 0)
806 output = get_output_next(D_UP, current_output);
807 else if (strcasecmp($3, "down") == 0)
808 output = get_output_next(D_DOWN, current_output);
809 else if (strcasecmp($3, "left") == 0)
810 output = get_output_next(D_LEFT, current_output);
811 else if (strcasecmp($3, "right") == 0)
812 output = get_output_next(D_RIGHT, current_output);
814 output = get_output_by_name($3);
820 /* get visible workspace on output */
822 GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
826 TAILQ_FOREACH(current, &owindows, owindows) {
827 printf("matching: %p / %s\n", current->con, current->con->name);
828 con_move_to_workspace(current->con, ws, true, false);
836 TOK_APPEND_LAYOUT STR
838 printf("restoring \"%s\"\n", $2);
839 tree_append_json($2);
846 TOK_LAYOUT layout_mode
848 printf("changing layout to %d\n", $2);
851 /* check if the match is empty, not if the result is empty */
852 if (match_is_empty(¤t_match))
853 con_set_layout(focused->parent, $2);
855 TAILQ_FOREACH(current, &owindows, owindows) {
856 printf("matching: %p / %s\n", current->con, current->con->name);
857 con_set_layout(current->con, $2);
866 TOK_DEFAULT { $$ = L_DEFAULT; }
867 | TOK_STACKED { $$ = L_STACKED; }
868 | TOK_TABBED { $$ = L_TABBED; }
874 printf("marking window with str %s\n", $2);
879 TAILQ_FOREACH(current, &owindows, owindows) {
880 printf("matching: %p / %s\n", current->con, current->con->name);
881 current->con->mark = sstrdup($2);
893 printf("-------------------------------------------------\n");
894 printf(" NOP: %s\n", $2);
895 printf("-------------------------------------------------\n");
901 TOK_RESIZE resize_way direction resize_px resize_tiling
903 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
904 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
908 if ($2 == TOK_SHRINK) {
913 if (con_is_floating(focused)) {
914 printf("floating resize\n");
915 if (direction == TOK_UP) {
916 focused->parent->rect.y -= px;
917 focused->parent->rect.height += px;
918 } else if (direction == TOK_DOWN) {
919 focused->parent->rect.height += px;
920 } else if (direction == TOK_LEFT) {
921 focused->parent->rect.x -= px;
922 focused->parent->rect.width += px;
924 focused->parent->rect.width += px;
927 LOG("tiling resize\n");
928 /* get the appropriate current container (skip stacked/tabbed cons) */
929 Con *current = focused;
930 while (current->parent->layout == L_STACKED ||
931 current->parent->layout == L_TABBED)
932 current = current->parent;
933 /* get the default percentage */
934 int children = con_num_children(current->parent);
936 LOG("ins. %d children\n", children);
937 double percentage = 1.0 / children;
938 LOG("default percentage = %f\n", percentage);
940 orientation_t orientation = current->parent->orientation;
942 if ((orientation == HORIZ &&
943 (direction == TOK_UP || direction == TOK_DOWN)) ||
944 (orientation == VERT &&
945 (direction == TOK_LEFT || direction == TOK_RIGHT))) {
946 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
947 (orientation == HORIZ ? "horizontal" : "vertical"));
951 if (direction == TOK_UP || direction == TOK_LEFT) {
952 other = TAILQ_PREV(current, nodes_head, nodes);
954 other = TAILQ_NEXT(current, nodes);
956 if (other == TAILQ_END(workspaces)) {
957 LOG("No other container in this direction found, cannot resize.\n");
960 LOG("other->percent = %f\n", other->percent);
961 LOG("current->percent before = %f\n", current->percent);
962 if (current->percent == 0.0)
963 current->percent = percentage;
964 if (other->percent == 0.0)
965 other->percent = percentage;
966 double new_current_percent = current->percent + ((double)ppt / 100.0);
967 double new_other_percent = other->percent - ((double)ppt / 100.0);
968 LOG("new_current_percent = %f\n", new_current_percent);
969 LOG("new_other_percent = %f\n", new_other_percent);
970 /* Ensure that the new percentages are positive and greater than
971 * 0.05 to have a reasonable minimum size. */
972 if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
973 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
974 current->percent += ((double)ppt / 100.0);
975 other->percent -= ((double)ppt / 100.0);
976 LOG("current->percent after = %f\n", current->percent);
977 LOG("other->percent after = %f\n", other->percent);
979 LOG("Not resizing, already at minimum size\n");
1003 | TOK_OR NUMBER TOK_PPT
1010 TOK_GROW { $$ = TOK_GROW; }
1011 | TOK_SHRINK { $$ = TOK_SHRINK; }
1015 TOK_UP { $$ = TOK_UP; }
1016 | TOK_DOWN { $$ = TOK_DOWN; }
1017 | TOK_LEFT { $$ = TOK_LEFT; }
1018 | TOK_RIGHT { $$ = TOK_RIGHT; }