]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
normalize file headers across **/*.{h,c}
[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_SPLIT           "split"
159 %token              TOK_HORIZONTAL      "horizontal"
160 %token              TOK_VERTICAL        "vertical"
161 %token              TOK_UP              "up"
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"
172 %token              TOK_PX              "px"
173 %token              TOK_OR              "or"
174 %token              TOK_PPT             "ppt"
175 %token              TOK_NOP             "nop"
176 %token              TOK_BACK_AND_FORTH  "back_and_forth"
177
178 %token              TOK_CLASS           "class"
179 %token              TOK_INSTANCE        "instance"
180 %token              TOK_WINDOW_ROLE     "window_role"
181 %token              TOK_ID              "id"
182 %token              TOK_CON_ID          "con_id"
183 %token              TOK_TITLE           "title"
184
185 %token  <string>    STR                 "<string>"
186 %token  <number>    NUMBER              "<number>"
187
188 %type   <number>    direction
189 %type   <number>    split_direction
190 %type   <number>    fullscreen_mode
191 %type   <number>    level
192 %type   <number>    window_mode
193 %type   <number>    boolean
194 %type   <number>    border_style
195 %type   <number>    layout_mode
196 %type   <number>    resize_px
197 %type   <number>    resize_way
198 %type   <number>    resize_tiling
199 %type   <number>    optional_kill_mode
200
201 %%
202
203 commands:
204     commands ';' command
205     | command
206     {
207         owindow *current;
208
209         printf("single command completely parsed, dropping state...\n");
210         while (!TAILQ_EMPTY(&owindows)) {
211             current = TAILQ_FIRST(&owindows);
212             TAILQ_REMOVE(&owindows, current, owindows);
213             free(current);
214         }
215         match_init(&current_match);
216     }
217     ;
218
219 command:
220     match operations
221     ;
222
223 match:
224     | matchstart criteria matchend
225     {
226         printf("match parsed\n");
227     }
228     ;
229
230 matchstart:
231     '['
232     {
233         printf("start\n");
234         match_init(&current_match);
235         TAILQ_INIT(&owindows);
236         /* copy all_cons */
237         Con *con;
238         TAILQ_FOREACH(con, &all_cons, all_cons) {
239             owindow *ow = smalloc(sizeof(owindow));
240             ow->con = con;
241             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
242         }
243     }
244     ;
245
246 matchend:
247     ']'
248     {
249         owindow *next, *current;
250
251         printf("match specification finished, matching...\n");
252         /* copy the old list head to iterate through it and start with a fresh
253          * list which will contain only matching windows */
254         struct owindows_head old = owindows;
255         TAILQ_INIT(&owindows);
256         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
257             /* make a copy of the next pointer and advance the pointer to the
258              * next element as we are going to invalidate the element’s
259              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
260             current = next;
261             next = TAILQ_NEXT(next, owindows);
262
263             printf("checking if con %p / %s matches\n", current->con, current->con->name);
264             if (current_match.con_id != NULL) {
265                 if (current_match.con_id == current->con) {
266                     printf("matches container!\n");
267                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
268
269                 }
270             } else if (current_match.mark != NULL && current->con->mark != NULL &&
271                        regex_matches(current_match.mark, current->con->mark)) {
272                 printf("match by mark\n");
273                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
274             } else {
275                 if (current->con->window == NULL)
276                     continue;
277                 if (match_matches_window(&current_match, current->con->window)) {
278                     printf("matches window!\n");
279                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
280                 } else {
281                     printf("doesnt match\n");
282                     free(current);
283                 }
284             }
285         }
286
287         TAILQ_FOREACH(current, &owindows, owindows) {
288             printf("matching: %p / %s\n", current->con, current->con->name);
289         }
290
291     }
292     ;
293
294 criteria:
295     criteria criterion
296     | criterion
297     ;
298
299 criterion:
300     TOK_CLASS '=' STR
301     {
302         printf("criteria: class = %s\n", $3);
303         current_match.class = regex_new($3);
304         free($3);
305     }
306     | TOK_INSTANCE '=' STR
307     {
308         printf("criteria: instance = %s\n", $3);
309         current_match.instance = regex_new($3);
310         free($3);
311     }
312     | TOK_WINDOW_ROLE '=' STR
313     {
314         printf("criteria: window_role = %s\n", $3);
315         current_match.role = regex_new($3);
316         free($3);
317     }
318     | TOK_CON_ID '=' STR
319     {
320         printf("criteria: id = %s\n", $3);
321         char *end;
322         long parsed = strtol($3, &end, 10);
323         if (parsed == LONG_MIN ||
324             parsed == LONG_MAX ||
325             parsed < 0 ||
326             (end && *end != '\0')) {
327             ELOG("Could not parse con id \"%s\"\n", $3);
328         } else {
329             current_match.con_id = (Con*)parsed;
330             printf("id as int = %p\n", current_match.con_id);
331         }
332     }
333     | TOK_ID '=' STR
334     {
335         printf("criteria: window id = %s\n", $3);
336         char *end;
337         long parsed = strtol($3, &end, 10);
338         if (parsed == LONG_MIN ||
339             parsed == LONG_MAX ||
340             parsed < 0 ||
341             (end && *end != '\0')) {
342             ELOG("Could not parse window id \"%s\"\n", $3);
343         } else {
344             current_match.id = parsed;
345             printf("window id as int = %d\n", current_match.id);
346         }
347     }
348     | TOK_MARK '=' STR
349     {
350         printf("criteria: mark = %s\n", $3);
351         current_match.mark = regex_new($3);
352         free($3);
353     }
354     | TOK_TITLE '=' STR
355     {
356         printf("criteria: title = %s\n", $3);
357         current_match.title = regex_new($3);
358         free($3);
359     }
360     ;
361
362 operations:
363     operation
364     | operations ',' operation
365     ;
366
367 operation:
368     exec
369     | exit
370     | restart
371     | reload
372     | border
373     | layout
374     | append_layout
375     | move
376     | workspace
377     | focus
378     | kill
379     | open
380     | fullscreen
381     | split
382     | floating
383     | mark
384     | resize
385     | nop
386     | mode
387     ;
388
389 exec:
390     TOK_EXEC STR
391     {
392         printf("should execute %s\n", $2);
393         start_application($2);
394         free($2);
395     }
396     ;
397
398 exit:
399     TOK_EXIT
400     {
401         printf("exit, bye bye\n");
402         exit(0);
403     }
404     ;
405
406 reload:
407     TOK_RELOAD
408     {
409         printf("reloading\n");
410         kill_configerror_nagbar(false);
411         load_configuration(conn, NULL, true);
412         x_set_i3_atoms();
413         /* Send an IPC event just in case the ws names have changed */
414         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
415     }
416     ;
417
418 restart:
419     TOK_RESTART
420     {
421         printf("restarting i3\n");
422         i3_restart(false);
423     }
424     ;
425
426 focus:
427     TOK_FOCUS
428     {
429         owindow *current;
430
431         if (match_is_empty(&current_match)) {
432             ELOG("You have to specify which window/container should be focused.\n");
433             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
434
435             sasprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
436                       "specify which window/container should be focused\"}");
437             break;
438         }
439
440         int count = 0;
441         TAILQ_FOREACH(current, &owindows, owindows) {
442             Con *ws = con_get_workspace(current->con);
443
444             /* If the container is not on the current workspace,
445              * workspace_show() will switch to a different workspace and (if
446              * enabled) trigger a mouse pointer warp to the currently focused
447              * container (!) on the target workspace.
448              *
449              * Therefore, before calling workspace_show(), we make sure that
450              * 'current' will be focused on the workspace. However, we cannot
451              * just con_focus(current) because then the pointer will not be
452              * warped at all (the code thinks we are already there).
453              *
454              * So we focus 'current' to make it the currently focused window of
455              * the target workspace, then revert focus. */
456             Con *currently_focused = focused;
457             con_focus(current->con);
458             con_focus(currently_focused);
459
460             /* Now switch to the workspace, then focus */
461             workspace_show(ws);
462             LOG("focusing %p / %s\n", current->con, current->con->name);
463             con_focus(current->con);
464             count++;
465         }
466
467         if (count > 1)
468             LOG("WARNING: Your criteria for the focus command matches %d containers, "
469                 "while only exactly one container can be focused at a time.\n", count);
470
471         tree_render();
472     }
473     | TOK_FOCUS direction
474     {
475         int direction = $2;
476         switch (direction) {
477             case TOK_LEFT:
478                 LOG("Focusing left\n");
479                 tree_next('p', HORIZ);
480                 break;
481             case TOK_RIGHT:
482                 LOG("Focusing right\n");
483                 tree_next('n', HORIZ);
484                 break;
485             case TOK_UP:
486                 LOG("Focusing up\n");
487                 tree_next('p', VERT);
488                 break;
489             case TOK_DOWN:
490                 LOG("Focusing down\n");
491                 tree_next('n', VERT);
492                 break;
493             default:
494                 ELOG("Invalid focus direction (%d)\n", direction);
495                 break;
496         }
497
498         tree_render();
499     }
500     | TOK_FOCUS window_mode
501     {
502         printf("should focus: ");
503
504         if ($2 == TOK_TILING)
505             printf("tiling\n");
506         else if ($2 == TOK_FLOATING)
507             printf("floating\n");
508         else printf("mode toggle\n");
509
510         Con *ws = con_get_workspace(focused);
511         Con *current;
512         if (ws != NULL) {
513             int to_focus = $2;
514             if ($2 == TOK_MODE_TOGGLE) {
515                 current = TAILQ_FIRST(&(ws->focus_head));
516                 if (current != NULL && current->type == CT_FLOATING_CON)
517                     to_focus = TOK_TILING;
518                 else to_focus = TOK_FLOATING;
519             }
520             TAILQ_FOREACH(current, &(ws->focus_head), focused) {
521                 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
522                     (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
523                     continue;
524
525                 con_focus(con_descend_focused(current));
526                 break;
527             }
528         }
529
530         tree_render();
531     }
532     | TOK_FOCUS level
533     {
534         if ($2 == TOK_PARENT)
535             level_up();
536         else level_down();
537
538         tree_render();
539     }
540     ;
541
542 window_mode:
543     TOK_TILING        { $$ = TOK_TILING; }
544     | TOK_FLOATING    { $$ = TOK_FLOATING; }
545     | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
546     ;
547
548 level:
549     TOK_PARENT  { $$ = TOK_PARENT; }
550     | TOK_CHILD { $$ = TOK_CHILD;  }
551     ;
552
553 kill:
554     TOK_KILL optional_kill_mode
555     {
556         owindow *current;
557
558         printf("killing!\n");
559         /* check if the match is empty, not if the result is empty */
560         if (match_is_empty(&current_match))
561             tree_close_con($2);
562         else {
563             TAILQ_FOREACH(current, &owindows, owindows) {
564                 printf("matching: %p / %s\n", current->con, current->con->name);
565                 tree_close(current->con, $2, false, false);
566             }
567         }
568
569         tree_render();
570     }
571     ;
572
573 optional_kill_mode:
574     /* empty */             { $$ = KILL_WINDOW; }
575     | TOK_WINDOW { $$ = KILL_WINDOW; }
576     | TOK_CLIENT { $$ = KILL_CLIENT; }
577     ;
578
579 workspace:
580     TOK_WORKSPACE TOK_NEXT
581     {
582         workspace_show(workspace_next());
583         tree_render();
584     }
585     | TOK_WORKSPACE TOK_PREV
586     {
587         workspace_show(workspace_prev());
588         tree_render();
589     }
590     | TOK_WORKSPACE TOK_BACK_AND_FORTH
591     {
592         workspace_back_and_forth();
593         tree_render();
594     }
595     | TOK_WORKSPACE STR
596     {
597         printf("should switch to workspace %s\n", $2);
598
599         Con *ws = con_get_workspace(focused);
600
601         /* Check if the command wants to switch to the current workspace */
602         if (strcmp(ws->name, $2) == 0) {
603             printf("This workspace is already focused.\n");
604             if (config.workspace_auto_back_and_forth) {
605                 workspace_back_and_forth();
606                 free($2);
607                 tree_render();
608             }
609             break;
610         }
611
612         workspace_show_by_name($2);
613         free($2);
614
615         tree_render();
616     }
617     ;
618
619 open:
620     TOK_OPEN
621     {
622         printf("opening new container\n");
623         Con *con = tree_open_con(NULL, NULL);
624         con_focus(con);
625         sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
626
627         tree_render();
628     }
629     ;
630
631 fullscreen:
632     TOK_FULLSCREEN fullscreen_mode
633     {
634         printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
635         owindow *current;
636
637         HANDLE_EMPTY_MATCH;
638
639         TAILQ_FOREACH(current, &owindows, owindows) {
640             printf("matching: %p / %s\n", current->con, current->con->name);
641             con_toggle_fullscreen(current->con, $2);
642         }
643
644         tree_render();
645     }
646     ;
647
648 fullscreen_mode:
649     /* empty */  { $$ = CF_OUTPUT; }
650     | TOK_GLOBAL { $$ = CF_GLOBAL; }
651     ;
652
653 split:
654     TOK_SPLIT split_direction
655     {
656         /* TODO: use matches */
657         printf("splitting in direction %c\n", $2);
658         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
659
660         tree_render();
661     }
662     ;
663
664 split_direction:
665     TOK_HORIZONTAL  { $$ = 'h'; }
666     | 'h'           { $$ = 'h'; }
667     | TOK_VERTICAL  { $$ = 'v'; }
668     | 'v'           { $$ = 'v'; }
669     ;
670
671 floating:
672     TOK_FLOATING boolean
673     {
674         HANDLE_EMPTY_MATCH;
675
676         owindow *current;
677         TAILQ_FOREACH(current, &owindows, owindows) {
678             printf("matching: %p / %s\n", current->con, current->con->name);
679             if ($2 == TOK_TOGGLE) {
680                 printf("should toggle mode\n");
681                 toggle_floating_mode(current->con, false);
682             } else {
683                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
684                 if ($2 == TOK_ENABLE) {
685                     floating_enable(current->con, false);
686                 } else {
687                     floating_disable(current->con, false);
688                 }
689             }
690         }
691
692         tree_render();
693     }
694     ;
695
696 boolean:
697     TOK_ENABLE    { $$ = TOK_ENABLE; }
698     | TOK_DISABLE { $$ = TOK_DISABLE; }
699     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
700     ;
701
702 border:
703     TOK_BORDER border_style
704     {
705         printf("border style should be changed to %d\n", $2);
706         owindow *current;
707
708         HANDLE_EMPTY_MATCH;
709
710         TAILQ_FOREACH(current, &owindows, owindows) {
711             printf("matching: %p / %s\n", current->con, current->con->name);
712             if ($2 == TOK_TOGGLE) {
713                 current->con->border_style++;
714                 current->con->border_style %= 3;
715             } else current->con->border_style = $2;
716         }
717
718         tree_render();
719     }
720     ;
721
722 border_style:
723     TOK_NORMAL      { $$ = BS_NORMAL; }
724     | TOK_NONE      { $$ = BS_NONE; }
725     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
726     | TOK_TOGGLE    { $$ = TOK_TOGGLE; }
727     ;
728
729 move:
730     TOK_MOVE direction
731     {
732         printf("moving in direction %d\n", $2);
733         tree_move($2);
734
735         tree_render();
736     }
737     | TOK_MOVE TOK_WORKSPACE STR
738     {
739         owindow *current;
740
741         printf("should move window to workspace %s\n", $3);
742         /* get the workspace */
743         Con *ws = workspace_get($3, NULL);
744         free($3);
745
746         HANDLE_EMPTY_MATCH;
747
748         TAILQ_FOREACH(current, &owindows, owindows) {
749             printf("matching: %p / %s\n", current->con, current->con->name);
750             con_move_to_workspace(current->con, ws, true, false);
751         }
752
753         tree_render();
754     }
755     | TOK_MOVE TOK_WORKSPACE TOK_NEXT
756     {
757         owindow *current;
758
759         /* get the workspace */
760         Con *ws = workspace_next();
761
762         HANDLE_EMPTY_MATCH;
763
764         TAILQ_FOREACH(current, &owindows, owindows) {
765             printf("matching: %p / %s\n", current->con, current->con->name);
766             con_move_to_workspace(current->con, ws, true, false);
767         }
768
769         tree_render();
770     }
771     | TOK_MOVE TOK_WORKSPACE TOK_PREV
772     {
773         owindow *current;
774
775         /* get the workspace */
776         Con *ws = workspace_prev();
777
778         HANDLE_EMPTY_MATCH;
779
780         TAILQ_FOREACH(current, &owindows, owindows) {
781             printf("matching: %p / %s\n", current->con, current->con->name);
782             con_move_to_workspace(current->con, ws, true, false);
783         }
784
785         tree_render();
786     }
787     | TOK_MOVE TOK_OUTPUT STR
788     {
789         owindow *current;
790
791         printf("should move window to output %s", $3);
792
793         HANDLE_EMPTY_MATCH;
794
795         /* get the output */
796         Output *current_output = NULL;
797         Output *output;
798
799         TAILQ_FOREACH(current, &owindows, owindows)
800             current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
801
802         assert(current_output != NULL);
803
804         if (strcasecmp($3, "up") == 0)
805             output = get_output_next(D_UP, current_output);
806         else if (strcasecmp($3, "down") == 0)
807             output = get_output_next(D_DOWN, current_output);
808         else if (strcasecmp($3, "left") == 0)
809             output = get_output_next(D_LEFT, current_output);
810         else if (strcasecmp($3, "right") == 0)
811             output = get_output_next(D_RIGHT, current_output);
812         else
813             output = get_output_by_name($3);
814         free($3);
815
816         if (!output)
817             break;
818
819         /* get visible workspace on output */
820         Con *ws = NULL;
821         GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
822         if (!ws)
823             break;
824
825         TAILQ_FOREACH(current, &owindows, owindows) {
826             printf("matching: %p / %s\n", current->con, current->con->name);
827             con_move_to_workspace(current->con, ws, true, false);
828         }
829
830         tree_render();
831     }
832     ;
833
834 append_layout:
835     TOK_APPEND_LAYOUT STR
836     {
837         printf("restoring \"%s\"\n", $2);
838         tree_append_json($2);
839         free($2);
840         tree_render();
841     }
842     ;
843
844 layout:
845     TOK_LAYOUT layout_mode
846     {
847         printf("changing layout to %d\n", $2);
848         owindow *current;
849
850         /* check if the match is empty, not if the result is empty */
851         if (match_is_empty(&current_match))
852             con_set_layout(focused->parent, $2);
853         else {
854             TAILQ_FOREACH(current, &owindows, owindows) {
855                 printf("matching: %p / %s\n", current->con, current->con->name);
856                 con_set_layout(current->con, $2);
857             }
858         }
859
860         tree_render();
861     }
862     ;
863
864 layout_mode:
865     TOK_DEFAULT   { $$ = L_DEFAULT; }
866     | TOK_STACKED { $$ = L_STACKED; }
867     | TOK_TABBED  { $$ = L_TABBED; }
868     ;
869
870 mark:
871     TOK_MARK STR
872     {
873         printf("marking window with str %s\n", $2);
874         owindow *current;
875
876         HANDLE_EMPTY_MATCH;
877
878         TAILQ_FOREACH(current, &owindows, owindows) {
879             printf("matching: %p / %s\n", current->con, current->con->name);
880             current->con->mark = sstrdup($2);
881         }
882
883         free($<string>2);
884
885         tree_render();
886     }
887     ;
888
889 nop:
890     TOK_NOP STR
891     {
892         printf("-------------------------------------------------\n");
893         printf("  NOP: %s\n", $2);
894         printf("-------------------------------------------------\n");
895         free($2);
896     }
897     ;
898
899 resize:
900     TOK_RESIZE resize_way direction resize_px resize_tiling
901     {
902         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
903         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
904         int direction = $3;
905         int px = $4;
906         int ppt = $5;
907         if ($2 == TOK_SHRINK) {
908             px *= -1;
909             ppt *= -1;
910         }
911
912         if (con_is_floating(focused)) {
913             printf("floating resize\n");
914             if (direction == TOK_UP) {
915                 focused->parent->rect.y -= px;
916                 focused->parent->rect.height += px;
917             } else if (direction == TOK_DOWN) {
918                 focused->parent->rect.height += px;
919             } else if (direction == TOK_LEFT) {
920                 focused->parent->rect.x -= px;
921                 focused->parent->rect.width += px;
922             } else {
923                 focused->parent->rect.width += px;
924             }
925         } else {
926             LOG("tiling resize\n");
927             /* get the appropriate current container (skip stacked/tabbed cons) */
928             Con *current = focused;
929             while (current->parent->layout == L_STACKED ||
930                    current->parent->layout == L_TABBED)
931                 current = current->parent;
932             /* get the default percentage */
933             int children = con_num_children(current->parent);
934             Con *other;
935             LOG("ins. %d children\n", children);
936             double percentage = 1.0 / children;
937             LOG("default percentage = %f\n", percentage);
938
939             orientation_t orientation = current->parent->orientation;
940
941             if ((orientation == HORIZ &&
942                  (direction == TOK_UP || direction == TOK_DOWN)) ||
943                 (orientation == VERT &&
944                  (direction == TOK_LEFT || direction == TOK_RIGHT))) {
945                 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
946                     (orientation == HORIZ ? "horizontal" : "vertical"));
947                 break;
948             }
949
950             if (direction == TOK_UP || direction == TOK_LEFT) {
951                 other = TAILQ_PREV(current, nodes_head, nodes);
952             } else {
953                 other = TAILQ_NEXT(current, nodes);
954             }
955             if (other == TAILQ_END(workspaces)) {
956                 LOG("No other container in this direction found, cannot resize.\n");
957                 break;
958             }
959             LOG("other->percent = %f\n", other->percent);
960             LOG("current->percent before = %f\n", current->percent);
961             if (current->percent == 0.0)
962                 current->percent = percentage;
963             if (other->percent == 0.0)
964                 other->percent = percentage;
965             double new_current_percent = current->percent + ((double)ppt / 100.0);
966             double new_other_percent = other->percent - ((double)ppt / 100.0);
967             LOG("new_current_percent = %f\n", new_current_percent);
968             LOG("new_other_percent = %f\n", new_other_percent);
969             /* Ensure that the new percentages are positive and greater than
970              * 0.05 to have a reasonable minimum size. */
971             if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
972                 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
973                 current->percent += ((double)ppt / 100.0);
974                 other->percent -= ((double)ppt / 100.0);
975                 LOG("current->percent after = %f\n", current->percent);
976                 LOG("other->percent after = %f\n", other->percent);
977             } else {
978                 LOG("Not resizing, already at minimum size\n");
979             }
980         }
981
982         tree_render();
983     }
984     ;
985
986 resize_px:
987     /* empty */
988     {
989         $$ = 10;
990     }
991     | NUMBER TOK_PX
992     {
993         $$ = $1;
994     }
995     ;
996
997 resize_tiling:
998     /* empty */
999     {
1000         $$ = 10;
1001     }
1002     | TOK_OR NUMBER TOK_PPT
1003     {
1004         $$ = $2;
1005     }
1006     ;
1007
1008 resize_way:
1009     TOK_GROW        { $$ = TOK_GROW; }
1010     | TOK_SHRINK    { $$ = TOK_SHRINK; }
1011     ;
1012
1013 direction:
1014     TOK_UP          { $$ = TOK_UP; }
1015     | TOK_DOWN      { $$ = TOK_DOWN; }
1016     | TOK_LEFT      { $$ = TOK_LEFT; }
1017     | TOK_RIGHT     { $$ = TOK_RIGHT; }
1018     ;
1019
1020 mode:
1021     TOK_MODE STR
1022     {
1023         switch_mode($2);
1024     }
1025     ;