From 565ef78b12069014c2e8b1e73c0763d2acbab5cd Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 30 Jun 2010 19:47:23 +0200 Subject: [PATCH] parser: implement resize command --- src/cmdparse.l | 10 +++++ src/cmdparse.y | 105 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/src/cmdparse.l b/src/cmdparse.l index f085a2a4..3b3aefe2 100644 --- a/src/cmdparse.l +++ b/src/cmdparse.l @@ -109,8 +109,16 @@ vertical { return TOK_VERTICAL; } level { return TOK_LEVEL; } up { return TOK_UP; } down { return TOK_DOWN; } +left { return TOK_LEFT; } +right { return TOK_RIGHT; } before { return TOK_BEFORE; } after { return TOK_AFTER; } +resize { return TOK_RESIZE; } +shrink { return TOK_SHRINK; } +grow { return TOK_GROW; } +px { return TOK_PX; } +or { return TOK_OR; } +ppt { return TOK_PPT; } restore { BEGIN(WANT_WS_STRING); return TOK_RESTORE; } mark { BEGIN(WANT_WS_STRING); return TOK_MARK; } @@ -119,6 +127,8 @@ id { BEGIN(WANT_QSTRING); return TOK_ID; } con_id { BEGIN(WANT_QSTRING); return TOK_CON_ID; } con_mark { BEGIN(WANT_QSTRING); return TOK_MARK; } +[0-9]+ { cmdyylval.number = atoi(yytext); return NUMBER; } + . { return (int)yytext[0]; } <> { diff --git a/src/cmdparse.y b/src/cmdparse.y index 49e0e4d7..9925bd7c 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -121,10 +121,18 @@ void parse_cmd(const char *new) { %token TOK_LEVEL "level" %token TOK_UP "up" %token TOK_DOWN "down" +%token TOK_LEFT "left" +%token TOK_RIGHT "right" %token TOK_AFTER "after" %token TOK_BEFORE "before" %token TOK_RESTORE "restore" %token TOK_MARK "mark" +%token TOK_RESIZE "resize" +%token TOK_GROW "grow" +%token TOK_SHRINK "shrink" +%token TOK_PX "px" +%token TOK_OR "or" +%token TOK_PPT "ppt" %token TOK_CLASS "class" %token TOK_ID "id" @@ -132,6 +140,7 @@ void parse_cmd(const char *new) { %token WHITESPACE "" %token STR "" +%token NUMBER "" %% @@ -270,7 +279,6 @@ operation: | restart | reload /* - | mark | border */ | layout | restore @@ -287,6 +295,7 @@ operation: | mode | level | mark + | resize ; exec: @@ -545,3 +554,97 @@ mark: free($3); } ; + +resize: + TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling + { + /* resize [ px] [or ppt] */ + printf("resizing in way %d, direction %d, px %d or ppt %d\n", $3, $5, $6, $7); + int direction = $5; + int px = $6; + int ppt = $7; + if ($3 == TOK_SHRINK) { + px *= -1; + ppt *= -1; + } + + if (con_is_floating(focused)) { + printf("floating resize\n"); + if (direction == TOK_UP) { + focused->parent->rect.y -= px; + focused->parent->rect.height += px; + } else if (direction == TOK_DOWN) { + focused->rect.height += px; + } else if (direction == TOK_LEFT) { + focused->rect.x -= px; + focused->rect.width += px; + } else { + focused->rect.width += px; + } + } else { + LOG("tiling resize\n"); + /* get the default percentage */ + int children = 0; + Con *other; + TAILQ_FOREACH(other, &(focused->parent->nodes_head), nodes) + children++; + LOG("ins. %d children\n", children); + double percentage = 1.0 / children; + LOG("default percentage = %f\n", percentage); + + if (direction == TOK_UP || direction == TOK_LEFT) { + other = TAILQ_PREV(focused, nodes_head, nodes); + } else { + other = TAILQ_NEXT(focused, nodes); + } + if (other == TAILQ_END(workspaces)) { + LOG("No other container in this direction found, cannot resize.\n"); + return 0; + } + LOG("other->percent = %f\n", other->percent); + LOG("focused->percent before = %f\n", focused->percent); + if (focused->percent == 0.0) + focused->percent = percentage; + if (other->percent == 0.0) + other->percent = percentage; + focused->percent += ((double)ppt / 100.0); + other->percent -= ((double)ppt / 100.0); + LOG("focused->percent after = %f\n", focused->percent); + LOG("other->percent after = %f\n", other->percent); + } + } + ; + +resize_px: + /* empty */ + { + $$ = 10; + } + | WHITESPACE NUMBER WHITESPACE TOK_PX + { + $$ = $2; + } + ; + +resize_tiling: + /* empty */ + { + $$ = 10; + } + | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT + { + $$ = $4; + } + ; + +resize_way: + TOK_GROW { $$ = TOK_GROW; } + | TOK_SHRINK { $$ = TOK_SHRINK; } + ; + +direction: + TOK_UP { $$ = TOK_UP; } + | TOK_DOWN { $$ = TOK_DOWN; } + | TOK_LEFT { $$ = TOK_LEFT; } + | TOK_RIGHT { $$ = TOK_RIGHT; } + ;