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>
20 typedef struct yy_buffer_state *YY_BUFFER_STATE;
21 extern int cmdyylex(struct context *context);
22 extern int cmdyyparse(void);
24 YY_BUFFER_STATE cmdyy_scan_string(const char *);
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);
96 %lex-param { struct context *context }
104 %token TOK_ATTACH "attach"
105 %token TOK_EXEC "exec"
106 %token TOK_EXIT "exit"
107 %token TOK_RELOAD "reload"
108 %token TOK_RESTART "restart"
109 %token TOK_KILL "kill"
110 %token TOK_WINDOW "window"
111 %token TOK_CLIENT "client"
112 %token TOK_FULLSCREEN "fullscreen"
113 %token TOK_GLOBAL "global"
114 %token TOK_LAYOUT "layout"
115 %token TOK_DEFAULT "default"
116 %token TOK_STACKED "stacked"
117 %token TOK_TABBED "tabbed"
118 %token TOK_BORDER "border"
119 %token TOK_NORMAL "normal"
120 %token TOK_NONE "none"
121 %token TOK_1PIXEL "1pixel"
122 %token TOK_MODE "mode"
123 %token TOK_TILING "tiling"
124 %token TOK_FLOATING "floating"
125 %token TOK_WORKSPACE "workspace"
126 %token TOK_TOGGLE "toggle"
127 %token TOK_FOCUS "focus"
128 %token TOK_MOVE "move"
129 %token TOK_OPEN "open"
130 %token TOK_NEXT "next"
131 %token TOK_PREV "prev"
132 %token TOK_SPLIT "split"
133 %token TOK_HORIZONTAL "horizontal"
134 %token TOK_VERTICAL "vertical"
135 %token TOK_LEVEL "level"
137 %token TOK_DOWN "down"
138 %token TOK_LEFT "left"
139 %token TOK_RIGHT "right"
140 %token TOK_RESTORE "restore"
141 %token TOK_MARK "mark"
142 %token TOK_RESIZE "resize"
143 %token TOK_GROW "grow"
144 %token TOK_SHRINK "shrink"
150 %token TOK_CLASS "class"
152 %token TOK_CON_ID "con_id"
154 %token WHITESPACE "<whitespace>"
155 %token <string> STR "<string>"
156 %token <number> NUMBER "<number>"
158 %type <number> direction
159 %type <chr> level_direction
160 %type <number> window_mode
161 %type <number> border_style
162 %type <number> layout_mode
163 %type <number> resize_px
164 %type <number> resize_way
165 %type <number> resize_tiling
166 %type <number> optional_kill_mode
171 commands optwhitespace ';' optwhitespace command
176 printf("single command completely parsed, dropping state...\n");
177 while (!TAILQ_EMPTY(&owindows)) {
178 current = TAILQ_FIRST(&owindows);
179 TAILQ_REMOVE(&owindows, current, owindows);
182 match_init(¤t_match);
191 match optwhitespace operations
195 | matchstart optwhitespace criteria optwhitespace matchend
197 printf("match parsed\n");
205 match_init(¤t_match);
206 TAILQ_INIT(&owindows);
209 TAILQ_FOREACH(con, &all_cons, all_cons) {
210 owindow *ow = smalloc(sizeof(owindow));
212 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
220 owindow *next, *current;
222 printf("match specification finished, matching...\n");
223 /* copy the old list head to iterate through it and start with a fresh
224 * list which will contain only matching windows */
225 struct owindows_head old = owindows;
226 TAILQ_INIT(&owindows);
227 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
228 /* make a copy of the next pointer and advance the pointer to the
229 * next element as we are going to invalidate the element’s
230 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
232 next = TAILQ_NEXT(next, owindows);
234 printf("checking if con %p / %s matches\n", current->con, current->con->name);
235 if (current_match.con_id != NULL) {
236 if (current_match.con_id == current->con) {
237 printf("matches container!\n");
238 TAILQ_INSERT_TAIL(&owindows, current, owindows);
241 } else if (current_match.mark != NULL && current->con->mark != NULL &&
242 strcasecmp(current_match.mark, current->con->mark) == 0) {
243 printf("match by mark\n");
244 TAILQ_INSERT_TAIL(&owindows, current, owindows);
247 if (current->con->window == NULL)
249 if (match_matches_window(¤t_match, current->con->window)) {
250 printf("matches window!\n");
251 TAILQ_INSERT_TAIL(&owindows, current, owindows);
253 printf("doesnt match\n");
259 TAILQ_FOREACH(current, &owindows, owindows) {
260 printf("matching: %p / %s\n", current->con, current->con->name);
269 printf("criteria: class = %s\n", $3);
270 current_match.class = $3;
274 printf("criteria: id = %s\n", $3);
276 long parsed = strtol($3, &end, 10);
277 if (parsed == LONG_MIN ||
278 parsed == LONG_MAX ||
280 (end && *end != '\0')) {
281 ELOG("Could not parse con id \"%s\"\n", $3);
283 current_match.con_id = (Con*)parsed;
284 printf("id as int = %p\n", current_match.con_id);
289 printf("criteria: window id = %s\n", $3);
291 long parsed = strtol($3, &end, 10);
292 if (parsed == LONG_MIN ||
293 parsed == LONG_MAX ||
295 (end && *end != '\0')) {
296 ELOG("Could not parse window id \"%s\"\n", $3);
298 current_match.id = parsed;
299 printf("window id as int = %d\n", current_match.id);
304 printf("criteria: mark = %s\n", $3);
305 current_match.mark = $3;
310 operation optwhitespace
311 | operations ',' optwhitespace operation
340 TOK_EXEC WHITESPACE STR
342 printf("should execute %s\n", $3);
343 start_application($3);
351 printf("exit, bye bye\n");
359 printf("reloading\n");
360 load_configuration(conn, NULL, true);
362 /* Send an IPC event just in case the ws names have changed */
363 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
370 printf("restarting i3\n");
378 printf("should attach\n");
387 printf("should focus\n");
388 if (match_is_empty(¤t_match)) {
389 /* TODO: better error message */
390 LOG("Error: The focus command requires you to use some criteria.\n");
394 /* TODO: warning if the match contains more than one entry. does not
395 * make so much sense when focusing */
396 TAILQ_FOREACH(current, &owindows, owindows) {
397 LOG("focusing %p / %s\n", current->con, current->con->name);
398 con_focus(current->con);
404 TOK_KILL optional_kill_mode
408 printf("killing!\n");
409 /* check if the match is empty, not if the result is empty */
410 if (match_is_empty(¤t_match))
413 TAILQ_FOREACH(current, &owindows, owindows) {
414 printf("matching: %p / %s\n", current->con, current->con->name);
415 tree_close(current->con, $2, false);
423 /* empty */ { $$ = KILL_WINDOW; }
424 | WHITESPACE { $$ = KILL_WINDOW; }
425 | WHITESPACE TOK_WINDOW { $$ = KILL_WINDOW; }
426 | WHITESPACE TOK_CLIENT { $$ = KILL_CLIENT; }
430 TOK_WORKSPACE WHITESPACE STR
432 printf("should switch to workspace %s\n", $3);
441 printf("opening new container\n");
442 Con *con = tree_open_con(NULL);
444 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
451 printf("toggling fullscreen\n");
454 /* check if the match is empty, not if the result is empty */
455 if (match_is_empty(¤t_match))
456 con_toggle_fullscreen(focused);
458 TAILQ_FOREACH(current, &owindows, owindows) {
459 printf("matching: %p / %s\n", current->con, current->con->name);
460 con_toggle_fullscreen(current->con);
468 TOK_NEXT WHITESPACE direction
470 /* TODO: use matches */
471 printf("should select next window in direction %c\n", $3);
472 tree_next('n', ($3 == 'v' ? VERT : HORIZ));
477 TOK_PREV WHITESPACE direction
479 /* TODO: use matches */
480 printf("should select prev window in direction %c\n", $3);
481 tree_next('p', ($3 == 'v' ? VERT : HORIZ));
486 TOK_SPLIT WHITESPACE direction
488 /* TODO: use matches */
489 printf("splitting in direction %c\n", $3);
490 tree_split(focused, ($3 == 'v' ? VERT : HORIZ));
495 TOK_HORIZONTAL { $$ = 'h'; }
497 | TOK_VERTICAL { $$ = 'v'; }
502 TOK_MODE WHITESPACE window_mode
504 if ($3 == TOK_TOGGLE) {
505 printf("should toggle mode\n");
506 toggle_floating_mode(focused, false);
508 printf("should switch mode to %s\n", ($3 == TOK_FLOATING ? "floating" : "tiling"));
509 if ($3 == TOK_FLOATING) {
510 floating_enable(focused, false);
512 floating_disable(focused, false);
519 TOK_FLOATING { $$ = TOK_FLOATING; }
520 | TOK_TILING { $$ = TOK_TILING; }
521 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
525 TOK_BORDER WHITESPACE border_style
527 printf("border style should be changed to %d\n", $3);
530 /* check if the match is empty, not if the result is empty */
531 if (match_is_empty(¤t_match))
532 focused->border_style = $3;
534 TAILQ_FOREACH(current, &owindows, owindows) {
535 printf("matching: %p / %s\n", current->con, current->con->name);
536 current->con->border_style = $3;
543 TOK_NORMAL { $$ = BS_NORMAL; }
544 | TOK_NONE { $$ = BS_NONE; }
545 | TOK_1PIXEL { $$ = BS_1PIXEL; }
550 TOK_LEVEL WHITESPACE level_direction
552 printf("level %c\n", $3);
561 | TOK_DOWN { $$ = 'd'; }
565 TOK_MOVE WHITESPACE direction
567 printf("moving in direction %d\n", $3);
570 | TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR
574 printf("should move window to workspace %s\n", $5);
575 /* get the workspace */
576 Con *ws = workspace_get($5, NULL);
579 /* check if the match is empty, not if the result is empty */
580 if (match_is_empty(¤t_match))
581 con_move_to_workspace(focused, ws);
583 TAILQ_FOREACH(current, &owindows, owindows) {
584 printf("matching: %p / %s\n", current->con, current->con->name);
585 con_move_to_workspace(current->con, ws);
592 TOK_RESTORE WHITESPACE STR
594 printf("restoring \"%s\"\n", $3);
595 tree_append_json($3);
601 TOK_LAYOUT WHITESPACE layout_mode
603 printf("changing layout to %d\n", $3);
606 /* check if the match is empty, not if the result is empty */
607 if (match_is_empty(¤t_match))
608 con_set_layout(focused->parent, $3);
610 TAILQ_FOREACH(current, &owindows, owindows) {
611 printf("matching: %p / %s\n", current->con, current->con->name);
612 con_set_layout(current->con, $3);
620 TOK_DEFAULT { $$ = L_DEFAULT; }
621 | TOK_STACKED { $$ = L_STACKED; }
622 | TOK_TABBED { $$ = L_TABBED; }
626 TOK_MARK WHITESPACE STR
628 printf("marking window with str %s\n", $3);
631 /* check if the match is empty, not if the result is empty */
632 if (match_is_empty(¤t_match))
633 focused->mark = sstrdup($3);
635 TAILQ_FOREACH(current, &owindows, owindows) {
636 printf("matching: %p / %s\n", current->con, current->con->name);
637 current->con->mark = sstrdup($3);
646 TOK_NOP WHITESPACE STR
648 printf("-------------------------------------------------\n");
649 printf(" NOP: %s\n", $3);
650 printf("-------------------------------------------------\n");
656 TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling
658 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
659 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $3, $5, $6, $7);
663 if ($3 == TOK_SHRINK) {
668 if (con_is_floating(focused)) {
669 printf("floating resize\n");
670 if (direction == TOK_UP) {
671 focused->parent->rect.y -= px;
672 focused->parent->rect.height += px;
673 } else if (direction == TOK_DOWN) {
674 focused->rect.height += px;
675 } else if (direction == TOK_LEFT) {
676 focused->rect.x -= px;
677 focused->rect.width += px;
679 focused->rect.width += px;
682 LOG("tiling resize\n");
683 /* get the default percentage */
684 int children = con_num_children(focused->parent);
686 LOG("ins. %d children\n", children);
687 double percentage = 1.0 / children;
688 LOG("default percentage = %f\n", percentage);
690 if (direction == TOK_UP || direction == TOK_LEFT) {
691 other = TAILQ_PREV(focused, nodes_head, nodes);
693 other = TAILQ_NEXT(focused, nodes);
695 if (other == TAILQ_END(workspaces)) {
696 LOG("No other container in this direction found, cannot resize.\n");
699 LOG("other->percent = %f\n", other->percent);
700 LOG("focused->percent before = %f\n", focused->percent);
701 if (focused->percent == 0.0)
702 focused->percent = percentage;
703 if (other->percent == 0.0)
704 other->percent = percentage;
705 focused->percent += ((double)ppt / 100.0);
706 other->percent -= ((double)ppt / 100.0);
707 LOG("focused->percent after = %f\n", focused->percent);
708 LOG("other->percent after = %f\n", other->percent);
718 | WHITESPACE NUMBER WHITESPACE TOK_PX
729 | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT
736 TOK_GROW { $$ = TOK_GROW; }
737 | TOK_SHRINK { $$ = TOK_SHRINK; }
741 TOK_UP { $$ = TOK_UP; }
742 | TOK_DOWN { $$ = TOK_DOWN; }
743 | TOK_LEFT { $$ = TOK_LEFT; }
744 | TOK_RIGHT { $$ = TOK_RIGHT; }