]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Restrict 'resize' command to left/right for horizontal cons, up/down for vertical...
[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                     strcasecmp(current_match.mark, current->con->mark) == 0) {
271                 printf("match by mark\n");
272                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
273
274             } else {
275                 if (current->con->window == NULL)
276                     continue;
277                 if (match_matches_window(&current_match, current->con->window)) {
278                     printf("matches window!\n");
279                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
280                 } else {
281                     printf("doesnt match\n");
282                     free(current);
283                 }
284             }
285         }
286
287         TAILQ_FOREACH(current, &owindows, owindows) {
288             printf("matching: %p / %s\n", current->con, current->con->name);
289         }
290
291     }
292     ;
293
294 criteria:
295     criteria criterion
296     | criterion
297     ;
298
299 criterion:
300     TOK_CLASS '=' STR
301     {
302         printf("criteria: class = %s\n", $3);
303         current_match.class = $3;
304     }
305     | TOK_INSTANCE '=' STR
306     {
307         printf("criteria: instance = %s\n", $3);
308         current_match.instance = $3;
309     }
310     | TOK_CON_ID '=' STR
311     {
312         printf("criteria: id = %s\n", $3);
313         char *end;
314         long parsed = strtol($3, &end, 10);
315         if (parsed == LONG_MIN ||
316             parsed == LONG_MAX ||
317             parsed < 0 ||
318             (end && *end != '\0')) {
319             ELOG("Could not parse con id \"%s\"\n", $3);
320         } else {
321             current_match.con_id = (Con*)parsed;
322             printf("id as int = %p\n", current_match.con_id);
323         }
324     }
325     | TOK_ID '=' STR
326     {
327         printf("criteria: window id = %s\n", $3);
328         char *end;
329         long parsed = strtol($3, &end, 10);
330         if (parsed == LONG_MIN ||
331             parsed == LONG_MAX ||
332             parsed < 0 ||
333             (end && *end != '\0')) {
334             ELOG("Could not parse window id \"%s\"\n", $3);
335         } else {
336             current_match.id = parsed;
337             printf("window id as int = %d\n", current_match.id);
338         }
339     }
340     | TOK_MARK '=' STR
341     {
342         printf("criteria: mark = %s\n", $3);
343         current_match.mark = $3;
344     }
345     | TOK_TITLE '=' STR
346     {
347         printf("criteria: title = %s\n", $3);
348         current_match.title = $3;
349     }
350     ;
351
352 operations:
353     operation
354     | operations ',' operation
355     ;
356
357 operation:
358     exec
359     | exit
360     | restart
361     | reload
362     | border
363     | layout
364     | append_layout
365     | move
366     | workspace
367     | focus
368     | kill
369     | open
370     | fullscreen
371     | split
372     | floating
373     | mark
374     | resize
375     | nop
376     | mode
377     ;
378
379 exec:
380     TOK_EXEC STR
381     {
382         printf("should execute %s\n", $2);
383         start_application($2);
384         free($2);
385     }
386     ;
387
388 exit:
389     TOK_EXIT
390     {
391         printf("exit, bye bye\n");
392         exit(0);
393     }
394     ;
395
396 reload:
397     TOK_RELOAD
398     {
399         printf("reloading\n");
400         kill_configerror_nagbar(false);
401         load_configuration(conn, NULL, true);
402         x_set_i3_atoms();
403         /* Send an IPC event just in case the ws names have changed */
404         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
405     }
406     ;
407
408 restart:
409     TOK_RESTART
410     {
411         printf("restarting i3\n");
412         i3_restart(false);
413     }
414     ;
415
416 focus:
417     TOK_FOCUS
418     {
419         owindow *current;
420
421         if (match_is_empty(&current_match)) {
422             ELOG("You have to specify which window/container should be focused.\n");
423             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
424
425             asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
426                      "specify which window/container should be focused\"}");
427             break;
428         }
429
430         int count = 0;
431         TAILQ_FOREACH(current, &owindows, owindows) {
432             Con *ws = con_get_workspace(current->con);
433             workspace_show(ws->name);
434             LOG("focusing %p / %s\n", current->con, current->con->name);
435             con_focus(current->con);
436             count++;
437         }
438
439         if (count > 1)
440             LOG("WARNING: Your criteria for the focus command matches %d containers, "
441                 "while only exactly one container can be focused at a time.\n", count);
442
443         tree_render();
444     }
445     | TOK_FOCUS direction
446     {
447         int direction = $2;
448         switch (direction) {
449             case TOK_LEFT:
450                 LOG("Focusing left\n");
451                 tree_next('p', HORIZ);
452                 break;
453             case TOK_RIGHT:
454                 LOG("Focusing right\n");
455                 tree_next('n', HORIZ);
456                 break;
457             case TOK_UP:
458                 LOG("Focusing up\n");
459                 tree_next('p', VERT);
460                 break;
461             case TOK_DOWN:
462                 LOG("Focusing down\n");
463                 tree_next('n', VERT);
464                 break;
465             default:
466                 ELOG("Invalid focus direction (%d)\n", direction);
467                 break;
468         }
469
470         tree_render();
471     }
472     | TOK_FOCUS window_mode
473     {
474         printf("should focus: ");
475
476         if ($2 == TOK_TILING)
477             printf("tiling\n");
478         else if ($2 == TOK_FLOATING)
479             printf("floating\n");
480         else printf("mode toggle\n");
481
482         Con *ws = con_get_workspace(focused);
483         Con *current;
484         if (ws != NULL) {
485             int to_focus = $2;
486             if ($2 == TOK_MODE_TOGGLE) {
487                 current = TAILQ_FIRST(&(ws->focus_head));
488                 if (current != NULL && current->type == CT_FLOATING_CON)
489                     to_focus = TOK_TILING;
490                 else to_focus = TOK_FLOATING;
491             }
492             TAILQ_FOREACH(current, &(ws->focus_head), focused) {
493                 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
494                     (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
495                     continue;
496
497                 con_focus(con_descend_focused(current));
498                 break;
499             }
500         }
501
502         tree_render();
503     }
504     | TOK_FOCUS level
505     {
506         if ($2 == TOK_PARENT)
507             level_up();
508         else level_down();
509
510         tree_render();
511     }
512     ;
513
514 window_mode:
515     TOK_TILING        { $$ = TOK_TILING; }
516     | TOK_FLOATING    { $$ = TOK_FLOATING; }
517     | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
518     ;
519
520 level:
521     TOK_PARENT  { $$ = TOK_PARENT; }
522     | TOK_CHILD { $$ = TOK_CHILD;  }
523     ;
524
525 kill:
526     TOK_KILL optional_kill_mode
527     {
528         owindow *current;
529
530         printf("killing!\n");
531         /* check if the match is empty, not if the result is empty */
532         if (match_is_empty(&current_match))
533             tree_close_con($2);
534         else {
535             TAILQ_FOREACH(current, &owindows, owindows) {
536                 printf("matching: %p / %s\n", current->con, current->con->name);
537                 tree_close(current->con, $2, false);
538             }
539         }
540
541         tree_render();
542     }
543     ;
544
545 optional_kill_mode:
546     /* empty */             { $$ = KILL_WINDOW; }
547     | TOK_WINDOW { $$ = KILL_WINDOW; }
548     | TOK_CLIENT { $$ = KILL_CLIENT; }
549     ;
550
551 workspace:
552     TOK_WORKSPACE TOK_NEXT
553     {
554         workspace_next();
555         tree_render();
556     }
557     | TOK_WORKSPACE TOK_PREV
558     {
559         workspace_prev();
560         tree_render();
561     }
562     | TOK_WORKSPACE STR
563     {
564         printf("should switch to workspace %s\n", $2);
565         workspace_show($2);
566         free($2);
567
568         tree_render();
569     }
570     ;
571
572 open:
573     TOK_OPEN
574     {
575         printf("opening new container\n");
576         Con *con = tree_open_con(NULL, NULL);
577         con_focus(con);
578         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
579
580         tree_render();
581     }
582     ;
583
584 fullscreen:
585     TOK_FULLSCREEN fullscreen_mode
586     {
587         printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
588         owindow *current;
589
590         HANDLE_EMPTY_MATCH;
591
592         TAILQ_FOREACH(current, &owindows, owindows) {
593             printf("matching: %p / %s\n", current->con, current->con->name);
594             con_toggle_fullscreen(current->con, $2);
595         }
596
597         tree_render();
598     }
599     ;
600
601 fullscreen_mode:
602     /* empty */  { $$ = CF_OUTPUT; }
603     | TOK_GLOBAL { $$ = CF_GLOBAL; }
604     ;
605
606 split:
607     TOK_SPLIT split_direction
608     {
609         /* TODO: use matches */
610         printf("splitting in direction %c\n", $2);
611         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
612
613         tree_render();
614     }
615     ;
616
617 split_direction:
618     TOK_HORIZONTAL  { $$ = 'h'; }
619     | 'h'           { $$ = 'h'; }
620     | TOK_VERTICAL  { $$ = 'v'; }
621     | 'v'           { $$ = 'v'; }
622     ;
623
624 floating:
625     TOK_FLOATING boolean
626     {
627         HANDLE_EMPTY_MATCH;
628
629         owindow *current;
630         TAILQ_FOREACH(current, &owindows, owindows) {
631             printf("matching: %p / %s\n", current->con, current->con->name);
632             if ($2 == TOK_TOGGLE) {
633                 printf("should toggle mode\n");
634                 toggle_floating_mode(current->con, false);
635             } else {
636                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
637                 if ($2 == TOK_ENABLE) {
638                     floating_enable(current->con, false);
639                 } else {
640                     floating_disable(current->con, false);
641                 }
642             }
643         }
644
645         tree_render();
646     }
647     ;
648
649 boolean:
650     TOK_ENABLE    { $$ = TOK_ENABLE; }
651     | TOK_DISABLE { $$ = TOK_DISABLE; }
652     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
653     ;
654
655 border:
656     TOK_BORDER border_style
657     {
658         printf("border style should be changed to %d\n", $2);
659         owindow *current;
660
661         HANDLE_EMPTY_MATCH;
662
663         TAILQ_FOREACH(current, &owindows, owindows) {
664             printf("matching: %p / %s\n", current->con, current->con->name);
665             if ($2 == TOK_TOGGLE) {
666                 current->con->border_style++;
667                 current->con->border_style %= 3;
668             } else current->con->border_style = $2;
669         }
670
671         tree_render();
672     }
673     ;
674
675 border_style:
676     TOK_NORMAL      { $$ = BS_NORMAL; }
677     | TOK_NONE      { $$ = BS_NONE; }
678     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
679     | TOK_TOGGLE    { $$ = TOK_TOGGLE; }
680     ;
681
682 move:
683     TOK_MOVE direction
684     {
685         printf("moving in direction %d\n", $2);
686         tree_move($2);
687
688         tree_render();
689     }
690     | TOK_MOVE TOK_WORKSPACE STR
691     {
692         owindow *current;
693
694         printf("should move window to workspace %s\n", $3);
695         /* get the workspace */
696         Con *ws = workspace_get($3, NULL);
697         free($3);
698
699         HANDLE_EMPTY_MATCH;
700
701         TAILQ_FOREACH(current, &owindows, owindows) {
702             printf("matching: %p / %s\n", current->con, current->con->name);
703             con_move_to_workspace(current->con, ws, false);
704         }
705
706         tree_render();
707     }
708     | TOK_MOVE TOK_OUTPUT STR
709     {
710         owindow *current;
711
712         printf("should move window to output %s", $3);
713
714         HANDLE_EMPTY_MATCH;
715
716         /* get the output */
717         Output *current_output = NULL;
718         Output *output;
719
720         TAILQ_FOREACH(current, &owindows, owindows)
721             current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
722
723         assert(current_output != NULL);
724
725         if (strcasecmp($3, "up") == 0)
726             output = get_output_next(D_UP, current_output);
727         else if (strcasecmp($3, "down") == 0)
728             output = get_output_next(D_DOWN, current_output);
729         else if (strcasecmp($3, "left") == 0)
730             output = get_output_next(D_LEFT, current_output);
731         else if (strcasecmp($3, "right") == 0)
732             output = get_output_next(D_RIGHT, current_output);
733         else
734             output = get_output_by_name($3);
735         free($3);
736
737         if (!output)
738             break;
739
740         /* get visible workspace on output */
741         Con *ws = NULL;
742         GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
743         if (!ws)
744             break;
745
746         TAILQ_FOREACH(current, &owindows, owindows) {
747             printf("matching: %p / %s\n", current->con, current->con->name);
748             con_move_to_workspace(current->con, ws, false);
749         }
750
751         tree_render();
752     }
753     ;
754
755 append_layout:
756     TOK_APPEND_LAYOUT STR
757     {
758         printf("restoring \"%s\"\n", $2);
759         tree_append_json($2);
760         free($2);
761         tree_render();
762     }
763     ;
764
765 layout:
766     TOK_LAYOUT layout_mode
767     {
768         printf("changing layout to %d\n", $2);
769         owindow *current;
770
771         /* check if the match is empty, not if the result is empty */
772         if (match_is_empty(&current_match))
773             con_set_layout(focused->parent, $2);
774         else {
775             TAILQ_FOREACH(current, &owindows, owindows) {
776                 printf("matching: %p / %s\n", current->con, current->con->name);
777                 con_set_layout(current->con, $2);
778             }
779         }
780
781         tree_render();
782     }
783     ;
784
785 layout_mode:
786     TOK_DEFAULT   { $$ = L_DEFAULT; }
787     | TOK_STACKED { $$ = L_STACKED; }
788     | TOK_TABBED  { $$ = L_TABBED; }
789     ;
790
791 mark:
792     TOK_MARK STR
793     {
794         printf("marking window with str %s\n", $2);
795         owindow *current;
796
797         HANDLE_EMPTY_MATCH;
798
799         TAILQ_FOREACH(current, &owindows, owindows) {
800             printf("matching: %p / %s\n", current->con, current->con->name);
801             current->con->mark = sstrdup($2);
802         }
803
804         free($<string>2);
805
806         tree_render();
807     }
808     ;
809
810 nop:
811     TOK_NOP STR
812     {
813         printf("-------------------------------------------------\n");
814         printf("  NOP: %s\n", $2);
815         printf("-------------------------------------------------\n");
816         free($2);
817     }
818     ;
819
820 resize:
821     TOK_RESIZE resize_way direction resize_px resize_tiling
822     {
823         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
824         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
825         int direction = $3;
826         int px = $4;
827         int ppt = $5;
828         if ($2 == TOK_SHRINK) {
829             px *= -1;
830             ppt *= -1;
831         }
832
833         if (con_is_floating(focused)) {
834             printf("floating resize\n");
835             if (direction == TOK_UP) {
836                 focused->parent->rect.y -= px;
837                 focused->parent->rect.height += px;
838             } else if (direction == TOK_DOWN) {
839                 focused->parent->rect.height += px;
840             } else if (direction == TOK_LEFT) {
841                 focused->parent->rect.x -= px;
842                 focused->parent->rect.width += px;
843             } else {
844                 focused->parent->rect.width += px;
845             }
846         } else {
847             LOG("tiling resize\n");
848             /* get the appropriate current container (skip stacked/tabbed cons) */
849             Con *current = focused;
850             while (current->parent->layout == L_STACKED ||
851                    current->parent->layout == L_TABBED)
852                 current = current->parent;
853             /* get the default percentage */
854             int children = con_num_children(current->parent);
855             Con *other;
856             LOG("ins. %d children\n", children);
857             double percentage = 1.0 / children;
858             LOG("default percentage = %f\n", percentage);
859
860             orientation_t orientation = current->parent->orientation;
861
862             if ((orientation == HORIZ &&
863                  (direction == TOK_UP || direction == TOK_DOWN)) ||
864                 (orientation == VERT &&
865                  (direction == TOK_LEFT || direction == TOK_RIGHT))) {
866                 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
867                     (orientation == HORIZ ? "horizontal" : "vertical"));
868                 break;
869             }
870
871             if (direction == TOK_UP || direction == TOK_LEFT) {
872                 other = TAILQ_PREV(current, nodes_head, nodes);
873             } else {
874                 other = TAILQ_NEXT(current, nodes);
875             }
876             if (other == TAILQ_END(workspaces)) {
877                 LOG("No other container in this direction found, cannot resize.\n");
878                 break;
879             }
880             LOG("other->percent = %f\n", other->percent);
881             LOG("current->percent before = %f\n", current->percent);
882             if (current->percent == 0.0)
883                 current->percent = percentage;
884             if (other->percent == 0.0)
885                 other->percent = percentage;
886             double new_current_percent = current->percent + ((double)ppt / 100.0);
887             double new_other_percent = other->percent - ((double)ppt / 100.0);
888             LOG("new_current_percent = %f\n", new_current_percent);
889             LOG("new_other_percent = %f\n", new_other_percent);
890             /* Ensure that the new percentages are positive and greater than
891              * 0.05 to have a reasonable minimum size. */
892             if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
893                 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
894                 current->percent += ((double)ppt / 100.0);
895                 other->percent -= ((double)ppt / 100.0);
896                 LOG("current->percent after = %f\n", current->percent);
897                 LOG("other->percent after = %f\n", other->percent);
898             } else {
899                 LOG("Not resizing, already at minimum size\n");
900             }
901         }
902
903         tree_render();
904     }
905     ;
906
907 resize_px:
908     /* empty */
909     {
910         $$ = 10;
911     }
912     | NUMBER TOK_PX
913     {
914         $$ = $1;
915     }
916     ;
917
918 resize_tiling:
919     /* empty */
920     {
921         $$ = 10;
922     }
923     | TOK_OR NUMBER TOK_PPT
924     {
925         $$ = $2;
926     }
927     ;
928
929 resize_way:
930     TOK_GROW        { $$ = TOK_GROW; }
931     | TOK_SHRINK    { $$ = TOK_SHRINK; }
932     ;
933
934 direction:
935     TOK_UP          { $$ = TOK_UP; }
936     | TOK_DOWN      { $$ = TOK_DOWN; }
937     | TOK_LEFT      { $$ = TOK_LEFT; }
938     | TOK_RIGHT     { $$ = TOK_RIGHT; }
939     ;
940
941 mode:
942     TOK_MODE STR
943     {
944         switch_mode($2);
945     }
946     ;