1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

#1802: Calc broken fix (#1803)

* [Theme] First move to double internal calculations.

* [Theme] Allow float numbers.

* [Theme] Fix unary - and tighten the grammar parser.

* [Theme] Rename % to modulo to fix compiler.

* [Theme] Dump right modulo syntax.

* [Test] add missing end_test

* [Grammar] Allow negative numbers as property value
This commit is contained in:
Dave Davenport 2023-02-14 15:53:10 +01:00 committed by GitHub
parent ab1dfe7f61
commit 1b1aa37f20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 218 additions and 96 deletions

View file

@ -918,7 +918,7 @@ It supports the following operations:
.IP \(bu 2 .IP \(bu 2
\fB\fC*\fR : Multiply \fB\fC*\fR : Multiply
.IP \(bu 2 .IP \(bu 2
\fB\fC%\fR : Modulo \fB\fCmodulo\fR : Modulo
.IP \(bu 2 .IP \(bu 2
\fB\fCmin\fR : Minimum of lvalue or rvalue; \fB\fCmin\fR : Minimum of lvalue or rvalue;
.IP \(bu 2 .IP \(bu 2
@ -1615,7 +1615,7 @@ property.
These cannot be changed using the \fB\fCchildren\fR property. These cannot be changed using the \fB\fCchildren\fR property.
.PP .PP
Each entries displayed by listview are captured by a \fB\fCbox\fR called \fB\fCelement\fR\&. Each Entry displayed by listview is captured by a \fB\fCbox\fR called \fB\fCelement\fR\&.
An \fB\fCelement\fR widget can contain the following special child widgets: An \fB\fCelement\fR widget can contain the following special child widgets:
.RS .RS
@ -1630,7 +1630,8 @@ An \fB\fCelement\fR widget can contain the following special child widgets:
.PP .PP
By default the \fB\fCelement-icon\fR and \fB\fCelement-text\fR child widgets are added to the By default the \fB\fCelement-icon\fR and \fB\fCelement-text\fR child widgets are added to the
\fB\fCelement\fR\&. This can be modified using the \fB\fCchildren\fR property. \fB\fCelement\fR\&. This can be modified using the \fB\fCchildren\fR property or the
\fB\fC[no]-show-icons\fR option.
.PP .PP
A child added with another name is seen as a \fB\fCbox\fR, this can be used as dynamic A child added with another name is seen as a \fB\fCbox\fR, this can be used as dynamic

View file

@ -577,7 +577,7 @@ It supports the following operations:
* `-` : Subtract * `-` : Subtract
* `/` : Divide * `/` : Divide
* `*` : Multiply * `*` : Multiply
* `%` : Modulo * `modulo` : Modulo
* `min` : Minimum of lvalue or rvalue; * `min` : Minimum of lvalue or rvalue;
* `max` : Maximum of lvalue or rvalue; * `max` : Maximum of lvalue or rvalue;
* `floor` : Round down lvalue to the next multiple of rvalue * `floor` : Round down lvalue to the next multiple of rvalue

View file

@ -184,8 +184,8 @@ STRING \"{UANYN}*\"
STRING_LIST \"{UANYNP}*\" STRING_LIST \"{UANYNP}*\"
CHAR \'({ASCN}|\\\\|\\\'|\\0)\' CHAR \'({ASCN}|\\\\|\\\'|\\0)\'
HEX [[:xdigit:]] HEX [[:xdigit:]]
NUMBER [[:digit:]] NUMBER [[:digit:]]+
PNNUMBER [-+]?[[:digit:]]+ UNARYMIN -
PX (px) PX (px)
MM (mm) MM (mm)
EM (em) EM (em)
@ -199,13 +199,13 @@ ASTERIX \*
ENV $\{[[:alpha:]_][[:alnum:]_]*\} ENV $\{[[:alpha:]_][[:alnum:]_]*\}
MODIFIER_ADD \+ MODIFIER_ADD \+
MODIFIER_SUBTRACT -
MODIFIER_MULTIPLY \* MODIFIER_MULTIPLY \*
MODIFIER_MIN (min) MODIFIER_MIN (min)
MODIFIER_MAX (max) MODIFIER_MAX (max)
MODIFIER_ROUND (round) MODIFIER_ROUND (round)
MODIFIER_FLOOR (floor) MODIFIER_FLOOR (floor)
MODIFIER_CEIL (ceil) MODIFIER_CEIL (ceil)
MODIFIER_MODULO (modulo)
/* Position */ /* Position */
CENTER (?i:center) CENTER (?i:center)
@ -510,8 +510,9 @@ if ( queue == NULL ) {
<SECTION>":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return T_PSEP; } <SECTION>":" { g_queue_push_head ( queue, GINT_TO_POINTER (YY_START) ); BEGIN(PROPERTIES); return T_PSEP; }
<PROPERTIES>";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return T_PCLOSE;} <PROPERTIES>";" { BEGIN(GPOINTER_TO_INT ( g_queue_pop_head ( queue ))); return T_PCLOSE;}
<PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_ENV_VAR_CONTENT,MEDIA_ENV_VAR_DEFAULT>(true|false) { yylval->bval= g_strcmp0(yytext, "true") == 0; return T_BOOLEAN;} <PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_ENV_VAR_CONTENT,MEDIA_ENV_VAR_DEFAULT>(true|false) { yylval->bval= g_strcmp0(yytext, "true") == 0; return T_BOOLEAN;}
<PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_CONTENT>{PNNUMBER}\.{NUMBER}+ { yylval->fval = g_ascii_strtod(yytext, NULL); return T_DOUBLE;} <PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_CONTENT>{NUMBER}\.{NUMBER} { yylval->fval = g_ascii_strtod(yytext, NULL); return T_DOUBLE;}
<PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_CONTENT>{PNNUMBER} { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;} <PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_CONTENT>{NUMBER} { yylval->ival = (int)g_ascii_strtoll(yytext, NULL, 10); return T_INT;}
<PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT,MEDIA_CONTENT>{UNARYMIN} { return T_MIN; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{STRING} { yytext[yyleng-1] = '\0'; yylval->sval = g_strcompress(&yytext[1]); return T_STRING;} <PROPERTIES,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{STRING} { yytext[yyleng-1] = '\0'; yylval->sval = g_strcompress(&yytext[1]); return T_STRING;}
<PROPERTIES_ARRAY>{STRING_LIST} { yytext[yyleng-1] = '\0'; yylval->sval = g_strcompress(&yytext[1]); return T_STRING;} <PROPERTIES_ARRAY>{STRING_LIST} { yytext[yyleng-1] = '\0'; yylval->sval = g_strcompress(&yytext[1]); return T_STRING;}
<PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{CHAR} { yytext[yyleng-1] = '\0'; yylval->cval = g_strcompress(&yytext[1])[0]; return T_CHAR;} <PROPERTIES,PROPERTIES_ARRAY,PROPERTIES_ENV,PROPERTIES_VAR_DEFAULT>{CHAR} { yytext[yyleng-1] = '\0'; yylval->cval = g_strcompress(&yytext[1])[0]; return T_CHAR;}
@ -539,13 +540,13 @@ if ( queue == NULL ) {
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{INHERIT} { return T_INHERIT; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{INHERIT} { return T_INHERIT; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_ADD} { return T_MODIFIER_ADD; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_ADD} { return T_MODIFIER_ADD; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_SUBTRACT} { return T_MODIFIER_SUBTRACT; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MULTIPLY} { return T_MODIFIER_MULTIPLY; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MULTIPLY} { return T_MODIFIER_MULTIPLY; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MIN} { return T_MODIFIER_MIN; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MIN} { return T_MODIFIER_MIN; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MAX} { return T_MODIFIER_MAX; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MAX} { return T_MODIFIER_MAX; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_ROUND} { return T_MODIFIER_ROUND; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_ROUND} { return T_MODIFIER_ROUND; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_FLOOR} { return T_MODIFIER_FLOOR; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_FLOOR} { return T_MODIFIER_FLOOR; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_CEIL} { return T_MODIFIER_CEIL; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_CEIL} { return T_MODIFIER_CEIL; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{MODIFIER_MODULO} { return T_MODIFIER_MODULO; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{CALC} { return T_CALC; } <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{CALC} { return T_CALC; }
<PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{ENV} { <PROPERTIES,PROPERTIES_ENV,PROPERTIES_ARRAY,PROPERTIES_VAR_DEFAULT>{ENV} {

View file

@ -195,7 +195,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%token T_UNIT_MM "mm" %token T_UNIT_MM "mm"
%token T_UNIT_EM "em" %token T_UNIT_EM "em"
%token T_UNIT_CH "ch" %token T_UNIT_CH "ch"
%token T_UNIT_PERCENT "%"
%token T_ANGLE_DEG "Degrees" %token T_ANGLE_DEG "Degrees"
%token T_ANGLE_GRAD "Gradians" %token T_ANGLE_GRAD "Gradians"
@ -231,7 +230,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%token T_PARENT_LEFT "Parent left ('(')" %token T_PARENT_LEFT "Parent left ('(')"
%token T_PARENT_RIGHT "Parent right (')')" %token T_PARENT_RIGHT "Parent right (')')"
%token T_COMMA "comma separator (',')" %token T_COMMA "comma separator (',')"
%token T_OPTIONAL_COMMA "Optional comma separator (',')"
%token T_FORWARD_SLASH "forward slash ('/')" %token T_FORWARD_SLASH "forward slash ('/')"
%token T_PERCENT "Percent sign ('%')" %token T_PERCENT "Percent sign ('%')"
@ -239,8 +237,8 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%token T_LIST_CLOSE "List close (']')" %token T_LIST_CLOSE "List close (']')"
%token T_MODIFIER_ADD "Add ('+')" %token T_MODIFIER_ADD "Add ('+')"
%token T_MODIFIER_SUBTRACT "Subtract ('-')"
%token T_MODIFIER_MULTIPLY "Multiply ('*')" %token T_MODIFIER_MULTIPLY "Multiply ('*')"
%token T_MODIFIER_MODULO "Modulo ('modulo')"
%token T_MODIFIER_MAX "Max ('max')" %token T_MODIFIER_MAX "Max ('max')"
%token T_MODIFIER_MIN "Min ('min')" %token T_MODIFIER_MIN "Min ('min')"
@ -257,7 +255,6 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%token T_NSEP "Name separator (' ' or '.')" %token T_NSEP "Name separator (' ' or '.')"
%token T_SSEP "Selector separator (',')" %token T_SSEP "Selector separator (',')"
%token T_NAME_PREFIX "Element section ('# {name} { ... }')" %token T_NAME_PREFIX "Element section ('# {name} { ... }')"
%token T_WHITESPACE "White space"
%token T_PDEFAULTS "Default settings section ( '* { ... }')" %token T_PDEFAULTS "Default settings section ( '* { ... }')"
%token T_CONFIGURATION "Configuration block" %token T_CONFIGURATION "Configuration block"
%token T_RESET_THEME "Reset Theme" %token T_RESET_THEME "Reset Theme"
@ -266,13 +263,7 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%token T_INHERIT "Inherit" %token T_INHERIT "Inherit"
%token T_MEDIA_WIDTH "Width" %token T_MIN "-"
%token T_MEDIA_HEIGHT "Height"
%token T_MEDIA_MIN "Min"
%token T_MEDIA_MONITOR_ID "Monitor-ID"
%token T_MEDIA_MAX "Max"
%token T_MEDIA_SEP "-"
%token T_VAR_START "var" %token T_VAR_START "var"
@ -313,6 +304,7 @@ static ThemeColor hwb_to_rgb ( double h, double w, double b )
%type <ival> t_property_orientation %type <ival> t_property_orientation
%type <ival> t_property_cursor %type <ival> t_property_cursor
%type <ival> t_name_prefix_optional %type <ival> t_name_prefix_optional
%type <fval> t_property_number
%start t_main %start t_main
%% %%
@ -390,21 +382,7 @@ t_entry_list T_CONFIGURATION T_BOPEN t_config_property_list_optional T_BCLOSE {
g_hash_table_destroy ( $4 ); g_hash_table_destroy ( $4 );
} }
} }
| t_entry_list T_MEDIA T_PARENT_LEFT T_MEDIA_TYPE T_PSEP T_INT T_PARENT_RIGHT T_BOPEN t_entry_list T_BCLOSE { | t_entry_list T_MEDIA T_PARENT_LEFT T_MEDIA_TYPE T_PSEP t_property_number T_PARENT_RIGHT T_BOPEN t_entry_list T_BCLOSE {
gchar *name = g_strdup_printf("@media ( %s: %d )",$4, $6);
ThemeWidget *widget = rofi_theme_find_or_create_name ( $1, name );
widget->set = TRUE;
widget->media = g_slice_new0(ThemeMedia);
widget->media->type = rofi_theme_parse_media_type ( $4 );
widget->media->value = (double)$6;
for ( unsigned int i = 0; i < $9->num_widgets; i++ ) {
ThemeWidget *d = $9->widgets[i];
rofi_theme_parse_merge_widgets(widget, d);
}
g_free ( $4 );
g_free ( name );
}
| t_entry_list T_MEDIA T_PARENT_LEFT T_MEDIA_TYPE T_PSEP T_DOUBLE T_PARENT_RIGHT T_BOPEN t_entry_list T_BCLOSE {
gchar *name = g_strdup_printf("@media ( %s: %f )",$4, $6); gchar *name = g_strdup_printf("@media ( %s: %f )",$4, $6);
ThemeWidget *widget = rofi_theme_find_or_create_name ( $1, name ); ThemeWidget *widget = rofi_theme_find_or_create_name ( $1, name );
widget->set = TRUE; widget->set = TRUE;
@ -577,6 +555,14 @@ t_property_element
$$ = rofi_theme_property_create ( P_DOUBLE ); $$ = rofi_theme_property_create ( P_DOUBLE );
$$->value.f = $1; $$->value.f = $1;
} }
| T_MIN T_INT {
$$ = rofi_theme_property_create ( P_INTEGER );
$$->value.i = -$2;
}
| T_MIN T_DOUBLE {
$$ = rofi_theme_property_create ( P_DOUBLE );
$$->value.f = -$2;
}
| T_STRING { | T_STRING {
$$ = rofi_theme_property_create ( P_STRING ); $$ = rofi_theme_property_create ( P_STRING );
$$->value.s = $1; $$->value.s = $1;
@ -771,8 +757,8 @@ t_property_highlight_style
t_property_distance_zero t_property_distance_zero
: T_INT t_property_line_style { : t_property_number t_property_line_style {
$$.base.distance = (double) $1; $$.base.distance = $1;
$$.base.type = ROFI_PU_PX; $$.base.type = ROFI_PU_PX;
$$.base.left = NULL; $$.base.left = NULL;
$$.base.right = NULL; $$.base.right = NULL;
@ -784,7 +770,7 @@ t_property_distance_zero
/** Distance. */ /** Distance. */
t_property_distance_unit t_property_distance_unit
: T_INT t_property_unit { : t_property_number t_property_unit {
$$ = g_slice_new0(RofiDistanceUnit); $$ = g_slice_new0(RofiDistanceUnit);
$$->distance = (double)$1; $$->distance = (double)$1;
$$->type = $2; $$->type = $2;
@ -792,7 +778,7 @@ t_property_distance_unit
$$->right = NULL; $$->right = NULL;
$$->modtype = ROFI_DISTANCE_MODIFIER_NONE; $$->modtype = ROFI_DISTANCE_MODIFIER_NONE;
} }
| T_INT { | t_property_number {
$$ = g_slice_new0(RofiDistanceUnit); $$ = g_slice_new0(RofiDistanceUnit);
$$->distance = (double)$1; $$->distance = (double)$1;
$$->type = ROFI_PU_PX; $$->type = ROFI_PU_PX;
@ -800,14 +786,6 @@ t_property_distance_unit
$$->right = NULL; $$->right = NULL;
$$->modtype = ROFI_DISTANCE_MODIFIER_NONE; $$->modtype = ROFI_DISTANCE_MODIFIER_NONE;
} }
| T_DOUBLE t_property_unit {
$$ = g_slice_new0(RofiDistanceUnit);
$$->distance = (double)$1;
$$->type = $2;
$$->left = NULL;
$$->right = NULL;
$$->modtype = ROFI_DISTANCE_MODIFIER_NONE;
}
| T_PARENT_LEFT t_property_distance_unit_math3 T_PARENT_RIGHT { | T_PARENT_LEFT t_property_distance_unit_math3 T_PARENT_RIGHT {
$$ = g_slice_new0(RofiDistanceUnit); $$ = g_slice_new0(RofiDistanceUnit);
$$->distance = 0; $$->distance = 0;
@ -834,7 +812,7 @@ t_property_distance_unit_math
$$->right = $3; $$->right = $3;
$$->modtype = ROFI_DISTANCE_MODIFIER_DIVIDE; $$->modtype = ROFI_DISTANCE_MODIFIER_DIVIDE;
} }
| t_property_distance_unit_math T_PERCENT t_property_distance_unit { | t_property_distance_unit_math T_MODIFIER_MODULO t_property_distance_unit {
$$ = g_slice_new0(RofiDistanceUnit); $$ = g_slice_new0(RofiDistanceUnit);
$$->left = $1; $$->left = $1;
$$->right = $3; $$->right = $3;
@ -853,7 +831,7 @@ t_property_distance_unit_math2
$$->right = $3; $$->right = $3;
$$->modtype = ROFI_DISTANCE_MODIFIER_ADD; $$->modtype = ROFI_DISTANCE_MODIFIER_ADD;
} }
| t_property_distance_unit_math2 T_MODIFIER_SUBTRACT t_property_distance_unit_math { | t_property_distance_unit_math2 T_MIN t_property_distance_unit_math {
$$ = g_slice_new0(RofiDistanceUnit); $$ = g_slice_new0(RofiDistanceUnit);
$$->left = $1; $$->left = $1;
$$->right = $3; $$->right = $3;
@ -901,23 +879,14 @@ t_property_distance_unit_math3
t_property_distance t_property_distance
/** Integer unit and line style */ /** Integer unit and line style */
: T_INT t_property_unit t_property_line_style { : t_property_number t_property_unit t_property_line_style {
$$.base.distance = (double)$1; $$.base.distance = $1;
$$.base.type = $2; $$.base.type = $2;
$$.base.left = NULL; $$.base.left = NULL;
$$.base.right = NULL; $$.base.right = NULL;
$$.base.modtype = ROFI_DISTANCE_MODIFIER_NONE; $$.base.modtype = ROFI_DISTANCE_MODIFIER_NONE;
$$.style = $3; $$.style = $3;
} }
/** Double unit and line style */
| T_DOUBLE t_property_unit t_property_line_style {
$$.base.distance = (double)$1;
$$.base.type = $2;
$$.base.modtype = ROFI_DISTANCE_MODIFIER_NONE;
$$.base.left = NULL;
$$.base.right = NULL;
$$.style = $3;
}
| T_CALC T_PARENT_LEFT t_property_distance_unit_math3 T_PARENT_RIGHT t_property_line_style { | T_CALC T_PARENT_LEFT t_property_distance_unit_math3 T_PARENT_RIGHT t_property_line_style {
$$.base.distance = 0; $$.base.distance = 0;
$$.base.type = ROFI_PU_PX; $$.base.type = ROFI_PU_PX;
@ -927,6 +896,11 @@ t_property_distance
$$.style = $5; $$.style = $5;
}; };
t_property_number
: T_INT { $$ = (double) $1; }
| T_DOUBLE { $$ = $1; }
| T_MIN t_property_number { $$ = -(double)$2; }
/** distance unit. px, em, % */ /** distance unit. px, em, % */
t_property_unit t_property_unit
: T_UNIT_PX { $$ = ROFI_PU_PX; } : T_UNIT_PX { $$ = ROFI_PU_PX; }

View file

@ -285,7 +285,7 @@ static void rofi_theme_print_distance_unit(RofiDistanceUnit *unit) {
} else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MULTIPLY) { } else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MULTIPLY) {
fputs(" * ", stdout); fputs(" * ", stdout);
} else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MODULO) { } else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MODULO) {
fputs(" % ", stdout); fputs(" modulo ", stdout);
} else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MIN) { } else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MIN) {
fputs(" min ", stdout); fputs(" min ", stdout);
} else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MAX) { } else if (unit->modtype == ROFI_DISTANCE_MODIFIER_MAX) {
@ -1336,8 +1336,8 @@ RofiHighlightColorStyle rofi_theme_get_highlight(widget *widget,
} }
return rofi_theme_get_highlight_inside(p, widget, property, th); return rofi_theme_get_highlight_inside(p, widget, property, th);
} }
static int get_pixels(RofiDistanceUnit *unit, RofiOrientation ori) { static double get_pixels(RofiDistanceUnit *unit, RofiOrientation ori) {
int val = unit->distance; double val = unit->distance;
if (unit->type == ROFI_PU_EM) { if (unit->type == ROFI_PU_EM) {
val = unit->distance * textbox_get_estimated_char_height(); val = unit->distance * textbox_get_estimated_char_height();
@ -1359,7 +1359,7 @@ static int get_pixels(RofiDistanceUnit *unit, RofiOrientation ori) {
return val; return val;
} }
static int distance_unit_get_pixel(RofiDistanceUnit *unit, static double distance_unit_get_pixel(RofiDistanceUnit *unit,
RofiOrientation ori) { RofiOrientation ori) {
switch (unit->modtype) { switch (unit->modtype) {
case ROFI_DISTANCE_MODIFIER_GROUP: case ROFI_DISTANCE_MODIFIER_GROUP:
@ -1375,45 +1375,45 @@ static int distance_unit_get_pixel(RofiDistanceUnit *unit,
return distance_unit_get_pixel(unit->left, ori) * return distance_unit_get_pixel(unit->left, ori) *
distance_unit_get_pixel(unit->right, ori); distance_unit_get_pixel(unit->right, ori);
case ROFI_DISTANCE_MODIFIER_DIVIDE: { case ROFI_DISTANCE_MODIFIER_DIVIDE: {
int a = distance_unit_get_pixel(unit->left, ori); double a = distance_unit_get_pixel(unit->left, ori);
int b = distance_unit_get_pixel(unit->right, ori); double b = distance_unit_get_pixel(unit->right, ori);
if (b != 0) { if (b != 0) {
return a / b; return a / b;
} }
return a; return a;
} }
case ROFI_DISTANCE_MODIFIER_MODULO: { case ROFI_DISTANCE_MODIFIER_MODULO: {
int a = distance_unit_get_pixel(unit->left, ori); double a = distance_unit_get_pixel(unit->left, ori);
int b = distance_unit_get_pixel(unit->right, ori); double b = distance_unit_get_pixel(unit->right, ori);
if (b != 0) { if (b != 0) {
return a % b; return fmod(a, b);
} }
return 0; return 0;
} }
case ROFI_DISTANCE_MODIFIER_MIN: { case ROFI_DISTANCE_MODIFIER_MIN: {
int a = distance_unit_get_pixel(unit->left, ori); double a = distance_unit_get_pixel(unit->left, ori);
int b = distance_unit_get_pixel(unit->right, ori); double b = distance_unit_get_pixel(unit->right, ori);
return MIN(a, b); return MIN(a, b);
} }
case ROFI_DISTANCE_MODIFIER_MAX: { case ROFI_DISTANCE_MODIFIER_MAX: {
int a = distance_unit_get_pixel(unit->left, ori); double a = distance_unit_get_pixel(unit->left, ori);
int b = distance_unit_get_pixel(unit->right, ori); double b = distance_unit_get_pixel(unit->right, ori);
return MAX(a, b); return MAX(a, b);
} }
case ROFI_DISTANCE_MODIFIER_ROUND: { case ROFI_DISTANCE_MODIFIER_ROUND: {
double a = (double)distance_unit_get_pixel(unit->left, ori); double a = (double)distance_unit_get_pixel(unit->left, ori);
double b = (double)distance_unit_get_pixel(unit->right, ori); double b = (double)distance_unit_get_pixel(unit->right, ori);
return (int)(round(a / b) * b); return (double)(round(a / b) * b);
} }
case ROFI_DISTANCE_MODIFIER_CEIL: { case ROFI_DISTANCE_MODIFIER_CEIL: {
double a = (double)distance_unit_get_pixel(unit->left, ori); double a = (double)distance_unit_get_pixel(unit->left, ori);
double b = (double)distance_unit_get_pixel(unit->right, ori); double b = (double)distance_unit_get_pixel(unit->right, ori);
return (int)(ceil(a / b) * b); return (double)(ceil(a / b) * b);
} }
case ROFI_DISTANCE_MODIFIER_FLOOR: { case ROFI_DISTANCE_MODIFIER_FLOOR: {
double a = (double)distance_unit_get_pixel(unit->left, ori); double a = (double)distance_unit_get_pixel(unit->left, ori);
double b = (double)distance_unit_get_pixel(unit->right, ori); double b = (double)distance_unit_get_pixel(unit->right, ori);
return (int)(floor(a / b) * b); return (double)(floor(a / b) * b);
} }
default: default:
break; break;

View file

@ -1456,7 +1456,7 @@ START_TEST(test_prepare_math_modulo) {
widget wid; widget wid;
wid.name = "window"; wid.name = "window";
wid.state = ""; wid.state = "";
rofi_theme_parse_string("window { width: calc( 255 % 4 % 3 % );}"); rofi_theme_parse_string("window { width: calc( 255 modulo 4 modulo 5 );}");
ck_assert_ptr_nonnull(rofi_theme); ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets ); // ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties); ck_assert_ptr_null(rofi_theme->properties);
@ -1499,6 +1499,143 @@ START_TEST(test_prepare_math_max) {
ck_assert_int_eq(dist, 256); ck_assert_int_eq(dist, 256);
} }
END_TEST END_TEST
START_TEST(test_prepare_math_failure) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc( 1/2 * 500 );}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, 250);
}
END_TEST
START_TEST(test_prepare_math_failure2) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc( -16/2 * 1.5 );}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, -12);
}
END_TEST
START_TEST(test_prepare_math_failure3) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc(10+3);}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, 13);
}
END_TEST
START_TEST(test_prepare_math_failure4) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc(10.0+3.2);}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, 13);
}
END_TEST
START_TEST(test_prepare_math_failure5) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc(10-3);}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, 7);
}
END_TEST
START_TEST(test_prepare_math_failure6) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc(10.0-3.2);}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, 6);
}
END_TEST
START_TEST(test_prepare_math_failure7) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc(-10--3);}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, -7);
}
END_TEST
START_TEST(test_prepare_math_failure8) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: calc(-10.0--3.2);}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, -6);
}
END_TEST
START_TEST(test_prepare_math_failure9) {
widget wid;
wid.name = "window";
wid.state = "";
rofi_theme_parse_string("window { width: -128;}");
ck_assert_ptr_nonnull(rofi_theme);
// ck_assert_ptr_null ( rofi_theme->widgets );
ck_assert_ptr_null(rofi_theme->properties);
ck_assert_ptr_null(rofi_theme->parent);
ck_assert_str_eq(rofi_theme->name, "Root");
RofiDistance l = rofi_theme_get_distance(&wid, "width", 0);
int dist = distance_get_pixel(l, ROFI_ORIENTATION_HORIZONTAL);
ck_assert_int_eq(dist, -128);
}
END_TEST
START_TEST(test_prepare_default) { START_TEST(test_prepare_default) {
rofi_theme_parse_string("@import \"default\""); rofi_theme_parse_string("@import \"default\"");
@ -1739,6 +1876,15 @@ static Suite *theme_parser_suite(void) {
tcase_add_test(tc_prepare_math, test_prepare_math_round); tcase_add_test(tc_prepare_math, test_prepare_math_round);
tcase_add_test(tc_prepare_math, test_prepare_math_min); tcase_add_test(tc_prepare_math, test_prepare_math_min);
tcase_add_test(tc_prepare_math, test_prepare_math_max); tcase_add_test(tc_prepare_math, test_prepare_math_max);
tcase_add_test(tc_prepare_math, test_prepare_math_failure);
tcase_add_test(tc_prepare_math, test_prepare_math_failure2);
tcase_add_test(tc_prepare_math, test_prepare_math_failure3);
tcase_add_test(tc_prepare_math, test_prepare_math_failure4);
tcase_add_test(tc_prepare_math, test_prepare_math_failure5);
tcase_add_test(tc_prepare_math, test_prepare_math_failure6);
tcase_add_test(tc_prepare_math, test_prepare_math_failure7);
tcase_add_test(tc_prepare_math, test_prepare_math_failure8);
tcase_add_test(tc_prepare_math, test_prepare_math_failure9);
suite_add_tcase(s, tc_prepare_math); suite_add_tcase(s, tc_prepare_math);
} }
return s; return s;