From 431780b851fe7eac9c00ffb29ed5f1eaf02ec565 Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Sat, 27 Dec 2014 21:01:23 -0500 Subject: redshift: Be less verbose when color values did not change This keeps a copy of the previous color_setting_t in the loop so that we can avoid printing the color temperature and brightness if they did not change. --- src/redshift.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'src/redshift.c') diff --git a/src/redshift.c b/src/redshift.c index 1c8723c..00bf9f9 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -1370,6 +1370,12 @@ main(int argc, char *argv[]) printf("Status: %s\n", "Enabled"); } + /* Save previous colors so we can avoid + printing status updates if the values + did not change. */ + color_setting_t prev_interp = + { -1, { NAN, NAN, NAN }, NAN }; + /* Continuously adjust color temperature */ int done = 0; int disabled = 0; @@ -1471,8 +1477,16 @@ main(int argc, char *argv[]) if (done && !short_trans_delta) break; if (verbose) { - printf(_("Color temperature: %uK\n"), interp.temperature); - printf(_("Brightness: %.2f\n"), interp.brightness); + if (interp.temperature != + prev_interp.temperature) { + printf(_("Color temperature: %uK\n"), + interp.temperature); + } + if (interp.brightness != + prev_interp.brightness) { + printf(_("Brightness: %.2f\n"), + interp.brightness); + } } /* Adjust temperature */ @@ -1486,6 +1500,10 @@ main(int argc, char *argv[]) } } + /* Save temperature as previous */ + memcpy(&prev_interp, &interp, + sizeof(color_setting_t)); + /* Sleep for 5 seconds or 0.1 second. */ #ifndef _WIN32 if (short_trans_delta) usleep(SLEEP_DURATION_SHORT*1000); -- cgit v1.2.3-70-g09d2 From 396ecd69a8d288ae127fc0750228758712ca7cf9 Mon Sep 17 00:00:00 2001 From: Jon Lund Steffensen Date: Sat, 27 Dec 2014 21:25:09 -0500 Subject: redshift: Only print period information when it changes This change adds functions to determine what the current period is so we can keep track and only print the period when it changes. --- src/redshift.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 9 deletions(-) (limited to 'src/redshift.c') diff --git a/src/redshift.c b/src/redshift.c index 00bf9f9..fa791bd 100644 --- a/src/redshift.c +++ b/src/redshift.c @@ -294,6 +294,20 @@ typedef enum { PROGRAM_MODE_MANUAL } program_mode_t; +/* Periods of day. */ +typedef enum { + PERIOD_DAYTIME = 0, + PERIOD_NIGHT, + PERIOD_TRANSITION +} period_t; + +/* Names of periods of day */ +static const char *period_names[] = { + "Daytime", + "Night", + "Transition" +}; + #if defined(HAVE_SIGNAL_H) && !defined(__WIN32__) static volatile sig_atomic_t exiting = 0; @@ -325,18 +339,46 @@ static float transition_low = TRANSITION_LOW; static float transition_high = TRANSITION_HIGH; -/* Print which period (night, day or transition) we're currently in. */ -static void -print_period(double elevation) +/* Determine which period we are currently in. */ +static period_t +get_period(double elevation) +{ + if (elevation < transition_low) { + return PERIOD_NIGHT; + } else if (elevation < transition_high) { + return PERIOD_TRANSITION; + } else { + return PERIOD_DAYTIME; + } +} + +/* Determine how far through the transition we are. */ +static double +get_transition_progress(double elevation) { if (elevation < transition_low) { - printf(_("Period: Night\n")); + return 0.0; } else if (elevation < transition_high) { - float a = (transition_low - elevation) / + return (transition_low - elevation) / (transition_low - transition_high); - printf(_("Period: Transition (%.2f%% day)\n"), a*100); } else { - printf(_("Period: Daytime\n")); + return 1.0; + } +} + +/* Print verbose description of the given period. */ +static void +print_period(period_t period, double transition) +{ + switch (period) { + case PERIOD_NIGHT: + case PERIOD_DAYTIME: + printf(_("Period: %s\n"), period_names[period]); + break; + case PERIOD_TRANSITION: + printf(_("Period: %s (%.2f%% day)\n"), + period_names[period], transition*100); + break; } } @@ -1289,7 +1331,9 @@ main(int argc, char *argv[]) interpolate_color_settings(elevation, &day, &night, &interp); if (verbose || mode == PROGRAM_MODE_PRINT) { - print_period(elevation); + period_t period = get_period(elevation); + double transition = get_transition_progress(elevation); + print_period(period, transition); printf(_("Color temperature: %uK\n"), interp.temperature); printf(_("Brightness: %.2f\n"), interp.brightness); } @@ -1373,6 +1417,7 @@ main(int argc, char *argv[]) /* Save previous colors so we can avoid printing status updates if the values did not change. */ + period_t prev_period = -1; color_setting_t prev_interp = { -1, { NAN, NAN, NAN }, NAN }; @@ -1446,7 +1491,17 @@ main(int argc, char *argv[]) color_setting_t interp; interpolate_color_settings(elevation, &day, &night, &interp); - if (verbose) print_period(elevation); + /* Print period if it changed during this update, + or if we are in transition. In transition we + print the progress, so we always print it in + that case. */ + period_t period = get_period(elevation); + if (verbose && (period != prev_period || + period == PERIOD_TRANSITION)) { + double transition = + get_transition_progress(elevation); + print_period(period, transition); + } /* Ongoing short transition */ if (short_trans_delta) { @@ -1501,6 +1556,7 @@ main(int argc, char *argv[]) } /* Save temperature as previous */ + prev_period = period; memcpy(&prev_interp, &interp, sizeof(color_setting_t)); -- cgit v1.2.3-70-g09d2