[gnome-screensaver] Revert "Use g_date_time_create_watch()"



commit c5b572b26523b579fbdcc82f303232634f793ab8
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Aug 31 12:59:19 2011 -0400

    Revert "Use g_date_time_create_watch()"
    
    This reverts commit efaa8d00bb9fa6f1f194676dd0b1d06cae346cd2.
    
    The API has been removed from GLib for now.

 src/gs-window-x11.c |   85 +++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 62 insertions(+), 23 deletions(-)
---
diff --git a/src/gs-window-x11.c b/src/gs-window-x11.c
index ab5a2c1..3114c3e 100644
--- a/src/gs-window-x11.c
+++ b/src/gs-window-x11.c
@@ -102,6 +102,8 @@ struct GSWindowPrivate
         guint      info_bar_timer_id;
         guint      clock_update_id;
 
+        gint64     clock_update_time;
+
         gint       lock_pid;
         gint       lock_watch_id;
         gint       dialog_response;
@@ -150,7 +152,7 @@ static guint           signals [LAST_SIGNAL] = { 0, };
 
 G_DEFINE_TYPE (GSWindow, gs_window, GTK_TYPE_WINDOW)
 
-static gboolean update_clock (gpointer data);
+static void queue_clock_update (GSWindow *window);
 
 static void
 set_invisible_cursor (GdkWindow *window,
@@ -2181,16 +2183,13 @@ on_panel_draw (GtkWidget    *widget,
         return FALSE;
 }
 
-static gboolean
-update_clock (gpointer data)
+static void
+update_clock (GSWindow *window)
 {
-        GSWindow *window = data;
-        GSource   *source;
-        GDateTime *now;
-        GDateTime *expiry;
         const char *clock_format;
         char *text;
         char *markup;
+        GDateTime *dt;
 
         /* clock */
         if (window->priv->clock_format == G_DESKTOP_CLOCK_FORMAT_24H)
@@ -2200,30 +2199,69 @@ update_clock (gpointer data)
                 /* Translators, this is the 12h date format used in the panel clock */
                 clock_format = _("%a %l:%M %p");
 
-        now = g_date_time_new_now_local ();
-        expiry = g_date_time_add_seconds (now, 60 - g_date_time_get_second (now));
-
-        if (window->priv->clock_update_id)
-                g_source_remove (window->priv->clock_update_id);
-
-        source = g_date_time_source_new (expiry, TRUE);
-        g_source_set_priority (source, G_PRIORITY_HIGH);
-        g_source_set_callback (source, update_clock, window, NULL);
-        window->priv->clock_update_id = g_source_attach (source, NULL);
-        g_source_unref (source);
-
-        text = g_date_time_format (now, clock_format);
+        window->priv->clock_update_time = g_get_real_time ();
+        dt = g_date_time_new_from_unix_local (window->priv->clock_update_time / G_USEC_PER_SEC);
+        text = g_date_time_format (dt, clock_format);
         markup = g_strdup_printf ("<b><span foreground=\"#ccc\">%s</span></b>", text);
         gtk_label_set_markup (GTK_LABEL (window->priv->clock), markup);
         g_free (markup);
         g_free (text);
+        g_date_time_unref (dt);
+}
+
+
+static gboolean
+update_clock_timer (GSWindow *window)
+{
+        update_clock (window);
+        queue_clock_update (window);
+        return FALSE;
+}
+
+static gboolean
+check_clock_timer (GSWindow *window)
+{
+        /* Update the panel clock when necessary.
+           This happens:
+
+           - Once a minute in the normal case
+           - When the machine resumes from suspend
+           - When the system time is adjusted
+
+           Right now this function is called much more frequently than any of the
+           above 3 events happen (see queue_clock_update ()).
 
-        g_date_time_unref (now);
-        g_date_time_unref (expiry);
+           We can wake up less often if bug 655129 gets fixed.  */
+        if (ABS (g_get_real_time () - window->priv->clock_update_time) > 60 * G_USEC_PER_SEC) {
+                update_clock (window);
+        }
 
+        queue_clock_update (window);
         return FALSE;
 }
 
+static void
+queue_clock_update (GSWindow *window)
+{
+        int timeouttime;
+        struct timeval tv;
+
+        gettimeofday (&tv, NULL);
+        timeouttime = (G_USEC_PER_SEC - tv.tv_usec) / 1000 + 1;
+
+        /* time until next minute */
+        timeouttime += 1000 * (59 - tv.tv_sec % 60);
+
+        /* If we are more than 2.5 seconds from the start of the next minute,
+           schedule less precise but more power friendly 2 second add_seconds
+           timeout to check if the system realtime clock has changed under us. */
+        if (timeouttime > 2500) {
+                window->priv->clock_update_id = g_timeout_add_seconds (2, (GSourceFunc)check_clock_timer, window);
+        } else {
+                window->priv->clock_update_id = g_timeout_add (timeouttime, (GSourceFunc)update_clock_timer, window);
+        }
+}
+
 static char *
 get_user_display_name (void)
 {
@@ -2333,7 +2371,7 @@ update_clock_format (GSettings  *settings,
         clock_format = g_settings_get_enum (settings, key);
         if (clock_format != window->priv->clock_format) {
                 window->priv->clock_format = clock_format;
-                update_clock (window);
+                queue_clock_update (window);
         }
 }
 
@@ -2391,6 +2429,7 @@ gs_window_init (GSWindow *window)
         g_signal_connect (window->priv->clock_settings, "changed::clock-format",
                           G_CALLBACK (update_clock_format), window);
         update_clock (window);
+        queue_clock_update (window);
 
         force_no_pixmap_background (window->priv->drawing_area);
 }



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