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>
21 /** When the command did not include match criteria (!), we use the currently
22 * focused command. Do not confuse this case with a command which included
23 * criteria but which did not match any windows. This macro has to be called in
26 #define HANDLE_EMPTY_MATCH do { \
27 if (match_is_empty(¤t_match)) { \
28 owindow *ow = smalloc(sizeof(owindow)); \
30 TAILQ_INIT(&owindows); \
31 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
35 typedef struct yy_buffer_state *YY_BUFFER_STATE;
36 extern int cmdyylex(struct context *context);
37 extern int cmdyyparse(void);
38 extern int cmdyylex_destroy(void);
40 YY_BUFFER_STATE cmdyy_scan_string(const char *);
42 static struct context *context;
43 static Match current_match;
46 * Helper data structure for an operation window (window on which the operation
47 * will be performed). Used to build the TAILQ owindows.
50 typedef struct owindow {
52 TAILQ_ENTRY(owindow) owindows;
54 static TAILQ_HEAD(owindows_head, owindow) owindows;
56 /* Holds the JSON which will be returned via IPC or NULL for the default return
58 static char *json_output;
60 /* We don’t need yydebug for now, as we got decent error messages using
61 * yyerror(). Should you ever want to extend the parser, it might be handy
62 * to just comment it in again, so it stays here. */
65 void cmdyyerror(const char *error_message) {
67 ELOG("CMD: %s\n", error_message);
68 ELOG("CMD: in command:\n");
69 ELOG("CMD: %s\n", context->line_copy);
71 for (int c = 1; c <= context->last_column; c++)
72 if (c >= context->first_column)
77 context->compact_error = sstrdup(error_message);
84 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";
92 if (cmdyyparse() != 0) {
93 fprintf(stderr, "Could not parse command\n");
94 asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
95 context->compact_error, context->first_column);
96 FREE(context->line_copy);
97 FREE(context->compact_error);
101 printf("done, json output = %s\n", json_output);
104 FREE(context->line_copy);
105 FREE(context->compact_error);
111 * Returns true if a is definitely greater than b (using the given epsilon)
114 bool definitelyGreaterThan(float a, float b, float epsilon) {
115 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
121 %lex-param { struct context *context }
129 %token TOK_EXEC "exec"
130 %token TOK_EXIT "exit"
131 %token TOK_RELOAD "reload"
132 %token TOK_RESTART "restart"
133 %token TOK_KILL "kill"
134 %token TOK_WINDOW "window"
135 %token TOK_CLIENT "client"
136 %token TOK_FULLSCREEN "fullscreen"
137 %token TOK_GLOBAL "global"
138 %token TOK_LAYOUT "layout"
139 %token TOK_DEFAULT "default"
140 %token TOK_STACKED "stacked"
141 %token TOK_TABBED "tabbed"
142 %token TOK_BORDER "border"
143 %token TOK_NORMAL "normal"
144 %token TOK_NONE "none"
145 %token TOK_1PIXEL "1pixel"
146 %token TOK_MODE "mode"
147 %token TOK_TILING "tiling"
148 %token TOK_FLOATING "floating"
149 %token TOK_MODE_TOGGLE "mode_toggle"
150 %token TOK_ENABLE "enable"
151 %token TOK_DISABLE "disable"
152 %token TOK_WORKSPACE "workspace"
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"
180 %token TOK_CON_ID "con_id"
181 %token TOK_TITLE "title"
183 %token <string> STR "<string>"
184 %token <number> NUMBER "<number>"
186 %type <number> direction
187 %type <number> split_direction
188 %type <number> fullscreen_mode
190 %type <number> window_mode
191 %type <number> boolean
192 %type <number> border_style
193 %type <number> layout_mode
194 %type <number> resize_px
195 %type <number> resize_way
196 %type <number> resize_tiling
197 %type <number> optional_kill_mode
207 printf("single command completely parsed, dropping state...\n");
208 while (!TAILQ_EMPTY(&owindows)) {
209 current = TAILQ_FIRST(&owindows);
210 TAILQ_REMOVE(&owindows, current, owindows);
213 match_init(¤t_match);
222 | matchstart criteria matchend
224 printf("match parsed\n");
232 match_init(¤t_match);
233 TAILQ_INIT(&owindows);
236 TAILQ_FOREACH(con, &all_cons, all_cons) {
237 owindow *ow = smalloc(sizeof(owindow));
239 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
247 owindow *next, *current;
249 printf("match specification finished, matching...\n");
250 /* copy the old list head to iterate through it and start with a fresh
251 * list which will contain only matching windows */
252 struct owindows_head old = owindows;
253 TAILQ_INIT(&owindows);
254 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
255 /* make a copy of the next pointer and advance the pointer to the
256 * next element as we are going to invalidate the element’s
257 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
259 next = TAILQ_NEXT(next, owindows);
261 printf("checking if con %p / %s matches\n", current->con, current->con->name);
262 if (current_match.con_id != NULL) {
263 if (current_match.con_id == current->con) {
264 printf("matches container!\n");
265 TAILQ_INSERT_TAIL(&owindows, current, owindows);
268 } else if (current_match.mark != NULL && current->con->mark != NULL &&
269 strcasecmp(current_match.mark, current->con->mark) == 0) {
270 printf("match by mark\n");
271 TAILQ_INSERT_TAIL(&owindows, current, owindows);
274 if (current->con->window == NULL)
276 if (match_matches_window(¤t_match, current->con->window)) {
277 printf("matches window!\n");
278 TAILQ_INSERT_TAIL(&owindows, current, owindows);
280 printf("doesnt match\n");
286 TAILQ_FOREACH(current, &owindows, owindows) {
287 printf("matching: %p / %s\n", current->con, current->con->name);
301 printf("criteria: class = %s\n", $3);
302 current_match.class = $3;
306 printf("criteria: id = %s\n", $3);
308 long parsed = strtol($3, &end, 10);
309 if (parsed == LONG_MIN ||
310 parsed == LONG_MAX ||
312 (end && *end != '\0')) {
313 ELOG("Could not parse con id \"%s\"\n", $3);
315 current_match.con_id = (Con*)parsed;
316 printf("id as int = %p\n", current_match.con_id);
321 printf("criteria: window 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 window id \"%s\"\n", $3);
330 current_match.id = parsed;
331 printf("window id as int = %d\n", current_match.id);
336 printf("criteria: mark = %s\n", $3);
337 current_match.mark = $3;
341 printf("criteria: title = %s\n", $3);
342 current_match.title = $3;
348 | operations ',' operation
376 printf("should execute %s\n", $2);
377 start_application($2);
385 printf("exit, bye bye\n");
393 printf("reloading\n");
394 kill_configerror_nagbar(false);
395 load_configuration(conn, NULL, true);
397 /* Send an IPC event just in case the ws names have changed */
398 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
405 printf("restarting i3\n");
415 if (match_is_empty(¤t_match)) {
416 ELOG("You have to specify which window/container should be focused.\n");
417 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
419 asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
420 "specify which window/container should be focused\"}");
425 TAILQ_FOREACH(current, &owindows, owindows) {
426 Con *ws = con_get_workspace(current->con);
427 workspace_show(ws->name);
428 LOG("focusing %p / %s\n", current->con, current->con->name);
429 con_focus(current->con);
434 LOG("WARNING: Your criteria for the focus command matches %d containers, "
435 "while only exactly one container can be focused at a time.\n", count);
439 | TOK_FOCUS direction
444 LOG("Focusing left\n");
445 tree_next('p', HORIZ);
448 LOG("Focusing right\n");
449 tree_next('n', HORIZ);
452 LOG("Focusing up\n");
453 tree_next('p', VERT);
456 LOG("Focusing down\n");
457 tree_next('n', VERT);
460 ELOG("Invalid focus direction (%d)\n", direction);
466 | TOK_FOCUS window_mode
468 printf("should focus: ");
470 if ($2 == TOK_TILING)
472 else if ($2 == TOK_FLOATING)
473 printf("floating\n");
474 else printf("mode toggle\n");
476 Con *ws = con_get_workspace(focused);
480 if ($2 == TOK_MODE_TOGGLE) {
481 current = TAILQ_FIRST(&(ws->focus_head));
482 if (current->type == CT_FLOATING_CON)
483 to_focus = TOK_TILING;
484 else to_focus = TOK_FLOATING;
486 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
487 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
488 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
491 con_focus(con_descend_focused(current));
500 if ($2 == TOK_PARENT)
509 TOK_TILING { $$ = TOK_TILING; }
510 | TOK_FLOATING { $$ = TOK_FLOATING; }
511 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
515 TOK_PARENT { $$ = TOK_PARENT; }
516 | TOK_CHILD { $$ = TOK_CHILD; }
520 TOK_KILL optional_kill_mode
524 printf("killing!\n");
525 /* check if the match is empty, not if the result is empty */
526 if (match_is_empty(¤t_match))
529 TAILQ_FOREACH(current, &owindows, owindows) {
530 printf("matching: %p / %s\n", current->con, current->con->name);
531 tree_close(current->con, $2, false);
540 /* empty */ { $$ = KILL_WINDOW; }
541 | TOK_WINDOW { $$ = KILL_WINDOW; }
542 | TOK_CLIENT { $$ = KILL_CLIENT; }
546 TOK_WORKSPACE TOK_NEXT
551 | TOK_WORKSPACE TOK_PREV
558 printf("should switch to workspace %s\n", $2);
569 printf("opening new container\n");
570 Con *con = tree_open_con(NULL, NULL);
572 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
579 TOK_FULLSCREEN fullscreen_mode
581 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
586 TAILQ_FOREACH(current, &owindows, owindows) {
587 printf("matching: %p / %s\n", current->con, current->con->name);
588 con_toggle_fullscreen(current->con, $2);
596 /* empty */ { $$ = CF_OUTPUT; }
597 | TOK_GLOBAL { $$ = CF_GLOBAL; }
601 TOK_SPLIT split_direction
603 /* TODO: use matches */
604 printf("splitting in direction %c\n", $2);
605 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
612 TOK_HORIZONTAL { $$ = 'h'; }
614 | TOK_VERTICAL { $$ = 'v'; }
624 TAILQ_FOREACH(current, &owindows, owindows) {
625 printf("matching: %p / %s\n", current->con, current->con->name);
626 if ($2 == TOK_TOGGLE) {
627 printf("should toggle mode\n");
628 toggle_floating_mode(current->con, false);
630 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
631 if ($2 == TOK_ENABLE) {
632 floating_enable(current->con, false);
634 floating_disable(current->con, false);
644 TOK_ENABLE { $$ = TOK_ENABLE; }
645 | TOK_DISABLE { $$ = TOK_DISABLE; }
646 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
650 TOK_BORDER border_style
652 printf("border style should be changed to %d\n", $2);
657 TAILQ_FOREACH(current, &owindows, owindows) {
658 printf("matching: %p / %s\n", current->con, current->con->name);
659 if ($2 == TOK_TOGGLE) {
660 current->con->border_style++;
661 current->con->border_style %= 3;
662 } else current->con->border_style = $2;
670 TOK_NORMAL { $$ = BS_NORMAL; }
671 | TOK_NONE { $$ = BS_NONE; }
672 | TOK_1PIXEL { $$ = BS_1PIXEL; }
673 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
679 printf("moving in direction %d\n", $2);
684 | TOK_MOVE TOK_WORKSPACE STR
688 printf("should move window to workspace %s\n", $3);
689 /* get the workspace */
690 Con *ws = workspace_get($3, NULL);
695 TAILQ_FOREACH(current, &owindows, owindows) {
696 printf("matching: %p / %s\n", current->con, current->con->name);
697 con_move_to_workspace(current->con, ws);
705 TOK_APPEND_LAYOUT STR
707 printf("restoring \"%s\"\n", $2);
708 tree_append_json($2);
715 TOK_LAYOUT layout_mode
717 printf("changing layout to %d\n", $2);
720 /* check if the match is empty, not if the result is empty */
721 if (match_is_empty(¤t_match))
722 con_set_layout(focused->parent, $2);
724 TAILQ_FOREACH(current, &owindows, owindows) {
725 printf("matching: %p / %s\n", current->con, current->con->name);
726 con_set_layout(current->con, $2);
735 TOK_DEFAULT { $$ = L_DEFAULT; }
736 | TOK_STACKED { $$ = L_STACKED; }
737 | TOK_TABBED { $$ = L_TABBED; }
743 printf("marking window with str %s\n", $2);
748 TAILQ_FOREACH(current, &owindows, owindows) {
749 printf("matching: %p / %s\n", current->con, current->con->name);
750 current->con->mark = sstrdup($2);
762 printf("-------------------------------------------------\n");
763 printf(" NOP: %s\n", $2);
764 printf("-------------------------------------------------\n");
770 TOK_RESIZE resize_way direction resize_px resize_tiling
772 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
773 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
777 if ($2 == TOK_SHRINK) {
782 if (con_is_floating(focused)) {
783 printf("floating resize\n");
784 if (direction == TOK_UP) {
785 focused->parent->rect.y -= px;
786 focused->parent->rect.height += px;
787 } else if (direction == TOK_DOWN) {
788 focused->rect.height += px;
789 } else if (direction == TOK_LEFT) {
790 focused->rect.x -= px;
791 focused->rect.width += px;
793 focused->rect.width += px;
796 LOG("tiling resize\n");
797 /* get the default percentage */
798 int children = con_num_children(focused->parent);
800 LOG("ins. %d children\n", children);
801 double percentage = 1.0 / children;
802 LOG("default percentage = %f\n", percentage);
804 if (direction == TOK_UP || direction == TOK_LEFT) {
805 other = TAILQ_PREV(focused, nodes_head, nodes);
807 other = TAILQ_NEXT(focused, nodes);
809 if (other == TAILQ_END(workspaces)) {
810 LOG("No other container in this direction found, cannot resize.\n");
813 LOG("other->percent = %f\n", other->percent);
814 LOG("focused->percent before = %f\n", focused->percent);
815 if (focused->percent == 0.0)
816 focused->percent = percentage;
817 if (other->percent == 0.0)
818 other->percent = percentage;
819 double new_focused_percent = focused->percent + ((double)ppt / 100.0);
820 double new_other_percent = other->percent - ((double)ppt / 100.0);
821 LOG("new_focused_percent = %f\n", new_focused_percent);
822 LOG("new_other_percent = %f\n", new_other_percent);
823 /* Ensure that the new percentages are positive and greater than
824 * 0.05 to have a reasonable minimum size. */
825 if (definitelyGreaterThan(new_focused_percent, 0.05, DBL_EPSILON) &&
826 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
827 focused->percent += ((double)ppt / 100.0);
828 other->percent -= ((double)ppt / 100.0);
829 LOG("focused->percent after = %f\n", focused->percent);
830 LOG("other->percent after = %f\n", other->percent);
832 LOG("Not resizing, already at minimum size\n");
856 | TOK_OR NUMBER TOK_PPT
863 TOK_GROW { $$ = TOK_GROW; }
864 | TOK_SHRINK { $$ = TOK_SHRINK; }
868 TOK_UP { $$ = TOK_UP; }
869 | TOK_DOWN { $$ = TOK_DOWN; }
870 | TOK_LEFT { $$ = TOK_LEFT; }
871 | TOK_RIGHT { $$ = TOK_RIGHT; }