From 2d93b29e1f06c23739e347d7601aef88d7b6372c Mon Sep 17 00:00:00 2001 From: karlstav Date: Tue, 21 Jul 2020 22:49:01 +0200 Subject: [PATCH] force ncurses mode if supported when hex colors are set in config instead of exiting with error it is better to force ncurses on. noncurses is now default, but does not support hex colors. so if hex colors are used (like in a gradient) it will be better to force ncurses mode. ncurses might have some performance issues and slight glitches. but it should be ok as long as it is documented in the config file. relates to #368 --- config.c | 27 ++++++++++++++++++--------- example_files/config | 3 ++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/config.c b/config.c index 30a1bf2..e24723f 100644 --- a/config.c +++ b/config.c @@ -52,18 +52,26 @@ void write_errorf(void *err, const char *fmt, ...) { va_end(args); } -int validate_color(char *checkColor, int om, void *err) { +int validate_color(char *checkColor, void *params, void *err) { + struct config_params *p = (struct config_params *)params; struct error_s *error = (struct error_s *)err; int validColor = 0; if (checkColor[0] == '#' && strlen(checkColor) == 7) { // If the output mode is not ncurses, tell the user to use a named colour instead of hex // colours. - if (om != OUTPUT_NCURSES) { - write_errorf( - error, "Only 'ncurses' output method supports HTML colors. Please change " - "the colours or the output method.\nAs of version 0.7.0 ncurses is no longer" - " the default output method\n"); + if (p->om != OUTPUT_NCURSES) { +#ifdef NCURSES + write_errorf(error, + "hex color configured, but ncurses not set. Forcing ncurses mode.\n"); + p->om = OUTPUT_NCURSES; +#else + write_errorf(error, + "Only 'ncurses' output method supports HTML colors " + "(required by gradient). " + "Cava was built without ncurses support, install ncurses(w) dev files " + "and rebuild.\n"); return 0; +#endif } // 0 to 9 and a to f for (int i = 1; checkColor[i]; ++i) { @@ -94,14 +102,14 @@ bool validate_colors(void *params, void *err) { struct error_s *error = (struct error_s *)err; // validate: color - if (!validate_color(p->color, p->om, error)) { + if (!validate_color(p->color, p, error)) { write_errorf(error, "The value for 'foreground' is invalid. It can be either one of the 7 " "named colors or a HTML color of the form '#xxxxxx'.\n"); return false; } // validate: background color - if (!validate_color(p->bcolor, p->om, error)) { + if (!validate_color(p->bcolor, p, error)) { write_errorf(error, "The value for 'background' is invalid. It can be either one of the 7 " "named colors or a HTML color of the form '#xxxxxx'.\n"); return false; @@ -109,7 +117,7 @@ bool validate_colors(void *params, void *err) { if (p->gradient) { for (int i = 0; i < p->gradient_count; i++) { - if (!validate_color(p->gradient_colors[i], p->om, error)) { + if (!validate_color(p->gradient_colors[i], p, error)) { write_errorf( error, "Gradient color %d is invalid. It must be HTML color of the form '#xxxxxx'.\n", @@ -267,6 +275,7 @@ bool validate_config(struct config_params *p, struct error_s *error) { return false; } + // validate: colors if (!validate_colors(p, error)) { return false; } diff --git a/example_files/config b/example_files/config index 463a85d..0402cd7 100644 --- a/example_files/config +++ b/example_files/config @@ -83,7 +83,7 @@ # 'raw' is an 8 or 16 bit (configurable via the 'bit_format' option) data # stream of the bar heights that can be used to send to other applications. # 'raw' defaults to 200 bars, which can be adjusted in the 'bars' option above. -; method = ncurses +; method = noncurses # Visual channels. Can be 'stereo' or 'mono'. # 'stereo' mirrors both channels with low frequencies in center. @@ -116,6 +116,7 @@ # Colors can be one of seven predefined: black, blue, cyan, green, magenta, red, white, yellow. # Or defined by hex code '#xxxxxx' (hex code must be within ''). User defined colors requires # ncurses output method and a terminal that can change color definitions such as Gnome-terminal or rxvt. +# if supported, ncurses mode will be forced on if user defined colors are used. # default is to keep current terminal color ; background = default ; foreground = default