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);
37 extern int cmdyylex_destroy(void);
39 YY_BUFFER_STATE cmdyy_scan_string(const char *);
41 static struct context *context;
42 static Match current_match;
45 * Helper data structure for an operation window (window on which the operation
46 * will be performed). Used to build the TAILQ owindows.
49 typedef struct owindow {
51 TAILQ_ENTRY(owindow) owindows;
53 static TAILQ_HEAD(owindows_head, owindow) owindows;
55 /* Holds the JSON which will be returned via IPC or NULL for the default return
57 static char *json_output;
59 /* We don’t need yydebug for now, as we got decent error messages using
60 * yyerror(). Should you ever want to extend the parser, it might be handy
61 * to just comment it in again, so it stays here. */
64 void cmdyyerror(const char *error_message) {
66 ELOG("CMD: %s\n", error_message);
67 ELOG("CMD: in command:\n");
68 ELOG("CMD: %s\n", context->line_copy);
70 for (int c = 1; c <= context->last_column; c++)
71 if (c >= context->first_column)
76 context->compact_error = sstrdup(error_message);
83 char *parse_cmd(const char *new) {
84 LOG("COMMAND: *%s*\n", new);
85 cmdyy_scan_string(new);
87 match_init(¤t_match);
88 context = scalloc(sizeof(struct context));
89 context->filename = "cmd";
91 if (cmdyyparse() != 0) {
92 fprintf(stderr, "Could not parse command\n");
93 asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
94 context->compact_error, context->first_column);
95 FREE(context->line_copy);
96 FREE(context->compact_error);
100 printf("done, json output = %s\n", json_output);
103 FREE(context->line_copy);
104 FREE(context->compact_error);
110 * Returns true if a is definitely greater than b (using the given epsilon)
113 bool definitelyGreaterThan(float a, float b, float epsilon) {
114 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
120 %lex-param { struct context *context }
128 %token TOK_EXEC "exec"
129 %token TOK_EXIT "exit"
130 %token TOK_RELOAD "reload"
131 %token TOK_RESTART "restart"
132 %token TOK_KILL "kill"
133 %token TOK_WINDOW "window"
134 %token TOK_CLIENT "client"
135 %token TOK_FULLSCREEN "fullscreen"
136 %token TOK_GLOBAL "global"
137 %token TOK_LAYOUT "layout"
138 %token TOK_DEFAULT "default"
139 %token TOK_STACKED "stacked"
140 %token TOK_TABBED "tabbed"
141 %token TOK_BORDER "border"
142 %token TOK_NORMAL "normal"
143 %token TOK_NONE "none"
144 %token TOK_1PIXEL "1pixel"
145 %token TOK_MODE "mode"
146 %token TOK_TILING "tiling"
147 %token TOK_FLOATING "floating"
148 %token TOK_MODE_TOGGLE "mode_toggle"
149 %token TOK_ENABLE "enable"
150 %token TOK_DISABLE "disable"
151 %token TOK_WORKSPACE "workspace"
152 %token TOK_TOGGLE "toggle"
153 %token TOK_FOCUS "focus"
154 %token TOK_MOVE "move"
155 %token TOK_OPEN "open"
156 %token TOK_NEXT "next"
157 %token TOK_PREV "prev"
158 %token TOK_SPLIT "split"
159 %token TOK_HORIZONTAL "horizontal"
160 %token TOK_VERTICAL "vertical"
162 %token TOK_DOWN "down"
163 %token TOK_LEFT "left"
164 %token TOK_RIGHT "right"
165 %token TOK_PARENT "parent"
166 %token TOK_CHILD "child"
167 %token TOK_APPEND_LAYOUT "append_layout"
168 %token TOK_MARK "mark"
169 %token TOK_RESIZE "resize"
170 %token TOK_GROW "grow"
171 %token TOK_SHRINK "shrink"
177 %token TOK_CLASS "class"
179 %token TOK_CON_ID "con_id"
180 %token TOK_TITLE "title"
182 %token <string> STR "<string>"
183 %token <number> NUMBER "<number>"
185 %type <number> direction
186 %type <number> split_direction
187 %type <number> fullscreen_mode
189 %type <number> window_mode
190 %type <number> boolean
191 %type <number> border_style
192 %type <number> layout_mode
193 %type <number> resize_px
194 %type <number> resize_way
195 %type <number> resize_tiling
196 %type <number> optional_kill_mode
206 printf("single command completely parsed, dropping state...\n");
207 while (!TAILQ_EMPTY(&owindows)) {
208 current = TAILQ_FIRST(&owindows);
209 TAILQ_REMOVE(&owindows, current, owindows);
212 match_init(¤t_match);
221 | matchstart criteria matchend
223 printf("match parsed\n");
231 match_init(¤t_match);
232 TAILQ_INIT(&owindows);
235 TAILQ_FOREACH(con, &all_cons, all_cons) {
236 owindow *ow = smalloc(sizeof(owindow));
238 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
246 owindow *next, *current;
248 printf("match specification finished, matching...\n");
249 /* copy the old list head to iterate through it and start with a fresh
250 * list which will contain only matching windows */
251 struct owindows_head old = owindows;
252 TAILQ_INIT(&owindows);
253 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
254 /* make a copy of the next pointer and advance the pointer to the
255 * next element as we are going to invalidate the element’s
256 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
258 next = TAILQ_NEXT(next, owindows);
260 printf("checking if con %p / %s matches\n", current->con, current->con->name);
261 if (current_match.con_id != NULL) {
262 if (current_match.con_id == current->con) {
263 printf("matches container!\n");
264 TAILQ_INSERT_TAIL(&owindows, current, owindows);
267 } else if (current_match.mark != NULL && current->con->mark != NULL &&
268 strcasecmp(current_match.mark, current->con->mark) == 0) {
269 printf("match by mark\n");
270 TAILQ_INSERT_TAIL(&owindows, current, owindows);
273 if (current->con->window == NULL)
275 if (match_matches_window(¤t_match, current->con->window)) {
276 printf("matches window!\n");
277 TAILQ_INSERT_TAIL(&owindows, current, owindows);
279 printf("doesnt match\n");
285 TAILQ_FOREACH(current, &owindows, owindows) {
286 printf("matching: %p / %s\n", current->con, current->con->name);
300 printf("criteria: class = %s\n", $3);
301 current_match.class = $3;
305 printf("criteria: id = %s\n", $3);
307 long parsed = strtol($3, &end, 10);
308 if (parsed == LONG_MIN ||
309 parsed == LONG_MAX ||
311 (end && *end != '\0')) {
312 ELOG("Could not parse con id \"%s\"\n", $3);
314 current_match.con_id = (Con*)parsed;
315 printf("id as int = %p\n", current_match.con_id);
320 printf("criteria: window id = %s\n", $3);
322 long parsed = strtol($3, &end, 10);
323 if (parsed == LONG_MIN ||
324 parsed == LONG_MAX ||
326 (end && *end != '\0')) {
327 ELOG("Could not parse window id \"%s\"\n", $3);
329 current_match.id = parsed;
330 printf("window id as int = %d\n", current_match.id);
335 printf("criteria: mark = %s\n", $3);
336 current_match.mark = $3;
340 printf("criteria: title = %s\n", $3);
341 current_match.title = $3;
347 | operations ',' operation
375 printf("should execute %s\n", $2);
376 start_application($2);
384 printf("exit, bye bye\n");
392 printf("reloading\n");
393 kill_configerror_nagbar(false);
394 load_configuration(conn, NULL, true);
396 /* Send an IPC event just in case the ws names have changed */
397 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
404 printf("restarting i3\n");
414 if (match_is_empty(¤t_match)) {
415 ELOG("You have to specify which window/container should be focused.\n");
416 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
418 asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
419 "specify which window/container should be focused\"}");
424 TAILQ_FOREACH(current, &owindows, owindows) {
425 Con *ws = con_get_workspace(current->con);
426 workspace_show(ws->name);
427 LOG("focusing %p / %s\n", current->con, current->con->name);
428 con_focus(current->con);
433 LOG("WARNING: Your criteria for the focus command matches %d containers, "
434 "while only exactly one container can be focused at a time.\n", count);
438 | TOK_FOCUS direction
443 LOG("Focusing left\n");
444 tree_next('p', HORIZ);
447 LOG("Focusing right\n");
448 tree_next('n', HORIZ);
451 LOG("Focusing up\n");
452 tree_next('p', VERT);
455 LOG("Focusing down\n");
456 tree_next('n', VERT);
459 ELOG("Invalid focus direction (%d)\n", direction);
465 | TOK_FOCUS window_mode
467 printf("should focus: ");
469 if ($2 == TOK_TILING)
471 else if ($2 == TOK_FLOATING)
472 printf("floating\n");
473 else printf("mode toggle\n");
475 Con *ws = con_get_workspace(focused);
479 if ($2 == TOK_MODE_TOGGLE) {
480 current = TAILQ_FIRST(&(ws->focus_head));
481 if (current->type == CT_FLOATING_CON)
482 to_focus = TOK_TILING;
483 else to_focus = TOK_FLOATING;
485 TAILQ_FOREACH(current, &(ws->focus_head), focused) {
486 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
487 (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
490 con_focus(con_descend_focused(current));
499 if ($2 == TOK_PARENT)
508 TOK_TILING { $$ = TOK_TILING; }
509 | TOK_FLOATING { $$ = TOK_FLOATING; }
510 | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
514 TOK_PARENT { $$ = TOK_PARENT; }
515 | TOK_CHILD { $$ = TOK_CHILD; }
519 TOK_KILL optional_kill_mode
523 printf("killing!\n");
524 /* check if the match is empty, not if the result is empty */
525 if (match_is_empty(¤t_match))
528 TAILQ_FOREACH(current, &owindows, owindows) {
529 printf("matching: %p / %s\n", current->con, current->con->name);
530 tree_close(current->con, $2, false);
539 /* empty */ { $$ = KILL_WINDOW; }
540 | TOK_WINDOW { $$ = KILL_WINDOW; }
541 | TOK_CLIENT { $$ = KILL_CLIENT; }
545 TOK_WORKSPACE TOK_NEXT
550 | TOK_WORKSPACE TOK_PREV
557 printf("should switch to workspace %s\n", $2);
568 printf("opening new container\n");
569 Con *con = tree_open_con(NULL, NULL);
571 asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
578 TOK_FULLSCREEN fullscreen_mode
580 printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
585 TAILQ_FOREACH(current, &owindows, owindows) {
586 printf("matching: %p / %s\n", current->con, current->con->name);
587 con_toggle_fullscreen(current->con, $2);
595 /* empty */ { $$ = CF_OUTPUT; }
596 | TOK_GLOBAL { $$ = CF_GLOBAL; }
600 TOK_SPLIT split_direction
602 /* TODO: use matches */
603 printf("splitting in direction %c\n", $2);
604 tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
611 TOK_HORIZONTAL { $$ = 'h'; }
613 | TOK_VERTICAL { $$ = 'v'; }
623 TAILQ_FOREACH(current, &owindows, owindows) {
624 printf("matching: %p / %s\n", current->con, current->con->name);
625 if ($2 == TOK_TOGGLE) {
626 printf("should toggle mode\n");
627 toggle_floating_mode(current->con, false);
629 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
630 if ($2 == TOK_ENABLE) {
631 floating_enable(current->con, false);
633 floating_disable(current->con, false);
643 TOK_ENABLE { $$ = TOK_ENABLE; }
644 | TOK_DISABLE { $$ = TOK_DISABLE; }
645 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
649 TOK_BORDER border_style
651 printf("border style should be changed to %d\n", $2);
656 TAILQ_FOREACH(current, &owindows, owindows) {
657 printf("matching: %p / %s\n", current->con, current->con->name);
658 if ($2 == TOK_TOGGLE) {
659 current->con->border_style++;
660 current->con->border_style %= 3;
661 } else current->con->border_style = $2;
669 TOK_NORMAL { $$ = BS_NORMAL; }
670 | TOK_NONE { $$ = BS_NONE; }
671 | TOK_1PIXEL { $$ = BS_1PIXEL; }
672 | TOK_TOGGLE { $$ = TOK_TOGGLE; }
678 printf("moving in direction %d\n", $2);
683 | TOK_MOVE TOK_WORKSPACE STR
687 printf("should move window to workspace %s\n", $3);
688 /* get the workspace */
689 Con *ws = workspace_get($3, NULL);
694 TAILQ_FOREACH(current, &owindows, owindows) {
695 printf("matching: %p / %s\n", current->con, current->con->name);
696 con_move_to_workspace(current->con, ws);
704 TOK_APPEND_LAYOUT STR
706 printf("restoring \"%s\"\n", $2);
707 tree_append_json($2);
714 TOK_LAYOUT layout_mode
716 printf("changing layout to %d\n", $2);
719 /* check if the match is empty, not if the result is empty */
720 if (match_is_empty(¤t_match))
721 con_set_layout(focused->parent, $2);
723 TAILQ_FOREACH(current, &owindows, owindows) {
724 printf("matching: %p / %s\n", current->con, current->con->name);
725 con_set_layout(current->con, $2);
734 TOK_DEFAULT { $$ = L_DEFAULT; }
735 | TOK_STACKED { $$ = L_STACKED; }
736 | TOK_TABBED { $$ = L_TABBED; }
742 printf("marking window with str %s\n", $2);
747 TAILQ_FOREACH(current, &owindows, owindows) {
748 printf("matching: %p / %s\n", current->con, current->con->name);
749 current->con->mark = sstrdup($2);
761 printf("-------------------------------------------------\n");
762 printf(" NOP: %s\n", $2);
763 printf("-------------------------------------------------\n");
769 TOK_RESIZE resize_way direction resize_px resize_tiling
771 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
772 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
776 if ($2 == TOK_SHRINK) {
781 if (con_is_floating(focused)) {
782 printf("floating resize\n");
783 if (direction == TOK_UP) {
784 focused->parent->rect.y -= px;
785 focused->parent->rect.height += px;
786 } else if (direction == TOK_DOWN) {
787 focused->parent->rect.height += px;
788 } else if (direction == TOK_LEFT) {
789 focused->parent->rect.x -= px;
790 focused->parent->rect.width += px;
792 focused->parent->rect.width += px;
795 LOG("tiling resize\n");
796 /* get the default percentage */
797 int children = con_num_children(focused->parent);
799 LOG("ins. %d children\n", children);
800 double percentage = 1.0 / children;
801 LOG("default percentage = %f\n", percentage);
803 if (direction == TOK_UP || direction == TOK_LEFT) {
804 other = TAILQ_PREV(focused, nodes_head, nodes);
806 other = TAILQ_NEXT(focused, nodes);
808 if (other == TAILQ_END(workspaces)) {
809 LOG("No other container in this direction found, cannot resize.\n");
812 LOG("other->percent = %f\n", other->percent);
813 LOG("focused->percent before = %f\n", focused->percent);
814 if (focused->percent == 0.0)
815 focused->percent = percentage;
816 if (other->percent == 0.0)
817 other->percent = percentage;
818 double new_focused_percent = focused->percent + ((double)ppt / 100.0);
819 double new_other_percent = other->percent - ((double)ppt / 100.0);
820 LOG("new_focused_percent = %f\n", new_focused_percent);
821 LOG("new_other_percent = %f\n", new_other_percent);
822 /* Ensure that the new percentages are positive and greater than
823 * 0.05 to have a reasonable minimum size. */
824 if (definitelyGreaterThan(new_focused_percent, 0.05, DBL_EPSILON) &&
825 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
826 focused->percent += ((double)ppt / 100.0);
827 other->percent -= ((double)ppt / 100.0);
828 LOG("focused->percent after = %f\n", focused->percent);
829 LOG("other->percent after = %f\n", other->percent);
831 LOG("Not resizing, already at minimum size\n");
855 | TOK_OR NUMBER TOK_PPT
862 TOK_GROW { $$ = TOK_GROW; }
863 | TOK_SHRINK { $$ = TOK_SHRINK; }
867 TOK_UP { $$ = TOK_UP; }
868 | TOK_DOWN { $$ = TOK_DOWN; }
869 | TOK_LEFT { $$ = TOK_LEFT; }
870 | TOK_RIGHT { $$ = TOK_RIGHT; }