Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to adjust fade duration (without Elektra) #840

Open
wants to merge 12 commits into
base: 814-improve-config
Choose a base branch
from
44 changes: 34 additions & 10 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -193,8 +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"
" -t DAY:NIGHT\tColor temperature to set at daytime/night\n"),
stdout);
stdout);
fputs("\n", stdout);

/* TRANSLATORS: help output 5 */
Expand All @@ -210,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);

Expand Down Expand Up @@ -320,6 +321,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;
Expand Down Expand Up @@ -357,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);
Expand Down Expand Up @@ -404,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);
}
Expand Down Expand Up @@ -435,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);
}
Expand All @@ -456,6 +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 't':
s = strchr(value, ':');
if (s == NULL) {
Expand Down Expand Up @@ -495,7 +507,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,
Expand Down Expand Up @@ -526,6 +538,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);
Expand Down Expand Up @@ -554,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])) {
Expand Down Expand Up @@ -676,4 +698,6 @@ options_set_defaults(options_t *options)
}

if (options->use_fade < 0) options->use_fade = 1;

if (options->fade_duration < 0) options->fade_duration = 4; // Set default fade duration to 4 seconds.
}
2 changes: 2 additions & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 7 additions & 4 deletions src/redshift.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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_duration * 1000)) / (FADE_LENGTH * SLEEP_DURATION_SHORT);
fade_time = 0;
fade_start_interp = interp;
}
}
Expand Down Expand Up @@ -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);
}
Expand Down