]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
bump copyright
[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_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 TOK_OUTPUT STR
532     {
533         owindow *current;
534
535         HANDLE_EMPTY_MATCH;
536
537         /* get the output */
538         Output *current_output = NULL;
539         Output *output;
540
541         TAILQ_FOREACH(current, &owindows, owindows)
542             current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
543         assert(current_output != NULL);
544
545         if (strcasecmp($3, "left") == 0) {
546             output = get_output_next(D_LEFT, current_output);
547             if (!output)
548                 output = get_output_most(D_RIGHT, current_output);
549         } else if (strcasecmp($3, "right") == 0) {
550             output = get_output_next(D_RIGHT, current_output);
551             if (!output)
552                 output = get_output_most(D_LEFT, current_output);
553         } else if (strcasecmp($3, "up") == 0) {
554             output = get_output_next(D_UP, current_output);
555             if (!output)
556                 output = get_output_most(D_DOWN, current_output);
557         } else if (strcasecmp($3, "down") == 0) {
558             output = get_output_next(D_DOWN, current_output);
559             if (!output)
560                 output = get_output_most(D_UP, current_output);
561         } else output = get_output_by_name($3);
562
563         free($3);
564
565         if (!output) {
566             printf("No such output found.\n");
567             break;
568         }
569
570         /* get visible workspace on output */
571         Con *ws = NULL;
572         GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
573         if (!ws)
574             break;
575
576         workspace_show(ws);
577         tree_render();
578     }
579     | TOK_FOCUS window_mode
580     {
581         if (focused &&
582             focused->type != CT_WORKSPACE &&
583             focused->fullscreen_mode != CF_NONE) {
584             LOG("Cannot change focus while in fullscreen mode.\n");
585             break;
586         }
587
588         printf("should focus: ");
589
590         if ($2 == TOK_TILING)
591             printf("tiling\n");
592         else if ($2 == TOK_FLOATING)
593             printf("floating\n");
594         else printf("mode toggle\n");
595
596         Con *ws = con_get_workspace(focused);
597         Con *current;
598         if (ws != NULL) {
599             int to_focus = $2;
600             if ($2 == TOK_MODE_TOGGLE) {
601                 current = TAILQ_FIRST(&(ws->focus_head));
602                 if (current != NULL && current->type == CT_FLOATING_CON)
603                     to_focus = TOK_TILING;
604                 else to_focus = TOK_FLOATING;
605             }
606             TAILQ_FOREACH(current, &(ws->focus_head), focused) {
607                 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
608                     (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
609                     continue;
610
611                 con_focus(con_descend_focused(current));
612                 break;
613             }
614         }
615
616         tree_render();
617     }
618     | TOK_FOCUS level
619     {
620         if (focused &&
621             focused->type != CT_WORKSPACE &&
622             focused->fullscreen_mode != CF_NONE) {
623             LOG("Cannot change focus while in fullscreen mode.\n");
624             break;
625         }
626
627         if ($2 == TOK_PARENT)
628             level_up();
629         else level_down();
630
631         tree_render();
632     }
633     ;
634
635 window_mode:
636     TOK_TILING        { $$ = TOK_TILING; }
637     | TOK_FLOATING    { $$ = TOK_FLOATING; }
638     | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
639     ;
640
641 level:
642     TOK_PARENT  { $$ = TOK_PARENT; }
643     | TOK_CHILD { $$ = TOK_CHILD;  }
644     ;
645
646 kill:
647     TOK_KILL optional_kill_mode
648     {
649         owindow *current;
650
651         printf("killing!\n");
652         /* check if the match is empty, not if the result is empty */
653         if (match_is_empty(&current_match))
654             tree_close_con($2);
655         else {
656             TAILQ_FOREACH(current, &owindows, owindows) {
657                 printf("matching: %p / %s\n", current->con, current->con->name);
658                 tree_close(current->con, $2, false, false);
659             }
660         }
661
662         tree_render();
663     }
664     ;
665
666 optional_kill_mode:
667     /* empty */             { $$ = KILL_WINDOW; }
668     | TOK_WINDOW { $$ = KILL_WINDOW; }
669     | TOK_CLIENT { $$ = KILL_CLIENT; }
670     ;
671
672 workspace:
673     TOK_WORKSPACE TOK_NEXT
674     {
675         workspace_show(workspace_next());
676         tree_render();
677     }
678     | TOK_WORKSPACE TOK_PREV
679     {
680         workspace_show(workspace_prev());
681         tree_render();
682     }
683     | TOK_WORKSPACE TOK_BACK_AND_FORTH
684     {
685         workspace_back_and_forth();
686         tree_render();
687     }
688     | TOK_WORKSPACE STR
689     {
690         if (strncasecmp($2, "__i3_", strlen("__i3_")) == 0) {
691             printf("You cannot switch to the i3 internal workspaces.\n");
692             break;
693         }
694
695         printf("should switch to workspace %s\n", $2);
696
697         Con *ws = con_get_workspace(focused);
698
699         /* Check if the command wants to switch to the current workspace */
700         if (strcmp(ws->name, $2) == 0) {
701             printf("This workspace is already focused.\n");
702             if (config.workspace_auto_back_and_forth) {
703                 workspace_back_and_forth();
704                 free($2);
705                 tree_render();
706             }
707             break;
708         }
709
710         workspace_show_by_name($2);
711         free($2);
712
713         tree_render();
714     }
715     ;
716
717 open:
718     TOK_OPEN
719     {
720         printf("opening new container\n");
721         Con *con = tree_open_con(NULL, NULL);
722         con_focus(con);
723         sasprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
724
725         tree_render();
726     }
727     ;
728
729 fullscreen:
730     TOK_FULLSCREEN fullscreen_mode
731     {
732         printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
733         owindow *current;
734
735         HANDLE_EMPTY_MATCH;
736
737         TAILQ_FOREACH(current, &owindows, owindows) {
738             printf("matching: %p / %s\n", current->con, current->con->name);
739             con_toggle_fullscreen(current->con, $2);
740         }
741
742         tree_render();
743     }
744     ;
745
746 fullscreen_mode:
747     /* empty */  { $$ = CF_OUTPUT; }
748     | TOK_GLOBAL { $$ = CF_GLOBAL; }
749     ;
750
751 split:
752     TOK_SPLIT split_direction
753     {
754         /* TODO: use matches */
755         printf("splitting in direction %c\n", $2);
756         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
757
758         tree_render();
759     }
760     ;
761
762 split_direction:
763     TOK_HORIZONTAL  { $$ = 'h'; }
764     | 'h'           { $$ = 'h'; }
765     | TOK_VERTICAL  { $$ = 'v'; }
766     | 'v'           { $$ = 'v'; }
767     ;
768
769 floating:
770     TOK_FLOATING boolean
771     {
772         HANDLE_EMPTY_MATCH;
773
774         owindow *current;
775         TAILQ_FOREACH(current, &owindows, owindows) {
776             printf("matching: %p / %s\n", current->con, current->con->name);
777             if ($2 == TOK_TOGGLE) {
778                 printf("should toggle mode\n");
779                 toggle_floating_mode(current->con, false);
780             } else {
781                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
782                 if ($2 == TOK_ENABLE) {
783                     floating_enable(current->con, false);
784                 } else {
785                     floating_disable(current->con, false);
786                 }
787             }
788         }
789
790         tree_render();
791     }
792     ;
793
794 boolean:
795     TOK_ENABLE    { $$ = TOK_ENABLE; }
796     | TOK_DISABLE { $$ = TOK_DISABLE; }
797     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
798     ;
799
800 border:
801     TOK_BORDER border_style
802     {
803         printf("border style should be changed to %d\n", $2);
804         owindow *current;
805
806         HANDLE_EMPTY_MATCH;
807
808         TAILQ_FOREACH(current, &owindows, owindows) {
809             printf("matching: %p / %s\n", current->con, current->con->name);
810             int border_style = current->con->border_style;
811             if ($2 == TOK_TOGGLE) {
812                 border_style++;
813                 border_style %= 3;
814             } else border_style = $2;
815             con_set_border_style(current->con, border_style);
816         }
817
818         tree_render();
819     }
820     ;
821
822 border_style:
823     TOK_NORMAL      { $$ = BS_NORMAL; }
824     | TOK_NONE      { $$ = BS_NONE; }
825     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
826     | TOK_TOGGLE    { $$ = TOK_TOGGLE; }
827     ;
828
829 move:
830     TOK_MOVE direction resize_px
831     {
832         int direction = $2;
833         int px = $3;
834
835         /* TODO: make 'move' work with criteria. */
836         printf("moving in direction %d\n", direction);
837         if (con_is_floating(focused)) {
838             printf("floating move with %d pixels\n", px);
839             Rect newrect = focused->parent->rect;
840             if (direction == TOK_LEFT) {
841                 newrect.x -= px;
842             } else if (direction == TOK_RIGHT) {
843                 newrect.x += px;
844             } else if (direction == TOK_UP) {
845                 newrect.y -= px;
846             } else if (direction == TOK_DOWN) {
847                 newrect.y += px;
848             }
849             floating_reposition(focused->parent, newrect);
850         } else {
851             tree_move(direction);
852             tree_render();
853         }
854     }
855     | TOK_MOVE TOK_WORKSPACE STR
856     {
857         if (strncasecmp($3, "__i3_", strlen("__i3_")) == 0) {
858             printf("You cannot switch to the i3 internal workspaces.\n");
859             break;
860         }
861
862         owindow *current;
863
864         /* Error out early to not create a non-existing workspace (in
865          * workspace_get()) if we are not actually able to move anything. */
866         if (match_is_empty(&current_match) && focused->type == CT_WORKSPACE)
867             break;
868
869         printf("should move window to workspace %s\n", $3);
870         /* get the workspace */
871         Con *ws = workspace_get($3, NULL);
872         free($3);
873
874         HANDLE_EMPTY_MATCH;
875
876         TAILQ_FOREACH(current, &owindows, owindows) {
877             printf("matching: %p / %s\n", current->con, current->con->name);
878             con_move_to_workspace(current->con, ws, true, false);
879         }
880
881         tree_render();
882     }
883     | TOK_MOVE TOK_WORKSPACE TOK_NEXT
884     {
885         owindow *current;
886
887         /* get the workspace */
888         Con *ws = workspace_next();
889
890         HANDLE_EMPTY_MATCH;
891
892         TAILQ_FOREACH(current, &owindows, owindows) {
893             printf("matching: %p / %s\n", current->con, current->con->name);
894             con_move_to_workspace(current->con, ws, true, false);
895         }
896
897         tree_render();
898     }
899     | TOK_MOVE TOK_WORKSPACE TOK_PREV
900     {
901         owindow *current;
902
903         /* get the workspace */
904         Con *ws = workspace_prev();
905
906         HANDLE_EMPTY_MATCH;
907
908         TAILQ_FOREACH(current, &owindows, owindows) {
909             printf("matching: %p / %s\n", current->con, current->con->name);
910             con_move_to_workspace(current->con, ws, true, false);
911         }
912
913         tree_render();
914     }
915     | TOK_MOVE TOK_OUTPUT STR
916     {
917         owindow *current;
918
919         printf("should move window to output %s\n", $3);
920
921         HANDLE_EMPTY_MATCH;
922
923         /* get the output */
924         Output *current_output = NULL;
925         Output *output;
926
927         TAILQ_FOREACH(current, &owindows, owindows)
928             current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
929
930         assert(current_output != NULL);
931
932         if (strcasecmp($3, "up") == 0)
933             output = get_output_next(D_UP, current_output);
934         else if (strcasecmp($3, "down") == 0)
935             output = get_output_next(D_DOWN, current_output);
936         else if (strcasecmp($3, "left") == 0)
937             output = get_output_next(D_LEFT, current_output);
938         else if (strcasecmp($3, "right") == 0)
939             output = get_output_next(D_RIGHT, current_output);
940         else
941             output = get_output_by_name($3);
942         free($3);
943
944         if (!output) {
945             printf("No such output found.\n");
946             break;
947         }
948
949         /* get visible workspace on output */
950         Con *ws = NULL;
951         GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
952         if (!ws)
953             break;
954
955         TAILQ_FOREACH(current, &owindows, owindows) {
956             printf("matching: %p / %s\n", current->con, current->con->name);
957             con_move_to_workspace(current->con, ws, true, false);
958         }
959
960         tree_render();
961     }
962     | TOK_MOVE TOK_SCRATCHPAD
963     {
964         printf("should move window to scratchpad\n");
965         owindow *current;
966
967         HANDLE_EMPTY_MATCH;
968
969         TAILQ_FOREACH(current, &owindows, owindows) {
970             printf("matching: %p / %s\n", current->con, current->con->name);
971             scratchpad_move(current->con);
972         }
973
974         tree_render();
975     }
976     ;
977
978 append_layout:
979     TOK_APPEND_LAYOUT STR
980     {
981         printf("restoring \"%s\"\n", $2);
982         tree_append_json($2);
983         free($2);
984         tree_render();
985     }
986     ;
987
988 layout:
989     TOK_LAYOUT layout_mode
990     {
991         printf("changing layout to %d\n", $2);
992         owindow *current;
993
994         /* check if the match is empty, not if the result is empty */
995         if (match_is_empty(&current_match))
996             con_set_layout(focused->parent, $2);
997         else {
998             TAILQ_FOREACH(current, &owindows, owindows) {
999                 printf("matching: %p / %s\n", current->con, current->con->name);
1000                 con_set_layout(current->con, $2);
1001             }
1002         }
1003
1004         tree_render();
1005     }
1006     ;
1007
1008 layout_mode:
1009     TOK_DEFAULT   { $$ = L_DEFAULT; }
1010     | TOK_STACKED { $$ = L_STACKED; }
1011     | TOK_TABBED  { $$ = L_TABBED; }
1012     ;
1013
1014 mark:
1015     TOK_MARK STR
1016     {
1017         printf("Clearing all windows which have that mark first\n");
1018
1019         Con *con;
1020         TAILQ_FOREACH(con, &all_cons, all_cons) {
1021             if (con->mark && strcmp(con->mark, $2) == 0)
1022                 FREE(con->mark);
1023         }
1024
1025         printf("marking window with str %s\n", $2);
1026         owindow *current;
1027
1028         HANDLE_EMPTY_MATCH;
1029
1030         TAILQ_FOREACH(current, &owindows, owindows) {
1031             printf("matching: %p / %s\n", current->con, current->con->name);
1032             current->con->mark = $2;
1033         }
1034
1035         tree_render();
1036     }
1037     ;
1038
1039 nop:
1040     TOK_NOP STR
1041     {
1042         printf("-------------------------------------------------\n");
1043         printf("  NOP: %s\n", $2);
1044         printf("-------------------------------------------------\n");
1045         free($2);
1046     }
1047     ;
1048
1049 scratchpad:
1050     TOK_SCRATCHPAD TOK_SHOW
1051     {
1052         printf("should show scratchpad window\n");
1053         owindow *current;
1054
1055         if (match_is_empty(&current_match)) {
1056             scratchpad_show(NULL);
1057         } else {
1058             TAILQ_FOREACH(current, &owindows, owindows) {
1059                 printf("matching: %p / %s\n", current->con, current->con->name);
1060                 scratchpad_show(current->con);
1061             }
1062         }
1063
1064         tree_render();
1065     }
1066     ;
1067
1068
1069 resize:
1070     TOK_RESIZE resize_way direction resize_px resize_tiling
1071     {
1072         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
1073         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
1074         int direction = $3;
1075         int px = $4;
1076         int ppt = $5;
1077         if ($2 == TOK_SHRINK) {
1078             px *= -1;
1079             ppt *= -1;
1080         }
1081
1082         Con *floating_con;
1083         if ((floating_con = con_inside_floating(focused))) {
1084             printf("floating resize\n");
1085             if (direction == TOK_UP) {
1086                 floating_con->rect.y -= px;
1087                 floating_con->rect.height += px;
1088             } else if (direction == TOK_DOWN) {
1089                 floating_con->rect.height += px;
1090             } else if (direction == TOK_LEFT) {
1091                 floating_con->rect.x -= px;
1092                 floating_con->rect.width += px;
1093             } else {
1094                 floating_con->rect.width += px;
1095             }
1096         } else {
1097             LOG("tiling resize\n");
1098             /* get the appropriate current container (skip stacked/tabbed cons) */
1099             Con *current = focused;
1100             while (current->parent->layout == L_STACKED ||
1101                    current->parent->layout == L_TABBED)
1102                 current = current->parent;
1103
1104             /* Then further go up until we find one with the matching orientation. */
1105             orientation_t search_orientation =
1106                 (direction == TOK_LEFT || direction == TOK_RIGHT ? HORIZ : VERT);
1107
1108             while (current->type != CT_WORKSPACE &&
1109                    current->type != CT_FLOATING_CON &&
1110                    current->parent->orientation != search_orientation)
1111                 current = current->parent;
1112
1113             /* get the default percentage */
1114             int children = con_num_children(current->parent);
1115             Con *other;
1116             LOG("ins. %d children\n", children);
1117             double percentage = 1.0 / children;
1118             LOG("default percentage = %f\n", percentage);
1119
1120             orientation_t orientation = current->parent->orientation;
1121
1122             if ((orientation == HORIZ &&
1123                  (direction == TOK_UP || direction == TOK_DOWN)) ||
1124                 (orientation == VERT &&
1125                  (direction == TOK_LEFT || direction == TOK_RIGHT))) {
1126                 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
1127                     (orientation == HORIZ ? "horizontal" : "vertical"));
1128                 break;
1129             }
1130
1131             if (direction == TOK_UP || direction == TOK_LEFT) {
1132                 other = TAILQ_PREV(current, nodes_head, nodes);
1133             } else {
1134                 other = TAILQ_NEXT(current, nodes);
1135             }
1136             if (other == TAILQ_END(workspaces)) {
1137                 LOG("No other container in this direction found, cannot resize.\n");
1138                 break;
1139             }
1140             LOG("other->percent = %f\n", other->percent);
1141             LOG("current->percent before = %f\n", current->percent);
1142             if (current->percent == 0.0)
1143                 current->percent = percentage;
1144             if (other->percent == 0.0)
1145                 other->percent = percentage;
1146             double new_current_percent = current->percent + ((double)ppt / 100.0);
1147             double new_other_percent = other->percent - ((double)ppt / 100.0);
1148             LOG("new_current_percent = %f\n", new_current_percent);
1149             LOG("new_other_percent = %f\n", new_other_percent);
1150             /* Ensure that the new percentages are positive and greater than
1151              * 0.05 to have a reasonable minimum size. */
1152             if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
1153                 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
1154                 current->percent += ((double)ppt / 100.0);
1155                 other->percent -= ((double)ppt / 100.0);
1156                 LOG("current->percent after = %f\n", current->percent);
1157                 LOG("other->percent after = %f\n", other->percent);
1158             } else {
1159                 LOG("Not resizing, already at minimum size\n");
1160             }
1161         }
1162
1163         tree_render();
1164     }
1165     ;
1166
1167 resize_px:
1168     /* empty */
1169     {
1170         $$ = 10;
1171     }
1172     | NUMBER TOK_PX
1173     {
1174         $$ = $1;
1175     }
1176     ;
1177
1178 resize_tiling:
1179     /* empty */
1180     {
1181         $$ = 10;
1182     }
1183     | TOK_OR NUMBER TOK_PPT
1184     {
1185         $$ = $2;
1186     }
1187     ;
1188
1189 resize_way:
1190     TOK_GROW        { $$ = TOK_GROW; }
1191     | TOK_SHRINK    { $$ = TOK_SHRINK; }
1192     ;
1193
1194 direction:
1195     TOK_UP          { $$ = TOK_UP; }
1196     | TOK_DOWN      { $$ = TOK_DOWN; }
1197     | TOK_LEFT      { $$ = TOK_LEFT; }
1198     | TOK_RIGHT     { $$ = TOK_RIGHT; }
1199     ;
1200
1201 mode:
1202     TOK_MODE STR
1203     {
1204         switch_mode($2);
1205     }
1206     ;