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