Code

mpdclient: try IP connect if default local socket path fails
[ncmpc.git] / src / mpdclient.h
index 50b8e9a92fe014d997a196042b1f66110715dd54..1d3b6e0b547d61f292480d6eff9da533230e4a03 100644 (file)
 #ifndef MPDCLIENT_H
 #define MPDCLIENT_H
-#include "libmpdclient.h"
 
-#define MPD_VERSION(c,x,y,z) (c->connection->version[0]*10000+c->connection->version[1]*100+c->connection->version[2] >= \
-                              x*10000+y*100+z)
-
-/****************************************************************************/
-/* Playlist */
-/****************************************************************************/
-
-typedef struct
-{
-  /* playlist id */
-  long long id;
-  /* list length */
-  guint length;
-  /* true if the list is updated */
-  gboolean updated;
-  /* the list */
-  GList *list;
-
-} mpdclient_playlist_t;
+#include "config.h"
+#include "playlist.h"
+#include "Compiler.h"
+
+#include <mpd/client.h>
+
+struct filelist;
+
+struct mpdclient {
+#ifdef ENABLE_ASYNC_CONNECT
+       /**
+        * These settings are used to connect to MPD asynchronously.
+        */
+       struct mpd_settings *settings;
+
+#ifndef WIN32
+       /**
+        * A second set of settings, just in case #settings did not
+        * work.  This is only used if #settings refers to a local
+        * socket path, and this one is supposed to be a fallback to
+        * IP on the default port (6600).
+        */
+       struct mpd_settings *settings2;
+#endif
 
+#else
+       const char *host;
+       unsigned port;
+#endif
 
-typedef struct
-{
-  guint flags;
-  mpd_InfoEntity *entity;
-} filelist_entry_t;
+       unsigned timeout_ms;
 
-typedef struct
-{
-  /* path */
-  gchar *path;
-  /* list length */
-  guint length;
-  /* true if the list is updated */
-  gboolean updated;
-  /* the list */
-  GList *list;
-
-} mpdclient_filelist_t;
-
-typedef struct
-{
-  /* playlist */
-  mpdclient_playlist_t playlist;
+       const char *password;
 
-  /* Callbacks */
-  GList *error_callbacks;
-  GList *playlist_callbacks;
-  GList *browse_callbacks;
+       /* playlist */
+       struct mpdclient_playlist playlist;
 
-  mpd_Connection *connection;
-  mpd_Status     *status;
-  mpd_Song       *song;
+#ifdef ENABLE_ASYNC_CONNECT
+       struct aconnect *async_connect;
+#endif
 
-  gboolean       need_update;
+       struct mpd_connection *connection;
 
-} mpdclient_t;
+       /**
+        * Tracks idle events.  It is automatically called by
+        * mpdclient_get_connection().
+        */
+       struct mpd_glib_source *source;
 
-/** functions ***************************************************************/
-mpdclient_t *mpdclient_new(void);
-mpdclient_t *mpdclient_free(mpdclient_t *c);
-gint mpdclient_connect(mpdclient_t *c, gchar *host, gint port,
-                      gfloat timeout, gchar *password);
-gint mpdclient_disconnect(mpdclient_t *c);
-gint mpdclient_update(mpdclient_t *c);
+       struct mpd_status *status;
+       const struct mpd_song *song;
 
+       /**
+        * The GLib source id which re-enters MPD idle mode before the
+        * next main loop interation.
+        */
+       unsigned enter_idle_source_id;
 
-/*** MPD Commands  **********************************************************/
-gint mpdclient_cmd_play(mpdclient_t *c, gint index);
-gint mpdclient_cmd_pause(mpdclient_t *c, gint value);
-gint mpdclient_cmd_stop(mpdclient_t *c);
-gint mpdclient_cmd_next(mpdclient_t *c);
-gint mpdclient_cmd_prev(mpdclient_t *c);
-gint mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos);
-gint mpdclient_cmd_shuffle(mpdclient_t *c);
-gint mpdclient_cmd_clear(mpdclient_t *c);
-gint mpdclient_cmd_repeat(mpdclient_t *c, gint value);
-gint mpdclient_cmd_random(mpdclient_t *c, gint value);
-gint mpdclient_cmd_crossfade(mpdclient_t *c, gint value);
-gint mpdclient_cmd_db_update(mpdclient_t *c);
-gint mpdclient_cmd_volume(mpdclient_t *c, gint value);
+       /**
+        * This attribute is incremented whenever the connection changes
+        * (i.e. on disconnection and (re-)connection).
+        */
+       unsigned connection_id;
 
-gint mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song);
-gint mpdclient_cmd_delete(mpdclient_t *c, gint index);
-gint mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index);
+       int volume;
 
-gint mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename);
-gint mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8);
-gint mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8);
+       /**
+        * A bit mask of idle events occurred since the last update.
+        */
+       enum mpd_idle events;
 
+#if defined(ENABLE_ASYNC_CONNECT) && !defined(WIN32)
+       bool connecting2;
+#endif
 
-/*** error callbacks *****************************************************/ 
+       /**
+        * This attribute is true when the connection is currently in
+        * "idle" mode, and the #mpd_glib_source waits for an event.
+        */
+       bool idle;
+
+       /**
+        * Is MPD currently playing?
+        */
+       bool playing;
+};
+
+enum {
+       /**
+        * all idle events the version of libmpdclient, ncmpc is compiled
+        * against, supports
+        */
+       MPD_IDLE_ALL = MPD_IDLE_DATABASE
+               | MPD_IDLE_STORED_PLAYLIST
+               | MPD_IDLE_QUEUE
+               | MPD_IDLE_PLAYER
+               | MPD_IDLE_MIXER
+               | MPD_IDLE_OUTPUT
+               | MPD_IDLE_OPTIONS
+               | MPD_IDLE_UPDATE
+               | MPD_IDLE_STICKER
+               | MPD_IDLE_SUBSCRIPTION
+               | MPD_IDLE_MESSAGE
+};
 
-#define IS_ACK_ERROR(n)       (n & MPD_ERROR_ACK)
-#define GET_ACK_ERROR_CODE(n) ((n & 0xFF00) >> 8)
+/** functions ***************************************************************/
 
-typedef void (*mpdc_error_cb_t) (mpdclient_t *c, gint error, gchar *msg);
+bool
+mpdclient_handle_error(struct mpdclient *c);
 
-void mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb);
-void mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb);
+static inline bool
+mpdclient_finish_command(struct mpdclient *c)
+{
+       return mpd_response_finish(c->connection)
+               ? true : mpdclient_handle_error(c);
+}
+
+struct mpdclient *
+mpdclient_new(const gchar *host, unsigned port,
+             unsigned timeout_ms, const gchar *password);
+
+void mpdclient_free(struct mpdclient *c);
+
+/**
+ * Determine a human-readable "name" of the settings currently used to
+ * connect to MPD.
+ *
+ * @return an allocated string that needs to be freed (with g_free())
+ * by the caller
+ */
+char *
+mpdclient_settings_name(const struct mpdclient *c);
+
+gcc_pure
+static inline bool
+mpdclient_is_connected(const struct mpdclient *c)
+{
+       return c->connection != NULL;
+}
+
+/**
+ * Is this object "dead"?  i.e. not connected and not currently doing
+ * anything to connect.
+ */
+gcc_pure
+static inline bool
+mpdclient_is_dead(const struct mpdclient *c)
+{
+       return c->connection == NULL
+#ifdef ENABLE_ASYNC_CONNECT
+               && c->async_connect == NULL
+#endif
+               ;
+}
 
-/*** playlist functions  **************************************************/
+gcc_pure
+static inline bool
+mpdclient_is_playing(const struct mpdclient *c)
+{
+       return c->status != NULL &&
+               (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
+                mpd_status_get_state(c->status) == MPD_STATE_PAUSE);
+}
+
+gcc_pure
+static inline const struct mpd_song *
+mpdclient_get_current_song(const struct mpdclient *c)
+{
+       return c->song != NULL && mpdclient_is_playing(c)
+               ? c->song
+               : NULL;
+}
 
-/* free a playlist */
-gint mpdclient_playlist_free(mpdclient_playlist_t *playlist);
-/* update the complete playlist */
-gint mpdclient_playlist_update(mpdclient_t *c);
-/* get playlist changes */
-gint mpdclient_playlist_update_changes(mpdclient_t *c);
+void
+mpdclient_connect(struct mpdclient *c);
 
-GList *playlist_lookup(mpdclient_t *c, gint id);
-mpd_Song *playlist_lookup_song(mpdclient_t *c, gint id);
-mpd_Song *playlist_get_song(mpdclient_t *c, gint index);
-gint playlist_get_index(mpdclient_t *c, mpd_Song *song);
-gint playlist_get_index_from_id(mpdclient_t *c, gint id);
-gint playlist_get_index_from_file(mpdclient_t *c, gchar *filename);
+void
+mpdclient_disconnect(struct mpdclient *c);
 
+bool
+mpdclient_update(struct mpdclient *c);
 
+struct mpd_connection *
+mpdclient_get_connection(struct mpdclient *c);
 
-/*** mpdclient playlist callbacks *****************************************/
+/*** MPD Commands  **********************************************************/
 
-#define PLAYLIST_EVENT_UPDATED     0x01
-#define PLAYLIST_EVENT_CLEAR       0x02
-#define PLAYLIST_EVENT_DELETE      0x03
-#define PLAYLIST_EVENT_ADD         0x04
-#define PLAYLIST_EVENT_MOVE        0x05
+bool
+mpdclient_cmd_crop(struct mpdclient *c);
 
+bool
+mpdclient_cmd_clear(struct mpdclient *c);
 
-typedef void (*mpdc_list_cb_t) (mpdclient_t *c, int event, gpointer data);
+bool
+mpdclient_cmd_volume(struct mpdclient *c, gint value);
 
-/* install a playlist callback function */
-void mpdclient_install_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb);
+bool
+mpdclient_cmd_volume_up(struct mpdclient *c);
 
-/* remove a playlist callback function */
-void mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb);
+bool
+mpdclient_cmd_volume_down(struct mpdclient *c);
 
+bool
+mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path);
 
-/* issue a playlist callback */
-void mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data);
+bool
+mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song);
 
+bool
+mpdclient_cmd_delete(struct mpdclient *c, gint index);
 
-/*** filelist functions  ***************************************************/
-mpdclient_filelist_t *mpdclient_filelist_free(mpdclient_filelist_t *filelist);
-mpdclient_filelist_t *mpdclient_filelist_get(mpdclient_t *c, gchar *path);
-mpdclient_filelist_t *mpdclient_filelist_update(mpdclient_t *c, 
-                                               mpdclient_filelist_t *flist);
+bool
+mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end);
 
-#define HIGHLIGHT  (0x01)
-void mpdclient_filelist_set_flags(mpdclient_filelist_t *flist, 
-                                 mpd_Song *song,
-                                 guint flags);
+bool
+mpdclient_cmd_move(struct mpdclient *c, unsigned dest, unsigned src);
 
-void mpdclient_filelist_clear_flags(mpdclient_filelist_t *flist);
-void mpdclient_filelist_clear_flags(mpdclient_filelist_t *flist);
-filelist_entry_t *mpdclient_filelist_find_song(mpdclient_filelist_t *flist,
-                                              mpd_Song *song);
+bool
+mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel);
 
-/*** mpdclient browse callbacks ********************************************/
+bool
+mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel);
 
-#define BROWSE_DB_UPDATED          0x01
-#define BROWSE_PLAYLIST_SAVED      0x02
-#define BROWSE_PLAYLIST_DELETED    0x03
+bool
+mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
+                          const char *text);
 
+bool
+mpdclient_send_read_messages(struct mpdclient *c);
 
-/* install a playlist callback function */
-void mpdclient_install_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb);
+struct mpd_message *
+mpdclient_recv_message(struct mpdclient *c);
 
-/* remove a playlist callback function */
-void mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb);
+/*** playlist functions  **************************************************/
 
+/* update the complete playlist */
+bool
+mpdclient_playlist_update(struct mpdclient *c);
 
-/* issue a playlist callback */
-void mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data);
+/* get playlist changes */
+bool
+mpdclient_playlist_update_changes(struct mpdclient *c);
 
+/* add all songs in filelist to the playlist */
+bool
+mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl);
 
 #endif