3 * vim:ts=4:sw=4:expandtab
5 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 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>
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int cmdyylex(struct context *context);
21 extern int cmdyyparse(void);
23 YY_BUFFER_STATE cmdyy_scan_string(const char *);
25 static struct bindings_head *current_bindings;
26 static struct context *context;
27 static Match current_match;
30 * Helper data structure for an operation window (window on which the operation
31 * will be performed). Used to build the TAILQ owindows.
34 typedef struct owindow {
36 TAILQ_ENTRY(owindow) owindows;
38 static TAILQ_HEAD(owindows_head, owindow) owindows;
40 /* Holds the JSON which will be returned via IPC or NULL for the default return
42 static char *json_output;
44 /* We don’t need yydebug for now, as we got decent error messages using
45 * yyerror(). Should you ever want to extend the parser, it might be handy
46 * to just comment it in again, so it stays here. */
49 void cmdyyerror(const char *error_message) {
51 ELOG("CMD: %s\n", error_message);
52 ELOG("CMD: in command:\n");
53 ELOG("CMD: %s\n", context->line_copy);
55 for (int c = 1; c <= context->last_column; c++)
56 if (c >= context->first_column)
61 context->compact_error = sstrdup(error_message);
68 char *parse_cmd(const char *new) {
69 cmdyy_scan_string(new);
71 match_init(¤t_match);
72 context = scalloc(sizeof(struct context));
73 context->filename = "cmd";
75 if (cmdyyparse() != 0) {
76 fprintf(stderr, "Could not parse command\n");
77 asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
78 context->compact_error, context->first_column);
79 FREE(context->line_copy);
80 FREE(context->compact_error);
84 printf("done, json output = %s\n", json_output);
86 FREE(context->line_copy);
87 FREE(context->compact_error);
95 %lex-param { struct context *context }
103 %token TOK_ATTACH "attach"
104 %token TOK_EXEC "exec"
105 %token TOK_EXIT "exit"
106 %token TOK_RELOAD "reload"
107 %token TOK_RESTART "restart"
108 %token TOK_KILL "kill"
109 %token TOK_FULLSCREEN "fullscreen"
110 %token TOK_GLOBAL "global"
111 %token TOK_LAYOUT "layout"
112 %token TOK_DEFAULT "default"
113 %token TOK_STACKED "stacked"
114 %token TOK_TABBED "tabbed"
115 %token TOK_BORDER "border"
116 %token TOK_NORMAL "normal"
117 %token TOK_NONE "none"
118 %token TOK_1PIXEL "1pixel"
119 %token TOK_MODE "mode"
120 %token TOK_TILING "tiling"
121 %token TOK_FLOATING "floating"
122 %token TOK_WORKSPACE "workspace"
123 %token TOK_TOGGLE "toggle"
124 %token TOK_FOCUS "focus"
125 %token TOK_MOVE "move"
126 %token TOK_OPEN "open"
127 %token TOK_NEXT "next"
128 %token TOK_PREV "prev"
129 %token TOK_SPLIT "split"
130 %token TOK_HORIZONTAL "horizontal"
131 %token TOK_VERTICAL "vertical"
132 %token TOK_LEVEL "level"
134 %token TOK_DOWN "down"
135 %token TOK_LEFT "left"
136 %token TOK_RIGHT "right"
137 %token TOK_AFTER "after"
138 %token TOK_BEFORE "before"
139 %token TOK_RESTORE "restore"
140 %token TOK_MARK "mark"
141 %token TOK_RESIZE "resize"
142 %token TOK_GROW "grow"
143 %token TOK_SHRINK "shrink"
149 %token TOK_CLASS "class"
151 %token TOK_CON_ID "con_id"
153 %token WHITESPACE "<whitespace>"
154 %token STR "<string>"
155 %token NUMBER "<number>"
159 commands: /* empty */
160 | commands optwhitespace ';' optwhitespace command
165 printf("single command completely parsed, dropping state...\n");
166 while (!TAILQ_EMPTY(&owindows)) {
167 current = TAILQ_FIRST(&owindows);
168 TAILQ_REMOVE(&owindows, current, owindows);
171 match_init(¤t_match);
180 match optwhitespace operations
184 | matchstart optwhitespace criteria optwhitespace matchend
186 printf("match parsed\n");
194 match_init(¤t_match);
195 TAILQ_INIT(&owindows);
198 TAILQ_FOREACH(con, &all_cons, all_cons) {
199 owindow *ow = smalloc(sizeof(owindow));
201 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
209 owindow *next, *current;
211 printf("match specification finished, matching...\n");
212 /* copy the old list head to iterate through it and start with a fresh
213 * list which will contain only matching windows */
214 struct owindows_head old = owindows;
215 TAILQ_INIT(&owindows);
216 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
217 /* make a copy of the next pointer and advance the pointer to the
218 * next element as we are going to invalidate the element’s
219 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
221 next = TAILQ_NEXT(next, owindows);
223 printf("checking if con %p / %s matches\n", current->con, current->con->name);
224 if (current_match.con_id != NULL) {
225 if (current_match.con_id == current->con) {
226 printf("matches container!\n");
227 TAILQ_INSERT_TAIL(&owindows, current, owindows);
230 } else if (current_match.mark != NULL && current->con->mark != NULL &&
231 strcasecmp(current_match.mark, current->con->mark) == 0) {
232 printf("match by mark\n");
233 TAILQ_INSERT_TAIL(&owindows, current, owindows);
236 if (current->con->window == NULL)
238 if (match_matches_window(¤t_match, current->con->window)) {
239 printf("matches window!\n");
240 TAILQ_INSERT_TAIL(&owindows, current, owindows);
242 printf("doesnt match\n");
248 TAILQ_FOREACH(current, &owindows, owindows) {
249 printf("matching: %p / %s\n", current->con, current->con->name);
258 printf("criteria: class = %s\n", $<string>3);
259 current_match.class = $<string>3;
263 printf("criteria: id = %s\n", $<string>3);
264 /* TODO: correctly parse number */
265 current_match.con_id = atoi($<string>3);
266 printf("id as int = %d\n", current_match.con_id);
270 printf("criteria: window id = %s\n", $<string>3);
271 /* TODO: correctly parse number */
272 current_match.id = atoi($<string>3);
273 printf("window id as int = %d\n", current_match.id);
277 printf("criteria: mark = %s\n", $<string>3);
278 current_match.mark = $<string>3;
284 | operation optwhitespace
285 | operations ',' optwhitespace operation
314 TOK_EXEC WHITESPACE STR
316 printf("should execute %s\n", $<string>3);
317 start_application($<string>3);
324 printf("exit, bye bye\n");
332 printf("reloading\n");
333 load_configuration(conn, NULL, true);
334 /* Send an IPC event just in case the ws names have changed */
335 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
342 printf("restarting i3\n");
350 printf("should attach\n");
359 printf("should focus\n");
360 if (match_is_empty(¤t_match)) {
361 /* TODO: better error message */
362 LOG("Error: The foucs command requires you to use some criteria.\n");
366 /* TODO: warning if the match contains more than one entry. does not
367 * make so much sense when focusing */
368 TAILQ_FOREACH(current, &owindows, owindows) {
369 LOG("focusing %p / %s\n", current->con, current->con->name);
370 con_focus(current->con);
380 printf("killing!\n");
381 /* check if the match is empty, not if the result is empty */
382 if (match_is_empty(¤t_match))
385 TAILQ_FOREACH(current, &owindows, owindows) {
386 printf("matching: %p / %s\n", current->con, current->con->name);
387 tree_close(current->con, true, false);
395 TOK_WORKSPACE WHITESPACE STR
397 printf("should switch to workspace %s\n", $<string>3);
398 workspace_show($<string>3);
406 printf("opening new container\n");
407 Con *con = tree_open_con(NULL);
408 asprintf(&json_output, "{\"success\":true, \"id\":%d}", (long int)con);
415 printf("toggling fullscreen\n");
418 /* check if the match is empty, not if the result is empty */
419 if (match_is_empty(¤t_match))
420 con_toggle_fullscreen(focused);
422 TAILQ_FOREACH(current, &owindows, owindows) {
423 printf("matching: %p / %s\n", current->con, current->con->name);
424 con_toggle_fullscreen(current->con);
432 TOK_NEXT WHITESPACE direction
434 /* TODO: use matches */
435 printf("should select next window in direction %c\n", $<chr>3);
436 tree_next('n', ($<chr>3 == 'v' ? VERT : HORIZ));
441 TOK_PREV WHITESPACE direction
443 /* TODO: use matches */
444 printf("should select prev window in direction %c\n", $<chr>3);
445 tree_next('p', ($<chr>3 == 'v' ? VERT : HORIZ));
450 TOK_SPLIT WHITESPACE direction
452 /* TODO: use matches */
453 printf("splitting in direction %c\n", $<chr>3);
454 tree_split(focused, ($<chr>3 == 'v' ? VERT : HORIZ));
459 TOK_HORIZONTAL { $<chr>$ = 'h'; }
460 | 'h' { $<chr>$ = 'h'; }
461 | TOK_VERTICAL { $<chr>$ = 'v'; }
462 | 'v' { $<chr>$ = 'v'; }
466 TOK_MODE WHITESPACE window_mode
468 if ($<number>3 == TOK_TOGGLE) {
469 printf("should toggle mode\n");
470 toggle_floating_mode(focused, false);
472 printf("should switch mode to %s\n", ($<number>3 == TOK_FLOATING ? "floating" : "tiling"));
473 if ($<number>3 == TOK_FLOATING) {
474 floating_enable(focused, false);
476 floating_disable(focused, false);
483 TOK_FLOATING { $<number>$ = TOK_FLOATING; }
484 | TOK_TILING { $<number>$ = TOK_TILING; }
485 | TOK_TOGGLE { $<number>$ = TOK_TOGGLE; }
489 TOK_BORDER WHITESPACE border_style
491 printf("border style should be changed to %d\n", $<number>3);
494 /* check if the match is empty, not if the result is empty */
495 if (match_is_empty(¤t_match))
496 focused->border_style = $<number>3;
498 TAILQ_FOREACH(current, &owindows, owindows) {
499 printf("matching: %p / %s\n", current->con, current->con->name);
500 current->con->border_style = $<number>3;
507 TOK_NORMAL { $<number>$ = BS_NORMAL; }
508 | TOK_NONE { $<number>$ = BS_NONE; }
509 | TOK_1PIXEL { $<number>$ = BS_1PIXEL; }
514 TOK_LEVEL WHITESPACE level_direction
516 printf("level %c\n", $<chr>3);
524 TOK_UP { $<chr>$ = 'u'; }
525 | TOK_DOWN { $<chr>$ = 'd'; }
529 TOK_MOVE WHITESPACE before_after WHITESPACE direction
531 printf("moving: %s and %c\n", ($<number>3 == TOK_BEFORE ? "before" : "after"), $<chr>5);
532 /* TODO: change API for the next call, we need to convert in both directions while ideally
533 * we should not need any of both */
534 tree_move(($<number>3 == TOK_BEFORE ? 'p' : 'n'), ($<chr>5 == 'v' ? VERT : HORIZ));
536 | TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR
540 printf("should move window to workspace %s\n", $<string>5);
541 /* get the workspace */
542 Con *ws = workspace_get($<string>5);
544 /* check if the match is empty, not if the result is empty */
545 if (match_is_empty(¤t_match))
546 con_move_to_workspace(focused, ws);
548 TAILQ_FOREACH(current, &owindows, owindows) {
549 printf("matching: %p / %s\n", current->con, current->con->name);
550 con_move_to_workspace(current->con, ws);
557 TOK_BEFORE { $<number>$ = TOK_BEFORE; }
558 | TOK_AFTER { $<number>$ = TOK_AFTER; }
562 TOK_RESTORE WHITESPACE STR
564 printf("restoring \"%s\"\n", $<string>3);
565 tree_append_json($<string>3);
570 TOK_LAYOUT WHITESPACE layout_mode
572 printf("changing layout to %d\n", $<number>3);
575 /* check if the match is empty, not if the result is empty */
576 if (match_is_empty(¤t_match))
577 con_set_layout(focused->parent, $<number>3);
579 TAILQ_FOREACH(current, &owindows, owindows) {
580 printf("matching: %p / %s\n", current->con, current->con->name);
581 con_set_layout(current->con, $<number>3);
589 TOK_DEFAULT { $<number>$ = L_DEFAULT; }
590 | TOK_STACKED { $<number>$ = L_STACKED; }
591 | TOK_TABBED { $<number>$ = L_TABBED; }
595 TOK_MARK WHITESPACE STR
597 printf("marking window with str %s\n", $<string>3);
600 /* check if the match is empty, not if the result is empty */
601 if (match_is_empty(¤t_match))
602 focused->mark = sstrdup($<string>3);
604 TAILQ_FOREACH(current, &owindows, owindows) {
605 printf("matching: %p / %s\n", current->con, current->con->name);
606 current->con->mark = sstrdup($<string>3);
615 TOK_NOP WHITESPACE STR
617 printf("-------------------------------------------------\n");
618 printf(" NOP: %s\n", $<string>3);
619 printf("-------------------------------------------------\n");
625 TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling
627 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
628 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $<number>3, $<number>5, $<number>6, $<number>7);
629 int direction = $<number>5;
631 int ppt = $<number>7;
632 if ($<number>3 == TOK_SHRINK) {
637 if (con_is_floating(focused)) {
638 printf("floating resize\n");
639 if (direction == TOK_UP) {
640 focused->parent->rect.y -= px;
641 focused->parent->rect.height += px;
642 } else if (direction == TOK_DOWN) {
643 focused->rect.height += px;
644 } else if (direction == TOK_LEFT) {
645 focused->rect.x -= px;
646 focused->rect.width += px;
648 focused->rect.width += px;
651 LOG("tiling resize\n");
652 /* get the default percentage */
655 TAILQ_FOREACH(other, &(focused->parent->nodes_head), nodes)
657 LOG("ins. %d children\n", children);
658 double percentage = 1.0 / children;
659 LOG("default percentage = %f\n", percentage);
661 if (direction == TOK_UP || direction == TOK_LEFT) {
662 other = TAILQ_PREV(focused, nodes_head, nodes);
664 other = TAILQ_NEXT(focused, nodes);
666 if (other == TAILQ_END(workspaces)) {
667 LOG("No other container in this direction found, cannot resize.\n");
670 LOG("other->percent = %f\n", other->percent);
671 LOG("focused->percent before = %f\n", focused->percent);
672 if (focused->percent == 0.0)
673 focused->percent = percentage;
674 if (other->percent == 0.0)
675 other->percent = percentage;
676 focused->percent += ((double)ppt / 100.0);
677 other->percent -= ((double)ppt / 100.0);
678 LOG("focused->percent after = %f\n", focused->percent);
679 LOG("other->percent after = %f\n", other->percent);
689 | WHITESPACE NUMBER WHITESPACE TOK_PX
691 $<number>$ = $<number>2;
700 | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT
702 $<number>$ = $<number>4;
707 TOK_GROW { $<number>$ = TOK_GROW; }
708 | TOK_SHRINK { $<number>$ = TOK_SHRINK; }
712 TOK_UP { $<number>$ = TOK_UP; }
713 | TOK_DOWN { $<number>$ = TOK_DOWN; }
714 | TOK_LEFT { $<number>$ = TOK_LEFT; }
715 | TOK_RIGHT { $<number>$ = TOK_RIGHT; }