From c701342e078fc9a57baf928cae587ce4ff64f329 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 20:39:53 +0200 Subject: [PATCH 1/8] Add CLI option parsing for "fade-mode" --- src/options.c | 29 ++++++++++++++++++++++++----- src/options.h | 2 ++ src/redshift.h | 10 ++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/options.c b/src/options.c index 33bf623a..4e927e5a 100644 --- a/src/options.c +++ b/src/options.c @@ -320,6 +320,7 @@ options_init(options_t *options) options->provider_args = NULL; options->use_fade = -1; + options->fade_mode = FADE_MODE_NOT_SPECIFIED; options->preserve_gamma = 1; options->mode = PROGRAM_MODE_CONTINUAL; options->verbose = 0; @@ -456,10 +457,26 @@ parse_command_line_option( case 'r': options->use_fade = 0; break; - case 't': - s = strchr(value, ':'); - if (s == NULL) { - fputs(_("Malformed temperature argument.\n"), stderr); + case 'e': + if(strlen(value) == 0) { + fprintf(stderr, _("No value was passed for option -e (fade mode).\nTry \"redshift -h\" for more information.\n")); + return -1; + } else if (strcmp(value, "linear") == 0) { + options->fade_mode = FADE_MODE_LINEAR; + } else if (strcmp(value, "ease-in") == 0) { + options->fade_mode = FADE_MODE_EASE_IN; + } else if (strcmp(value, "ease-out") == 0) { + options->fade_mode = FADE_MODE_EASE_OUT; + } else if (strcmp(value, "ease-in-out") == 0) { + options->fade_mode = FADE_MODE_EASE_IN_OUT; + } else { + fprintf(stderr, _("Value \"%s\" for option -e (fade mode) is not supported.\nTry \"redshift -h\" for more information."), value); + return -1; + } + case 't': + s = strchr(value, ':'); + if (s == NULL) { + fputs(_("Malformed temperature argument.\n"), stderr); fputs(_("Try `-h' for more information.\n"), stderr); return -1; } @@ -495,7 +512,7 @@ options_parse_args( { const char* program_name = argv[0]; int opt; - while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:vVx")) != -1) { + while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:e:vVx")) != -1) { char option = opt; int r = parse_command_line_option( option, optarg, options, program_name, gamma_methods, @@ -676,4 +693,6 @@ options_set_defaults(options_t *options) } if (options->use_fade < 0) options->use_fade = 1; + + if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) options->fade_mode = FADE_MODE_NOT_SPECIFIED; } diff --git a/src/options.h b/src/options.h index 9993a07f..9fdf9b5f 100644 --- a/src/options.h +++ b/src/options.h @@ -34,6 +34,8 @@ typedef struct { int temp_set; /* Whether to fade between large skips in color temperature. */ int use_fade; + /* The fading function to use when transitioning between color temperatures. */ + fade_mode_t fade_mode; /* Whether to preserve gamma ramps if supported by gamma method. */ int preserve_gamma; diff --git a/src/redshift.h b/src/redshift.h index 0282d839..d5f4a8ac 100644 --- a/src/redshift.h +++ b/src/redshift.h @@ -57,6 +57,16 @@ typedef enum { PROGRAM_MODE_MANUAL } program_mode_t; +/* Fade modes. */ +typedef enum { + FADE_MODE_NOT_SPECIFIED, + FADE_MODE_LINEAR, + FADE_MODE_EASE_IN, + FADE_MODE_EASE_OUT, + FADE_MODE_EASE_IN_OUT, +} fade_mode_t; + + /* Time range. Fields are offsets from midnight in seconds. */ typedef struct { From 2f0e2a3241cb2d41094ff9dac5197432faa9d9a8 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 20:40:26 +0200 Subject: [PATCH 2/8] Add config file parsing for "fade-mode" --- src/options.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index 4e927e5a..9d49c905 100644 --- a/src/options.c +++ b/src/options.c @@ -543,7 +543,24 @@ parse_config_file_option( if (options->use_fade < 0) { options->use_fade = !!atoi(value); } - } else if (strcasecmp(key, "brightness") == 0) { + } else if (strcasecmp(key, "fade-mode") == 0) { + if(strlen(value) == 0) { + fprintf(stderr, _("fade-mode was set to an empty value in config file. Please execute \"redshift -h\" to see supported values.\n")); + return -1; + } else if (strcmp(value, "linear") == 0) { + options->fade_mode = FADE_MODE_LINEAR; + } else if (strcmp(value, "ease-in") == 0) { + options->fade_mode = FADE_MODE_EASE_IN; + } else if (strcmp(value, "ease-out") == 0) { + options->fade_mode = FADE_MODE_EASE_OUT; + } else if (strcmp(value, "ease-in-out") == 0) { + options->fade_mode = FADE_MODE_EASE_IN_OUT; + } else { + fprintf(stderr, _("Fade mode \"%s\" set in config file is not supported.\n"), value); + return -1; + } + } + else if (strcasecmp(key, "brightness") == 0) { if (isnan(options->scheme.day.brightness)) { options->scheme.day.brightness = atof(value); } From ebf0cd99f1d2c28c157e2f599952f6f01f47ba2c Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 20:41:36 +0200 Subject: [PATCH 3/8] Modify continual mode to respect "fade mode" --- src/redshift.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/redshift.c b/src/redshift.c index d2ba577c..6be98bf8 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -586,15 +586,23 @@ provider_get_location( return 1; } -/* Easing function for fade. - See https://github.com/mietek/ease-tween */ +/* Easing function for fade.*/ static double -ease_fade(double t) +ease_fade(double t, fade_mode_t fade_mode) { - if (t <= 0) return 0; - if (t >= 1) return 1; - return 1.0042954579734844 * exp( - -6.4041738958415664 * exp(-7.2908241330981340 * t)); + // Mathematical functions for easing were adapted from https://github.com/d3/d3-ease/blob/a0919680efc6f8e667275ba1d6330bf6a4cc9301/src/sin.js + if (t <= 0) return 0; + if (t >= 1) return 1; + switch (fade_mode) { + case FADE_MODE_LINEAR: + return t; + case FADE_MODE_EASE_IN: + return 1 - cos(t * M_PI_2); + case FADE_MODE_EASE_OUT: + return sin(t * M_PI_2); + case FADE_MODE_EASE_IN_OUT: + return (1 - cos (M_PI * t)) / 2; + } } @@ -608,7 +616,8 @@ run_continual_mode(const location_provider_t *provider, const transition_scheme_t *scheme, const gamma_method_t *method, gamma_state_t *method_state, - int use_fade, int preserve_gamma, int verbose) + int use_fade, fade_mode_t fade_mode, + int preserve_gamma, int verbose) { int r; @@ -771,7 +780,7 @@ run_continual_mode(const location_provider_t *provider, if (fade_length != 0) { fade_time += 1; double frac = fade_time / (double)fade_length; - double alpha = CLAMP(0.0, ease_fade(frac), 1.0); + double alpha = CLAMP(0.0, ease_fade(frac, fade_mode), 1.0); interpolate_color_settings( &fade_start_interp, &target_interp, alpha, @@ -1307,7 +1316,8 @@ main(int argc, char *argv[]) r = run_continual_mode( options.provider, location_state, scheme, options.method, method_state, - options.use_fade, options.preserve_gamma, + options.use_fade, options.fade_mode, + options.preserve_gamma, options.verbose); if (r < 0) exit(EXIT_FAILURE); } From b93d8af4a7b0ecebadb4e2f64ed8b8f33e895883 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 20:49:24 +0200 Subject: [PATCH 4/8] Fix bug in setting default value for fade mode --- src/options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index 9d49c905..c3d56fea 100644 --- a/src/options.c +++ b/src/options.c @@ -711,5 +711,5 @@ options_set_defaults(options_t *options) if (options->use_fade < 0) options->use_fade = 1; - if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) options->fade_mode = FADE_MODE_NOT_SPECIFIED; + if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) options->fade_mode = FADE_MODE_LINEAR; } From 26b0fbc70efdf14f0990d41af58cebddb6851cf0 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 21:01:47 +0200 Subject: [PATCH 5/8] Add explanation for "fade-mode" in help text --- src/options.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/options.c b/src/options.c index c3d56fea..bc62d7c6 100644 --- a/src/options.c +++ b/src/options.c @@ -193,6 +193,10 @@ print_help(const char *program_name) " color effect\n" " -x\t\tReset mode (remove adjustment from screen)\n" " -r\t\tDisable fading between color temperatures\n" + " -e\t\tSet the fading mode to use during a fade between color temperatures.\n" + " \t\tUse \"ease-in\", \"ease-out\" or \"ease-in-out\" to make fades pleasant for your eyes.\n" + " \t\tOtherwise, use \"linear\".\n" + " \t\tFor details about the supported easing functions, see https://easings.net/\n" " -t DAY:NIGHT\tColor temperature to set at daytime/night\n"), stdout); fputs("\n", stdout); From 02bd9a69ecb5778f7e3848ea08a5af73a6756b02 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 21:03:44 +0200 Subject: [PATCH 6/8] Add missing break statement to CLI parsing --- src/options.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/options.c b/src/options.c index bc62d7c6..e8559c99 100644 --- a/src/options.c +++ b/src/options.c @@ -477,6 +477,7 @@ parse_command_line_option( fprintf(stderr, _("Value \"%s\" for option -e (fade mode) is not supported.\nTry \"redshift -h\" for more information."), value); return -1; } + break; case 't': s = strchr(value, ':'); if (s == NULL) { From 416727da5199174adaa5edc98f2f9c97d3214716 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 21:04:13 +0200 Subject: [PATCH 7/8] Give CLI option for fade-mode priority over config file --- src/options.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/options.c b/src/options.c index e8559c99..2f6289eb 100644 --- a/src/options.c +++ b/src/options.c @@ -549,20 +549,22 @@ parse_config_file_option( options->use_fade = !!atoi(value); } } else if (strcasecmp(key, "fade-mode") == 0) { - if(strlen(value) == 0) { - fprintf(stderr, _("fade-mode was set to an empty value in config file. Please execute \"redshift -h\" to see supported values.\n")); - return -1; - } else if (strcmp(value, "linear") == 0) { - options->fade_mode = FADE_MODE_LINEAR; - } else if (strcmp(value, "ease-in") == 0) { - options->fade_mode = FADE_MODE_EASE_IN; - } else if (strcmp(value, "ease-out") == 0) { - options->fade_mode = FADE_MODE_EASE_OUT; - } else if (strcmp(value, "ease-in-out") == 0) { - options->fade_mode = FADE_MODE_EASE_IN_OUT; - } else { - fprintf(stderr, _("Fade mode \"%s\" set in config file is not supported.\n"), value); - return -1; + if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) { + if(strlen(value) == 0) { + fprintf(stderr, _("fade-mode was set to an empty value in config file. Please execute \"redshift -h\" to see supported values.\n")); + return -1; + } else if (strcmp(value, "linear") == 0) { + options->fade_mode = FADE_MODE_LINEAR; + } else if (strcmp(value, "ease-in") == 0) { + options->fade_mode = FADE_MODE_EASE_IN; + } else if (strcmp(value, "ease-out") == 0) { + options->fade_mode = FADE_MODE_EASE_OUT; + } else if (strcmp(value, "ease-in-out") == 0) { + options->fade_mode = FADE_MODE_EASE_IN_OUT; + } else { + fprintf(stderr, _("Fade mode \"%s\" set in config file is not supported.\n"), value); + return -1; + } } } else if (strcasecmp(key, "brightness") == 0) { From 57af25d7c4ddcae21b1f3ecb14f80da0fccd5ce8 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 21:05:27 +0200 Subject: [PATCH 8/8] Convert spaces to tabs. --- src/options.c | 94 +++++++++++++++++++++++++------------------------- src/redshift.c | 30 ++++++++-------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/options.c b/src/options.c index 2f6289eb..b38ac346 100644 --- a/src/options.c +++ b/src/options.c @@ -193,10 +193,10 @@ print_help(const char *program_name) " color effect\n" " -x\t\tReset mode (remove adjustment from screen)\n" " -r\t\tDisable fading between color temperatures\n" - " -e\t\tSet the fading mode to use during a fade between color temperatures.\n" - " \t\tUse \"ease-in\", \"ease-out\" or \"ease-in-out\" to make fades pleasant for your eyes.\n" - " \t\tOtherwise, use \"linear\".\n" - " \t\tFor details about the supported easing functions, see https://easings.net/\n" + " -e\t\tSet the fading mode to use during a fade between color temperatures.\n" + " \t\tUse \"ease-in\", \"ease-out\" or \"ease-in-out\" to make fades pleasant for your eyes.\n" + " \t\tOtherwise, use \"linear\".\n" + " \t\tFor details about the supported easing functions, see https://easings.net/\n" " -t DAY:NIGHT\tColor temperature to set at daytime/night\n"), stdout); fputs("\n", stdout); @@ -324,7 +324,7 @@ options_init(options_t *options) options->provider_args = NULL; options->use_fade = -1; - options->fade_mode = FADE_MODE_NOT_SPECIFIED; + options->fade_mode = FADE_MODE_NOT_SPECIFIED; options->preserve_gamma = 1; options->mode = PROGRAM_MODE_CONTINUAL; options->verbose = 0; @@ -461,27 +461,27 @@ parse_command_line_option( case 'r': options->use_fade = 0; break; - case 'e': - if(strlen(value) == 0) { - fprintf(stderr, _("No value was passed for option -e (fade mode).\nTry \"redshift -h\" for more information.\n")); - return -1; - } else if (strcmp(value, "linear") == 0) { - options->fade_mode = FADE_MODE_LINEAR; - } else if (strcmp(value, "ease-in") == 0) { - options->fade_mode = FADE_MODE_EASE_IN; - } else if (strcmp(value, "ease-out") == 0) { - options->fade_mode = FADE_MODE_EASE_OUT; - } else if (strcmp(value, "ease-in-out") == 0) { - options->fade_mode = FADE_MODE_EASE_IN_OUT; - } else { - fprintf(stderr, _("Value \"%s\" for option -e (fade mode) is not supported.\nTry \"redshift -h\" for more information."), value); - return -1; - } - break; - case 't': - s = strchr(value, ':'); - if (s == NULL) { - fputs(_("Malformed temperature argument.\n"), stderr); + case 'e': + if(strlen(value) == 0) { + fprintf(stderr, _("No value was passed for option -e (fade mode).\nTry \"redshift -h\" for more information.\n")); + return -1; + } else if (strcmp(value, "linear") == 0) { + options->fade_mode = FADE_MODE_LINEAR; + } else if (strcmp(value, "ease-in") == 0) { + options->fade_mode = FADE_MODE_EASE_IN; + } else if (strcmp(value, "ease-out") == 0) { + options->fade_mode = FADE_MODE_EASE_OUT; + } else if (strcmp(value, "ease-in-out") == 0) { + options->fade_mode = FADE_MODE_EASE_IN_OUT; + } else { + fprintf(stderr, _("Value \"%s\" for option -e (fade mode) is not supported.\nTry \"redshift -h\" for more information."), value); + return -1; + } + break; + case 't': + s = strchr(value, ':'); + if (s == NULL) { + fputs(_("Malformed temperature argument.\n"), stderr); fputs(_("Try `-h' for more information.\n"), stderr); return -1; } @@ -549,25 +549,25 @@ parse_config_file_option( options->use_fade = !!atoi(value); } } else if (strcasecmp(key, "fade-mode") == 0) { - if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) { - if(strlen(value) == 0) { - fprintf(stderr, _("fade-mode was set to an empty value in config file. Please execute \"redshift -h\" to see supported values.\n")); - return -1; - } else if (strcmp(value, "linear") == 0) { - options->fade_mode = FADE_MODE_LINEAR; - } else if (strcmp(value, "ease-in") == 0) { - options->fade_mode = FADE_MODE_EASE_IN; - } else if (strcmp(value, "ease-out") == 0) { - options->fade_mode = FADE_MODE_EASE_OUT; - } else if (strcmp(value, "ease-in-out") == 0) { - options->fade_mode = FADE_MODE_EASE_IN_OUT; - } else { - fprintf(stderr, _("Fade mode \"%s\" set in config file is not supported.\n"), value); - return -1; - } - } - } - else if (strcasecmp(key, "brightness") == 0) { + if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) { + if(strlen(value) == 0) { + fprintf(stderr, _("fade-mode was set to an empty value in config file. Please execute \"redshift -h\" to see supported values.\n")); + return -1; + } else if (strcmp(value, "linear") == 0) { + options->fade_mode = FADE_MODE_LINEAR; + } else if (strcmp(value, "ease-in") == 0) { + options->fade_mode = FADE_MODE_EASE_IN; + } else if (strcmp(value, "ease-out") == 0) { + options->fade_mode = FADE_MODE_EASE_OUT; + } else if (strcmp(value, "ease-in-out") == 0) { + options->fade_mode = FADE_MODE_EASE_IN_OUT; + } else { + fprintf(stderr, _("Fade mode \"%s\" set in config file is not supported.\n"), value); + return -1; + } + } + } + else if (strcasecmp(key, "brightness") == 0) { if (isnan(options->scheme.day.brightness)) { options->scheme.day.brightness = atof(value); } @@ -717,6 +717,6 @@ options_set_defaults(options_t *options) } if (options->use_fade < 0) options->use_fade = 1; - - if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) options->fade_mode = FADE_MODE_LINEAR; + + if (options->fade_mode == FADE_MODE_NOT_SPECIFIED) options->fade_mode = FADE_MODE_LINEAR; } diff --git a/src/redshift.c b/src/redshift.c index 6be98bf8..69ce4464 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -590,19 +590,19 @@ provider_get_location( static double ease_fade(double t, fade_mode_t fade_mode) { - // Mathematical functions for easing were adapted from https://github.com/d3/d3-ease/blob/a0919680efc6f8e667275ba1d6330bf6a4cc9301/src/sin.js - if (t <= 0) return 0; - if (t >= 1) return 1; - switch (fade_mode) { - case FADE_MODE_LINEAR: - return t; - case FADE_MODE_EASE_IN: - return 1 - cos(t * M_PI_2); - case FADE_MODE_EASE_OUT: - return sin(t * M_PI_2); - case FADE_MODE_EASE_IN_OUT: - return (1 - cos (M_PI * t)) / 2; - } + // Mathematical functions for easing were adapted from https://github.com/d3/d3-ease/blob/a0919680efc6f8e667275ba1d6330bf6a4cc9301/src/sin.js + if (t <= 0) return 0; + if (t >= 1) return 1; + switch (fade_mode) { + case FADE_MODE_LINEAR: + return t; + case FADE_MODE_EASE_IN: + return 1 - cos(t * M_PI_2); + case FADE_MODE_EASE_OUT: + return sin(t * M_PI_2); + case FADE_MODE_EASE_IN_OUT: + return (1 - cos (M_PI * t)) / 2; + } } @@ -617,7 +617,7 @@ run_continual_mode(const location_provider_t *provider, const gamma_method_t *method, gamma_state_t *method_state, int use_fade, fade_mode_t fade_mode, - int preserve_gamma, int verbose) + int preserve_gamma, int verbose) { int r; @@ -1317,7 +1317,7 @@ main(int argc, char *argv[]) options.provider, location_state, scheme, options.method, method_state, options.use_fade, options.fade_mode, - options.preserve_gamma, + options.preserve_gamma, options.verbose); if (r < 0) exit(EXIT_FAILURE); }