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