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>
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int cmdyylex(struct context *context);
21 extern int cmdyyparse(void);
23 YY_BUFFER_STATE cmdyy_scan_string(const char *);
25 static struct bindings_head *current_bindings;
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 file \"%s\", line %d:\n",
53 context->filename, context->line_number);
54 ELOG("CMD: %s\n", context->line_copy);
56 for (int c = 1; c <= context->last_column; c++)
57 if (c >= context->first_column)
68 char *parse_cmd(const char *new) {
70 //const char *new = "[level-up workspace] attach $output, focus";
72 cmdyy_scan_string(new);
74 context = scalloc(sizeof(struct context));
75 context->filename = "cmd";
77 if (cmdyyparse() != 0) {
78 fprintf(stderr, "Could not parse configfile\n");
81 printf("done, json output = %s\n", json_output);
83 FREE(context->line_copy);
91 %lex-param { struct context *context }
99 %token TOK_ATTACH "attach"
100 %token TOK_EXEC "exec"
101 %token TOK_EXIT "exit"
102 %token TOK_RELOAD "reload"
103 %token TOK_RESTART "restart"
104 %token TOK_KILL "kill"
105 %token TOK_FULLSCREEN "fullscreen"
106 %token TOK_GLOBAL "global"
107 %token TOK_LAYOUT "layout"
108 %token TOK_DEFAULT "default"
109 %token TOK_STACKED "stacked"
110 %token TOK_TABBED "tabbed"
111 %token TOK_BORDER "border"
112 %token TOK_NONE "none"
113 %token TOK_1PIXEL "1pixel"
114 %token TOK_MODE "mode"
115 %token TOK_TILING "tiling"
116 %token TOK_FLOATING "floating"
117 %token TOK_WORKSPACE "workspace"
118 %token TOK_TOGGLE "toggle"
119 %token TOK_FOCUS "focus"
120 %token TOK_MOVE "move"
121 %token TOK_OPEN "open"
122 %token TOK_NEXT "next"
123 %token TOK_PREV "prev"
124 %token TOK_SPLIT "split"
125 %token TOK_HORIZONTAL "horizontal"
126 %token TOK_VERTICAL "vertical"
127 %token TOK_LEVEL "level"
129 %token TOK_DOWN "down"
130 %token TOK_LEFT "left"
131 %token TOK_RIGHT "right"
132 %token TOK_AFTER "after"
133 %token TOK_BEFORE "before"
134 %token TOK_RESTORE "restore"
135 %token TOK_MARK "mark"
136 %token TOK_RESIZE "resize"
137 %token TOK_GROW "grow"
138 %token TOK_SHRINK "shrink"
143 %token TOK_CLASS "class"
145 %token TOK_CON_ID "con_id"
147 %token WHITESPACE "<whitespace>"
148 %token STR "<string>"
149 %token NUMBER "<number>"
153 commands: /* empty */
154 | commands optwhitespace ';' optwhitespace command
159 printf("single command completely parsed, dropping state...\n");
160 while (!TAILQ_EMPTY(&owindows)) {
161 current = TAILQ_FIRST(&owindows);
162 TAILQ_REMOVE(&owindows, current, owindows);
165 memset(¤t_match, 0, sizeof(Match));
174 match optwhitespace operations
178 | matchstart optwhitespace criteria optwhitespace matchend
180 printf("match parsed\n");
188 memset(¤t_match, '\0', sizeof(Match));
189 TAILQ_INIT(&owindows);
192 TAILQ_FOREACH(con, &all_cons, all_cons) {
193 owindow *ow = smalloc(sizeof(owindow));
195 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
203 owindow *next, *current;
205 printf("match specification finished, matching...\n");
206 /* copy the old list head to iterate through it and start with a fresh
207 * list which will contain only matching windows */
208 struct owindows_head old = owindows;
209 TAILQ_INIT(&owindows);
210 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
211 /* make a copy of the next pointer and advance the pointer to the
212 * next element as we are going to invalidate the element’s
213 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
215 next = TAILQ_NEXT(next, owindows);
217 printf("checking if con %p / %s matches\n", current->con, current->con->name);
218 if (current_match.con_id != NULL) {
219 if (current_match.con_id == current->con) {
220 printf("matches container!\n");
221 TAILQ_INSERT_TAIL(&owindows, current, owindows);
224 } else if (current_match.mark != NULL && current->con->mark != NULL &&
225 strcasecmp(current_match.mark, current->con->mark) == 0) {
226 printf("match by mark\n");
227 TAILQ_INSERT_TAIL(&owindows, current, owindows);
230 if (current->con->window == NULL)
232 if (match_matches_window(¤t_match, current->con->window)) {
233 printf("matches window!\n");
234 TAILQ_INSERT_TAIL(&owindows, current, owindows);
236 printf("doesnt match\n");
242 TAILQ_FOREACH(current, &owindows, owindows) {
243 printf("matching: %p / %s\n", current->con, current->con->name);
252 printf("criteria: class = %s\n", $<string>3);
253 current_match.class = $<string>3;
257 printf("criteria: id = %s\n", $<string>3);
258 /* TODO: correctly parse number */
259 current_match.con_id = atoi($<string>3);
260 printf("id as int = %d\n", current_match.con_id);
264 printf("criteria: window id = %s\n", $<string>3);
265 /* TODO: correctly parse number */
266 current_match.id = atoi($<string>3);
267 printf("window id as int = %d\n", current_match.id);
271 printf("criteria: mark = %s\n", $<string>3);
272 current_match.mark = $<string>3;
278 | operation optwhitespace
279 | operations ',' optwhitespace operation
308 TOK_EXEC WHITESPACE STR
310 printf("should execute %s\n", $<string>3);
311 start_application($<string>3);
318 printf("exit, bye bye\n");
326 printf("reloading\n");
327 load_configuration(conn, NULL, true);
328 /* Send an IPC event just in case the ws names have changed */
329 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
336 printf("restarting i3\n");
344 printf("should attach\n");
353 printf("should focus\n");
354 if (match_is_empty(¤t_match)) {
355 /* TODO: better error message */
356 LOG("Error: The foucs command requires you to use some criteria.\n");
360 /* TODO: warning if the match contains more than one entry. does not
361 * make so much sense when focusing */
362 TAILQ_FOREACH(current, &owindows, owindows) {
363 LOG("focusing %p / %s\n", current->con, current->con->name);
364 con_focus(current->con);
374 printf("killing!\n");
375 /* check if the match is empty, not if the result is empty */
376 if (match_is_empty(¤t_match))
379 TAILQ_FOREACH(current, &owindows, owindows) {
380 printf("matching: %p / %s\n", current->con, current->con->name);
381 tree_close(current->con, true);
389 TOK_WORKSPACE WHITESPACE STR
391 printf("should switch to workspace %s\n", $<string>3);
392 workspace_show($<string>3);
400 printf("opening new container\n");
401 Con *con = tree_open_con(NULL);
402 asprintf(&json_output, "{\"success\":true, \"id\":%d}", (long int)con);
409 printf("toggling fullscreen\n");
412 /* check if the match is empty, not if the result is empty */
413 if (match_is_empty(¤t_match))
414 con_toggle_fullscreen(focused);
416 TAILQ_FOREACH(current, &owindows, owindows) {
417 printf("matching: %p / %s\n", current->con, current->con->name);
418 con_toggle_fullscreen(current->con);
426 TOK_NEXT WHITESPACE direction
428 /* TODO: use matches */
429 printf("should select next window in direction %c\n", $<chr>3);
430 tree_next('n', ($<chr>3 == 'v' ? VERT : HORIZ));
435 TOK_PREV WHITESPACE direction
437 /* TODO: use matches */
438 printf("should select prev window in direction %c\n", $<chr>3);
439 tree_next('p', ($<chr>3 == 'v' ? VERT : HORIZ));
444 TOK_SPLIT WHITESPACE direction
446 /* TODO: use matches */
447 printf("splitting in direction %c\n", $<chr>3);
448 tree_split(focused, ($<chr>3 == 'v' ? VERT : HORIZ));
453 TOK_HORIZONTAL { $<chr>$ = 'h'; }
454 | 'h' { $<chr>$ = 'h'; }
455 | TOK_VERTICAL { $<chr>$ = 'v'; }
456 | 'v' { $<chr>$ = 'v'; }
460 TOK_MODE WHITESPACE window_mode
462 if ($<number>3 == TOK_TOGGLE) {
463 printf("should toggle mode\n");
464 toggle_floating_mode(focused, false);
466 printf("should switch mode to %s\n", ($<number>3 == TOK_FLOATING ? "floating" : "tiling"));
467 if ($<number>3 == TOK_FLOATING) {
468 floating_enable(focused, false);
470 floating_disable(focused, false);
477 TOK_FLOATING { $<number>$ = TOK_FLOATING; }
478 | TOK_TILING { $<number>$ = TOK_TILING; }
479 | TOK_TOGGLE { $<number>$ = TOK_TOGGLE; }
483 TOK_LEVEL WHITESPACE level_direction
485 printf("level %c\n", $<chr>3);
493 TOK_UP { $<chr>$ = 'u'; }
494 | TOK_DOWN { $<chr>$ = 'd'; }
498 TOK_MOVE WHITESPACE before_after WHITESPACE direction
500 printf("moving: %s and %c\n", ($<number>3 == TOK_BEFORE ? "before" : "after"), $<chr>5);
501 /* TODO: change API for the next call, we need to convert in both directions while ideally
502 * we should not need any of both */
503 tree_move(($<number>3 == TOK_BEFORE ? 'p' : 'n'), ($<chr>5 == 'v' ? VERT : HORIZ));
505 | TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR
509 printf("should move window to workspace %s\n", $<string>5);
510 /* get the workspace */
511 Con *ws = workspace_get($<string>5);
513 /* check if the match is empty, not if the result is empty */
514 if (match_is_empty(¤t_match))
515 con_move_to_workspace(focused, ws);
517 TAILQ_FOREACH(current, &owindows, owindows) {
518 printf("matching: %p / %s\n", current->con, current->con->name);
519 con_move_to_workspace(current->con, ws);
526 TOK_BEFORE { $<number>$ = TOK_BEFORE; }
527 | TOK_AFTER { $<number>$ = TOK_AFTER; }
531 TOK_RESTORE WHITESPACE STR
533 printf("restoring \"%s\"\n", $<string>3);
534 tree_append_json($<string>3);
539 TOK_LAYOUT WHITESPACE layout_mode
541 printf("changing layout to %d\n", $<number>3);
544 /* check if the match is empty, not if the result is empty */
545 if (match_is_empty(¤t_match))
546 focused->parent->layout = $<number>3;
548 TAILQ_FOREACH(current, &owindows, owindows) {
549 printf("matching: %p / %s\n", current->con, current->con->name);
550 current->con->layout = $<number>3;
558 TOK_DEFAULT { $<number>$ = L_DEFAULT; }
559 | TOK_STACKED { $<number>$ = L_STACKED; }
560 | TOK_TABBED { $<number>$ = L_TABBED; }
564 TOK_MARK WHITESPACE STR
566 printf("marking window with str %s\n", $<string>3);
569 /* check if the match is empty, not if the result is empty */
570 if (match_is_empty(¤t_match))
571 focused->mark = sstrdup($<string>3);
573 TAILQ_FOREACH(current, &owindows, owindows) {
574 printf("matching: %p / %s\n", current->con, current->con->name);
575 current->con->mark = sstrdup($<string>3);
584 TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling
586 /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
587 printf("resizing in way %d, direction %d, px %d or ppt %d\n", $<number>3, $<number>5, $<number>6, $<number>7);
588 int direction = $<number>5;
590 int ppt = $<number>7;
591 if ($<number>3 == TOK_SHRINK) {
596 if (con_is_floating(focused)) {
597 printf("floating resize\n");
598 if (direction == TOK_UP) {
599 focused->parent->rect.y -= px;
600 focused->parent->rect.height += px;
601 } else if (direction == TOK_DOWN) {
602 focused->rect.height += px;
603 } else if (direction == TOK_LEFT) {
604 focused->rect.x -= px;
605 focused->rect.width += px;
607 focused->rect.width += px;
610 LOG("tiling resize\n");
611 /* get the default percentage */
614 TAILQ_FOREACH(other, &(focused->parent->nodes_head), nodes)
616 LOG("ins. %d children\n", children);
617 double percentage = 1.0 / children;
618 LOG("default percentage = %f\n", percentage);
620 if (direction == TOK_UP || direction == TOK_LEFT) {
621 other = TAILQ_PREV(focused, nodes_head, nodes);
623 other = TAILQ_NEXT(focused, nodes);
625 if (other == TAILQ_END(workspaces)) {
626 LOG("No other container in this direction found, cannot resize.\n");
629 LOG("other->percent = %f\n", other->percent);
630 LOG("focused->percent before = %f\n", focused->percent);
631 if (focused->percent == 0.0)
632 focused->percent = percentage;
633 if (other->percent == 0.0)
634 other->percent = percentage;
635 focused->percent += ((double)ppt / 100.0);
636 other->percent -= ((double)ppt / 100.0);
637 LOG("focused->percent after = %f\n", focused->percent);
638 LOG("other->percent after = %f\n", other->percent);
648 | WHITESPACE NUMBER WHITESPACE TOK_PX
650 $<number>$ = $<number>2;
659 | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT
661 $<number>$ = $<number>4;
666 TOK_GROW { $<number>$ = TOK_GROW; }
667 | TOK_SHRINK { $<number>$ = TOK_SHRINK; }
671 TOK_UP { $<number>$ = TOK_UP; }
672 | TOK_DOWN { $<number>$ = TOK_DOWN; }
673 | TOK_LEFT { $<number>$ = TOK_LEFT; }
674 | TOK_RIGHT { $<number>$ = TOK_RIGHT; }