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