Code

main: move mpd_glib_new() call to mpdclient.c
[ncmpc.git] / src / mpdclient.c
index 04c1435e213441bc9f1219d7cff532525c280849..f0076b6047442e4682097da63883a1ae9029a090 100644 (file)
@@ -1,25 +1,25 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
-
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "mpdclient.h"
+#include "callbacks.h"
 #include "filelist.h"
-#include "screen_client.h"
 #include "config.h"
 #include "options.h"
 #include "strfsong.h"
 
 #define BUFSIZE 1024
 
-/* sort by list-format */
+/* sort by song format */
 gint
 compare_filelistentry_format(gconstpointer filelist_entry1,
-                            gconstpointer filelist_entry2)
+                            gconstpointer filelist_entry2,
+                            const char *song_format)
 {
-       const struct mpd_entity *e1, *e2;
-       char key1[BUFSIZE], key2[BUFSIZE];
-       int n = 0;
-
-       e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
-       e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
+       const struct mpd_entity *e1 =
+               ((const struct filelist_entry *)filelist_entry1)->entity;
+       const struct mpd_entity *e2 =
+               ((const struct filelist_entry *)filelist_entry2)->entity;
 
+       int n = 0;
        if (e1 && e2 &&
            mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
            mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
-               strfsong(key1, BUFSIZE, options.list_format, mpd_entity_get_song(e1));
-               strfsong(key2, BUFSIZE, options.list_format, mpd_entity_get_song(e2));
+               char key1[BUFSIZE], key2[BUFSIZE];
+               strfsong(key1, BUFSIZE, song_format, mpd_entity_get_song(e1));
+               strfsong(key2, BUFSIZE, song_format, mpd_entity_get_song(e2));
                n = strcmp(key1,key2);
        }
 
@@ -72,10 +73,10 @@ mpdclient_handle_error(struct mpdclient *c)
 
        if (error == MPD_ERROR_SERVER &&
            mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
-           screen_auth(c))
+           mpdclient_auth_callback(c))
                return true;
 
-       mpdclient_ui_error(mpd_connection_get_error_message(c->connection));
+       mpdclient_error_callback(mpd_connection_get_error_message(c->connection));
 
        if (!mpd_connection_clear_error(c->connection))
                mpdclient_disconnect(c);
@@ -83,19 +84,10 @@ mpdclient_handle_error(struct mpdclient *c)
        return false;
 }
 
-static bool
-mpdclient_finish_command(struct mpdclient *c)
-{
-       return mpd_response_finish(c->connection)
-               ? true : mpdclient_handle_error(c);
-}
-
 struct mpdclient *
 mpdclient_new(void)
 {
-       struct mpdclient *c;
-
-       c = g_new0(struct mpdclient, 1);
+       struct mpdclient *c = g_new0(struct mpdclient, 1);
        playlist_init(&c->playlist);
        c->volume = -1;
        c->events = 0;
@@ -122,8 +114,10 @@ mpdclient_disconnect(struct mpdclient *c)
                c->idle = false;
        }
 
-       if (c->connection)
+       if (c->connection) {
                mpd_connection_free(c->connection);
+               ++c->connection_id;
+       }
        c->connection = NULL;
 
        if (c->status)
@@ -136,24 +130,22 @@ mpdclient_disconnect(struct mpdclient *c)
                c->song = NULL;
 
        /* everything has changed after a disconnect */
-       c->events |= 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;
+       c->events |= MPD_IDLE_ALL;
 }
 
 bool
 mpdclient_connect(struct mpdclient *c,
                  const gchar *host,
                  gint port,
-                 gfloat _timeout,
+                 unsigned timeout_ms,
                  const gchar *password)
 {
        /* close any open connection */
-       if( c->connection )
+       if (c->connection)
                mpdclient_disconnect(c);
 
        /* connect to MPD */
-       c->connection = mpd_connection_new(host, port, _timeout * 1000);
+       c->connection = mpd_connection_new(host, port, timeout_ms);
        if (c->connection == NULL)
                g_error("Out of memory");
 
@@ -170,6 +162,11 @@ mpdclient_connect(struct mpdclient *c,
                return false;
        }
 
+       c->source = mpd_glib_new(c->connection,
+                                mpdclient_idle_callback, c);
+
+       ++c->connection_id;
+
        return true;
 }
 
@@ -177,18 +174,12 @@ bool
 mpdclient_update(struct mpdclient *c)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       bool retval;
 
        c->volume = -1;
 
        if (connection == NULL)
                return false;
 
-       /* always announce these options as long as we don't have
-          "idle" support */
-       if (c->source == NULL)
-               c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
-
        /* free the old status */
        if (c->status)
                mpd_status_free(c->status);
@@ -198,41 +189,29 @@ mpdclient_update(struct mpdclient *c)
        if (c->status == NULL)
                return mpdclient_handle_error(c);
 
-       if (c->source == NULL &&
-           c->update_id != mpd_status_get_update_id(c->status)) {
-               c->events |= MPD_IDLE_UPDATE;
-
-               if (c->update_id > 0)
-                       c->events |= MPD_IDLE_DATABASE;
-       }
-
        c->update_id = mpd_status_get_update_id(c->status);
 
-       if (c->source == NULL &&
-           c->volume != mpd_status_get_volume(c->status))
-               c->events |= MPD_IDLE_MIXER;
-
        c->volume = mpd_status_get_volume(c->status);
 
        /* check if the playlist needs an update */
        if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
-               if (c->source == NULL)
-                       c->events |= MPD_IDLE_PLAYLIST;
+               bool retval;
 
                if (!playlist_is_empty(&c->playlist))
                        retval = mpdclient_playlist_update_changes(c);
                else
                        retval = mpdclient_playlist_update(c);
-       } else
-               retval = true;
+               if (!retval)
+                       return false;
+       }
 
        /* update the current song */
-       if (!c->song || mpd_status_get_song_id(c->status)) {
+       if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
                c->song = playlist_get_song(&c->playlist,
                                            mpd_status_get_song_pos(c->status));
        }
 
-       return retval;
+       return true;
 }
 
 struct mpd_connection *
@@ -256,6 +235,21 @@ mpdclient_put_connection(struct mpdclient *c)
        }
 }
 
+static struct mpd_status *
+mpdclient_recv_status(struct mpdclient *c)
+{
+       assert(c->connection != NULL);
+
+       struct mpd_status *status = mpd_recv_status(c->connection);
+       if (status == NULL) {
+               mpdclient_handle_error(c);
+               return NULL;
+       }
+
+       if (c->status != NULL)
+               mpd_status_free(c->status);
+       return c->status = status;
+}
 
 /****************************************************************************/
 /*** MPD Commands  **********************************************************/
@@ -264,33 +258,24 @@ mpdclient_put_connection(struct mpdclient *c)
 bool
 mpdclient_cmd_crop(struct mpdclient *c)
 {
-       struct mpd_connection *connection = mpdclient_get_connection(c);
-       struct mpd_status *status;
-       bool playing;
-       int length, current;
-
-       if (connection == NULL)
+       if (!mpdclient_is_playing(c))
                return false;
 
-       status = mpd_run_status(connection);
-       if (status == NULL)
-               return mpdclient_handle_error(c);
-
-       playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
-               mpd_status_get_state(status) == MPD_STATE_PAUSE;
-       length = mpd_status_get_queue_length(status);
-       current = mpd_status_get_song_pos(status);
-
-       mpd_status_free(status);
-
-       if (!playing || length < 2)
+       int length = mpd_status_get_queue_length(c->status);
+       int current = mpd_status_get_song_pos(c->status);
+       if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
                return true;
 
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
        mpd_command_list_begin(connection, false);
 
-       while (--length >= 0)
-               if (length != current)
-                       mpd_send_delete(connection, length);
+       if (current < length - 1)
+               mpd_send_delete_range(connection, current + 1, length);
+       if (current > 0)
+               mpd_send_delete_range(connection, 0, current);
 
        mpd_command_list_end(connection);
 
@@ -301,8 +286,6 @@ bool
 mpdclient_cmd_clear(struct mpdclient *c)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       struct mpd_status *status;
-
        if (connection == NULL)
                return false;
 
@@ -315,13 +298,9 @@ mpdclient_cmd_clear(struct mpdclient *c)
 
        /* receive the new status, store it in the mpdclient struct */
 
-       status = mpd_recv_status(connection);
+       struct mpd_status *status = mpdclient_recv_status(c);
        if (status == NULL)
-               return mpdclient_handle_error(c);
-
-       if (c->status != NULL)
-               mpd_status_free(c->status);
-       c->status = status;
+               return false;
 
        if (!mpd_response_finish(connection))
                return mpdclient_handle_error(c);
@@ -334,6 +313,7 @@ mpdclient_cmd_clear(struct mpdclient *c)
                   reducing the UI latency */
                playlist_clear(&c->playlist);
                c->playlist.version = mpd_status_get_queue_version(status);
+               c->song = NULL;
        }
 
        c->events |= MPD_IDLE_QUEUE;
@@ -397,20 +377,17 @@ mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
        if (connection == NULL)
                return false;
 
-       mpd_send_add(connection, path_utf8);
-       return mpdclient_finish_command(c);
+       return mpd_send_add(connection, path_utf8)?
+               mpdclient_finish_command(c) : false;
 }
 
 bool
 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
 {
-       struct mpd_connection *connection = mpdclient_get_connection(c);
-       struct mpd_status *status;
-       struct mpd_song *new_song;
-
        assert(c != NULL);
        assert(song != NULL);
 
+       struct mpd_connection *connection = mpdclient_get_connection(c);
        if (connection == NULL || c->status == NULL)
                return false;
 
@@ -427,19 +404,16 @@ mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
            !mpd_response_next(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
-       status = mpd_recv_status(connection);
-       if (status != NULL) {
-               if (c->status != NULL)
-                       mpd_status_free(c->status);
-               c->status = status;
-       }
+       struct mpd_status *status = mpdclient_recv_status(c);
+       if (status == NULL)
+               return false;
 
        if (!mpd_response_next(connection))
                return mpdclient_handle_error(c);
 
-       new_song = mpd_recv_song(connection);
+       struct mpd_song *new_song = mpd_recv_song(connection);
        if (!mpd_response_finish(connection) || new_song == NULL) {
                if (new_song != NULL)
                        mpd_song_free(new_song);
@@ -469,8 +443,6 @@ bool
 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       const struct mpd_song *song;
-       struct mpd_status *status;
 
        if (connection == NULL || c->status == NULL)
                return false;
@@ -478,7 +450,7 @@ mpdclient_cmd_delete(struct mpdclient *c, gint idx)
        if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
                return false;
 
-       song = playlist_get(&c->playlist, idx);
+       const struct mpd_song *song = playlist_get(&c->playlist, idx);
 
        /* send the delete command to mpd; at the same time, get the
           new status (to verify the playlist id) */
@@ -489,14 +461,11 @@ mpdclient_cmd_delete(struct mpdclient *c, gint idx)
            !mpd_command_list_end(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
-       status = mpd_recv_status(connection);
-       if (status != NULL) {
-               if (c->status != NULL)
-                       mpd_status_free(c->status);
-               c->status = status;
-       }
+       struct mpd_status *status = mpdclient_recv_status(c);
+       if (status == NULL)
+               return false;
 
        if (!mpd_response_finish(connection))
                return mpdclient_handle_error(c);
@@ -519,52 +488,18 @@ mpdclient_cmd_delete(struct mpdclient *c, gint idx)
        return true;
 }
 
-/**
- * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
- * It emulates the "delete range" command with a list of simple
- * "delete" commands.
- */
-static bool
-mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
-                                   unsigned start, unsigned end)
-{
-       struct mpd_connection *connection = mpdclient_get_connection(c);
-       if (connection == NULL)
-               return false;
-
-       if (!mpd_command_list_begin(connection, false))
-               return mpdclient_handle_error(c);
-
-       for (; start < end; --end)
-               mpd_send_delete(connection, start);
-
-       if (!mpd_command_list_end(connection) ||
-           !mpd_response_finish(connection))
-               return mpdclient_handle_error(c);
-
-       return true;
-}
-
 bool
 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
 {
-       struct mpd_connection *connection;
-       struct mpd_status *status;
-
        if (end == start + 1)
                /* if that's not really a range, we choose to use the
                   safer "deleteid" version */
                return mpdclient_cmd_delete(c, start);
 
-       connection = mpdclient_get_connection(c);
+       struct mpd_connection *connection = mpdclient_get_connection(c);
        if (connection == NULL)
                return false;
 
-       if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
-               return mpdclient_cmd_delete_range_fallback(c, start, end);
-
-       /* MPD 0.16 supports "delete" with a range argument */
-
        /* send the delete command to mpd; at the same time, get the
           new status (to verify the playlist id) */
 
@@ -574,14 +509,11 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
            !mpd_command_list_end(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
-       status = mpd_recv_status(connection);
-       if (status != NULL) {
-               if (c->status != NULL)
-                       mpd_status_free(c->status);
-               c->status = status;
-       }
+       struct mpd_status *status = mpdclient_recv_status(c);
+       if (status == NULL)
+               return false;
 
        if (!mpd_response_finish(connection))
                return mpdclient_handle_error(c);
@@ -609,40 +541,29 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
 }
 
 bool
-mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
+mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
 {
-       struct mpd_connection *connection = mpdclient_get_connection(c);
-       const struct mpd_song *song1, *song2;
-       struct mpd_status *status;
+       if (dest_pos == src_pos)
+               return true;
 
+       struct mpd_connection *connection = mpdclient_get_connection(c);
        if (connection == NULL)
                return false;
 
-       if (old_index == new_index || new_index < 0 ||
-           (guint)new_index >= c->playlist.list->len)
-               return false;
-
-       song1 = playlist_get(&c->playlist, old_index);
-       song2 = playlist_get(&c->playlist, new_index);
-
-       /* send the delete command to mpd; at the same time, get the
+       /* send the "move" command to MPD; at the same time, get the
           new status (to verify the playlist id) */
 
        if (!mpd_command_list_begin(connection, false) ||
-           !mpd_send_swap_id(connection, mpd_song_get_id(song1),
-                             mpd_song_get_id(song2)) ||
+           !mpd_send_move(connection, src_pos, dest_pos) ||
            !mpd_send_status(connection) ||
            !mpd_command_list_end(connection))
                return mpdclient_handle_error(c);
 
-       c->events |= MPD_IDLE_PLAYLIST;
+       c->events |= MPD_IDLE_QUEUE;
 
-       status = mpd_recv_status(connection);
-       if (status != NULL) {
-               if (c->status != NULL)
-                       mpd_status_free(c->status);
-               c->status = status;
-       }
+       struct mpd_status *status = mpdclient_recv_status(c);
+       if (status == NULL)
+               return false;
 
        if (!mpd_response_finish(connection))
                return mpdclient_handle_error(c);
@@ -655,12 +576,80 @@ mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
                c->playlist.version = mpd_status_get_queue_version(status);
 
                /* swap songs in the local playlist */
-               playlist_swap(&c->playlist, old_index, new_index);
+               playlist_move(&c->playlist, dest_pos, src_pos);
        }
 
        return true;
 }
 
+/* The client-to-client protocol (MPD 0.17.0) */
+
+bool
+mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+
+       if (connection == NULL)
+               return false;
+
+       if (!mpd_send_subscribe(connection, channel))
+               return mpdclient_handle_error(c);
+
+       return mpdclient_finish_command(c);
+}
+
+bool
+mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       if (!mpd_send_unsubscribe(connection, channel))
+               return mpdclient_handle_error(c);
+
+       return mpdclient_finish_command(c);
+}
+
+bool
+mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
+                          const char *text)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       if (!mpd_send_send_message(connection, channel, text))
+               return mpdclient_handle_error(c);
+
+       return mpdclient_finish_command(c);
+}
+
+bool
+mpdclient_send_read_messages(struct mpdclient *c)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       return mpd_send_read_messages(connection)?
+               true : mpdclient_handle_error(c);
+}
+
+struct mpd_message *
+mpdclient_recv_message(struct mpdclient *c)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       if (connection == NULL)
+               return false;
+
+       struct mpd_message *message = mpd_recv_message(connection);
+       if (message == NULL &&
+           mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
+               mpdclient_handle_error(c);
+
+       return message;
+}
 
 /****************************************************************************/
 /*** Playlist management functions ******************************************/
@@ -671,14 +660,14 @@ bool
 mpdclient_playlist_update(struct mpdclient *c)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       struct mpd_entity *entity;
-
        if (connection == NULL)
                return false;
 
        playlist_clear(&c->playlist);
 
        mpd_send_list_queue_meta(connection);
+
+       struct mpd_entity *entity;
        while ((entity = mpd_recv_entity(connection))) {
                if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
                        playlist_append(&c->playlist, mpd_entity_get_song(entity));
@@ -697,14 +686,13 @@ bool
 mpdclient_playlist_update_changes(struct mpdclient *c)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       struct mpd_song *song;
-       guint length;
 
        if (connection == NULL)
                return false;
 
        mpd_send_queue_changes_meta(connection, c->playlist.version);
 
+       struct mpd_song *song;
        while ((song = mpd_recv_song(connection)) != NULL) {
                int pos = mpd_song_get_pos(song);
 
@@ -721,7 +709,7 @@ mpdclient_playlist_update_changes(struct mpdclient *c)
 
        /* remove trailing songs */
 
-       length = mpd_status_get_queue_length(c->status);
+       unsigned length = mpd_status_get_queue_length(c->status);
        while (length < c->playlist.list->len) {
                guint pos = c->playlist.list->len - 1;
 
@@ -744,8 +732,6 @@ bool
 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       guint i;
-
        if (connection == NULL)
                return false;
 
@@ -754,7 +740,7 @@ mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
 
        mpd_command_list_begin(connection, false);
 
-       for (i = 0; i < filelist_length(fl); ++i) {
+       for (unsigned i = 0; i < filelist_length(fl); ++i) {
                struct filelist_entry *entry = filelist_get(fl, i);
                struct mpd_entity *entity  = entry->entity;