From dc42433b225c52a35751c8758e245568535cfaa8 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 14:59:32 +0200 Subject: [PATCH 01/12] Add new configuration option "fade_duration" defaulting to 4 seconds --- src/options.c | 3 +++ src/options.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/options.c b/src/options.c index 33bf623a..c507357a 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_duration = -1; options->preserve_gamma = 1; options->mode = PROGRAM_MODE_CONTINUAL; options->verbose = 0; @@ -676,4 +677,6 @@ options_set_defaults(options_t *options) } if (options->use_fade < 0) options->use_fade = 1; + + if (options->fade_duration < 0) options->fade_duration = 4000; // Set default fade duration to 4 seconds. } diff --git a/src/options.h b/src/options.h index 9993a07f..42cc104b 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 length of the fade duration in seconds */ + long fade_duration; /* Whether to preserve gamma ramps if supported by gamma method. */ int preserve_gamma; From a2807ef9b58a1d7f9207c90bd2ae0b236e2eeb09 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:00:06 +0200 Subject: [PATCH 02/12] Add CLI option parsing for "fade_duration" --- src/options.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index c507357a..8d03d6b9 100644 --- a/src/options.c +++ b/src/options.c @@ -457,6 +457,16 @@ parse_command_line_option( case 'r': options->use_fade = 0; break; + case 'f': + errno = 0; + long fade_duration = strtol(value, NULL, 10); + if (errno != 0 || fade_duration <= 0) { + fputs(_("Malformed fade duration argument.\n"), stderr); + fputs(_("Try `-h' for more information.\n"), stderr); + return -1; + } + options->fade_duration = fade_duration; + break; case 't': s = strchr(value, ':'); if (s == NULL) { @@ -496,7 +506,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:pPrf:t:vVx")) != -1) { char option = opt; int r = parse_command_line_option( option, optarg, options, program_name, gamma_methods, From 3beda6ae708924ba8201091454d99e591493b094 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:00:26 +0200 Subject: [PATCH 03/12] Add config file parsing for "fade_duration" --- src/options.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/options.c b/src/options.c index 8d03d6b9..f12e516d 100644 --- a/src/options.c +++ b/src/options.c @@ -537,6 +537,16 @@ parse_config_file_option( if (options->use_fade < 0) { options->use_fade = !!atoi(value); } + } else if (strcasecmp(key, "fade-duration") == 0) { + if (options->fade_duration < 0) { + errno = 0; + long fade_duration = strtol(value, NULL, 10); + if (errno != 0 || fade_duration <= 0) { + fputs(_("Malformed fade duration setting.\n"), stderr); + return -1; + } + options->fade_duration = fade_duration; + } } else if (strcasecmp(key, "brightness") == 0) { if (isnan(options->scheme.day.brightness)) { options->scheme.day.brightness = atof(value); From c716a3edc653d8c19c989d7efc89ffe18b6f3088 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:01:29 +0200 Subject: [PATCH 04/12] Add explanation for "fade_duration" in help text --- src/options.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/options.c b/src/options.c index f12e516d..ae9af9a8 100644 --- a/src/options.c +++ b/src/options.c @@ -193,6 +193,7 @@ 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" + " -f\t\tSet fade duration (for fading between color temperatures) in full seconds\n" " -t DAY:NIGHT\tColor temperature to set at daytime/night\n"), stdout); fputs("\n", stdout); From 04565170317bff84bc386f22e0856fb199139f11 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:01:57 +0200 Subject: [PATCH 05/12] Modify continual mode to respect "fade_duration" --- src/redshift.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/redshift.c b/src/redshift.c index d2ba577c..f7fcacfb 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -608,7 +608,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, long fade_duration, + int preserve_gamma, int verbose) { int r; @@ -761,8 +762,9 @@ run_continual_mode(const location_provider_t *provider, color_setting_diff_is_major( &target_interp, &prev_target_interp))) { - fade_length = FADE_LENGTH; - fade_time = 0; + // Scale fade_length according to desired fade_duration. + fade_length = (FADE_LENGTH / ((FADE_LENGTH * SLEEP_DURATION_SHORT) / fade_duration)); + fade_time = 0; fade_start_interp = interp; } } @@ -1307,7 +1309,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_duration, + options.preserve_gamma, options.verbose); if (r < 0) exit(EXIT_FAILURE); } From 48b6551b5c750e6168efc1345a8325199343df0e Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:03:29 +0200 Subject: [PATCH 06/12] Change data type of "fade_duration" to unsigned long --- src/options.c | 4 ++-- src/options.h | 2 +- src/redshift.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/options.c b/src/options.c index ae9af9a8..98d68345 100644 --- a/src/options.c +++ b/src/options.c @@ -460,7 +460,7 @@ parse_command_line_option( break; case 'f': errno = 0; - long fade_duration = strtol(value, NULL, 10); + unsigned long fade_duration = strtoul(value, NULL, 10); if (errno != 0 || fade_duration <= 0) { fputs(_("Malformed fade duration argument.\n"), stderr); fputs(_("Try `-h' for more information.\n"), stderr); @@ -541,7 +541,7 @@ parse_config_file_option( } else if (strcasecmp(key, "fade-duration") == 0) { if (options->fade_duration < 0) { errno = 0; - long fade_duration = strtol(value, NULL, 10); + unsigned long fade_duration = strtoul(value, NULL, 10); if (errno != 0 || fade_duration <= 0) { fputs(_("Malformed fade duration setting.\n"), stderr); return -1; diff --git a/src/options.h b/src/options.h index 42cc104b..76238501 100644 --- a/src/options.h +++ b/src/options.h @@ -35,7 +35,7 @@ typedef struct { /* Whether to fade between large skips in color temperature. */ int use_fade; /* The length of the fade duration in seconds */ - long fade_duration; + unsigned long fade_duration; /* Whether to preserve gamma ramps if supported by gamma method. */ int preserve_gamma; diff --git a/src/redshift.c b/src/redshift.c index f7fcacfb..10307770 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -608,7 +608,7 @@ 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, long fade_duration, + int use_fade, unsigned long fade_duration, int preserve_gamma, int verbose) { int r; From 7d264caf4da3487ab10c0afe701f04a631d09bca Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:05:23 +0200 Subject: [PATCH 07/12] Reorder calculation of fade_length so it uses multiplication instead of division. --- src/redshift.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redshift.c b/src/redshift.c index 10307770..a11275f0 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -763,7 +763,7 @@ run_continual_mode(const location_provider_t *provider, &target_interp, &prev_target_interp))) { // Scale fade_length according to desired fade_duration. - fade_length = (FADE_LENGTH / ((FADE_LENGTH * SLEEP_DURATION_SHORT) / fade_duration)); + fade_length = (FADE_LENGTH * fade_duration) / (FADE_LENGTH * SLEEP_DURATION_SHORT); fade_time = 0; fade_start_interp = interp; } From a2fa0d46a05b6b206ebb80c7e1f367905c3128fe Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:08:04 +0200 Subject: [PATCH 08/12] Fix calculation of fade_length to respect that fade_duration is given in seconds --- src/redshift.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redshift.c b/src/redshift.c index a11275f0..c31f4311 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -763,7 +763,7 @@ run_continual_mode(const location_provider_t *provider, &target_interp, &prev_target_interp))) { // Scale fade_length according to desired fade_duration. - fade_length = (FADE_LENGTH * fade_duration) / (FADE_LENGTH * SLEEP_DURATION_SHORT); + fade_length = (FADE_LENGTH * (fade_duration * 1000)) / (FADE_LENGTH * SLEEP_DURATION_SHORT); fade_time = 0; fade_start_interp = interp; } From 801a32e4390f47c5a0211be2bd915d0f5421ed1b Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:14:23 +0200 Subject: [PATCH 09/12] Fix default value of fade_duration to be in seconds. --- src/options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index 98d68345..26cd5881 100644 --- a/src/options.c +++ b/src/options.c @@ -699,5 +699,5 @@ options_set_defaults(options_t *options) if (options->use_fade < 0) options->use_fade = 1; - if (options->fade_duration < 0) options->fade_duration = 4000; // Set default fade duration to 4 seconds. + if (options->fade_duration < 0) options->fade_duration = 4; // Set default fade duration to 4 seconds. } From 3c36ab11800bd8aa53c781c83815f8d4193a5a04 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Mon, 20 Sep 2021 15:47:55 +0200 Subject: [PATCH 10/12] Change data type of "fade_duration" back to long to prevent issues with strtoul() and negative values This reverts commit 48b6551b5c750e6168efc1345a8325199343df0e --- src/options.c | 4 ++-- src/options.h | 2 +- src/redshift.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/options.c b/src/options.c index 26cd5881..517bae70 100644 --- a/src/options.c +++ b/src/options.c @@ -460,7 +460,7 @@ parse_command_line_option( break; case 'f': errno = 0; - unsigned long fade_duration = strtoul(value, NULL, 10); + long fade_duration = strtol(value, NULL, 10); if (errno != 0 || fade_duration <= 0) { fputs(_("Malformed fade duration argument.\n"), stderr); fputs(_("Try `-h' for more information.\n"), stderr); @@ -541,7 +541,7 @@ parse_config_file_option( } else if (strcasecmp(key, "fade-duration") == 0) { if (options->fade_duration < 0) { errno = 0; - unsigned long fade_duration = strtoul(value, NULL, 10); + long fade_duration = strtol(value, NULL, 10); if (errno != 0 || fade_duration <= 0) { fputs(_("Malformed fade duration setting.\n"), stderr); return -1; diff --git a/src/options.h b/src/options.h index 76238501..42cc104b 100644 --- a/src/options.h +++ b/src/options.h @@ -35,7 +35,7 @@ typedef struct { /* Whether to fade between large skips in color temperature. */ int use_fade; /* The length of the fade duration in seconds */ - unsigned long fade_duration; + long fade_duration; /* Whether to preserve gamma ramps if supported by gamma method. */ int preserve_gamma; diff --git a/src/redshift.c b/src/redshift.c index c31f4311..a4bf96da 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -608,7 +608,7 @@ 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, unsigned long fade_duration, + int use_fade, long fade_duration, int preserve_gamma, int verbose) { int r; From c745ceb7e6a27520ee12fedbd4483151610ef699 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 10:25:08 +0200 Subject: [PATCH 11/12] Replace spaces by tab --- src/options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index 517bae70..9ba685ea 100644 --- a/src/options.c +++ b/src/options.c @@ -321,7 +321,7 @@ options_init(options_t *options) options->provider_args = NULL; options->use_fade = -1; - options->fade_duration = -1; + options->fade_duration = -1; options->preserve_gamma = 1; options->mode = PROGRAM_MODE_CONTINUAL; options->verbose = 0; From 19589684ae30b83ab796ba79cf44275e444a86a8 Mon Sep 17 00:00:00 2001 From: Tobias Schubert Date: Tue, 21 Sep 2021 10:29:24 +0200 Subject: [PATCH 12/12] Normalize indent: Convert all spaces to tabs --- src/options.c | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/options.c b/src/options.c index 9ba685ea..45ea88ad 100644 --- a/src/options.c +++ b/src/options.c @@ -105,7 +105,7 @@ parse_transition_time(const char *str, const char **end) errno = 0; long hours = strtol(str, (char **)&min, 10); if (errno != 0 || min == str || min[0] != ':' || - hours < 0 || hours >= 24) { + hours < 0 || hours >= 24) { return -1; } @@ -193,9 +193,9 @@ 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" - " -f\t\tSet fade duration (for fading between color temperatures) in full seconds\n" + " -f\t\tSet fade duration (for fading between color temperatures) in full seconds\n" " -t DAY:NIGHT\tColor temperature to set at daytime/night\n"), - stdout); + stdout); fputs("\n", stdout); /* TRANSLATORS: help output 5 */ @@ -211,7 +211,7 @@ print_help(const char *program_name) printf(_("Default values:\n\n" " Daytime temperature: %uK\n" " Night temperature: %uK\n"), - DEFAULT_DAY_TEMP, DEFAULT_NIGHT_TEMP); + DEFAULT_DAY_TEMP, DEFAULT_NIGHT_TEMP); fputs("\n", stdout); @@ -359,8 +359,8 @@ parse_command_line_option( To set these to distinct values use the config file. */ memcpy(options->scheme.night.gamma, - options->scheme.day.gamma, - sizeof(options->scheme.night.gamma)); + options->scheme.day.gamma, + sizeof(options->scheme.night.gamma)); break; case 'h': print_help(program_name); @@ -406,7 +406,7 @@ parse_command_line_option( /* Print provider help if arg is `help'. */ if (options->provider_args != NULL && - strcasecmp(options->provider_args, "help") == 0) { + strcasecmp(options->provider_args, "help") == 0) { options->provider->print_help(stdout); exit(EXIT_SUCCESS); } @@ -437,7 +437,7 @@ parse_command_line_option( /* Print method help if arg is `help'. */ if (options->method_args != NULL && - strcasecmp(options->method_args, "help") == 0) { + strcasecmp(options->method_args, "help") == 0) { options->method->print_help(stdout); exit(EXIT_SUCCESS); } @@ -458,16 +458,16 @@ parse_command_line_option( case 'r': options->use_fade = 0; break; - case 'f': - errno = 0; - long fade_duration = strtol(value, NULL, 10); - if (errno != 0 || fade_duration <= 0) { - fputs(_("Malformed fade duration argument.\n"), stderr); - fputs(_("Try `-h' for more information.\n"), stderr); - return -1; - } - options->fade_duration = fade_duration; - break; + case 'f': + errno = 0; + long fade_duration = strtol(value, NULL, 10); + if (errno != 0 || fade_duration <= 0) { + fputs(_("Malformed fade duration argument.\n"), stderr); + fputs(_("Try `-h' for more information.\n"), stderr); + return -1; + } + options->fade_duration = fade_duration; + break; case 't': s = strchr(value, ':'); if (s == NULL) { @@ -539,15 +539,15 @@ parse_config_file_option( options->use_fade = !!atoi(value); } } else if (strcasecmp(key, "fade-duration") == 0) { - if (options->fade_duration < 0) { - errno = 0; - long fade_duration = strtol(value, NULL, 10); - if (errno != 0 || fade_duration <= 0) { - fputs(_("Malformed fade duration setting.\n"), stderr); - return -1; - } - options->fade_duration = fade_duration; - } + if (options->fade_duration < 0) { + errno = 0; + long fade_duration = strtol(value, NULL, 10); + if (errno != 0 || fade_duration <= 0) { + fputs(_("Malformed fade duration setting.\n"), stderr); + return -1; + } + options->fade_duration = fade_duration; + } } else if (strcasecmp(key, "brightness") == 0) { if (isnan(options->scheme.day.brightness)) { options->scheme.day.brightness = atof(value); @@ -576,8 +576,8 @@ parse_config_file_option( return -1; } memcpy(options->scheme.night.gamma, - options->scheme.day.gamma, - sizeof(options->scheme.night.gamma)); + options->scheme.day.gamma, + sizeof(options->scheme.night.gamma)); } } else if (strcasecmp(key, "gamma-day") == 0) { if (isnan(options->scheme.day.gamma[0])) {