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);
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) {
83 LOG("COMMAND: *%s*\n", new);
84 cmdyy_scan_string(new);
86 match_init(¤t_match);
87 context = scalloc(sizeof(struct context));
88 context->filename = "cmd";
90 if (cmdyyparse() != 0) {
91 fprintf(stderr, "Could not parse command\n");
92 asprintf(&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);
101 FREE(context->line_copy);
102 FREE(context->compact_error);
110 %lex-param { struct context *context }
118 %token TOK_EXEC "exec"
119 %token TOK_EXIT "exit"
120 %token TOK_RELOAD "reload"
121 %token TOK_RESTART "restart"
122 %token TOK_KILL "kill"
123 %token TOK_WINDOW "window"
124 %token TOK_CLIENT "client"
125 %token TOK_FULLSCREEN "fullscreen"
126 %token TOK_GLOBAL "global"
127 %token TOK_LAYOUT "layout"
128 %token TOK_DEFAULT "default"
129 %token TOK_STACKED "stacked"
130 %token TOK_TABBED "tabbed"
131 %token TOK_BORDER "border"
132 %token TOK_NORMAL "normal"
133 %token TOK_NONE "none"
134 %token TOK_1PIXEL "1pixel"
135 %token TOK_MODE "mode"
136 %token TOK_TILING "tiling"
137 %token TOK_FLOATING "floating"
138 %token TOK_MODE_TOGGLE "mode_toggle"
139 %token TOK_ENABLE "enable"
140 %token TOK_DISABLE "disable"
141 %token TOK_WORKSPACE "workspace"
142 %token TOK_TOGGLE "toggle"
143 %token TOK_FOCUS "focus"
144 %token TOK_MOVE "move"
145 %token TOK_OPEN "open"
146 %token TOK_NEXT "next"
147 %token TOK_PREV "prev"
148 %token TOK_SPLIT "split"
149 %token TOK_HORIZONTAL "horizontal"
150 %token TOK_VERTICAL "vertical"
152 %token TOK_DOWN "down"
153 %token TOK_LEFT "left"
154 %token TOK_RIGHT "right"
155 %token TOK_PARENT "parent"
156 %token TOK_CHILD "child"
157 %token TOK_APPEND_LAYOUT "append_layout"
158 %token TOK_MARK "mark"
159 %token TOK_RESIZE "resize"
160 %token TOK_GROW "grow"
161 %token TOK_SHRINK "shrink"
167 %token TOK_CLASS "class"
169 %token TOK_CON_ID "con_id"
170 %token TOK_TITLE "title"
172 %token <string> STR "<string>"
173 %token <number> NUMBER "<number>"
175 %type <number> direction
176 %type <number> split_direction
177 %type <number> fullscreen_mode
179 %type <number> window_mode
180 %type <number> boolean
181 %type <number> border_style
182 %type <number> layout_mode
183 %type <number> resize_px
184 %type <number> resize_way
185 %type <number> resize_tiling
186 %type <number> optional_kill_mode
196 printf("single command completely parsed, dropping state...\n");
197 while (!TAILQ_EMPTY(&owindows)) {
198 current = TAILQ_FIRST(&owindows);
199 TAILQ_REMOVE(&owindows, current, owindows);
202 match_init(¤t_match);
211 | matchstart criteria matchend
213 printf("match parsed\n");
221 match_init(¤t_match);
222 TAILQ_INIT(&owindows);
225 TAILQ_FOREACH(con, &all_cons, all_cons) {
226 owindow *ow = smalloc(sizeof(owindow));
228 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
236 owindow *next, *current;
238 printf("match specification finished, matching...\n");
239 /* copy the old list head to iterate through it and start with a fresh
240 * list which will contain only matching windows */
241 struct owindows_head old = owindows;
242 TAILQ_INIT(&owindows);
243 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
244 /* make a copy of the next pointer and advance the pointer to the
245 * next element as we are going to invalidate the element’s
246 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
248 next = TAILQ_NEXT(next, owindows);
250 printf("checking if con %p / %s matches\n", current->con, current->con->name);
251 if (current_match.con_id != NULL) {
252 if (current_match.con_id == current->con) {
253 printf("matches container!\n");
254 TAILQ_INSERT_TAIL(&owindows, current, owindows);
257 } else if (current_match.mark != NULL && current->con->mark != NULL &&
258 strcasecmp(current_match.mark, current->con->mark) == 0) {
259 printf("match by mark\n");
260 TAILQ_INSERT_TAIL(&owindows, current, owindows);
263 if (current->con->window == NULL)
265 if (match_matches_window(¤t_match, current->con->window)) {
266 printf("matches window!\n");
267 TAILQ_INSERT_TAIL(&owindows, current, owindows);
269 printf("doesnt match\n");
275 TAILQ_FOREACH(current, &owindows, owindows) {
276 printf("matching: %p / %s\n", current->con, current->con->name);
290 printf("criteria: class = %s\n", $3);
291 current_match.class = $3;
295 printf("criteria: id = %s\n", $3);
297 long parsed = strtol($3, &end, 10);
298 if (parsed == LONG_MIN ||
299 parsed == LONG_MAX ||
301 (end && *end != '\0')) {
302 ELOG("Could not parse con id \"%s\"\n", $3);
304 current_match.con_id = (Con*)parsed;
305 printf("id as int = %p\n", current_match.con_id);
310 printf("criteria: window id = %s\n", $3);
312 long parsed = strtol($3, &end, 10);
313 if (parsed == LONG_MIN ||
314 parsed == LONG_MAX ||
316 (end && *end != '\0')) {
317 ELOG("Could not parse window id \"%s\"\n", $3);
319 current_match.id = parsed;
320 printf("window id as int = %d\n", current_match.id);
325 printf("criteria: mark = %s\n", $3);
326 current_match.mark = $3;
330 printf("criteria: title = %s\n", $3);
331 current_match.title = $3;
337 | operations ',' operation
365 printf("should execute %s\n", $2);
366 start_application($2);
374 printf("exit, bye bye\n");
382 printf("reloading\n");
383 kill_configerror_nagbar(false);
384 load_configuration(conn, NULL, true);
386 /* Send an IPC event just in case the ws names have changed */
387 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
394 printf("restarting i3\n");
404 if (match_is_empty(¤t_match)) {
405 ELOG("You have to specify which window/container should be focused.\n");
406 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
408 asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
409 "specify which window/container should be focused\"}");
414 TAILQ_FOREACH(current, &owindows, owindows) {
415 LOG("focusing %p / %s\n", current->con, current->con->name);
416 con_focus(current->con);
421 LOG("WARNING: Your criteria for the focus command matches %d containers, "
422 "while only exactly one container can be focused at a time.\n", count);
426 | TOK_FOCUS direction
431 LOG("Focusing left\n");
432 tree_next('p', HORIZ);
435 LOG("Focusing right\n");
436 tree_next('n', HORIZ);
439 LOG("Focusing up\n");
440 tree_next('p', VERT);
443 LOG("Focusing down\n");
444 tree_next('n', VERT);
447 ELOG("Invalid focus direction (%d)\n", direction);
453 | TOK_FOCUS window_mode
455 printf("should focus: ");
457 if ($2 == TOK_TILING)
459 else if ($2 == TOK_FLOATING)
460 printf("floating\n");
461 else printf("mode toggle\n");
463 Con *ws = con_get_workspace(focused);
467 if ($2 == TOK_MODE_TOGGLE) {
468 current = TAILQ_FIRST(&(ws->focus_head));
469 if (current->type == CT_FLOATING_CON)
470 to_focus = TOK_TILING;
471 else to_focus = TOK_FLOATING;
473 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
474 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
475 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
478 con_focus(con_descend_focused(current));
487 if ($2 == TOK_PARENT)
496 TOK_TILING { $$ = TOK_TILING; }
497 | TOK_FLOATING { $$ = TOK_FLOATING; }
498 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
502 TOK_PARENT { $$ = TOK_PARENT; }
503 | TOK_CHILD { $$ = TOK_CHILD; }
507 TOK_KILL optional_kill_mode
511 printf("killing!\n");
512 /* check if the match is empty, not if the result is empty */
513 if (match_is_empty(¤t_match))
516 TAILQ_FOREACH(current, &owindows, owindows) {
517 printf("matching: %p / %s\n", current->con, current->con->name);
518 tree_close(current->con, $2, false);
527 /* empty */ { $$ = KILL_WINDOW; }
528 | TOK_WINDOW { $$ = KILL_WINDOW; }
529 | TOK_CLIENT { $$ = KILL_CLIENT; }
533 TOK_WORKSPACE TOK_NEXT
538 | TOK_WORKSPACE TOK_PREV
545 printf("should switch to workspace %s\n", $2);
556 printf("opening new container\n");
557 Con *con = tree_open_con(NULL, NULL);
559 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
566 TOK_FULLSCREEN fullscreen_mode
568 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
573 TAILQ_FOREACH(current, &owindows, owindows) {
574 printf("matching: %p / %s\n", current->con, current->con->name);
575 con_toggle_fullscreen(current->con, $2);
583 /* empty */ { $$ = CF_OUTPUT; }
584 | TOK_GLOBAL { $$ = CF_GLOBAL; }
588 TOK_SPLIT split_direction
590 /* TODO: use matches */
591 printf("splitting in direction %c\n", $2);
592 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
599 TOK_HORIZONTAL { $$ = 'h'; }
601 | TOK_VERTICAL { $$ = 'v'; }
611 TAILQ_FOREACH(current, &owindows, owindows) {
612 printf("matching: %p / %s\n", current->con, current->con->name);
613 if ($2 == TOK_TOGGLE) {
614 printf("should toggle mode\n");
615 toggle_floating_mode(current->con, false);
617 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
618 if ($2 == TOK_ENABLE) {
619 floating_enable(current->con, false);
621 floating_disable(current->con, false);
631 TOK_ENABLE { $$ = TOK_ENABLE; }
632 | TOK_DISABLE { $$ = TOK_DISABLE; }
633 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
637 TOK_BORDER border_style
639 printf("border style should be changed to %d\n", $2);
644 TAILQ_FOREACH(current, &owindows, owindows) {
645 printf("matching: %p / %s\n", current->con, current->con->name);
646 if ($2 == TOK_TOGGLE) {
647 current->con->border_style++;
648 current->con->border_style %= 3;
649 } else current->con->border_style = $2;
657 TOK_NORMAL { $$ = BS_NORMAL; }
658 | TOK_NONE { $$ = BS_NONE; }
659 | TOK_1PIXEL { $$ = BS_1PIXEL; }
660 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
666 printf("moving in direction %d\n", $2);
671 | TOK_MOVE TOK_WORKSPACE STR
675 printf("should move window to workspace %s\n", $3);
676 /* get the workspace */
677 Con *ws = workspace_get($3, NULL);
682 TAILQ_FOREACH(current, &owindows, owindows) {
683 printf("matching: %p / %s\n", current->con, current->con->name);
684 con_move_to_workspace(current->con, ws);
692 TOK_APPEND_LAYOUT STR
694 printf("restoring \"%s\"\n", $2);
695 tree_append_json($2);
702 TOK_LAYOUT layout_mode
704 printf("changing layout to %d\n", $2);
707 /* check if the match is empty, not if the result is empty */
708 if (match_is_empty(¤t_match))
709 con_set_layout(focused->parent, $2);
711 TAILQ_FOREACH(current, &owindows, owindows) {
712 printf("matching: %p / %s\n", current->con, current->con->name);
713 con_set_layout(current->con, $2);
722 TOK_DEFAULT { $$ = L_DEFAULT; }
723 | TOK_STACKED { $$ = L_STACKED; }
724 | TOK_TABBED { $$ = L_TABBED; }
730 printf("marking window with str %s\n", $2);
735 TAILQ_FOREACH(current, &owindows, owindows) {
736 printf("matching: %p / %s\n", current->con, current->con->name);
737 current->con->mark = sstrdup($2);
749 printf("-------------------------------------------------\n");
750 printf(" NOP: %s\n", $2);
751 printf("-------------------------------------------------\n");
757 TOK_RESIZE resize_way direction resize_px resize_tiling
759 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
760 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
764 if ($2 == TOK_SHRINK) {
769 if (con_is_floating(focused)) {
770 printf("floating resize\n");
771 if (direction == TOK_UP) {
772 focused->parent->rect.y -= px;
773 focused->parent->rect.height += px;
774 } else if (direction == TOK_DOWN) {
775 focused->rect.height += px;
776 } else if (direction == TOK_LEFT) {
777 focused->rect.x -= px;
778 focused->rect.width += px;
780 focused->rect.width += px;
783 LOG("tiling resize\n");
784 /* get the default percentage */
785 int children = con_num_children(focused->parent);
787 LOG("ins. %d children\n", children);
788 double percentage = 1.0 / children;
789 LOG("default percentage = %f\n", percentage);
791 if (direction == TOK_UP || direction == TOK_LEFT) {
792 other = TAILQ_PREV(focused, nodes_head, nodes);
794 other = TAILQ_NEXT(focused, nodes);
796 if (other == TAILQ_END(workspaces)) {
797 LOG("No other container in this direction found, cannot resize.\n");
800 LOG("other->percent = %f\n", other->percent);
801 LOG("focused->percent before = %f\n", focused->percent);
802 if (focused->percent == 0.0)
803 focused->percent = percentage;
804 if (other->percent == 0.0)
805 other->percent = percentage;
806 focused->percent += ((double)ppt / 100.0);
807 other->percent -= ((double)ppt / 100.0);
808 LOG("focused->percent after = %f\n", focused->percent);
809 LOG("other->percent after = %f\n", other->percent);
832 | TOK_OR NUMBER TOK_PPT
839 TOK_GROW { $$ = TOK_GROW; }
840 | TOK_SHRINK { $$ = TOK_SHRINK; }
844 TOK_UP { $$ = TOK_UP; }
845 | TOK_DOWN { $$ = TOK_DOWN; }
846 | TOK_LEFT { $$ = TOK_LEFT; }
847 | TOK_RIGHT { $$ = TOK_RIGHT; }