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) {
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";
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);
112 %lex-param { struct context *context }
120 %token TOK_EXEC "exec"
121 %token TOK_EXIT "exit"
122 %token TOK_RELOAD "reload"
123 %token TOK_RESTART "restart"
124 %token TOK_KILL "kill"
125 %token TOK_WINDOW "window"
126 %token TOK_CLIENT "client"
127 %token TOK_FULLSCREEN "fullscreen"
128 %token TOK_GLOBAL "global"
129 %token TOK_LAYOUT "layout"
130 %token TOK_DEFAULT "default"
131 %token TOK_STACKED "stacked"
132 %token TOK_TABBED "tabbed"
133 %token TOK_BORDER "border"
134 %token TOK_NORMAL "normal"
135 %token TOK_NONE "none"
136 %token TOK_1PIXEL "1pixel"
137 %token TOK_MODE "mode"
138 %token TOK_TILING "tiling"
139 %token TOK_FLOATING "floating"
140 %token TOK_MODE_TOGGLE "mode_toggle"
141 %token TOK_ENABLE "enable"
142 %token TOK_DISABLE "disable"
143 %token TOK_WORKSPACE "workspace"
144 %token TOK_TOGGLE "toggle"
145 %token TOK_FOCUS "focus"
146 %token TOK_MOVE "move"
147 %token TOK_OPEN "open"
148 %token TOK_NEXT "next"
149 %token TOK_PREV "prev"
150 %token TOK_SPLIT "split"
151 %token TOK_HORIZONTAL "horizontal"
152 %token TOK_VERTICAL "vertical"
154 %token TOK_DOWN "down"
155 %token TOK_LEFT "left"
156 %token TOK_RIGHT "right"
157 %token TOK_PARENT "parent"
158 %token TOK_CHILD "child"
159 %token TOK_APPEND_LAYOUT "append_layout"
160 %token TOK_MARK "mark"
161 %token TOK_RESIZE "resize"
162 %token TOK_GROW "grow"
163 %token TOK_SHRINK "shrink"
169 %token TOK_CLASS "class"
171 %token TOK_CON_ID "con_id"
172 %token TOK_TITLE "title"
174 %token <string> STR "<string>"
175 %token <number> NUMBER "<number>"
177 %type <number> direction
178 %type <number> split_direction
179 %type <number> fullscreen_mode
181 %type <number> window_mode
182 %type <number> boolean
183 %type <number> border_style
184 %type <number> layout_mode
185 %type <number> resize_px
186 %type <number> resize_way
187 %type <number> resize_tiling
188 %type <number> optional_kill_mode
198 printf("single command completely parsed, dropping state...\n");
199 while (!TAILQ_EMPTY(&owindows)) {
200 current = TAILQ_FIRST(&owindows);
201 TAILQ_REMOVE(&owindows, current, owindows);
204 match_init(¤t_match);
213 | matchstart criteria matchend
215 printf("match parsed\n");
223 match_init(¤t_match);
224 TAILQ_INIT(&owindows);
227 TAILQ_FOREACH(con, &all_cons, all_cons) {
228 owindow *ow = smalloc(sizeof(owindow));
230 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
238 owindow *next, *current;
240 printf("match specification finished, matching...\n");
241 /* copy the old list head to iterate through it and start with a fresh
242 * list which will contain only matching windows */
243 struct owindows_head old = owindows;
244 TAILQ_INIT(&owindows);
245 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
246 /* make a copy of the next pointer and advance the pointer to the
247 * next element as we are going to invalidate the element’s
248 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
250 next = TAILQ_NEXT(next, owindows);
252 printf("checking if con %p / %s matches\n", current->con, current->con->name);
253 if (current_match.con_id != NULL) {
254 if (current_match.con_id == current->con) {
255 printf("matches container!\n");
256 TAILQ_INSERT_TAIL(&owindows, current, owindows);
259 } else if (current_match.mark != NULL && current->con->mark != NULL &&
260 strcasecmp(current_match.mark, current->con->mark) == 0) {
261 printf("match by mark\n");
262 TAILQ_INSERT_TAIL(&owindows, current, owindows);
265 if (current->con->window == NULL)
267 if (match_matches_window(¤t_match, current->con->window)) {
268 printf("matches window!\n");
269 TAILQ_INSERT_TAIL(&owindows, current, owindows);
271 printf("doesnt match\n");
277 TAILQ_FOREACH(current, &owindows, owindows) {
278 printf("matching: %p / %s\n", current->con, current->con->name);
292 printf("criteria: class = %s\n", $3);
293 current_match.class = $3;
297 printf("criteria: id = %s\n", $3);
299 long parsed = strtol($3, &end, 10);
300 if (parsed == LONG_MIN ||
301 parsed == LONG_MAX ||
303 (end && *end != '\0')) {
304 ELOG("Could not parse con id \"%s\"\n", $3);
306 current_match.con_id = (Con*)parsed;
307 printf("id as int = %p\n", current_match.con_id);
312 printf("criteria: window id = %s\n", $3);
314 long parsed = strtol($3, &end, 10);
315 if (parsed == LONG_MIN ||
316 parsed == LONG_MAX ||
318 (end && *end != '\0')) {
319 ELOG("Could not parse window id \"%s\"\n", $3);
321 current_match.id = parsed;
322 printf("window id as int = %d\n", current_match.id);
327 printf("criteria: mark = %s\n", $3);
328 current_match.mark = $3;
332 printf("criteria: title = %s\n", $3);
333 current_match.title = $3;
339 | operations ',' operation
367 printf("should execute %s\n", $2);
368 start_application($2);
376 printf("exit, bye bye\n");
384 printf("reloading\n");
385 kill_configerror_nagbar(false);
386 load_configuration(conn, NULL, true);
388 /* Send an IPC event just in case the ws names have changed */
389 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
396 printf("restarting i3\n");
406 if (match_is_empty(¤t_match)) {
407 ELOG("You have to specify which window/container should be focused.\n");
408 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
410 asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
411 "specify which window/container should be focused\"}");
416 TAILQ_FOREACH(current, &owindows, owindows) {
417 Con *ws = con_get_workspace(current->con);
418 workspace_show(ws->name);
419 LOG("focusing %p / %s\n", current->con, current->con->name);
420 con_focus(current->con);
425 LOG("WARNING: Your criteria for the focus command matches %d containers, "
426 "while only exactly one container can be focused at a time.\n", count);
430 | TOK_FOCUS direction
435 LOG("Focusing left\n");
436 tree_next('p', HORIZ);
439 LOG("Focusing right\n");
440 tree_next('n', HORIZ);
443 LOG("Focusing up\n");
444 tree_next('p', VERT);
447 LOG("Focusing down\n");
448 tree_next('n', VERT);
451 ELOG("Invalid focus direction (%d)\n", direction);
457 | TOK_FOCUS window_mode
459 printf("should focus: ");
461 if ($2 == TOK_TILING)
463 else if ($2 == TOK_FLOATING)
464 printf("floating\n");
465 else printf("mode toggle\n");
467 Con *ws = con_get_workspace(focused);
471 if ($2 == TOK_MODE_TOGGLE) {
472 current = TAILQ_FIRST(&(ws->focus_head));
473 if (current->type == CT_FLOATING_CON)
474 to_focus = TOK_TILING;
475 else to_focus = TOK_FLOATING;
477 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
478 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
479 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
482 con_focus(con_descend_focused(current));
491 if ($2 == TOK_PARENT)
500 TOK_TILING { $$ = TOK_TILING; }
501 | TOK_FLOATING { $$ = TOK_FLOATING; }
502 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
506 TOK_PARENT { $$ = TOK_PARENT; }
507 | TOK_CHILD { $$ = TOK_CHILD; }
511 TOK_KILL optional_kill_mode
515 printf("killing!\n");
516 /* check if the match is empty, not if the result is empty */
517 if (match_is_empty(¤t_match))
520 TAILQ_FOREACH(current, &owindows, owindows) {
521 printf("matching: %p / %s\n", current->con, current->con->name);
522 tree_close(current->con, $2, false);
531 /* empty */ { $$ = KILL_WINDOW; }
532 | TOK_WINDOW { $$ = KILL_WINDOW; }
533 | TOK_CLIENT { $$ = KILL_CLIENT; }
537 TOK_WORKSPACE TOK_NEXT
542 | TOK_WORKSPACE TOK_PREV
549 printf("should switch to workspace %s\n", $2);
560 printf("opening new container\n");
561 Con *con = tree_open_con(NULL, NULL);
563 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
570 TOK_FULLSCREEN fullscreen_mode
572 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
577 TAILQ_FOREACH(current, &owindows, owindows) {
578 printf("matching: %p / %s\n", current->con, current->con->name);
579 con_toggle_fullscreen(current->con, $2);
587 /* empty */ { $$ = CF_OUTPUT; }
588 | TOK_GLOBAL { $$ = CF_GLOBAL; }
592 TOK_SPLIT split_direction
594 /* TODO: use matches */
595 printf("splitting in direction %c\n", $2);
596 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
603 TOK_HORIZONTAL { $$ = 'h'; }
605 | TOK_VERTICAL { $$ = 'v'; }
615 TAILQ_FOREACH(current, &owindows, owindows) {
616 printf("matching: %p / %s\n", current->con, current->con->name);
617 if ($2 == TOK_TOGGLE) {
618 printf("should toggle mode\n");
619 toggle_floating_mode(current->con, false);
621 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
622 if ($2 == TOK_ENABLE) {
623 floating_enable(current->con, false);
625 floating_disable(current->con, false);
635 TOK_ENABLE { $$ = TOK_ENABLE; }
636 | TOK_DISABLE { $$ = TOK_DISABLE; }
637 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
641 TOK_BORDER border_style
643 printf("border style should be changed to %d\n", $2);
648 TAILQ_FOREACH(current, &owindows, owindows) {
649 printf("matching: %p / %s\n", current->con, current->con->name);
650 if ($2 == TOK_TOGGLE) {
651 current->con->border_style++;
652 current->con->border_style %= 3;
653 } else current->con->border_style = $2;
661 TOK_NORMAL { $$ = BS_NORMAL; }
662 | TOK_NONE { $$ = BS_NONE; }
663 | TOK_1PIXEL { $$ = BS_1PIXEL; }
664 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
670 printf("moving in direction %d\n", $2);
675 | TOK_MOVE TOK_WORKSPACE STR
679 printf("should move window to workspace %s\n", $3);
680 /* get the workspace */
681 Con *ws = workspace_get($3, NULL);
686 TAILQ_FOREACH(current, &owindows, owindows) {
687 printf("matching: %p / %s\n", current->con, current->con->name);
688 con_move_to_workspace(current->con, ws);
696 TOK_APPEND_LAYOUT STR
698 printf("restoring \"%s\"\n", $2);
699 tree_append_json($2);
706 TOK_LAYOUT layout_mode
708 printf("changing layout to %d\n", $2);
711 /* check if the match is empty, not if the result is empty */
712 if (match_is_empty(¤t_match))
713 con_set_layout(focused->parent, $2);
715 TAILQ_FOREACH(current, &owindows, owindows) {
716 printf("matching: %p / %s\n", current->con, current->con->name);
717 con_set_layout(current->con, $2);
726 TOK_DEFAULT { $$ = L_DEFAULT; }
727 | TOK_STACKED { $$ = L_STACKED; }
728 | TOK_TABBED { $$ = L_TABBED; }
734 printf("marking window with str %s\n", $2);
739 TAILQ_FOREACH(current, &owindows, owindows) {
740 printf("matching: %p / %s\n", current->con, current->con->name);
741 current->con->mark = sstrdup($2);
753 printf("-------------------------------------------------\n");
754 printf(" NOP: %s\n", $2);
755 printf("-------------------------------------------------\n");
761 TOK_RESIZE resize_way direction resize_px resize_tiling
763 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
764 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
768 if ($2 == TOK_SHRINK) {
773 if (con_is_floating(focused)) {
774 printf("floating resize\n");
775 if (direction == TOK_UP) {
776 focused->parent->rect.y -= px;
777 focused->parent->rect.height += px;
778 } else if (direction == TOK_DOWN) {
779 focused->rect.height += px;
780 } else if (direction == TOK_LEFT) {
781 focused->rect.x -= px;
782 focused->rect.width += px;
784 focused->rect.width += px;
787 LOG("tiling resize\n");
788 /* get the default percentage */
789 int children = con_num_children(focused->parent);
791 LOG("ins. %d children\n", children);
792 double percentage = 1.0 / children;
793 LOG("default percentage = %f\n", percentage);
795 if (direction == TOK_UP || direction == TOK_LEFT) {
796 other = TAILQ_PREV(focused, nodes_head, nodes);
798 other = TAILQ_NEXT(focused, nodes);
800 if (other == TAILQ_END(workspaces)) {
801 LOG("No other container in this direction found, cannot resize.\n");
804 LOG("other->percent = %f\n", other->percent);
805 LOG("focused->percent before = %f\n", focused->percent);
806 if (focused->percent == 0.0)
807 focused->percent = percentage;
808 if (other->percent == 0.0)
809 other->percent = percentage;
810 focused->percent += ((double)ppt / 100.0);
811 other->percent -= ((double)ppt / 100.0);
812 LOG("focused->percent after = %f\n", focused->percent);
813 LOG("other->percent after = %f\n", other->percent);
836 | TOK_OR NUMBER TOK_PPT
843 TOK_GROW { $$ = TOK_GROW; }
844 | TOK_SHRINK { $$ = TOK_SHRINK; }
848 TOK_UP { $$ = TOK_UP; }
849 | TOK_DOWN { $$ = TOK_DOWN; }
850 | TOK_LEFT { $$ = TOK_LEFT; }
851 | TOK_RIGHT { $$ = TOK_RIGHT; }