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_ENABLE "enable"
139 %token TOK_DISABLE "disable"
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"
151 %token TOK_DOWN "down"
152 %token TOK_LEFT "left"
153 %token TOK_RIGHT "right"
154 %token TOK_PARENT "parent"
155 %token TOK_CHILD "child"
156 %token TOK_APPEND_LAYOUT "append_layout"
157 %token TOK_MARK "mark"
158 %token TOK_RESIZE "resize"
159 %token TOK_GROW "grow"
160 %token TOK_SHRINK "shrink"
166 %token TOK_CLASS "class"
168 %token TOK_CON_ID "con_id"
169 %token TOK_TITLE "title"
171 %token <string> STR "<string>"
172 %token <number> NUMBER "<number>"
174 %type <number> direction
175 %type <number> split_direction
176 %type <number> fullscreen_mode
178 %type <number> boolean
179 %type <number> border_style
180 %type <number> layout_mode
181 %type <number> resize_px
182 %type <number> resize_way
183 %type <number> resize_tiling
184 %type <number> optional_kill_mode
194 printf("single command completely parsed, dropping state...\n");
195 while (!TAILQ_EMPTY(&owindows)) {
196 current = TAILQ_FIRST(&owindows);
197 TAILQ_REMOVE(&owindows, current, owindows);
200 match_init(¤t_match);
209 | matchstart criteria matchend
211 printf("match parsed\n");
219 match_init(¤t_match);
220 TAILQ_INIT(&owindows);
223 TAILQ_FOREACH(con, &all_cons, all_cons) {
224 owindow *ow = smalloc(sizeof(owindow));
226 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
234 owindow *next, *current;
236 printf("match specification finished, matching...\n");
237 /* copy the old list head to iterate through it and start with a fresh
238 * list which will contain only matching windows */
239 struct owindows_head old = owindows;
240 TAILQ_INIT(&owindows);
241 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
242 /* make a copy of the next pointer and advance the pointer to the
243 * next element as we are going to invalidate the element’s
244 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
246 next = TAILQ_NEXT(next, owindows);
248 printf("checking if con %p / %s matches\n", current->con, current->con->name);
249 if (current_match.con_id != NULL) {
250 if (current_match.con_id == current->con) {
251 printf("matches container!\n");
252 TAILQ_INSERT_TAIL(&owindows, current, owindows);
255 } else if (current_match.mark != NULL && current->con->mark != NULL &&
256 strcasecmp(current_match.mark, current->con->mark) == 0) {
257 printf("match by mark\n");
258 TAILQ_INSERT_TAIL(&owindows, current, owindows);
261 if (current->con->window == NULL)
263 if (match_matches_window(¤t_match, current->con->window)) {
264 printf("matches window!\n");
265 TAILQ_INSERT_TAIL(&owindows, current, owindows);
267 printf("doesnt match\n");
273 TAILQ_FOREACH(current, &owindows, owindows) {
274 printf("matching: %p / %s\n", current->con, current->con->name);
288 printf("criteria: class = %s\n", $3);
289 current_match.class = $3;
293 printf("criteria: id = %s\n", $3);
295 long parsed = strtol($3, &end, 10);
296 if (parsed == LONG_MIN ||
297 parsed == LONG_MAX ||
299 (end && *end != '\0')) {
300 ELOG("Could not parse con id \"%s\"\n", $3);
302 current_match.con_id = (Con*)parsed;
303 printf("id as int = %p\n", current_match.con_id);
308 printf("criteria: window id = %s\n", $3);
310 long parsed = strtol($3, &end, 10);
311 if (parsed == LONG_MIN ||
312 parsed == LONG_MAX ||
314 (end && *end != '\0')) {
315 ELOG("Could not parse window id \"%s\"\n", $3);
317 current_match.id = parsed;
318 printf("window id as int = %d\n", current_match.id);
323 printf("criteria: mark = %s\n", $3);
324 current_match.mark = $3;
328 printf("criteria: title = %s\n", $3);
329 current_match.title = $3;
335 | operations ',' operation
363 printf("should execute %s\n", $2);
364 start_application($2);
372 printf("exit, bye bye\n");
380 printf("reloading\n");
381 load_configuration(conn, NULL, true);
383 /* Send an IPC event just in case the ws names have changed */
384 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
391 printf("restarting i3\n");
401 if (match_is_empty(¤t_match)) {
402 ELOG("You have to specify which window/container should be focused.\n");
403 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
405 asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
406 "specify which window/container should be focused\"}");
411 TAILQ_FOREACH(current, &owindows, owindows) {
412 LOG("focusing %p / %s\n", current->con, current->con->name);
413 con_focus(current->con);
418 LOG("WARNING: Your criteria for the focus command matches %d containers, "
419 "while only exactly one container can be focused at a time.\n", count);
423 | TOK_FOCUS direction
428 LOG("Focusing left\n");
429 tree_next('p', HORIZ);
432 LOG("Focusing right\n");
433 tree_next('n', HORIZ);
436 LOG("Focusing up\n");
437 tree_next('p', VERT);
440 LOG("Focusing down\n");
441 tree_next('n', VERT);
444 ELOG("Invalid focus direction (%d)\n", direction);
452 if ($2 == TOK_PARENT)
461 TOK_PARENT { $$ = TOK_PARENT; }
462 | TOK_CHILD { $$ = TOK_CHILD; }
466 TOK_KILL optional_kill_mode
470 printf("killing!\n");
471 /* check if the match is empty, not if the result is empty */
472 if (match_is_empty(¤t_match))
475 TAILQ_FOREACH(current, &owindows, owindows) {
476 printf("matching: %p / %s\n", current->con, current->con->name);
477 tree_close(current->con, $2, false);
486 /* empty */ { $$ = KILL_WINDOW; }
487 | TOK_WINDOW { $$ = KILL_WINDOW; }
488 | TOK_CLIENT { $$ = KILL_CLIENT; }
492 TOK_WORKSPACE TOK_NEXT
497 | TOK_WORKSPACE TOK_PREV
504 printf("should switch to workspace %s\n", $2);
515 printf("opening new container\n");
516 Con *con = tree_open_con(NULL, NULL);
518 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
525 TOK_FULLSCREEN fullscreen_mode
527 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
532 TAILQ_FOREACH(current, &owindows, owindows) {
533 printf("matching: %p / %s\n", current->con, current->con->name);
534 con_toggle_fullscreen(current->con, $2);
542 /* empty */ { $$ = CF_OUTPUT; }
543 | TOK_GLOBAL { $$ = CF_GLOBAL; }
547 TOK_SPLIT split_direction
549 /* TODO: use matches */
550 printf("splitting in direction %c\n", $2);
551 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
558 TOK_HORIZONTAL { $$ = 'h'; }
560 | TOK_VERTICAL { $$ = 'v'; }
570 TAILQ_FOREACH(current, &owindows, owindows) {
571 printf("matching: %p / %s\n", current->con, current->con->name);
572 if ($2 == TOK_TOGGLE) {
573 printf("should toggle mode\n");
574 toggle_floating_mode(current->con, false);
576 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
577 if ($2 == TOK_ENABLE) {
578 floating_enable(current->con, false);
580 floating_disable(current->con, false);
590 TOK_ENABLE { $$ = TOK_ENABLE; }
591 | TOK_DISABLE { $$ = TOK_DISABLE; }
592 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
596 TOK_BORDER border_style
598 printf("border style should be changed to %d\n", $2);
603 TAILQ_FOREACH(current, &owindows, owindows) {
604 printf("matching: %p / %s\n", current->con, current->con->name);
605 if ($2 == TOK_TOGGLE) {
606 current->con->border_style++;
607 current->con->border_style %= 3;
608 } else current->con->border_style = $2;
616 TOK_NORMAL { $$ = BS_NORMAL; }
617 | TOK_NONE { $$ = BS_NONE; }
618 | TOK_1PIXEL { $$ = BS_1PIXEL; }
619 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
625 printf("moving in direction %d\n", $2);
630 | TOK_MOVE TOK_WORKSPACE STR
634 printf("should move window to workspace %s\n", $3);
635 /* get the workspace */
636 Con *ws = workspace_get($3, NULL);
641 TAILQ_FOREACH(current, &owindows, owindows) {
642 printf("matching: %p / %s\n", current->con, current->con->name);
643 con_move_to_workspace(current->con, ws);
651 TOK_APPEND_LAYOUT STR
653 printf("restoring \"%s\"\n", $2);
654 tree_append_json($2);
661 TOK_LAYOUT layout_mode
663 printf("changing layout to %d\n", $2);
666 /* check if the match is empty, not if the result is empty */
667 if (match_is_empty(¤t_match))
668 con_set_layout(focused->parent, $2);
670 TAILQ_FOREACH(current, &owindows, owindows) {
671 printf("matching: %p / %s\n", current->con, current->con->name);
672 con_set_layout(current->con, $2);
681 TOK_DEFAULT { $$ = L_DEFAULT; }
682 | TOK_STACKED { $$ = L_STACKED; }
683 | TOK_TABBED { $$ = L_TABBED; }
689 printf("marking window with str %s\n", $2);
694 TAILQ_FOREACH(current, &owindows, owindows) {
695 printf("matching: %p / %s\n", current->con, current->con->name);
696 current->con->mark = sstrdup($2);
708 printf("-------------------------------------------------\n");
709 printf(" NOP: %s\n", $2);
710 printf("-------------------------------------------------\n");
716 TOK_RESIZE resize_way direction resize_px resize_tiling
718 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
719 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
723 if ($2 == TOK_SHRINK) {
728 if (con_is_floating(focused)) {
729 printf("floating resize\n");
730 if (direction == TOK_UP) {
731 focused->parent->rect.y -= px;
732 focused->parent->rect.height += px;
733 } else if (direction == TOK_DOWN) {
734 focused->rect.height += px;
735 } else if (direction == TOK_LEFT) {
736 focused->rect.x -= px;
737 focused->rect.width += px;
739 focused->rect.width += px;
742 LOG("tiling resize\n");
743 /* get the default percentage */
744 int children = con_num_children(focused->parent);
746 LOG("ins. %d children\n", children);
747 double percentage = 1.0 / children;
748 LOG("default percentage = %f\n", percentage);
750 if (direction == TOK_UP || direction == TOK_LEFT) {
751 other = TAILQ_PREV(focused, nodes_head, nodes);
753 other = TAILQ_NEXT(focused, nodes);
755 if (other == TAILQ_END(workspaces)) {
756 LOG("No other container in this direction found, cannot resize.\n");
759 LOG("other->percent = %f\n", other->percent);
760 LOG("focused->percent before = %f\n", focused->percent);
761 if (focused->percent == 0.0)
762 focused->percent = percentage;
763 if (other->percent == 0.0)
764 other->percent = percentage;
765 focused->percent += ((double)ppt / 100.0);
766 other->percent -= ((double)ppt / 100.0);
767 LOG("focused->percent after = %f\n", focused->percent);
768 LOG("other->percent after = %f\n", other->percent);
791 | TOK_OR NUMBER TOK_PPT
798 TOK_GROW { $$ = TOK_GROW; }
799 | TOK_SHRINK { $$ = TOK_SHRINK; }
803 TOK_UP { $$ = TOK_UP; }
804 | TOK_DOWN { $$ = TOK_DOWN; }
805 | TOK_LEFT { $$ = TOK_LEFT; }
806 | TOK_RIGHT { $$ = TOK_RIGHT; }