changeset 25:c0280cf3ca84

first try to avoid too frequent updates.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sat, 24 Jan 2009 14:57:20 +0900
parents 8ac1ebc63fab
children 47b3caeb47c5
files pidgin-audacious.c pidgin-audacious.h
diffstat 2 files changed, 81 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-audacious.c	Sat Jan 10 14:41:46 2009 +0900
+++ b/pidgin-audacious.c	Sat Jan 24 14:57:20 2009 +0900
@@ -27,6 +27,7 @@
 static DBusGConnection *connection = NULL;
 static DBusGProxy *session = NULL;
 static PurpleCmdId cmdid_paste_current_song;
+static GTimeVal latest; /* time of the latest notification */
 
 
 /* implementation */
@@ -201,12 +202,11 @@
 }
 
 
-
-static void
-track_signal_cb(DBusGProxy *player_proxy, GHashTable *table, gpointer data)
+static gboolean
+track_signal_hook(gpointer data)
 {
+    song_tuple *tuple = (song_tuple *)data;
     gchar *song_info = NULL;
-    song_tuple *tuple = get_song_tuple(table);
 
     /* set current song */
     purple_util_set_current_song(tuple->title ? tuple->title : "",
@@ -215,28 +215,91 @@
 
     song_info = format_song_info(tuple);
 
-	aud_process(song_info);
+    aud_process(song_info);
     free_song_tuple(tuple);
-	g_free(song_info);
+    g_free(song_info);
+
+    return FALSE; /* one time */
+}
+
+
+static void
+track_signal_cb(DBusGProxy *player_proxy, GHashTable *table, gpointer data)
+{
+    GTimeVal current;
+    gulong interval;
+    song_tuple *tuple = get_song_tuple(table);
+
+    g_get_current_time(&current);
+
+    if(current.tv_sec > latest.tv_sec) {
+        interval = current.tv_sec - latest.tv_sec;
+
+        if(interval < MINIMAL_INTERVAL) {
+            g_timeout_add_seconds(MINIMAL_INTERVAL - interval,
+                                  track_signal_hook, (gpointer)tuple);
+
+            latest.tv_sec = current.tv_sec + MINIMAL_INTERVAL - interval;
+        }
+        else {
+            track_signal_hook((gpointer)tuple);
+            latest.tv_sec = current.tv_sec;
+        }
+    }
+    else {
+        latest.tv_sec = latest.tv_sec + MINIMAL_INTERVAL;
+        g_timeout_add_seconds(latest.tv_sec - current.tv_sec,
+                              track_signal_hook, (gpointer)tuple);
+    }
+}
+
+
+static gboolean
+status_signal_hook(gpointer data)
+{
+    gboolean stopped = !is_app_playing();
+
+    if(stopped) {
+        /* clear status/user info */
+        aud_process(NULL);
+        /* clear current song */
+        purple_util_set_current_song(NULL, NULL, NULL);
+    }
+
+    return FALSE;
 }
 
 
 static void
 status_signal_cb(DBusGProxy *player_proxy, gint status, gpointer data)
 {
+    GTimeVal current;
+    gulong interval;
+
 	aud_debug("StatusChange %d\n", status);
 
-    switch(status) {
-    case STOPPED:
-        /* clear status/user info */
-		aud_process(NULL);
-        /* clear current song */
-        purple_util_set_current_song(NULL, NULL, NULL);
-        break;
-    case PLAYING:
-    case PAUSED:
-    default:
-        break;
+    g_get_current_time(&current);
+
+    if(current.tv_sec > latest.tv_sec) {
+        interval = current.tv_sec - latest.tv_sec;
+
+        if(interval < MINIMAL_INTERVAL) {
+            g_timeout_add_seconds(MINIMAL_INTERVAL - interval,
+                                  status_signal_hook, NULL);
+            latest.tv_sec = current.tv_sec + MINIMAL_INTERVAL - interval;
+        }
+        else {
+            status_signal_hook(NULL);
+            latest.tv_sec = current.tv_sec;
+        }
+    }
+    else { /* current < latest */
+        /* latest.tv_sec = latest.tv_sec + MINIMAL_INTERVAL; */
+        /* g_timeout_add_seconds(latest.tv_sec - current.tv_sec, */
+        /*                       status_signal_hook, NULL); */
+        latest.tv_sec = latest.tv_sec + MINIMAL_INTERVAL;
+        g_timeout_add_seconds(latest.tv_sec - current.tv_sec,
+                              status_signal_hook, NULL);
     }
 }
 
--- a/pidgin-audacious.h	Sat Jan 10 14:41:46 2009 +0900
+++ b/pidgin-audacious.h	Sat Jan 24 14:57:20 2009 +0900
@@ -40,6 +40,7 @@
 
 /* constants */
 #define DBUS_TIMEOUT 1000
+#define MINIMAL_INTERVAL 8
 #define PLAYING 0
 #define PAUSED  1
 #define STOPPED 2