[Goodies-commits] r4862 - in xfce4-datetime-plugin/trunk: . panel-plugin

Diego Ongaro ongardie at xfce.org
Fri May 30 00:33:44 CEST 2008


Author: ongardie
Date: 2008-05-29 22:33:44 +0000 (Thu, 29 May 2008)
New Revision: 4862

Modified:
   xfce4-datetime-plugin/trunk/ChangeLog
   xfce4-datetime-plugin/trunk/panel-plugin/datetime.c
   xfce4-datetime-plugin/trunk/panel-plugin/datetime.h
Log:
Update on the top of every second or minute and no more
(Bug #4119, based on patch by Steve Tyler)


Modified: xfce4-datetime-plugin/trunk/ChangeLog
===================================================================
--- xfce4-datetime-plugin/trunk/ChangeLog	2008-05-29 22:33:35 UTC (rev 4861)
+++ xfce4-datetime-plugin/trunk/ChangeLog	2008-05-29 22:33:44 UTC (rev 4862)
@@ -1,5 +1,7 @@
 2008-05-29	Diego Ongaro <ongardie at gmail.com>
 
+	* panl-plugin/datetime.{c,h}: Update on the top of every second or
+	minute and no more (Bug #4119, based on patch by Steve Tyler)
 	* panel-plugin/datetime.c: Always update every second when seconds are
 	shown (Bug #4117)
 

Modified: xfce4-datetime-plugin/trunk/panel-plugin/datetime.c
===================================================================
--- xfce4-datetime-plugin/trunk/panel-plugin/datetime.c	2008-05-29 22:33:35 UTC (rev 4861)
+++ xfce4-datetime-plugin/trunk/panel-plugin/datetime.c	2008-05-29 22:33:44 UTC (rev 4862)
@@ -37,8 +37,18 @@
 #include "datetime-dialog.h"
 
 #define USE_GTK_TOOLTIP_API     GTK_CHECK_VERSION(2,12,0)
+
 #define DATETIME_MAX_STRLEN 256
 
+/**
+ * Calculate the number of milliseconds from a GTimeVal.
+ * Anything smaller than one millisecond will be truncated.
+ */
+static inline gint64 datetime_gtimeval_to_ms(const GTimeVal t)
+{
+  return ((gint64) t.tv_sec * 1000) + ((gint64) t.tv_usec / 1000);
+}
+
 /*
  * Get date/time string
  */
@@ -111,6 +121,7 @@
   gchar *utf8str;
   struct tm *current;
   t_datetime *datetime;
+  guint wake_interval;  /* milliseconds to next update */
 
   if (data == NULL)
   {
@@ -119,6 +130,12 @@
 
   datetime = (t_datetime*)data;
 
+  /* stop timer */
+  if (datetime->timeout_id)
+  {
+    g_source_remove(datetime->timeout_id);
+  }
+
   g_get_current_time(&timeval);
   current = localtime((time_t *)&timeval.tv_sec);
   if (datetime->date_format != NULL && GTK_IS_LABEL(datetime->date_label))
@@ -185,6 +202,15 @@
       break;
   }
 
+  /*
+   * Compute the time to the next update and start the timer.
+   * The calculation finds the next larger integral multiple of the given update interval.
+   * This results in the next update occurring on the second or minute
+   * when the update interval is 1 or 60 seconds, respectively.
+   */
+  wake_interval = datetime->update_interval - datetime_gtimeval_to_ms(timeval) % datetime->update_interval;
+  datetime->timeout_id = g_timeout_add(wake_interval, datetime_update, datetime);
+
   return TRUE;
 }
 
@@ -455,19 +481,14 @@
       gtk_widget_show(GTK_WIDGET(datetime->time_label));
   }
 
-  if (datetime->timeout_id)
-  {
-    g_source_remove(datetime->timeout_id);
-  }
-
   if (datetime_format_has_seconds(datetime->date_format) ||
       datetime_format_has_seconds(datetime->time_format))
   {
-    datetime->timeout_id = g_timeout_add(1000, datetime_update, datetime);
+    datetime->update_interval = 1000; /* 1 second */
   }
   else
   {
-    datetime->timeout_id = g_timeout_add(10000, datetime_update, datetime);
+    datetime->update_interval = 60000; /* 1 minute */
   }
 }
 
@@ -621,6 +642,7 @@
   gtk_widget_show_all(datetime->button);
 
   /* set date and time labels */
+  datetime->timeout_id = 0;
   datetime_update(datetime);
 
   return datetime;

Modified: xfce4-datetime-plugin/trunk/panel-plugin/datetime.h
===================================================================
--- xfce4-datetime-plugin/trunk/panel-plugin/datetime.h	2008-05-29 22:33:35 UTC (rev 4861)
+++ xfce4-datetime-plugin/trunk/panel-plugin/datetime.h	2008-05-29 22:33:44 UTC (rev 4862)
@@ -46,6 +46,7 @@
   GtkWidget *date_label;
   GtkWidget *time_label;
   GtkTooltips *tips;
+  guint update_interval;  /* time between updates in milliseconds */
   guint timeout_id;
 
   /* settings */




More information about the Goodies-commits mailing list