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