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_WORKSPACE "workspace"
139 %token TOK_TOGGLE "toggle"
140 %token TOK_FOCUS "focus"
141 %token TOK_MOVE "move"
142 %token TOK_OPEN "open"
143 %token TOK_NEXT "next"
144 %token TOK_PREV "prev"
145 %token TOK_SPLIT "split"
146 %token TOK_HORIZONTAL "horizontal"
147 %token TOK_VERTICAL "vertical"
148 %token TOK_LEVEL "level"
150 %token TOK_DOWN "down"
151 %token TOK_LEFT "left"
152 %token TOK_RIGHT "right"
153 %token TOK_RESTORE "restore"
154 %token TOK_MARK "mark"
155 %token TOK_RESIZE "resize"
156 %token TOK_GROW "grow"
157 %token TOK_SHRINK "shrink"
163 %token TOK_CLASS "class"
165 %token TOK_CON_ID "con_id"
167 %token <string> STR "<string>"
168 %token <number> NUMBER "<number>"
170 %type <number> direction
171 %type <chr> level_direction
172 %type <number> window_mode
173 %type <number> border_style
174 %type <number> layout_mode
175 %type <number> resize_px
176 %type <number> resize_way
177 %type <number> resize_tiling
178 %type <number> optional_kill_mode
188 printf("single command completely parsed, dropping state...\n");
189 while (!TAILQ_EMPTY(&owindows)) {
190 current = TAILQ_FIRST(&owindows);
191 TAILQ_REMOVE(&owindows, current, owindows);
194 match_init(¤t_match);
203 | matchstart criteria matchend
205 printf("match parsed\n");
213 match_init(¤t_match);
214 TAILQ_INIT(&owindows);
217 TAILQ_FOREACH(con, &all_cons, all_cons) {
218 owindow *ow = smalloc(sizeof(owindow));
220 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
228 owindow *next, *current;
230 printf("match specification finished, matching...\n");
231 /* copy the old list head to iterate through it and start with a fresh
232 * list which will contain only matching windows */
233 struct owindows_head old = owindows;
234 TAILQ_INIT(&owindows);
235 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
236 /* make a copy of the next pointer and advance the pointer to the
237 * next element as we are going to invalidate the element’s
238 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
240 next = TAILQ_NEXT(next, owindows);
242 printf("checking if con %p / %s matches\n", current->con, current->con->name);
243 if (current_match.con_id != NULL) {
244 if (current_match.con_id == current->con) {
245 printf("matches container!\n");
246 TAILQ_INSERT_TAIL(&owindows, current, owindows);
249 } else if (current_match.mark != NULL && current->con->mark != NULL &&
250 strcasecmp(current_match.mark, current->con->mark) == 0) {
251 printf("match by mark\n");
252 TAILQ_INSERT_TAIL(&owindows, current, owindows);
255 if (current->con->window == NULL)
257 if (match_matches_window(¤t_match, current->con->window)) {
258 printf("matches window!\n");
259 TAILQ_INSERT_TAIL(&owindows, current, owindows);
261 printf("doesnt match\n");
267 TAILQ_FOREACH(current, &owindows, owindows) {
268 printf("matching: %p / %s\n", current->con, current->con->name);
277 printf("criteria: class = %s\n", $3);
278 current_match.class = $3;
282 printf("criteria: id = %s\n", $3);
284 long parsed = strtol($3, &end, 10);
285 if (parsed == LONG_MIN ||
286 parsed == LONG_MAX ||
288 (end && *end != '\0')) {
289 ELOG("Could not parse con id \"%s\"\n", $3);
291 current_match.con_id = (Con*)parsed;
292 printf("id as int = %p\n", current_match.con_id);
297 printf("criteria: window 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 window id \"%s\"\n", $3);
306 current_match.id = parsed;
307 printf("window id as int = %d\n", current_match.id);
312 printf("criteria: mark = %s\n", $3);
313 current_match.mark = $3;
319 | operations ',' operation
349 printf("should execute %s\n", $2);
350 start_application($2);
358 printf("exit, bye bye\n");
366 printf("reloading\n");
367 load_configuration(conn, NULL, true);
369 /* Send an IPC event just in case the ws names have changed */
370 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
377 printf("restarting i3\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 | TOK_WINDOW { $$ = KILL_WINDOW; }
425 | TOK_CLIENT { $$ = KILL_CLIENT; }
431 printf("should switch to workspace %s\n", $2);
440 printf("opening new container\n");
441 Con *con = tree_open_con(NULL);
443 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
450 printf("toggling fullscreen\n");
456 TAILQ_FOREACH(current, &owindows, owindows) {
457 printf("matching: %p / %s\n", current->con, current->con->name);
458 con_toggle_fullscreen(current->con);
466 /* TODO: use matches */
467 printf("should select next window in direction %c\n", $2);
468 tree_next('n', ($2 == 'v' ? VERT : HORIZ));
475 /* TODO: use matches */
476 printf("should select prev window in direction %c\n", $2);
477 tree_next('p', ($2 == 'v' ? VERT : HORIZ));
484 /* TODO: use matches */
485 printf("splitting in direction %c\n", $2);
486 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
491 TOK_HORIZONTAL { $$ = 'h'; }
493 | TOK_VERTICAL { $$ = 'v'; }
503 TAILQ_FOREACH(current, &owindows, owindows) {
504 printf("matching: %p / %s\n", current->con, current->con->name);
505 if ($2 == TOK_TOGGLE) {
506 printf("should toggle mode\n");
507 toggle_floating_mode(current->con, false);
509 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
510 if ($2 == TOK_FLOATING) {
511 floating_enable(current->con, false);
513 floating_disable(current->con, false);
521 TOK_FLOATING { $$ = TOK_FLOATING; }
522 | TOK_TILING { $$ = TOK_TILING; }
523 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
527 TOK_BORDER border_style
529 printf("border style should be changed to %d\n", $2);
534 TAILQ_FOREACH(current, &owindows, owindows) {
535 printf("matching: %p / %s\n", current->con, current->con->name);
536 current->con->border_style = $2;
542 TOK_NORMAL { $$ = BS_NORMAL; }
543 | TOK_NONE { $$ = BS_NONE; }
544 | TOK_1PIXEL { $$ = BS_1PIXEL; }
549 TOK_LEVEL level_direction
551 printf("level %c\n", $2);
560 | TOK_DOWN { $$ = 'd'; }
566 printf("moving in direction %d\n", $2);
569 | TOK_MOVE TOK_WORKSPACE STR
573 printf("should move window to workspace %s\n", $3);
574 /* get the workspace */
575 Con *ws = workspace_get($3, NULL);
580 TAILQ_FOREACH(current, &owindows, owindows) {
581 printf("matching: %p / %s\n", current->con, current->con->name);
582 con_move_to_workspace(current->con, ws);
590 printf("restoring \"%s\"\n", $2);
591 tree_append_json($2);
597 TOK_LAYOUT layout_mode
599 printf("changing layout to %d\n", $2);
602 /* check if the match is empty, not if the result is empty */
603 if (match_is_empty(¤t_match))
604 con_set_layout(focused->parent, $2);
606 TAILQ_FOREACH(current, &owindows, owindows) {
607 printf("matching: %p / %s\n", current->con, current->con->name);
608 con_set_layout(current->con, $2);
615 TOK_DEFAULT { $$ = L_DEFAULT; }
616 | TOK_STACKED { $$ = L_STACKED; }
617 | TOK_TABBED { $$ = L_TABBED; }
623 printf("marking window with str %s\n", $2);
628 TAILQ_FOREACH(current, &owindows, owindows) {
629 printf("matching: %p / %s\n", current->con, current->con->name);
630 current->con->mark = sstrdup($2);
640 printf("-------------------------------------------------\n");
641 printf(" NOP: %s\n", $2);
642 printf("-------------------------------------------------\n");
648 TOK_RESIZE resize_way direction resize_px resize_tiling
650 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
651 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
655 if ($2 == TOK_SHRINK) {
660 if (con_is_floating(focused)) {
661 printf("floating resize\n");
662 if (direction == TOK_UP) {
663 focused->parent->rect.y -= px;
664 focused->parent->rect.height += px;
665 } else if (direction == TOK_DOWN) {
666 focused->rect.height += px;
667 } else if (direction == TOK_LEFT) {
668 focused->rect.x -= px;
669 focused->rect.width += px;
671 focused->rect.width += px;
674 LOG("tiling resize\n");
675 /* get the default percentage */
676 int children = con_num_children(focused->parent);
678 LOG("ins. %d children\n", children);
679 double percentage = 1.0 / children;
680 LOG("default percentage = %f\n", percentage);
682 if (direction == TOK_UP || direction == TOK_LEFT) {
683 other = TAILQ_PREV(focused, nodes_head, nodes);
685 other = TAILQ_NEXT(focused, nodes);
687 if (other == TAILQ_END(workspaces)) {
688 LOG("No other container in this direction found, cannot resize.\n");
691 LOG("other->percent = %f\n", other->percent);
692 LOG("focused->percent before = %f\n", focused->percent);
693 if (focused->percent == 0.0)
694 focused->percent = percentage;
695 if (other->percent == 0.0)
696 other->percent = percentage;
697 focused->percent += ((double)ppt / 100.0);
698 other->percent -= ((double)ppt / 100.0);
699 LOG("focused->percent after = %f\n", focused->percent);
700 LOG("other->percent after = %f\n", other->percent);
721 | TOK_OR NUMBER TOK_PPT
728 TOK_GROW { $$ = TOK_GROW; }
729 | TOK_SHRINK { $$ = TOK_SHRINK; }
733 TOK_UP { $$ = TOK_UP; }
734 | TOK_DOWN { $$ = TOK_DOWN; }
735 | TOK_LEFT { $$ = TOK_LEFT; }
736 | TOK_RIGHT { $$ = TOK_RIGHT; }