[gnome-settings-daemon/benzea/night-light-smearing: 6/7] color: Allow night light to be always on



commit 6029588e468e64d1f165ea8703e8b027848b9082
Author: Benjamin Berg <bberg redhat com>
Date:   Fri Feb 15 10:52:39 2019 +0100

    color: Allow night light to be always on
    
    Currently if the user configures the "from" and "to" times for the night
    light schedule to be close together or equal, g-s-d gets confused. It
    tries to smear the transition from cool to warm light and vice versa
    over the course of an hour, but if the two times are within an hour of each
    other the night light ends up being disabled except for a period around
    the two times.
    
    Based on a patch by Matthew Leeds <matthew leeds endlessm com>.

 plugins/color/gcm-self-test.c          | 18 +++++++++++-------
 plugins/color/gsd-night-light-common.c |  8 +++++++-
 plugins/color/gsd-night-light-common.h |  3 ++-
 plugins/color/gsd-night-light.c        | 25 +++++++++++++++++--------
 4 files changed, 37 insertions(+), 17 deletions(-)
---
diff --git a/plugins/color/gcm-self-test.c b/plugins/color/gcm-self-test.c
index 5cdcf987..60ddcffc 100644
--- a/plugins/color/gcm-self-test.c
+++ b/plugins/color/gcm-self-test.c
@@ -338,17 +338,21 @@ gcm_test_frac_day (void)
         g_assert_cmpfloat (fd, <, fd_actual + 0.01);
 
         /* test same day */
-        g_assert (gsd_night_light_frac_day_is_between (12, 6, 20));
-        g_assert (!gsd_night_light_frac_day_is_between (5, 6, 20));
-        g_assert (gsd_night_light_frac_day_is_between (12, 0, 24));
-        g_assert (gsd_night_light_frac_day_is_between (12, -1, 25));
+        g_assert_true (gsd_night_light_frac_day_is_between (12, 6, 20, FALSE));
+        g_assert_false (gsd_night_light_frac_day_is_between (5, 6, 20, FALSE));
+        g_assert_true (gsd_night_light_frac_day_is_between (12, 0, 24, FALSE));
+        g_assert_true (gsd_night_light_frac_day_is_between (12, -1, 25, FALSE));
 
         /* test rollover to next day */
-        g_assert (gsd_night_light_frac_day_is_between (23, 20, 6));
-        g_assert (!gsd_night_light_frac_day_is_between (12, 20, 6));
+        g_assert_true (gsd_night_light_frac_day_is_between (23, 20, 6, FALSE));
+        g_assert_false (gsd_night_light_frac_day_is_between (12, 20, 6, TRUE));
 
         /* test rollover to the previous day */
-        g_assert (gsd_night_light_frac_day_is_between (5, 16, 8));
+        g_assert_true (gsd_night_light_frac_day_is_between (5, 16, 8, FALSE));
+
+        /* test equality parameter */
+        g_assert_false (gsd_night_light_frac_day_is_between (12, 0.5, 0.5, FALSE));
+        g_assert_true (gsd_night_light_frac_day_is_between (12, 0.5, 0.5, TRUE));
 }
 
 int
diff --git a/plugins/color/gsd-night-light-common.c b/plugins/color/gsd-night-light-common.c
index fe82bbf2..05ed0549 100644
--- a/plugins/color/gsd-night-light-common.c
+++ b/plugins/color/gsd-night-light-common.c
@@ -115,8 +115,14 @@ gsd_night_light_frac_day_from_dt (GDateTime *dt)
 }
 
 gboolean
-gsd_night_light_frac_day_is_between (gdouble value, gdouble start, gdouble end)
+gsd_night_light_frac_day_is_between (gdouble  value,
+                                     gdouble  start,
+                                     gdouble  end,
+                                     gboolean on_equal)
 {
+        if (start == end)
+                return on_equal;
+
         /* wraparound to the next day */
         if (end < start)
                 end += 24;
diff --git a/plugins/color/gsd-night-light-common.h b/plugins/color/gsd-night-light-common.h
index 4995da52..ba774bfe 100644
--- a/plugins/color/gsd-night-light-common.h
+++ b/plugins/color/gsd-night-light-common.h
@@ -32,7 +32,8 @@ gboolean gsd_night_light_get_sunrise_sunset     (GDateTime      *dt,
 gdouble  gsd_night_light_frac_day_from_dt       (GDateTime      *dt);
 gboolean gsd_night_light_frac_day_is_between    (gdouble         value,
                                                  gdouble         start,
-                                                 gdouble         end);
+                                                 gdouble         end,
+                                                 gboolean        on_equal);
 
 G_END_DECLS
 
diff --git a/plugins/color/gsd-night-light.c b/plugins/color/gsd-night-light.c
index acbcecbc..805435ec 100644
--- a/plugins/color/gsd-night-light.c
+++ b/plugins/color/gsd-night-light.c
@@ -305,13 +305,14 @@ night_light_recheck (GsdNightLight *self)
                 if (time_span > (GTimeSpan) 24 * 60 * 60 * 1000000) {
                         g_debug ("night light disabled until tomorrow is older than 24h, resetting disabled 
until tomorrow");
                         reset = TRUE;
-                } else {
+                } else if (time_span > 0) {
                         /* Or if a sunrise lies between the time it was disabled and now. */
                         gdouble frac_disabled;
                         frac_disabled = gsd_night_light_frac_day_from_dt (self->disabled_until_tmw_dt);
                         if (gsd_night_light_frac_day_is_between (schedule_to,
                                                                  frac_disabled,
-                                                                 frac_day)) {
+                                                                 frac_day,
+                                                                 TRUE)) {
                                 g_debug ("night light sun rise happened, resetting disabled until tomorrow");
                                 reset = TRUE;
                         }
@@ -329,9 +330,15 @@ night_light_recheck (GsdNightLight *self)
                 }
         }
 
+        /* lower smearing period to be smaller than the time between start/stop */
+        smear = MIN (smear,
+                     MIN (     ABS (schedule_to - schedule_from),
+                          24 - ABS (schedule_to - schedule_from)));
+
         if (!gsd_night_light_frac_day_is_between (frac_day,
-                                                    schedule_from - smear,
-                                                    schedule_to)) {
+                                                  schedule_from - smear,
+                                                  schedule_to,
+                                                  TRUE)) {
                 g_debug ("not time for night-light");
                 gsd_night_light_set_active (self, FALSE);
                 return;
@@ -349,14 +356,16 @@ night_light_recheck (GsdNightLight *self)
          */
         temperature = g_settings_get_uint (self->settings, "night-light-temperature");
         if (gsd_night_light_frac_day_is_between (frac_day,
-                                                   schedule_from - smear,
-                                                   schedule_from)) {
+                                                 schedule_from - smear,
+                                                 schedule_from,
+                                                 FALSE)) {
                 gdouble factor = 1.f - ((frac_day - (schedule_from - smear)) / smear);
                 temp_smeared = linear_interpolate (GSD_COLOR_TEMPERATURE_DEFAULT,
                                                    temperature, factor);
         } else if (gsd_night_light_frac_day_is_between (frac_day,
-                                                          schedule_to - smear,
-                                                          schedule_to)) {
+                                                        schedule_to - smear,
+                                                        schedule_to,
+                                                        FALSE)) {
                 gdouble factor = (frac_day - (schedule_to - smear)) / smear;
                 temp_smeared = linear_interpolate (GSD_COLOR_TEMPERATURE_DEFAULT,
                                                    temperature, factor);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]