diff options
Diffstat (limited to 'src/config.c')
-rw-r--r-- | src/config.c | 97 |
1 files changed, 80 insertions, 17 deletions
diff --git a/src/config.c b/src/config.c index 47f964d..7ee29e8 100644 --- a/src/config.c +++ b/src/config.c @@ -17,6 +17,11 @@ /* Anyone with a 10M config file is doing something very wrong */ #define MAX_CONFIG_SIZE (10*1024*1024) +struct uint32_percent { + uint32_t value; + bool percent; +}; + static char *strip(const char *str); static bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const char *option, const char *value); static char *get_config_path(void); @@ -26,7 +31,7 @@ static bool parse_bool(const char *filename, size_t lineno, const char *str, boo static struct color parse_color(const char *filename, size_t lineno, const char *str, bool *err); static uint32_t parse_uint32(const char *filename, size_t lineno, const char *str, bool *err); static int32_t parse_int32(const char *filename, size_t lineno, const char *str, bool *err); -static uint32_t parse_uint32_percent(const char *filename, size_t lineno, const char *str, bool *err, uint32_t max); +static struct uint32_percent parse_uint32_percent(const char *filename, size_t lineno, const char *str, bool *err); /* * Function-like macro. Yuck. @@ -231,6 +236,7 @@ char *strip(const char *str) bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const char *option, const char *value) { bool err = false; + struct uint32_percent percent; if (strcasecmp(option, "anchor") == 0) { tofi->anchor = parse_anchor(filename, lineno, value, &err); } else if (strcasecmp(option, "background-color") == 0) { @@ -270,25 +276,45 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const } else if (strcasecmp(option, "selection-background") == 0) { tofi->window.entry.selection_background_color = parse_color(filename, lineno, value, &err); } else if (strcasecmp(option, "width") == 0) { - tofi->window.width = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.width = percent.value; + tofi->window.width_is_percent = percent.percent; } else if (strcasecmp(option, "height") == 0) { - tofi->window.height = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.height = percent.value; + tofi->window.height_is_percent = percent.percent; } else if (strcasecmp(option, "margin-top") == 0) { - tofi->window.margin_top = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.margin_top = percent.value; + tofi->window.margin_top_is_percent = percent.percent; } else if (strcasecmp(option, "margin-bottom") == 0) { - tofi->window.margin_bottom = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.margin_bottom = percent.value; + tofi->window.margin_bottom_is_percent = percent.percent; } else if (strcasecmp(option, "margin-left") == 0) { - tofi->window.margin_left = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.margin_left = percent.value; + tofi->window.margin_left_is_percent = percent.percent; } else if (strcasecmp(option, "margin-right") == 0) { - tofi->window.margin_right = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.margin_right = percent.value; + tofi->window.margin_right_is_percent = percent.percent; } else if (strcasecmp(option, "padding-top") == 0) { - tofi->window.entry.padding_top = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.entry.padding_top = percent.value; + tofi->window.entry.padding_top_is_percent = percent.percent; } else if (strcasecmp(option, "padding-bottom") == 0) { - tofi->window.entry.padding_bottom = parse_uint32_percent(filename, lineno, value, &err, tofi->output_height); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.entry.padding_bottom = percent.value; + tofi->window.entry.padding_bottom_is_percent = percent.percent; } else if (strcasecmp(option, "padding-left") == 0) { - tofi->window.entry.padding_left = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.entry.padding_left = percent.value; + tofi->window.entry.padding_left_is_percent = percent.percent; } else if (strcasecmp(option, "padding-right") == 0) { - tofi->window.entry.padding_right = parse_uint32_percent(filename, lineno, value, &err, tofi->output_width); + percent = parse_uint32_percent(filename, lineno, value, &err); + tofi->window.entry.padding_right = percent.value; + tofi->window.entry.padding_right_is_percent = percent.percent; } else if (strcasecmp(option, "horizontal") == 0) { tofi->window.entry.horizontal = parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "hide-cursor") == 0) { @@ -304,6 +330,8 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const tofi->window.entry.harfbuzz.disable_hinting = !parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "late-keyboard-init") == 0) { tofi->late_keyboard_init = parse_bool(filename, lineno, value, &err); + } else if (strcasecmp(option, "output") == 0) { + snprintf(tofi->target_output_name, N_ELEM(tofi->target_output_name), "%s", value); } else { PARSE_ERROR(filename, lineno, "Unknown option \"%s\"\n", option); err = true; @@ -311,13 +339,47 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const return !err; } -void apply_option(struct tofi *tofi, const char *option, const char *value) +void config_apply(struct tofi *tofi, const char *option, const char *value) { if (!parse_option(tofi, "", 0, option, value)) { exit(EXIT_FAILURE); }; } +void config_fix_percentages(struct tofi *tofi) +{ + if (tofi->window.width_is_percent) { + tofi->window.width = tofi->window.width * tofi->output_width / 100; + } + if (tofi->window.height_is_percent) { + tofi->window.height = tofi->window.height * tofi->output_height / 100; + } + if (tofi->window.margin_top_is_percent) { + tofi->window.margin_top = tofi->window.margin_top * tofi->output_height / 100; + } + if (tofi->window.margin_bottom_is_percent) { + tofi->window.margin_bottom = tofi->window.margin_bottom * tofi->output_height / 100; + } + if (tofi->window.margin_left_is_percent) { + tofi->window.margin_left = tofi->window.margin_left * tofi->output_width / 100; + } + if (tofi->window.margin_right_is_percent) { + tofi->window.margin_right = tofi->window.margin_right * tofi->output_width / 100; + } + if (tofi->window.entry.padding_top_is_percent) { + tofi->window.entry.padding_top = tofi->window.entry.padding_top * tofi->output_height / 100; + } + if (tofi->window.entry.padding_bottom_is_percent) { + tofi->window.entry.padding_bottom = tofi->window.entry.padding_bottom * tofi->output_height / 100; + } + if (tofi->window.entry.padding_left_is_percent) { + tofi->window.entry.padding_left = tofi->window.entry.padding_left * tofi->output_width / 100; + } + if (tofi->window.entry.padding_right_is_percent) { + tofi->window.entry.padding_right = tofi->window.entry.padding_right * tofi->output_width / 100; + } +} + char *get_config_path() { char *base_dir = getenv("XDG_CONFIG_HOME"); @@ -450,17 +512,18 @@ int32_t parse_int32(const char *filename, size_t lineno, const char *str, bool * return ret; } -uint32_t parse_uint32_percent(const char *filename, size_t lineno, const char *str, bool *err, uint32_t max) +struct uint32_percent parse_uint32_percent(const char *filename, size_t lineno, const char *str, bool *err) { errno = 0; char *endptr; - int32_t ret = strtoul(str, &endptr, 0); + int32_t val = strtoul(str, &endptr, 0); + bool percent = false; if (endptr == str) { PARSE_ERROR(filename, lineno, "Failed to parse \"%s\" as unsigned int.\n", str); if (err) { *err = true; } - } else if (errno || ret < 0) { + } else if (errno || val < 0) { PARSE_ERROR(filename, lineno, "Unsigned int value \"%s\" out of range.\n", str); if (err) { *err = true; @@ -468,8 +531,8 @@ uint32_t parse_uint32_percent(const char *filename, size_t lineno, const char *s } if (!err || !*err) { if (*endptr == '%') { - ret = max * ret / 100; + percent = true; } } - return ret; + return (struct uint32_percent){ val, percent }; } |