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