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);
111 %lex-param { struct context *context }
119 %token TOK_ATTACH "attach"
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_WORKSPACE "workspace"
141 %token TOK_TOGGLE "toggle"
142 %token TOK_FOCUS "focus"
143 %token TOK_MOVE "move"
144 %token TOK_OPEN "open"
145 %token TOK_NEXT "next"
146 %token TOK_PREV "prev"
147 %token TOK_SPLIT "split"
148 %token TOK_HORIZONTAL "horizontal"
149 %token TOK_VERTICAL "vertical"
150 %token TOK_LEVEL "level"
152 %token TOK_DOWN "down"
153 %token TOK_LEFT "left"
154 %token TOK_RIGHT "right"
155 %token TOK_RESTORE "restore"
156 %token TOK_MARK "mark"
157 %token TOK_RESIZE "resize"
158 %token TOK_GROW "grow"
159 %token TOK_SHRINK "shrink"
165 %token TOK_CLASS "class"
167 %token TOK_CON_ID "con_id"
169 %token WHITESPACE "<whitespace>"
170 %token <string> STR "<string>"
171 %token <number> NUMBER "<number>"
173 %type <number> direction
174 %type <chr> level_direction
175 %type <number> window_mode
176 %type <number> border_style
177 %type <number> layout_mode
178 %type <number> resize_px
179 %type <number> resize_way
180 %type <number> resize_tiling
181 %type <number> optional_kill_mode
186 commands optwhitespace ';' optwhitespace command
191 printf("single command completely parsed, dropping state...\n");
192 while (!TAILQ_EMPTY(&owindows)) {
193 current = TAILQ_FIRST(&owindows);
194 TAILQ_REMOVE(&owindows, current, owindows);
197 match_init(¤t_match);
206 match optwhitespace operations
210 | matchstart optwhitespace criteria optwhitespace matchend
212 printf("match parsed\n");
220 match_init(¤t_match);
221 TAILQ_INIT(&owindows);
224 TAILQ_FOREACH(con, &all_cons, all_cons) {
225 owindow *ow = smalloc(sizeof(owindow));
227 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
235 owindow *next, *current;
237 printf("match specification finished, matching...\n");
238 /* copy the old list head to iterate through it and start with a fresh
239 * list which will contain only matching windows */
240 struct owindows_head old = owindows;
241 TAILQ_INIT(&owindows);
242 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
243 /* make a copy of the next pointer and advance the pointer to the
244 * next element as we are going to invalidate the element’s
245 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
247 next = TAILQ_NEXT(next, owindows);
249 printf("checking if con %p / %s matches\n", current->con, current->con->name);
250 if (current_match.con_id != NULL) {
251 if (current_match.con_id == current->con) {
252 printf("matches container!\n");
253 TAILQ_INSERT_TAIL(&owindows, current, owindows);
256 } else if (current_match.mark != NULL && current->con->mark != NULL &&
257 strcasecmp(current_match.mark, current->con->mark) == 0) {
258 printf("match by mark\n");
259 TAILQ_INSERT_TAIL(&owindows, current, owindows);
262 if (current->con->window == NULL)
264 if (match_matches_window(¤t_match, current->con->window)) {
265 printf("matches window!\n");
266 TAILQ_INSERT_TAIL(&owindows, current, owindows);
268 printf("doesnt match\n");
274 TAILQ_FOREACH(current, &owindows, owindows) {
275 printf("matching: %p / %s\n", current->con, current->con->name);
284 printf("criteria: class = %s\n", $3);
285 current_match.class = $3;
289 printf("criteria: 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 con id \"%s\"\n", $3);
298 current_match.con_id = (Con*)parsed;
299 printf("id as int = %p\n", current_match.con_id);
304 printf("criteria: window id = %s\n", $3);
306 long parsed = strtol($3, &end, 10);
307 if (parsed == LONG_MIN ||
308 parsed == LONG_MAX ||
310 (end && *end != '\0')) {
311 ELOG("Could not parse window id \"%s\"\n", $3);
313 current_match.id = parsed;
314 printf("window id as int = %d\n", current_match.id);
319 printf("criteria: mark = %s\n", $3);
320 current_match.mark = $3;
325 operation optwhitespace
326 | operations ',' optwhitespace operation
355 TOK_EXEC WHITESPACE STR
357 printf("should execute %s\n", $3);
358 start_application($3);
366 printf("exit, bye bye\n");
374 printf("reloading\n");
375 load_configuration(conn, NULL, true);
377 /* Send an IPC event just in case the ws names have changed */
378 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
385 printf("restarting i3\n");
393 printf("should attach\n");
402 printf("should focus\n");
403 if (match_is_empty(¤t_match)) {
404 /* TODO: better error message */
405 LOG("Error: The focus command requires you to use some criteria.\n");
409 /* TODO: warning if the match contains more than one entry. does not
410 * make so much sense when focusing */
411 TAILQ_FOREACH(current, &owindows, owindows) {
412 LOG("focusing %p / %s\n", current->con, current->con->name);
413 con_focus(current->con);
419 TOK_KILL optional_kill_mode
423 printf("killing!\n");
424 /* check if the match is empty, not if the result is empty */
425 if (match_is_empty(¤t_match))
428 TAILQ_FOREACH(current, &owindows, owindows) {
429 printf("matching: %p / %s\n", current->con, current->con->name);
430 tree_close(current->con, $2, false);
438 /* empty */ { $$ = KILL_WINDOW; }
439 | WHITESPACE { $$ = KILL_WINDOW; }
440 | WHITESPACE TOK_WINDOW { $$ = KILL_WINDOW; }
441 | WHITESPACE TOK_CLIENT { $$ = KILL_CLIENT; }
445 TOK_WORKSPACE WHITESPACE STR
447 printf("should switch to workspace %s\n", $3);
456 printf("opening new container\n");
457 Con *con = tree_open_con(NULL);
459 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
466 printf("toggling fullscreen\n");
472 TAILQ_FOREACH(current, &owindows, owindows) {
473 printf("matching: %p / %s\n", current->con, current->con->name);
474 con_toggle_fullscreen(current->con);
480 TOK_NEXT WHITESPACE direction
482 /* TODO: use matches */
483 printf("should select next window in direction %c\n", $3);
484 tree_next('n', ($3 == 'v' ? VERT : HORIZ));
489 TOK_PREV WHITESPACE direction
491 /* TODO: use matches */
492 printf("should select prev window in direction %c\n", $3);
493 tree_next('p', ($3 == 'v' ? VERT : HORIZ));
498 TOK_SPLIT WHITESPACE direction
500 /* TODO: use matches */
501 printf("splitting in direction %c\n", $3);
502 tree_split(focused, ($3 == 'v' ? VERT : HORIZ));
507 TOK_HORIZONTAL { $$ = 'h'; }
509 | TOK_VERTICAL { $$ = 'v'; }
514 TOK_MODE WHITESPACE window_mode
519 TAILQ_FOREACH(current, &owindows, owindows) {
520 printf("matching: %p / %s\n", current->con, current->con->name);
521 if ($3 == TOK_TOGGLE) {
522 printf("should toggle mode\n");
523 toggle_floating_mode(current->con, false);
525 printf("should switch mode to %s\n", ($3 == TOK_FLOATING ? "floating" : "tiling"));
526 if ($3 == TOK_FLOATING) {
527 floating_enable(current->con, false);
529 floating_disable(current->con, false);
537 TOK_FLOATING { $$ = TOK_FLOATING; }
538 | TOK_TILING { $$ = TOK_TILING; }
539 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
543 TOK_BORDER WHITESPACE border_style
545 printf("border style should be changed to %d\n", $3);
550 TAILQ_FOREACH(current, &owindows, owindows) {
551 printf("matching: %p / %s\n", current->con, current->con->name);
552 current->con->border_style = $3;
558 TOK_NORMAL { $$ = BS_NORMAL; }
559 | TOK_NONE { $$ = BS_NONE; }
560 | TOK_1PIXEL { $$ = BS_1PIXEL; }
565 TOK_LEVEL WHITESPACE level_direction
567 printf("level %c\n", $3);
576 | TOK_DOWN { $$ = 'd'; }
580 TOK_MOVE WHITESPACE direction
582 printf("moving in direction %d\n", $3);
585 | TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR
589 printf("should move window to workspace %s\n", $5);
590 /* get the workspace */
591 Con *ws = workspace_get($5, NULL);
596 TAILQ_FOREACH(current, &owindows, owindows) {
597 printf("matching: %p / %s\n", current->con, current->con->name);
598 con_move_to_workspace(current->con, ws);
604 TOK_RESTORE WHITESPACE STR
606 printf("restoring \"%s\"\n", $3);
607 tree_append_json($3);
613 TOK_LAYOUT WHITESPACE layout_mode
615 printf("changing layout to %d\n", $3);
618 /* check if the match is empty, not if the result is empty */
619 if (match_is_empty(¤t_match))
620 con_set_layout(focused->parent, $3);
622 TAILQ_FOREACH(current, &owindows, owindows) {
623 printf("matching: %p / %s\n", current->con, current->con->name);
624 con_set_layout(current->con, $3);
631 TOK_DEFAULT { $$ = L_DEFAULT; }
632 | TOK_STACKED { $$ = L_STACKED; }
633 | TOK_TABBED { $$ = L_TABBED; }
637 TOK_MARK WHITESPACE STR
639 printf("marking window with str %s\n", $3);
644 TAILQ_FOREACH(current, &owindows, owindows) {
645 printf("matching: %p / %s\n", current->con, current->con->name);
646 current->con->mark = sstrdup($3);
654 TOK_NOP WHITESPACE STR
656 printf("-------------------------------------------------\n");
657 printf(" NOP: %s\n", $3);
658 printf("-------------------------------------------------\n");
664 TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling
666 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
667 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $3, $5, $6, $7);
671 if ($3 == TOK_SHRINK) {
676 if (con_is_floating(focused)) {
677 printf("floating resize\n");
678 if (direction == TOK_UP) {
679 focused->parent->rect.y -= px;
680 focused->parent->rect.height += px;
681 } else if (direction == TOK_DOWN) {
682 focused->rect.height += px;
683 } else if (direction == TOK_LEFT) {
684 focused->rect.x -= px;
685 focused->rect.width += px;
687 focused->rect.width += px;
690 LOG("tiling resize\n");
691 /* get the default percentage */
692 int children = con_num_children(focused->parent);
694 LOG("ins. %d children\n", children);
695 double percentage = 1.0 / children;
696 LOG("default percentage = %f\n", percentage);
698 if (direction == TOK_UP || direction == TOK_LEFT) {
699 other = TAILQ_PREV(focused, nodes_head, nodes);
701 other = TAILQ_NEXT(focused, nodes);
703 if (other == TAILQ_END(workspaces)) {
704 LOG("No other container in this direction found, cannot resize.\n");
707 LOG("other->percent = %f\n", other->percent);
708 LOG("focused->percent before = %f\n", focused->percent);
709 if (focused->percent == 0.0)
710 focused->percent = percentage;
711 if (other->percent == 0.0)
712 other->percent = percentage;
713 focused->percent += ((double)ppt / 100.0);
714 other->percent -= ((double)ppt / 100.0);
715 LOG("focused->percent after = %f\n", focused->percent);
716 LOG("other->percent after = %f\n", other->percent);
726 | WHITESPACE NUMBER WHITESPACE TOK_PX
737 | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT
744 TOK_GROW { $$ = TOK_GROW; }
745 | TOK_SHRINK { $$ = TOK_SHRINK; }
749 TOK_UP { $$ = TOK_UP; }
750 | TOK_DOWN { $$ = TOK_DOWN; }
751 | TOK_LEFT { $$ = TOK_LEFT; }
752 | TOK_RIGHT { $$ = TOK_RIGHT; }