summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.c97
-rw-r--r--src/config.h3
-rw-r--r--src/entry.h4
-rw-r--r--src/main.c46
-rw-r--r--src/tofi.h6
5 files changed, 108 insertions, 48 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 };
}
diff --git a/src/config.h b/src/config.h
index 65e4f4d..c8db6b4 100644
--- a/src/config.h
+++ b/src/config.h
@@ -4,6 +4,7 @@
#include "tofi.h"
void config_load(struct tofi *tofi, const char *filename);
-void apply_option(struct tofi *tofi, const char *option, const char *value);
+void config_apply(struct tofi *tofi, const char *option, const char *value);
+void config_fix_percentages(struct tofi *tofi);
#endif /* TOFI_CONFIG_H */
diff --git a/src/entry.h b/src/entry.h
index 45865af..2dfa47e 100644
--- a/src/entry.h
+++ b/src/entry.h
@@ -61,6 +61,10 @@ struct entry {
uint32_t padding_bottom;
uint32_t padding_left;
uint32_t padding_right;
+ bool padding_top_is_percent;
+ bool padding_bottom_is_percent;
+ bool padding_left_is_percent;
+ bool padding_right_is_percent;
int32_t selection_background_padding;
uint32_t input_width;
uint32_t border_width;
diff --git a/src/main.c b/src/main.c
index 805f364..32f6e15 100644
--- a/src/main.c
+++ b/src/main.c
@@ -824,33 +824,12 @@ const struct option long_options[] = {
{"drun-launch", required_argument, NULL, 0},
{"drun-print-exec", required_argument, NULL, 0},
{"hint-font", required_argument, NULL, 0},
- {"output", required_argument, NULL, 'o'},
- {"late-keyboard-init", no_argument, NULL, 'k'},
+ {"output", required_argument, NULL, 0},
+ {"late-keyboard-init", optional_argument, NULL, 'k'},
{NULL, 0, NULL, 0}
};
const char *short_options = ":hc:";
-static void parse_early_args(struct tofi *tofi, int argc, char *argv[])
-{
- /* Handle errors ourselves (i.e. ignore them for now). */
- opterr = 0;
-
- /* Just check for help, late-keyboard-init and output */
- optind = 1;
- int opt = getopt_long(argc, argv, short_options, long_options, NULL);
- while (opt != -1) {
- if (opt == 'h') {
- usage();
- exit(EXIT_SUCCESS);
- } else if (opt == 'k') {
- tofi->late_keyboard_init = true;
- } else if (opt == 'o') {
- snprintf(tofi->target_output_name, N_ELEM(tofi->target_output_name), "%s", optarg);
- }
- opt = getopt_long(argc, argv, short_options, long_options, NULL);
- }
-}
-
static void parse_args(struct tofi *tofi, int argc, char *argv[])
{
@@ -894,7 +873,17 @@ static void parse_args(struct tofi *tofi, int argc, char *argv[])
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
while (opt != -1) {
if (opt == 0) {
- apply_option(tofi, long_options[option_index].name, optarg);
+ config_apply(tofi, long_options[option_index].name, optarg);
+ } else if (opt == 'k') {
+ /*
+ * Backwards compatibility for --late-keyboard-init not
+ * taking an argument.
+ */
+ if (optarg) {
+ config_apply(tofi, long_options[option_index].name, optarg);
+ } else {
+ tofi->late_keyboard_init = true;
+ }
}
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
}
@@ -983,7 +972,7 @@ int main(int argc, char *argv[])
};
wl_list_init(&tofi.output_list);
- parse_early_args(&tofi, argc, argv);
+ parse_args(&tofi, argc, argv);
/*
* Initial Wayland & XKB setup.
@@ -1148,11 +1137,8 @@ int main(int argc, char *argv[])
log_debug("Selected output %s.\n", el->name);
}
- /*
- * We can now parse our arguments and config file, as we know the
- * output size required for specifying window sizes in percent.
- */
- parse_args(&tofi, argc, argv);
+ /* We can now calculate any percentages, as we know the output size. */
+ config_fix_percentages(&tofi);
/* Scale fonts to the correct size. */
tofi.window.entry.font_size *= tofi.window.scale;
diff --git a/src/tofi.h b/src/tofi.h
index bb677a0..1771ecd 100644
--- a/src/tofi.h
+++ b/src/tofi.h
@@ -60,6 +60,12 @@ struct tofi {
int32_t margin_bottom;
int32_t margin_left;
int32_t margin_right;
+ bool width_is_percent;
+ bool height_is_percent;
+ bool margin_top_is_percent;
+ bool margin_bottom_is_percent;
+ bool margin_left_is_percent;
+ bool margin_right_is_percent;
} window;
struct {
uint32_t rate;