changeset 20:4aa711530489

- made player configurable. - is_app_playing() now uses GetStatus method.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sat, 04 Oct 2008 17:59:07 +0900
parents 12d809123d69
children 83002f858ee9
files pidgin-audacious.c
diffstat 1 files changed, 116 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-audacious.c	Sat Oct 04 12:06:45 2008 +0900
+++ b/pidgin-audacious.c	Sat Oct 04 17:59:07 2008 +0900
@@ -40,8 +40,10 @@
 #define OPT_SONG_TEMPLATE       OPT_PIDGINAUD "/song_template"
 #define OPT_PASTE_TEMPLATE      OPT_PIDGINAUD "/paste_template"
 #define OPT_LOG_OUTPUT          OPT_PIDGINAUD "/log_output"
+#define OPT_PLAYER              OPT_PIDGINAUD "/player"
 
-#define DEFAULT_SONG_TEMPLATE   "%artist - %title"
+#define DEFAULT_SONG_TEMPLATE   "%title - %artist"
+#define DEFAULT_PLAYER          "audacious"
 #define SONG_TOKEN              "%song"
 #define NO_SONG_MESSAGE         "No song being played."
 
@@ -93,15 +95,21 @@
 {
     GError *error = NULL;
     DBusGProxy *proxy;
+    gchar *player = g_strconcat("org.mpris.",
+                                purple_prefs_get_string(OPT_PLAYER),
+                                NULL);
+
     connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
     g_clear_error(&error);
 
+
 	proxy = dbus_g_proxy_new_for_name(connection,
-                                      "org.mpris.audacious",
+                                      player,
                                       "/Player",
                                       "org.freedesktop.MediaPlayer");
 
     g_clear_error(&error);
+    g_free(player);
     return proxy;
 }
 
@@ -244,6 +252,11 @@
         song_info = tmp;
     }
 
+    if(song_info && !strcmp(song_info, "")) {
+        g_free(song_info);
+        song_info = NULL;
+    }
+
     return song_info;
 }
 
@@ -274,7 +287,7 @@
 	aud_debug("StatusChange %d\n", status);
 
 	if (status != 0) {
-		aud_process("");
+		aud_process(NULL);
 
         /* clear current song */
         purple_util_set_current_song(NULL, NULL, NULL);
@@ -543,32 +556,89 @@
     return PURPLE_CMD_RET_OK;
 }
 
+#define DBUS_TIMEOUT 1000
+#define DBUS_TYPE_MPRIS_STATUS (dbus_g_type_get_struct ("GValueArray", G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INVALID))
+
+#define PLAYING 0
+#define PAUSED  1
+#define STOPPED 2
+
 static gboolean
 is_app_playing(void)
 {
-	gchar *player_name = g_strconcat("org.mpris.", "audacious", NULL);
+	gchar *player_name = g_strconcat("org.mpris.",
+                                     purple_prefs_get_string(OPT_PLAYER),
+                                     NULL);
+
+
+    aud_debug("player_name = %s\n", player_name);
+
+    if(!session) {
+        session = get_dbus_session();
+        connect_dbus_signals();
+        aud_debug("session == NULL. new session = %p\n", session);
+    }
+
+    /* audacious only supports this form */
+    GError *error = NULL;
+    int status = STOPPED;
 
+    gboolean result =
+        dbus_g_proxy_call_with_timeout(
+            session,
+            "GetStatus",
+            DBUS_TIMEOUT,
+            &error,
+            G_TYPE_INVALID,
+            G_TYPE_INT,
+            &status,
+            G_TYPE_INVALID);
+
+    aud_debug("GetStatus result = %d\n", result);
+
+    if(status == PLAYING || status == PAUSED)
+        return TRUE;
+    else
+        return FALSE;
+
+    aud_debug("GetStatus status = %d\n", status);
+
+
+    /* mpris specification 1.0 insists this is the way. no player
+     * supports this at this time. --yaz */
 #if 0
-	if(g_strcasecmp(pidginmpris->player_name, player_name) != 0) {
-		pidginmpris->player_name = g_strdup(player_name);
-		g_object_unref(pidginmpris->player);
-		mpris_connect_dbus_signals();
-	}
+    GError *error = NULL;
+    GValueArray *s;
+    gboolean result =
+        dbus_g_proxy_call_with_timeout(
+            session,
+            "GetStatus",
+            DBUS_TIMEOUT, //msec
+            &error,
+            G_TYPE_INVALID,
+            DBUS_TYPE_MPRIS_STATUS,
+            &s,
+            G_TYPE_INVALID);
+
+    aud_debug("GetStatus result = %d\n", result);
+
+    if(result) {
+        /* parse GValueArray here */
+        GValue *val = g_value_array_get_nth(s, 0);
+        int status = g_value_get_int(val);
+        g_value_array_free(s);
+
+        aud_debug("status = %d\n", status);
+
+        if(status == STOPPED)
+            return FALSE;
+        else
+            return TRUE;
+    }
+
+	return FALSE;
 #endif
 
-	DBusGProxy *player =
-        dbus_g_proxy_new_for_name_owner(connection,
-                                        player_name,
-                                        "/Player",
-                                        "org.freedesktop.MediaPlayer",
-                                        NULL);
-
-	if(!player)
-		return FALSE;
-
-	g_object_unref(player);
-	g_free(player_name);
-	return TRUE;
 }
 
 static void
@@ -580,10 +650,21 @@
 
     aud_debug("called\n");
 
+    if(!is_app_playing())
+        return;
+
     table = get_song_table();
     tuple = get_song_tuple(table);
     g_hash_table_destroy(table);
 
+    if(!tuple) {
+        aud_debug("tuple == NULL\n");
+        return;
+    }
+
+    if(tuple->title)
+        aud_debug("title = %s\n", tuple->title);
+
     /* set current song */
     purple_util_set_current_song(tuple->title ? tuple->title : "",
                                  tuple->artist ? tuple->artist : "",
@@ -621,8 +702,11 @@
 
     session = get_dbus_session();
 
+    aud_debug("session = %p\n", session);
+
     /* connect to mpris signals */
-	connect_dbus_signals();
+    if(session)
+        connect_dbus_signals();
 
     /* connect to purple signals */
     connect_purple_signals(plugin);
@@ -677,6 +761,12 @@
         "Pidgin-Audacious Configuration");
     purple_plugin_pref_frame_add(frame, pref);
 
+    /* player to connect */
+    pref = purple_plugin_pref_new_with_name_and_label(
+        OPT_PLAYER,
+        "Player to connect");
+    purple_plugin_pref_frame_add(frame, pref);
+
     pref = purple_plugin_pref_new_with_name_and_label(
         OPT_PROCESS_STATUS,
         "Expand " SONG_TOKEN " to song info in the status message");
@@ -690,13 +780,13 @@
     /* song template */
     pref = purple_plugin_pref_new_with_name_and_label(
         OPT_SONG_TEMPLATE,
-        "Song template");
+        "%song compiled with");
     purple_plugin_pref_frame_add(frame, pref);
 
     /* paste template */
     pref = purple_plugin_pref_new_with_name_and_label(
         OPT_PASTE_TEMPLATE,
-        "Paste template");
+        "/song command template");
     purple_plugin_pref_frame_add(frame, pref);
 
     return frame;
@@ -744,6 +834,7 @@
     purple_prefs_add_none(OPT_PIDGINAUD);
     purple_prefs_add_bool(OPT_PROCESS_STATUS, TRUE);
     purple_prefs_add_bool(OPT_PROCESS_USERINFO, TRUE);
+    purple_prefs_add_string(OPT_PLAYER, DEFAULT_PLAYER);
     purple_prefs_add_string(OPT_SONG_TEMPLATE, DEFAULT_SONG_TEMPLATE);
     purple_prefs_add_string(OPT_PASTE_TEMPLATE, SONG_TOKEN);
     purple_prefs_add_bool(OPT_LOG_OUTPUT, TRUE);