[Goodies-commits] r3877 - xfmpc/trunk/src

Mike Massonnet mmassonnet at xfce.org
Sun Jan 27 14:54:59 CET 2008


Author: mmassonnet
Date: 2008-01-27 13:54:59 +0000 (Sun, 27 Jan 2008)
New Revision: 3877

Modified:
   xfmpc/trunk/src/interface.c
   xfmpc/trunk/src/mpdclient.c
   xfmpc/trunk/src/mpdclient.h
Log:
Watch the status inside XfmpcMpdclient and update only some parts of XfmpcInterface where the status has changed.
* src/mpdclient.c, src/mpdclient.h:
  - Remove all the useless reconnections
  - Add status functions xfmpc_mpdclient_update_status,
    cb_xfmpc_mpdclient_status_changed, and xfmpc_mpdclient_update_status to:
    update the current status of mpd, a callback to mark the different status
    bits, and a function that returns a boolean to know if a bit was changed
    and reset it to 0 as it were read
* src/mpdclient.c(xfmpc_mpdclient_connect):
  - Send password
* src/interface.c:
  - Remove _refresh_volume and the delay to update the volume;  instead update
    the volume in mpd on value-changed signals from GtkVolumeButton
  - Make use of the changes in XfmpcMpdclient inside xfmpc_interface_refresh
* src/interface.c(xfmpc_interface_finalize):
  - Free XfmpcMpdclient


Modified: xfmpc/trunk/src/interface.c
===================================================================
--- xfmpc/trunk/src/interface.c	2008-01-26 15:32:07 UTC (rev 3876)
+++ xfmpc/trunk/src/interface.c	2008-01-27 13:54:59 UTC (rev 3877)
@@ -79,8 +79,6 @@
   GtkWidget            *progress_bar; /* position in song */
   GtkWidget            *title;
   GtkWidget            *subtitle;
-
-  gint                  volume;
 };
 
 
@@ -145,22 +143,22 @@
 xfmpc_interface_init (XfmpcInterface *interface)
 {
   interface->priv = g_slice_new0 (XfmpcInterfacePriv);
-  interface->priv->mpdclient = xfmpc_mpdclient_new ();
-  interface->priv->volume = -1;
-
   interface->preferences = xfmpc_preferences_get ();
 
-  gint posx, posy;
-  g_object_get (G_OBJECT (interface->preferences),
-                "last-window-posx", &posx,
-                "last-window-posy", &posy,
-                NULL);
+  /* === Mpd client === */
+  interface->priv->mpdclient = xfmpc_mpdclient_new ();
 
   /* === Window === */
   gtk_window_set_icon_name (GTK_WINDOW (interface), "stock_volume");
   gtk_window_set_title (GTK_WINDOW (interface), _("Xfmpc"));
   gtk_container_set_border_width (GTK_CONTAINER (interface), BORDER);
   g_signal_connect (G_OBJECT (interface), "delete-event", G_CALLBACK (xfmpc_interface_closed), interface);
+
+  gint posx, posy;
+  g_object_get (G_OBJECT (interface->preferences),
+                "last-window-posx", &posx,
+                "last-window-posy", &posy,
+                NULL);
   if (G_LIKELY (posx != -1 && posy != -1))
     gtk_window_move (GTK_WINDOW (interface), posx, posy);
 
@@ -275,9 +273,12 @@
 {
   XfmpcInterface *interface = XFMPC_INTERFACE (object);
   g_object_unref (G_OBJECT (interface->preferences));
+  xfmpc_mpdclient_free (interface->priv->mpdclient);
   (*G_OBJECT_CLASS (parent_class)->finalize) (object);
 }
 
+
+
 GtkWidget*
 xfmpc_interface_new ()
 {
@@ -343,7 +344,8 @@
 xfmpc_interface_volume_changed (XfmpcInterface *interface,
                                 gdouble value)
 {
-  interface->priv->volume = value * 100;
+  guint8 volume = value * 100;
+  xfmpc_mpdclient_set_volume (interface->priv->mpdclient, volume);
 }
 
 void
@@ -354,20 +356,6 @@
 }
 
 void
-xfmpc_interface_refresh_volume (XfmpcInterface *interface)
-{
-  XfmpcMpdclient *mpdclient = interface->priv->mpdclient;
-
-  if (G_UNLIKELY (interface->priv->volume != -1))
-    {
-      xfmpc_mpdclient_set_volume (mpdclient, interface->priv->volume);
-      interface->priv->volume = -1;
-    }
-
-  xfmpc_interface_set_volume (interface, xfmpc_mpdclient_get_volume (mpdclient));
-}
-
-void
 xfmpc_interface_set_time (XfmpcInterface *interface,
                           gint time,
                           gint time_total)
@@ -397,68 +385,78 @@
   XfmpcMpdclient       *mpdclient = interface->priv->mpdclient;
   gchar                *text = NULL;
 
-  TRACE ("ping");
   if (G_UNLIKELY (xfmpc_mpdclient_connect (mpdclient) == FALSE))
     {
-      DBG ("Couldn't connect to host %s port %d", mpdclient->host, mpdclient->port);
+      g_warning ("Failed to connect to MPD (host %s port %d)", mpdclient->host, mpdclient->port);
       xfmpc_mpdclient_disconnect (mpdclient);
       xfmpc_interface_set_pp (interface, FALSE);
       xfmpc_interface_set_time (interface, 0, 0);
       xfmpc_interface_set_volume (interface, 0);
-      interface->priv->volume = -1; /* override the volume changed signal */
       xfmpc_interface_set_title (interface, _("Not connected"));
       xfmpc_interface_set_subtitle (interface, PACKAGE_STRING);
-      /* Return FALSE to kill the refresh timeout and start a reconnection timeout */
+
+      /* Start a reconnection timeout and return FALSE to kill the refresh timeout */
       g_timeout_add (15000, (GSourceFunc)xfmpc_interface_reconnect, interface);
       return FALSE;
     }
-  else if (G_UNLIKELY (xfmpc_mpdclient_is_stopped (mpdclient) == TRUE))
+
+  xfmpc_mpdclient_update_status (mpdclient);
+
+  if (G_UNLIKELY (xfmpc_mpdclient_status (mpdclient, VOLUME_CHANGED)))
     {
-      xfmpc_interface_set_pp (interface, FALSE);
-      xfmpc_interface_set_time (interface, 0, 0);
-      xfmpc_interface_refresh_volume (interface);
-      xfmpc_interface_set_title (interface, _("Stopped"));
-      xfmpc_interface_set_subtitle (interface, PACKAGE_STRING);
+      /* volume */
+      xfmpc_interface_set_volume (interface, xfmpc_mpdclient_get_volume (mpdclient));
+    }
 
-      /* Some hack to invalidate the state.  Hint: FIXME */
-      xfmpc_mpdclient_disconnect (mpdclient);
+  if (G_UNLIKELY (xfmpc_mpdclient_is_stopped (mpdclient)))
+    {
+      /* stopped */
+      if (xfmpc_mpdclient_status (mpdclient, STOP_CHANGED))
+        {
+          xfmpc_interface_set_pp (interface, FALSE);
+          xfmpc_interface_set_time (interface, 0, 0);
+          xfmpc_interface_set_title (interface, _("Stopped"));
+          xfmpc_interface_set_subtitle (interface, PACKAGE_STRING);
+        }
 
       return TRUE;
     }
 
-  /* play/pause */
-  xfmpc_interface_set_pp (interface, xfmpc_mpdclient_is_playing (mpdclient));
+  if (G_LIKELY (xfmpc_mpdclient_status (mpdclient, TIME_CHANGED)))
+    {
+      /* song time */
+      xfmpc_interface_set_time (interface,
+                                xfmpc_mpdclient_get_time (mpdclient),
+                                xfmpc_mpdclient_get_total_time (mpdclient));
+    }
 
-  /* song time */
-  xfmpc_interface_set_time (interface,
-                            xfmpc_mpdclient_get_time (mpdclient),
-                            xfmpc_mpdclient_get_total_time (mpdclient));
+  if (G_UNLIKELY (xfmpc_mpdclient_status (mpdclient, PP_CHANGED)))
+    {
+      /* play/pause */
+      xfmpc_interface_set_pp (interface, xfmpc_mpdclient_is_playing (mpdclient));
+    }
 
-  /* volume */
-  xfmpc_interface_refresh_volume (interface);
+  if (G_UNLIKELY (xfmpc_mpdclient_status (mpdclient, SONG_CHANGED)))
+    {
+      /* title */
+      xfmpc_interface_set_title (interface, xfmpc_mpdclient_get_title (mpdclient));
 
-  /* title */
-  xfmpc_interface_set_title (interface, xfmpc_mpdclient_get_title (mpdclient));
+      /* subtitle "by \"artist\" from \"album\" (year)" */
+      text = g_strdup_printf (_("by \"%s\" from \"%s\" (%s)"),
+                              xfmpc_mpdclient_get_artist (mpdclient),
+                              xfmpc_mpdclient_get_album (mpdclient),
+                              xfmpc_mpdclient_get_date (mpdclient));
+      /* text = xfmpc_interface_get_subtitle (interface); to avoid "n/a" values */
+      xfmpc_interface_set_subtitle (interface, text);
+      g_free (text);
+    }
 
-  /* subtitle "by \"artist\" from \"album\" (year)" */
-  text = g_strdup_printf (_("by \"%s\" from \"%s\" (%s)"),
-                          xfmpc_mpdclient_get_artist (mpdclient),
-                          xfmpc_mpdclient_get_album (mpdclient),
-                          xfmpc_mpdclient_get_date (mpdclient));
-  /* text = xfmpc_interface_get_subtitle (interface); to avoid "n/a" values */
-  xfmpc_interface_set_subtitle (interface, text);
-  g_free (text);
-
-  /* Some hack to invalidate the cached state.  Hint: FIXME */
-  xfmpc_mpdclient_disconnect (mpdclient);
-
   return TRUE;
 }
 
 static gboolean
 xfmpc_interface_reconnect (XfmpcInterface *interface)
 {
-  TRACE ("pong");
   if (G_UNLIKELY (xfmpc_mpdclient_connect (interface->priv->mpdclient) == FALSE))
     return TRUE;
 

Modified: xfmpc/trunk/src/mpdclient.c
===================================================================
--- xfmpc/trunk/src/mpdclient.c	2008-01-26 15:32:07 UTC (rev 3876)
+++ xfmpc/trunk/src/mpdclient.c	2008-01-27 13:54:59 UTC (rev 3877)
@@ -30,17 +30,29 @@
 
 
 
+static void             cb_xfmpc_mpdclient_status_changed       (MpdObj *mi,
+                                                                 ChangedStatusType what,
+                                                                 gpointer user_data);
+
+
+
 XfmpcMpdclient *
 xfmpc_mpdclient_new ()
 {
   XfmpcMpdclient *mpdclient = g_slice_new0 (XfmpcMpdclient);
   xfmpc_mpdclient_initenv (mpdclient);
   mpdclient->mi = mpd_new (mpdclient->host, mpdclient->port, mpdclient->passwd);
-
+  mpd_signal_connect_status_changed (mpdclient->mi, (StatusChangedCallback)cb_xfmpc_mpdclient_status_changed, mpdclient);
   return mpdclient;
 }
 
 void
+xfmpc_mpdclient_free (XfmpcMpdclient *mpdclient)
+{
+  mpd_free (mpdclient->mi);
+}
+
+void
 xfmpc_mpdclient_initenv (XfmpcMpdclient *mpdclient)
 {
   /* Hostname */
@@ -74,6 +86,8 @@
   if (mpd_connect (mpdclient->mi) != MPD_OK)
     return FALSE;
 
+  mpd_send_password (mpdclient->mi);
+
   return TRUE;
 }
 
@@ -90,16 +104,9 @@
   return mpd_check_connected (mpdclient->mi);
 }
 
-void
-xfmpc_mpdclient_free (XfmpcMpdclient *mpdclient)
-{
-  mpd_free (mpdclient->mi);
-}
-
 gboolean
 xfmpc_mpdclient_previous (XfmpcMpdclient *mpdclient)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_player_prev (mpdclient->mi) != MPD_OK)
     return FALSE;
   else
@@ -118,7 +125,6 @@
 gboolean
 xfmpc_mpdclient_play (XfmpcMpdclient *mpdclient)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_player_play (mpdclient->mi) != MPD_OK)
     return FALSE;
   else
@@ -128,7 +134,6 @@
 gboolean
 xfmpc_mpdclient_pause (XfmpcMpdclient *mpdclient)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_player_pause (mpdclient->mi) != MPD_OK)
     return FALSE;
   else
@@ -138,7 +143,6 @@
 gboolean
 xfmpc_mpdclient_stop (XfmpcMpdclient *mpdclient)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_player_stop (mpdclient->mi) != MPD_OK)
     return FALSE;
   else
@@ -148,7 +152,6 @@
 gboolean
 xfmpc_mpdclient_next (XfmpcMpdclient *mpdclient)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_player_next (mpdclient->mi) != MPD_OK)
     return FALSE;
   else
@@ -159,7 +162,6 @@
 xfmpc_mpdclient_set_volume (XfmpcMpdclient *mpdclient,
                             guint8 volume)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_status_set_volume (mpdclient->mi, volume) < 0)
     return FALSE;
   else
@@ -170,26 +172,12 @@
 xfmpc_mpdclient_set_song_time (XfmpcMpdclient *mpdclient,
                                guint time)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   if (mpd_player_seek (mpdclient->mi, time) != MPD_OK)
     return FALSE;
   else
     return TRUE;
 }
 
-gboolean
-xfmpc_mpdclient_is_playing (XfmpcMpdclient *mpdclient)
-{
-  xfmpc_mpdclient_connect (mpdclient);
-  return mpd_player_get_state (mpdclient->mi) == MPD_PLAYER_PLAY;
-}
-
-gboolean
-xfmpc_mpdclient_is_stopped (XfmpcMpdclient *mpdclient)
-{
-  return mpd_player_get_state (mpdclient->mi) == MPD_PLAYER_STOP;
-}
-
 const gchar *
 xfmpc_mpdclient_get_artist (XfmpcMpdclient *mpdclient)
 {
@@ -251,7 +239,6 @@
 gint
 xfmpc_mpdclient_get_total_time (XfmpcMpdclient *mpdclient)
 {
-  xfmpc_mpdclient_connect (mpdclient);
   return mpd_status_get_total_song_time (mpdclient->mi);
 }
 
@@ -262,3 +249,74 @@
   return (volume < 0) ? 100 : volume;
 }
 
+gboolean
+xfmpc_mpdclient_is_playing (XfmpcMpdclient *mpdclient)
+{
+  return mpd_player_get_state (mpdclient->mi) == MPD_PLAYER_PLAY;
+}
+
+gboolean
+xfmpc_mpdclient_is_stopped (XfmpcMpdclient *mpdclient)
+{
+  return mpd_player_get_state (mpdclient->mi) == MPD_PLAYER_STOP;
+}
+
+void
+xfmpc_mpdclient_update_status (XfmpcMpdclient *mpdclient)
+{
+  mpd_status_update (mpdclient->mi);
+}
+
+gboolean
+xfmpc_mpdclient_status (XfmpcMpdclient *mpdclient,
+                        gint bits)
+{
+  if (mpdclient->status & bits)
+    {
+      mpdclient->status &= ~bits;
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static void
+cb_xfmpc_mpdclient_status_changed (MpdObj *mi,
+                                   ChangedStatusType what,
+                                   gpointer user_data)
+{
+  XfmpcMpdclient *mpdclient = user_data;
+  g_return_if_fail (G_LIKELY (NULL != user_data));
+
+  if (what & MPD_CST_STATE)
+    {
+      switch (mpd_player_get_state (mi))
+        {
+        case MPD_PLAYER_STOP:
+          mpdclient->status |= STOP_CHANGED;
+          mpdclient->status |= SONG_CHANGED; /* as to say that the next time
+                                                the song plays again, it needs
+                                                to update its title in the
+                                                interface */
+          break;
+
+        case MPD_PLAYER_PLAY:
+        case MPD_PLAYER_PAUSE:
+          mpdclient->status |= PP_CHANGED;
+          break;
+
+        default:
+          break;
+        }
+    }
+
+  if (what & MPD_CST_SONGID)
+    mpdclient->status |= SONG_CHANGED;
+
+  if (what & MPD_CST_VOLUME)
+    mpdclient->status |= VOLUME_CHANGED;
+
+  if (what & (MPD_CST_ELAPSED_TIME|MPD_CST_TOTAL_TIME))
+    mpdclient->status |= TIME_CHANGED;
+}
+

Modified: xfmpc/trunk/src/mpdclient.h
===================================================================
--- xfmpc/trunk/src/mpdclient.h	2008-01-26 15:32:07 UTC (rev 3876)
+++ xfmpc/trunk/src/mpdclient.h	2008-01-27 13:54:59 UTC (rev 3877)
@@ -22,6 +22,17 @@
 #include <glib.h>
 #include <libmpd/libmpd.h>
 
+G_BEGIN_DECLS
+
+typedef enum
+{
+  STOP_CHANGED          = 1 << 0,
+  PP_CHANGED            = 1 << 1,
+  SONG_CHANGED          = 1 << 2,
+  VOLUME_CHANGED        = 1 << 3,
+  TIME_CHANGED          = 1 << 4,
+} StatusField;
+
 typedef struct _XfmpcMpdclient          XfmpcMpdclient;
 
 struct _XfmpcMpdclient
@@ -31,7 +42,7 @@
   guint                 port;
   gchar                *passwd;
 
-  guint8                volume;
+  StatusField           status;
 };
 
 XfmpcMpdclient *        xfmpc_mpdclient_new                     ();
@@ -62,10 +73,6 @@
                                                                  guint8 volume);
 gboolean                xfmpc_mpdclient_set_song_time           (XfmpcMpdclient *mpdclient,
                                                                  guint time);
-gboolean                xfmpc_mpdclient_is_playing              (XfmpcMpdclient *mpdclient);
-
-gboolean                xfmpc_mpdclient_is_stopped              (XfmpcMpdclient *mpdclient);
-
 const gchar *           xfmpc_mpdclient_get_artist              (XfmpcMpdclient *mpdclient);
 
 const gchar *           xfmpc_mpdclient_get_title               (XfmpcMpdclient *mpdclient);
@@ -80,5 +87,16 @@
 
 guint8                  xfmpc_mpdclient_get_volume              (XfmpcMpdclient *mpdclient);
 
+gboolean                xfmpc_mpdclient_is_playing              (XfmpcMpdclient *mpdclient);
+
+gboolean                xfmpc_mpdclient_is_stopped              (XfmpcMpdclient *mpdclient);
+
+void                    xfmpc_mpdclient_update_status           (XfmpcMpdclient *mpdclient);
+
+gboolean                xfmpc_mpdclient_status                  (XfmpcMpdclient *mpdclient,
+                                                                 gint bits);
+
+G_END_DECLS
+
 #endif
 




More information about the Goodies-commits mailing list