]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Merge branch 'master' into next
[i3/i3] / src / cmdparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  * i3 - an improved dynamic tiling window manager
6  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7  *
8  * cmdparse.y: the parser for commands you send to i3 (or bind on keys)
9  *
10  */
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <float.h>
16
17 #include "all.h"
18
19 /** When the command did not include match criteria (!), we use the currently
20  * focused command. Do not confuse this case with a command which included
21  * criteria but which did not match any windows. This macro has to be called in
22  * every command.
23  */
24 #define HANDLE_EMPTY_MATCH do { \
25     if (match_is_empty(&current_match)) { \
26         owindow *ow = smalloc(sizeof(owindow)); \
27         ow->con = focused; \
28         TAILQ_INIT(&owindows); \
29         TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
30     } \
31 } while (0)
32
33 typedef struct yy_buffer_state *YY_BUFFER_STATE;
34 extern int cmdyylex(struct context *context);
35 extern int cmdyyparse(void);
36 extern int cmdyylex_destroy(void);
37 extern FILE *cmdyyin;
38 YY_BUFFER_STATE cmdyy_scan_string(const char *);
39
40 static struct context *context;
41 static Match current_match;
42
43 /*
44  * Helper data structure for an operation window (window on which the operation
45  * will be performed). Used to build the TAILQ owindows.
46  *
47  */
48 typedef struct owindow {
49     Con *con;
50     TAILQ_ENTRY(owindow) owindows;
51 } owindow;
52 static TAILQ_HEAD(owindows_head, owindow) owindows;
53
54 /* Holds the JSON which will be returned via IPC or NULL for the default return
55  * message */
56 static char *json_output;
57
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. */
61 //int cmdyydebug = 1;
62
63 void cmdyyerror(const char *error_message) {
64     ELOG("\n");
65     ELOG("CMD: %s\n", error_message);
66     ELOG("CMD: in command:\n");
67     ELOG("CMD:   %s\n", context->line_copy);
68     ELOG("CMD:   ");
69     for (int c = 1; c <= context->last_column; c++)
70         if (c >= context->first_column)
71                 printf("^");
72         else printf(" ");
73     printf("\n");
74     ELOG("\n");
75     context->compact_error = sstrdup(error_message);
76 }
77
78 int cmdyywrap() {
79     return 1;
80 }
81
82 char *parse_cmd(const char *new) {
83     json_output = NULL;
84     LOG("COMMAND: *%s*\n", new);
85     cmdyy_scan_string(new);
86
87     match_init(&current_match);
88     context = scalloc(sizeof(struct context));
89     context->filename = "cmd";
90     if (cmdyyparse() != 0) {
91         fprintf(stderr, "Could not parse command\n");
92         sasprintf(&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);
96         free(context);
97         return json_output;
98     }
99     printf("done, json output = %s\n", json_output);
100
101     cmdyylex_destroy();
102     FREE(context->line_copy);
103     FREE(context->compact_error);
104     free(context);
105     return json_output;
106 }
107
108 /*
109  * Returns true if a is definitely greater than b (using the given epsilon)
110  *
111  */
112 bool definitelyGreaterThan(float a, float b, float epsilon) {
113     return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
114 }
115
116 %}
117
118 %error-verbose
119 %lex-param { struct context *context }
120
121 %union {
122     char *string;
123     char chr;
124     int number;
125 }
126
127 %token              TOK_EXEC            "exec"
128 %token              TOK_EXIT            "exit"
129 %token              TOK_RELOAD          "reload"
130 %token              TOK_RESTART         "restart"
131 %token              TOK_KILL            "kill"
132 %token              TOK_WINDOW          "window"
133 %token              TOK_CLIENT          "client"
134 %token              TOK_FULLSCREEN      "fullscreen"
135 %token              TOK_GLOBAL          "global"
136 %token              TOK_LAYOUT          "layout"
137 %token              TOK_DEFAULT         "default"
138 %token              TOK_STACKED         "stacked"
139 %token              TOK_TABBED          "tabbed"
140 %token              TOK_BORDER          "border"
141 %token              TOK_NORMAL          "normal"
142 %token              TOK_NONE            "none"
143 %token              TOK_1PIXEL          "1pixel"
144 %token              TOK_MODE            "mode"
145 %token              TOK_TILING          "tiling"
146 %token              TOK_FLOATING        "floating"
147 %token              TOK_MODE_TOGGLE     "mode_toggle"
148 %token              TOK_ENABLE          "enable"
149 %token              TOK_DISABLE         "disable"
150 %token              TOK_WORKSPACE       "workspace"
151 %token              TOK_OUTPUT          "output"
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_SCRATCHPAD      "scratchpad"
159 %token              TOK_SHOW            "show"
160 %token              TOK_SPLIT           "split"
161 %token              TOK_HORIZONTAL      "horizontal"
162 %token              TOK_VERTICAL        "vertical"
163 %token              TOK_UP              "up"
164 %token              TOK_DOWN            "down"
165 %token              TOK_LEFT            "left"
166 %token              TOK_RIGHT           "right"
167 %token              TOK_PARENT          "parent"
168 %token              TOK_CHILD           "child"
169 %token              TOK_APPEND_LAYOUT   "append_layout"
170 %token              TOK_MARK            "mark"
171 %token              TOK_RESIZE          "resize"
172 %token              TOK_GROW            "grow"
173 %token              TOK_SHRINK          "shrink"
174 %token              TOK_PX              "px"
175 %token              TOK_OR              "or"
176 %token              TOK_PPT             "ppt"
177 %token              TOK_NOP             "nop"
178 %token              TOK_BACK_AND_FORTH  "back_and_forth"
179 %token              TOK_NO_STARTUP_ID   "--no-startup-id"
180
181 %token              TOK_CLASS           "class"
182 %token              TOK_INSTANCE        "instance"
183 %token              TOK_WINDOW_ROLE     "window_role"
184 %token              TOK_ID              "id"
185 %token              TOK_CON_ID          "con_id"
186 %token              TOK_TITLE           "title"
187
188 %token  <string>    STR                 "<string>"
189 %token  <number>    NUMBER              "<number>"
190
191 %type   <number>    direction
192 %type   <number>    split_direction
193 %type   <number>    fullscreen_mode
194 %type   <number>    level
195 %type   <number>    window_mode
196 %type   <number>    boolean
197 %type   <number>    border_style
198 %type   <number>    layout_mode
199 %type   <number>    resize_px
200 %type   <number>    resize_way
201 %type   <number>    resize_tiling
202 %type   <number>    optional_kill_mode
203 %type   <number>    optional_no_startup_id
204
205 %%
206
207 commands:
208     commands ';' command
209     | command
210     {
211         owindow *current;
212
213         printf("single command completely parsed, dropping state...\n");
214         while (!TAILQ_EMPTY(&owindows)) {
215             current = TAILQ_FIRST(&owindows);
216             TAILQ_REMOVE(&owindows, current, owindows);
217             free(current);
218         }
219         match_init(&current_match);
220     }
221     ;
222
223 command:
224     match operations
225     ;
226
227 match:
228     | matchstart criteria matchend
229     {
230         printf("match parsed\n");
231     }
232     ;
233
234 matchstart:
235     '['
236     {
237         printf("start\n");
238         match_init(&current_match);
239         TAILQ_INIT(&owindows);
240         /* copy all_cons */
241         Con *con;
242         TAILQ_FOREACH(con, &all_cons, all_cons) {
243             owindow *ow = smalloc(sizeof(owindow));
244             ow->con = con;
245             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
246         }
247     }
248     ;
249
250 matchend:
251     ']'
252     {
253         owindow *next, *current;
254
255         printf("match specification finished, matching...\n");
256         /* copy the old list head to iterate through it and start with a fresh
257          * list which will contain only matching windows */
258         struct owindows_head old = owindows;
259         TAILQ_INIT(&owindows);
260         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
261             /* make a copy of the next pointer and advance the pointer to the
262              * next element as we are going to invalidate the element’s
263              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
264             current = next;
265             next = TAILQ_NEXT(next, owindows);
266
267             printf("checking if con %p / %s matches\n", current->con, current->con->name);
268             if (current_match.con_id != NULL) {
269                 if (current_match.con_id == current->con) {
270                     printf("matches container!\n");
271                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
272
273                 }
274             } else if (current_match.mark != NULL && current->con->mark != NULL &&
275                        regex_matches(current_match.mark, current->con->mark)) {
276                 printf("match by mark\n");
277                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
278             } else {
279                 if (current->con->window == NULL)
280                     continue;
281                 if (match_matches_window(&current_match, current->con->window)) {
282                     printf("matches window!\n");
283                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
284                 } else {
285                     printf("doesnt match\n");
286                     free(current);
287                 }
288             }
289         }
290
291         TAILQ_FOREACH(current, &owindows, owindows) {
292             printf("matching: %p / %s\n", current->con, current->con->name);
293         }
294
295     }
296     ;
297
298 criteria:
299     criteria criterion
300     | criterion
301     ;
302
303 criterion:
304     TOK_CLASS '=' STR
305     {
306         printf("criteria: class = %s\n", $3);
307         current_match.class = regex_new($3);
308         free($3);
309     }
310     | TOK_INSTANCE '=' STR
311     {
312         printf("criteria: instance = %s\n", $3);
313         current_match.instance = regex_new($3);
314         free($3);
315     }
316     | TOK_WINDOW_ROLE '=' STR
317     {
318         printf("criteria: window_role = %s\n", $3);
319         current_match.role = regex_new($3);
320         free($3);
321     }
322     | TOK_CON_ID '=' STR
323     {
324         printf("criteria: id = %s\n", $3);
325         char *end;
326         long parsed = strtol($3, &end, 10);
327         if (parsed == LONG_MIN ||
328             parsed == LONG_MAX ||
329             parsed < 0 ||
330             (end && *end != '\0')) {
331             ELOG("Could not parse con id \"%s\"\n", $3);
332         } else {
333             current_match.con_id = (Con*)parsed;
334             printf("id as int = %p\n", current_match.con_id);
335         }
336     }
337     | TOK_ID '=' STR
338     {
339         printf("criteria: window id = %s\n", $3);
340         char *end;
341         long parsed = strtol($3, &end, 10);
342         if (parsed == LONG_MIN ||
343             parsed == LONG_MAX ||
344             parsed < 0 ||
345             (end && *end != '\0')) {
346             ELOG("Could not parse window id \"%s\"\n", $3);
347         } else {
348             current_match.id = parsed;
349             printf("window id as int = %d\n", current_match.id);
350         }
351     }
352     | TOK_MARK '=' STR
353     {
354         printf("criteria: mark = %s\n", $3);
355         current_match.mark = regex_new($3);
356         free($3);
357     }
358     | TOK_TITLE '=' STR
359     {
360         printf("criteria: title = %s\n", $3);
361         current_match.title = regex_new($3);
362         free($3);
363     }
364     ;
365
366 operations:
367     operation
368     | operations ',' operation
369     ;
370
371 operation:
372     exec
373     | exit
374     | restart
375     | reload
376     | border
377     | layout
378     | append_layout
379     | move
380     | workspace
381     | focus
382     | kill
383     | open
384     | fullscreen
385     | split
386     | floating
387     | mark
388     | resize
389     | nop
390     | scratchpad
391     | mode
392     ;
393
394 exec:
395     TOK_EXEC optional_no_startup_id STR
396     {
397         char *command = $3;
398         bool no_startup_id = $2;
399
400         printf("should execute %s, no_startup_id = %d\n", command, no_startup_id);
401         start_application(command, no_startup_id);
402         free($3);
403     }
404     ;
405
406 optional_no_startup_id:
407     /* empty */ { $$ = false; }
408     | TOK_NO_STARTUP_ID  { $$ = true; }
409     ;
410
411 exit:
412     TOK_EXIT
413     {
414         printf("exit, bye bye\n");
415         exit(0);
416     }
417     ;
418
419 reload:
420     TOK_RELOAD
421     {
422         printf("reloading\n");
423         kill_configerror_nagbar(false);
424         load_configuration(conn, NULL, true);
425         x_set_i3_atoms();
426         /* Send an IPC event just in case the ws names have changed */
427         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
428     }
429     ;
430
431 restart:
432     TOK_RESTART
433     {
434         printf("restarting i3\n");
435         i3_restart(false);
436     }
437     ;
438
439 focus:
440     TOK_FOCUS
441     {
442         if (focused &&
443             focused->type != CT_WORKSPACE &&
444             focused->fullscreen_mode != CF_NONE) {
445             LOG("Cannot change focus while in fullscreen mode.\n");
446             break;
447         }
448
449         owindow *current;
450
451         if (match_is_empty(&current_match)) {
452             ELOG("You have to specify which window/container should be focused.\n");
453             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
454
455             sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
456                       "specify which window/container should be focused\"}");
457             break;
458         }
459
460         int count = 0;
461         TAILQ_FOREACH(current, &owindows, owindows) {
462             Con *ws = con_get_workspace(current->con);
463             /* If no workspace could be found, this was a dock window.
464              * Just skip it, you cannot focus dock windows. */
465             if (!ws)
466                 continue;
467
468             /* If the container is not on the current workspace,
469              * workspace_show() will switch to a different workspace and (if
470              * enabled) trigger a mouse pointer warp to the currently focused
471              * container (!) on the target workspace.
472              *
473              * Therefore, before calling workspace_show(), we make sure that
474              * 'current' will be focused on the workspace. However, we cannot
475              * just con_focus(current) because then the pointer will not be
476              * warped at all (the code thinks we are already there).
477              *
478              * So we focus 'current' to make it the currently focused window of
479              * the target workspace, then revert focus. */
480             Con *currently_focused = focused;
481             con_focus(current->con);
482             con_focus(currently_focused);
483
484             /* Now switch to the workspace, then focus */
485             workspace_show(ws);
486             LOG("focusing %p / %s\n", current->con, current->con->name);
487             con_focus(current->con);
488             count++;
489         }
490
491         if (count > 1)
492             LOG("WARNING: Your criteria for the focus command matches %d containers, "
493                 "while only exactly one container can be focused at a time.\n", count);
494
495         tree_render();
496     }
497     | TOK_FOCUS direction
498     {
499         if (focused &&
500             focused->type != CT_WORKSPACE &&
501             focused->fullscreen_mode != CF_NONE) {
502             LOG("Cannot change focus while in fullscreen mode.\n");
503             break;
504         }
505
506         int direction = $2;
507         switch (direction) {
508             case TOK_LEFT:
509                 LOG("Focusing left\n");
510                 tree_next('p', HORIZ);
511                 break;
512             case TOK_RIGHT:
513                 LOG("Focusing right\n");
514                 tree_next('n', HORIZ);
515                 break;
516             case TOK_UP:
517                 LOG("Focusing up\n");
518                 tree_next('p', VERT);
519                 break;
520             case TOK_DOWN:
521                 LOG("Focusing down\n");
522                 tree_next('n', VERT);
523                 break;
524             default:
525                 ELOG("Invalid focus direction (%d)\n", direction);
526                 break;
527         }
528
529         tree_render();
530     }
531     | TOK_FOCUS window_mode
532     {
533         if (focused &&
534             focused->type != CT_WORKSPACE &&
535             focused->fullscreen_mode != CF_NONE) {
536             LOG("Cannot change focus while in fullscreen mode.\n");
537             break;
538         }
539
540         printf("should focus: ");
541
542         if ($2 == TOK_TILING)
543             printf("tiling\n");
544         else if ($2 == TOK_FLOATING)
545             printf("floating\n");
546         else printf("mode toggle\n");
547
548         Con *ws = con_get_workspace(focused);
549         Con *current;
550         if (ws != NULL) {
551             int to_focus = $2;
552             if ($2 == TOK_MODE_TOGGLE) {
553                 current = TAILQ_FIRST(&(ws->focus_head));
554                 if (current != NULL && current->type == CT_FLOATING_CON)
555                     to_focus = TOK_TILING;
556                 else to_focus = TOK_FLOATING;
557             }
558             TAILQ_FOREACH(current, &(ws->focus_head), focused) {
559                 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
560                     (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
561                     continue;
562
563                 con_focus(con_descend_focused(current));
564                 break;
565             }
566         }
567
568         tree_render();
569     }
570     | TOK_FOCUS level
571     {
572         if (focused &&
573             focused->type != CT_WORKSPACE &&
574             focused->fullscreen_mode != CF_NONE) {
575             LOG("Cannot change focus while in fullscreen mode.\n");
576             break;
577         }
578
579         if ($2 == TOK_PARENT)
580             level_up();
581         else level_down();
582
583         tree_render();
584     }
585     ;
586
587 window_mode:
588     TOK_TILING        { $$ = TOK_TILING; }
589     | TOK_FLOATING    { $$ = TOK_FLOATING; }
590     | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
591     ;
592
593 level:
594     TOK_PARENT  { $$ = TOK_PARENT; }
595     | TOK_CHILD { $$ = TOK_CHILD;  }
596     ;
597
598 kill:
599     TOK_KILL optional_kill_mode
600     {
601         owindow *current;
602
603         printf("killing!\n");
604         /* check if the match is empty, not if the result is empty */
605         if (match_is_empty(&current_match))
606             tree_close_con($2);
607         else {
608             TAILQ_FOREACH(current, &owindows, owindows) {
609                 printf("matching: %p / %s\n", current->con, current->con->name);
610                 tree_close(current->con, $2, false, false);
611             }
612         }
613
614         tree_render();
615     }
616     ;
617
618 optional_kill_mode:
619     /* empty */             { $$ = KILL_WINDOW; }
620     | TOK_WINDOW { $$ = KILL_WINDOW; }
621     | TOK_CLIENT { $$ = KILL_CLIENT; }
622     ;
623
624 workspace:
625     TOK_WORKSPACE TOK_NEXT
626     {
627         workspace_show(workspace_next());
628         tree_render();
629     }
630     | TOK_WORKSPACE TOK_PREV
631     {
632         workspace_show(workspace_prev());
633         tree_render();
634     }
635     | TOK_WORKSPACE TOK_BACK_AND_FORTH
636     {
637         workspace_back_and_forth();
638         tree_render();
639     }
640     | TOK_WORKSPACE STR
641     {
642         if (strncasecmp($2, "__i3_", strlen("__i3_")) == 0) {
643             printf("You cannot switch to the i3 internal workspaces.\n");
644             break;
645         }
646
647         printf("should switch to workspace %s\n", $2);
648
649         Con *ws = con_get_workspace(focused);
650
651         /* Check if the command wants to switch to the current workspace */
652         if (strcmp(ws->name, $2) == 0) {
653             printf("This workspace is already focused.\n");
654             if (config.workspace_auto_back_and_forth) {
655                 workspace_back_and_forth();
656                 free($2);
657                 tree_render();
658             }
659             break;
660         }
661
662         workspace_show_by_name($2);
663         free($2);
664
665         tree_render();
666     }
667     ;
668
669 open:
670     TOK_OPEN
671     {
672         printf("opening new container\n");
673         Con *con = tree_open_con(NULL, NULL);
674         con_focus(con);
675         sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
676
677         tree_render();
678     }
679     ;
680
681 fullscreen:
682     TOK_FULLSCREEN fullscreen_mode
683     {
684         printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
685         owindow *current;
686
687         HANDLE_EMPTY_MATCH;
688
689         TAILQ_FOREACH(current, &owindows, owindows) {
690             printf("matching: %p / %s\n", current->con, current->con->name);
691             con_toggle_fullscreen(current->con, $2);
692         }
693
694         tree_render();
695     }
696     ;
697
698 fullscreen_mode:
699     /* empty */  { $$ = CF_OUTPUT; }
700     | TOK_GLOBAL { $$ = CF_GLOBAL; }
701     ;
702
703 split:
704     TOK_SPLIT split_direction
705     {
706         /* TODO: use matches */
707         printf("splitting in direction %c\n", $2);
708         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
709
710         tree_render();
711     }
712     ;
713
714 split_direction:
715     TOK_HORIZONTAL  { $$ = 'h'; }
716     | 'h'           { $$ = 'h'; }
717     | TOK_VERTICAL  { $$ = 'v'; }
718     | 'v'           { $$ = 'v'; }
719     ;
720
721 floating:
722     TOK_FLOATING boolean
723     {
724         HANDLE_EMPTY_MATCH;
725
726         owindow *current;
727         TAILQ_FOREACH(current, &owindows, owindows) {
728             printf("matching: %p / %s\n", current->con, current->con->name);
729             if ($2 == TOK_TOGGLE) {
730                 printf("should toggle mode\n");
731                 toggle_floating_mode(current->con, false);
732             } else {
733                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
734                 if ($2 == TOK_ENABLE) {
735                     floating_enable(current->con, false);
736                 } else {
737                     floating_disable(current->con, false);
738                 }
739             }
740         }
741
742         tree_render();
743     }
744     ;
745
746 boolean:
747     TOK_ENABLE    { $$ = TOK_ENABLE; }
748     | TOK_DISABLE { $$ = TOK_DISABLE; }
749     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
750     ;
751
752 border:
753     TOK_BORDER border_style
754     {
755         printf("border style should be changed to %d\n", $2);
756         owindow *current;
757
758         HANDLE_EMPTY_MATCH;
759
760         TAILQ_FOREACH(current, &owindows, owindows) {
761             printf("matching: %p / %s\n", current->con, current->con->name);
762             int border_style = current->con->border_style;
763             if ($2 == TOK_TOGGLE) {
764                 border_style++;
765                 border_style %= 3;
766             } else border_style = $2;
767             con_set_border_style(current->con, border_style);
768         }
769
770         tree_render();
771     }
772     ;
773
774 border_style:
775     TOK_NORMAL      { $$ = BS_NORMAL; }
776     | TOK_NONE      { $$ = BS_NONE; }
777     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
778     | TOK_TOGGLE    { $$ = TOK_TOGGLE; }
779     ;
780
781 move:
782     TOK_MOVE direction resize_px
783     {
784         int direction = $2;
785         int px = $3;
786
787         /* TODO: make 'move' work with criteria. */
788         printf("moving in direction %d\n", direction);
789         if (con_is_floating(focused)) {
790             printf("floating move with %d pixels\n", px);
791             Rect newrect = focused->parent->rect;
792             if (direction == TOK_LEFT) {
793                 newrect.x -= px;
794             } else if (direction == TOK_RIGHT) {
795                 newrect.x += px;
796             } else if (direction == TOK_UP) {
797                 newrect.y -= px;
798             } else if (direction == TOK_DOWN) {
799                 newrect.y += px;
800             }
801             floating_reposition(focused->parent, newrect);
802         } else {
803             tree_move(direction);
804             tree_render();
805         }
806     }
807     | TOK_MOVE TOK_WORKSPACE STR
808     {
809         if (strncasecmp($3, "__i3_", strlen("__i3_")) == 0) {
810             printf("You cannot switch to the i3 internal workspaces.\n");
811             break;
812         }
813
814         owindow *current;
815
816         /* Error out early to not create a non-existing workspace (in
817          * workspace_get()) if we are not actually able to move anything. */
818         if (match_is_empty(&current_match) && focused->type == CT_WORKSPACE)
819             break;
820
821         printf("should move window to workspace %s\n", $3);
822         /* get the workspace */
823         Con *ws = workspace_get($3, NULL);
824         free($3);
825
826         HANDLE_EMPTY_MATCH;
827
828         TAILQ_FOREACH(current, &owindows, owindows) {
829             printf("matching: %p / %s\n", current->con, current->con->name);
830             con_move_to_workspace(current->con, ws, true, false);
831         }
832
833         tree_render();
834     }
835     | TOK_MOVE TOK_WORKSPACE TOK_NEXT
836     {
837         owindow *current;
838
839         /* get the workspace */
840         Con *ws = workspace_next();
841
842         HANDLE_EMPTY_MATCH;
843
844         TAILQ_FOREACH(current, &owindows, owindows) {
845             printf("matching: %p / %s\n", current->con, current->con->name);
846             con_move_to_workspace(current->con, ws, true, false);
847         }
848
849         tree_render();
850     }
851     | TOK_MOVE TOK_WORKSPACE TOK_PREV
852     {
853         owindow *current;
854
855         /* get the workspace */
856         Con *ws = workspace_prev();
857
858         HANDLE_EMPTY_MATCH;
859
860         TAILQ_FOREACH(current, &owindows, owindows) {
861             printf("matching: %p / %s\n", current->con, current->con->name);
862             con_move_to_workspace(current->con, ws, true, false);
863         }
864
865         tree_render();
866     }
867     | TOK_MOVE TOK_OUTPUT STR
868     {
869         owindow *current;
870
871         printf("should move window to output %s\n", $3);
872
873         HANDLE_EMPTY_MATCH;
874
875         /* get the output */
876         Output *current_output = NULL;
877         Output *output;
878
879         TAILQ_FOREACH(current, &owindows, owindows)
880             current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
881
882         assert(current_output != NULL);
883
884         if (strcasecmp($3, "up") == 0)
885             output = get_output_next(D_UP, current_output);
886         else if (strcasecmp($3, "down") == 0)
887             output = get_output_next(D_DOWN, current_output);
888         else if (strcasecmp($3, "left") == 0)
889             output = get_output_next(D_LEFT, current_output);
890         else if (strcasecmp($3, "right") == 0)
891             output = get_output_next(D_RIGHT, current_output);
892         else
893             output = get_output_by_name($3);
894         free($3);
895
896         if (!output) {
897             printf("No such output found.\n");
898             break;
899         }
900
901         /* get visible workspace on output */
902         Con *ws = NULL;
903         GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
904         if (!ws)
905             break;
906
907         TAILQ_FOREACH(current, &owindows, owindows) {
908             printf("matching: %p / %s\n", current->con, current->con->name);
909             con_move_to_workspace(current->con, ws, true, false);
910         }
911
912         tree_render();
913     }
914     | TOK_MOVE TOK_SCRATCHPAD
915     {
916         printf("should move window to scratchpad\n");
917         owindow *current;
918
919         HANDLE_EMPTY_MATCH;
920
921         TAILQ_FOREACH(current, &owindows, owindows) {
922             printf("matching: %p / %s\n", current->con, current->con->name);
923             scratchpad_move(current->con);
924         }
925
926         tree_render();
927     }
928     ;
929
930 append_layout:
931     TOK_APPEND_LAYOUT STR
932     {
933         printf("restoring \"%s\"\n", $2);
934         tree_append_json($2);
935         free($2);
936         tree_render();
937     }
938     ;
939
940 layout:
941     TOK_LAYOUT layout_mode
942     {
943         printf("changing layout to %d\n", $2);
944         owindow *current;
945
946         /* check if the match is empty, not if the result is empty */
947         if (match_is_empty(&current_match))
948             con_set_layout(focused->parent, $2);
949         else {
950             TAILQ_FOREACH(current, &owindows, owindows) {
951                 printf("matching: %p / %s\n", current->con, current->con->name);
952                 con_set_layout(current->con, $2);
953             }
954         }
955
956         tree_render();
957     }
958     ;
959
960 layout_mode:
961     TOK_DEFAULT   { $$ = L_DEFAULT; }
962     | TOK_STACKED { $$ = L_STACKED; }
963     | TOK_TABBED  { $$ = L_TABBED; }
964     ;
965
966 mark:
967     TOK_MARK STR
968     {
969         printf("Clearing all windows which have that mark first\n");
970
971         Con *con;
972         TAILQ_FOREACH(con, &all_cons, all_cons) {
973             if (con->mark && strcmp(con->mark, $2) == 0)
974                 FREE(con->mark);
975         }
976
977         printf("marking window with str %s\n", $2);
978         owindow *current;
979
980         HANDLE_EMPTY_MATCH;
981
982         TAILQ_FOREACH(current, &owindows, owindows) {
983             printf("matching: %p / %s\n", current->con, current->con->name);
984             current->con->mark = $2;
985         }
986
987         tree_render();
988     }
989     ;
990
991 nop:
992     TOK_NOP STR
993     {
994         printf("-------------------------------------------------\n");
995         printf("  NOP: %s\n", $2);
996         printf("-------------------------------------------------\n");
997         free($2);
998     }
999     ;
1000
1001 scratchpad:
1002     TOK_SCRATCHPAD TOK_SHOW
1003     {
1004         printf("should show scratchpad window\n");
1005         owindow *current;
1006
1007         if (match_is_empty(&current_match)) {
1008             scratchpad_show(NULL);
1009         } else {
1010             TAILQ_FOREACH(current, &owindows, owindows) {
1011                 printf("matching: %p / %s\n", current->con, current->con->name);
1012                 scratchpad_show(current->con);
1013             }
1014         }
1015
1016         tree_render();
1017     }
1018     ;
1019
1020
1021 resize:
1022     TOK_RESIZE resize_way direction resize_px resize_tiling
1023     {
1024         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
1025         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
1026         int direction = $3;
1027         int px = $4;
1028         int ppt = $5;
1029         if ($2 == TOK_SHRINK) {
1030             px *= -1;
1031             ppt *= -1;
1032         }
1033
1034         Con *floating_con;
1035         if ((floating_con = con_inside_floating(focused))) {
1036             printf("floating resize\n");
1037             if (direction == TOK_UP) {
1038                 floating_con->rect.y -= px;
1039                 floating_con->rect.height += px;
1040             } else if (direction == TOK_DOWN) {
1041                 floating_con->rect.height += px;
1042             } else if (direction == TOK_LEFT) {
1043                 floating_con->rect.x -= px;
1044                 floating_con->rect.width += px;
1045             } else {
1046                 floating_con->rect.width += px;
1047             }
1048         } else {
1049             LOG("tiling resize\n");
1050             /* get the appropriate current container (skip stacked/tabbed cons) */
1051             Con *current = focused;
1052             while (current->parent->layout == L_STACKED ||
1053                    current->parent->layout == L_TABBED)
1054                 current = current->parent;
1055
1056             /* Then further go up until we find one with the matching orientation. */
1057             orientation_t search_orientation =
1058                 (direction == TOK_LEFT || direction == TOK_RIGHT ? HORIZ : VERT);
1059
1060             while (current->type != CT_WORKSPACE &&
1061                    current->type != CT_FLOATING_CON &&
1062                    current->parent->orientation != search_orientation)
1063                 current = current->parent;
1064
1065             /* get the default percentage */
1066             int children = con_num_children(current->parent);
1067             Con *other;
1068             LOG("ins. %d children\n", children);
1069             double percentage = 1.0 / children;
1070             LOG("default percentage = %f\n", percentage);
1071
1072             orientation_t orientation = current->parent->orientation;
1073
1074             if ((orientation == HORIZ &&
1075                  (direction == TOK_UP || direction == TOK_DOWN)) ||
1076                 (orientation == VERT &&
1077                  (direction == TOK_LEFT || direction == TOK_RIGHT))) {
1078                 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
1079                     (orientation == HORIZ ? "horizontal" : "vertical"));
1080                 break;
1081             }
1082
1083             if (direction == TOK_UP || direction == TOK_LEFT) {
1084                 other = TAILQ_PREV(current, nodes_head, nodes);
1085             } else {
1086                 other = TAILQ_NEXT(current, nodes);
1087             }
1088             if (other == TAILQ_END(workspaces)) {
1089                 LOG("No other container in this direction found, cannot resize.\n");
1090                 break;
1091             }
1092             LOG("other->percent = %f\n", other->percent);
1093             LOG("current->percent before = %f\n", current->percent);
1094             if (current->percent == 0.0)
1095                 current->percent = percentage;
1096             if (other->percent == 0.0)
1097                 other->percent = percentage;
1098             double new_current_percent = current->percent + ((double)ppt / 100.0);
1099             double new_other_percent = other->percent - ((double)ppt / 100.0);
1100             LOG("new_current_percent = %f\n", new_current_percent);
1101             LOG("new_other_percent = %f\n", new_other_percent);
1102             /* Ensure that the new percentages are positive and greater than
1103              * 0.05 to have a reasonable minimum size. */
1104             if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
1105                 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
1106                 current->percent += ((double)ppt / 100.0);
1107                 other->percent -= ((double)ppt / 100.0);
1108                 LOG("current->percent after = %f\n", current->percent);
1109                 LOG("other->percent after = %f\n", other->percent);
1110             } else {
1111                 LOG("Not resizing, already at minimum size\n");
1112             }
1113         }
1114
1115         tree_render();
1116     }
1117     ;
1118
1119 resize_px:
1120     /* empty */
1121     {
1122         $$ = 10;
1123     }
1124     | NUMBER TOK_PX
1125     {
1126         $$ = $1;
1127     }
1128     ;
1129
1130 resize_tiling:
1131     /* empty */
1132     {
1133         $$ = 10;
1134     }
1135     | TOK_OR NUMBER TOK_PPT
1136     {
1137         $$ = $2;
1138     }
1139     ;
1140
1141 resize_way:
1142     TOK_GROW        { $$ = TOK_GROW; }
1143     | TOK_SHRINK    { $$ = TOK_SHRINK; }
1144     ;
1145
1146 direction:
1147     TOK_UP          { $$ = TOK_UP; }
1148     | TOK_DOWN      { $$ = TOK_DOWN; }
1149     | TOK_LEFT      { $$ = TOK_LEFT; }
1150     | TOK_RIGHT     { $$ = TOK_RIGHT; }
1151     ;
1152
1153 mode:
1154     TOK_MODE STR
1155     {
1156         switch_mode($2);
1157     }
1158     ;