X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmpdclient.c;h=4882d548f1b21563112626f59bfd7489134d8429;hb=0f777a7c20e1a95665cf3f1e68de55d49b80aad1;hp=152942e011b97ea79d3ffb48fad82e9a1f6d8202;hpb=e0cf6984b61a936e36f5c683f5cac8b1c092b9da;p=ncmpc.git diff --git a/src/mpdclient.c b/src/mpdclient.c index 152942e..4882d54 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -1,5 +1,5 @@ /* ncmpc (Ncurses MPD Client) - * (c) 2004-2009 The Music Player Daemon Project + * (c) 2004-2010 The Music Player Daemon Project * Project homepage: http://musicpd.org * This program is free software; you can redistribute it and/or modify @@ -24,6 +24,7 @@ #include "options.h" #include "strfsong.h" #include "utils.h" +#include "gidle.h" #include @@ -32,49 +33,8 @@ #include #include -#undef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */ -#define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE -#define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE - #define BUFSIZE 1024 -static bool -MPD_ERROR(const struct mpdclient *client) -{ - return client->connection == NULL || - mpd_connection_get_error(client->connection) != MPD_ERROR_SUCCESS; -} - -/* filelist sorting functions */ -static gint -compare_filelistentry(gconstpointer filelist_entry1, - gconstpointer filelist_entry2) -{ - const struct mpd_entity *e1, *e2; - int n = 0; - - e1 = ((const struct filelist_entry *)filelist_entry1)->entity; - e2 = ((const struct filelist_entry *)filelist_entry2)->entity; - - if (e1 != NULL && e2 != NULL && - mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) { - switch (mpd_entity_get_type(e1)) { - case MPD_ENTITY_TYPE_UNKNOWN: - break; - case MPD_ENTITY_TYPE_DIRECTORY: - n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)), - mpd_directory_get_path(mpd_entity_get_directory(e2))); - break; - case MPD_ENTITY_TYPE_SONG: - break; - case MPD_ENTITY_TYPE_PLAYLIST: - n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)), - mpd_playlist_get_path(mpd_entity_get_playlist(e2))); - } - } - return n; -} - /* sort by list-format */ gint compare_filelistentry_format(gconstpointer filelist_entry1, @@ -103,7 +63,7 @@ compare_filelistentry_format(gconstpointer filelist_entry1, /*** mpdclient functions ****************************************************/ /****************************************************************************/ -gint +bool mpdclient_handle_error(struct mpdclient *c) { enum mpd_error error = mpd_connection_get_error(c->connection); @@ -113,24 +73,21 @@ 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)) - return 0; - - if (error == MPD_ERROR_SERVER) - error = error | (mpd_connection_get_server_error(c->connection) << 8); + return true; mpdclient_ui_error(mpd_connection_get_error_message(c->connection)); if (!mpd_connection_clear_error(c->connection)) mpdclient_disconnect(c); - return error; + return false; } -static gint +static bool mpdclient_finish_command(struct mpdclient *c) { return mpd_response_finish(c->connection) - ? 0 : mpdclient_handle_error(c); + ? true : mpdclient_handle_error(c); } struct mpdclient * @@ -141,6 +98,7 @@ mpdclient_new(void) c = g_new0(struct mpdclient, 1); playlist_init(&c->playlist); c->volume = -1; + c->events = 0; return c; } @@ -152,14 +110,18 @@ mpdclient_free(struct mpdclient *c) mpdclient_playlist_free(&c->playlist); - g_list_free(c->playlist_callbacks); - g_list_free(c->browse_callbacks); g_free(c); } void mpdclient_disconnect(struct mpdclient *c) { + if (c->source != NULL) { + mpd_glib_free(c->source); + c->source = NULL; + c->idle = false; + } + if (c->connection) mpd_connection_free(c->connection); c->connection = NULL; @@ -172,13 +134,18 @@ mpdclient_disconnect(struct mpdclient *c) if (c->song) 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; } bool mpdclient_connect(struct mpdclient *c, const gchar *host, gint port, - gfloat _timeout, + unsigned timeout_ms, const gchar *password) { /* close any open connection */ @@ -186,7 +153,7 @@ mpdclient_connect(struct mpdclient *c, 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"); @@ -209,374 +176,487 @@ mpdclient_connect(struct mpdclient *c, bool mpdclient_update(struct mpdclient *c) { - bool retval; + struct mpd_connection *connection = mpdclient_get_connection(c); c->volume = -1; - if (MPD_ERROR(c)) + 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); /* retrieve new status */ - c->status = mpd_run_status(c->connection); + c->status = mpd_run_status(connection); if (c->status == NULL) - return mpdclient_handle_error(c) == 0; + return mpdclient_handle_error(c); - if (c->update_id > 0 && - c->update_id != mpd_status_get_update_id(c->status)) - mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL); + 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.id != mpd_status_get_queue_version(c->status)) { + if (c->playlist.version != mpd_status_get_queue_version(c->status)) { + bool retval; + + if (c->source == NULL) + c->events |= MPD_IDLE_QUEUE; + 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; } - -/****************************************************************************/ -/*** MPD Commands **********************************************************/ -/****************************************************************************/ - -gint -mpdclient_cmd_play(struct mpdclient *c, gint idx) +struct mpd_connection * +mpdclient_get_connection(struct mpdclient *c) { - const struct mpd_song *song = playlist_get_song(&c->playlist, idx); + if (c->source != NULL && c->idle) { + c->idle = false; + mpd_glib_leave(c->source); + } - if (MPD_ERROR(c)) - return -1; + return c->connection; +} - if (song) - mpd_send_play_id(c->connection, mpd_song_get_id(song)); - else - mpd_send_play(c->connection); +void +mpdclient_put_connection(struct mpdclient *c) +{ + assert(c->source == NULL || c->connection != NULL); - return mpdclient_finish_command(c); + if (c->source != NULL && !c->idle) { + c->idle = mpd_glib_enter(c->source); + } } -gint -mpdclient_cmd_crop(struct mpdclient *c) +static struct mpd_status * +mpdclient_recv_status(struct mpdclient *c) { struct mpd_status *status; - bool playing; - int length, current; - if (MPD_ERROR(c)) - return -1; + assert(c->connection != NULL); - status = mpd_run_status(c->connection); - if (status == NULL) - return mpdclient_handle_error(c); + status = mpd_recv_status(c->connection); + if (status == NULL) { + mpdclient_handle_error(c); + return NULL; + } - 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); + if (c->status != NULL) + mpd_status_free(c->status); + return c->status = status; +} - mpd_status_free(status); +/****************************************************************************/ +/*** MPD Commands **********************************************************/ +/****************************************************************************/ - if (!playing || length < 2) - return 0; +bool +mpdclient_cmd_crop(struct mpdclient *c) +{ + struct mpd_connection *connection; + int length, current; - mpd_command_list_begin(c->connection, false); + if (!mpdclient_is_playing(c)) + return false; - while (--length >= 0) - if (length != current) - mpd_send_delete(c->connection, length); + length = mpd_status_get_queue_length(c->status); + current = mpd_status_get_song_pos(c->status); + if (current < 0 || mpd_status_get_queue_length(c->status) < 2) + return true; - mpd_command_list_end(c->connection); + connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; - return mpdclient_finish_command(c); -} + mpd_command_list_begin(connection, false); + + if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) { + if (current < length - 1) + mpd_send_delete_range(connection, current + 1, length); + if (current > 0) + mpd_send_delete_range(connection, 0, current); + } else + while (--length >= 0) + if (length != current) + mpd_send_delete(connection, length); + + mpd_command_list_end(connection); -gint -mpdclient_cmd_shuffle_range(struct mpdclient *c, guint start, guint end) -{ - mpd_send_shuffle_range(c->connection, start, end); return mpdclient_finish_command(c); } -gint +bool mpdclient_cmd_clear(struct mpdclient *c) { - gint retval = 0; + struct mpd_connection *connection = mpdclient_get_connection(c); + struct mpd_status *status; - if (MPD_ERROR(c)) - return -1; + if (connection == NULL) + return false; - mpd_send_clear(c->connection); - retval = mpdclient_finish_command(c); - /* call playlist updated callback */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL); - return retval; + /* send "clear" and "status" */ + if (!mpd_command_list_begin(connection, false) || + !mpd_send_clear(connection) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) + return mpdclient_handle_error(c); + + /* receive the new status, store it in the mpdclient struct */ + + status = mpdclient_recv_status(c); + if (status == NULL) + return false; + + if (!mpd_response_finish(connection)) + return mpdclient_handle_error(c); + + /* update mpdclient.playlist */ + + if (mpd_status_get_queue_length(status) == 0) { + /* after the "clear" command, the queue is really + empty - this means we can clear it locally, + 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; + return true; } -gint +bool mpdclient_cmd_volume(struct mpdclient *c, gint value) { - if (MPD_ERROR(c)) - return -1; + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; - mpd_send_set_volume(c->connection, value); + mpd_send_set_volume(connection, value); return mpdclient_finish_command(c); } -gint mpdclient_cmd_volume_up(struct mpdclient *c) +bool +mpdclient_cmd_volume_up(struct mpdclient *c) { - if (MPD_ERROR(c)) - return -1; + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; if (c->status == NULL || mpd_status_get_volume(c->status) == -1) - return 0; + return true; if (c->volume < 0) c->volume = mpd_status_get_volume(c->status); if (c->volume >= 100) - return 0; + return true; return mpdclient_cmd_volume(c, ++c->volume); } -gint mpdclient_cmd_volume_down(struct mpdclient *c) +bool +mpdclient_cmd_volume_down(struct mpdclient *c) { - if (MPD_ERROR(c)) - return -1; + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; if (c->status == NULL || mpd_status_get_volume(c->status) < 0) - return 0; + return true; if (c->volume < 0) c->volume = mpd_status_get_volume(c->status); if (c->volume <= 0) - return 0; + return true; return mpdclient_cmd_volume(c, --c->volume); } -gint +bool mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8) { - if (MPD_ERROR(c)) - return -1; + struct mpd_connection *connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; - mpd_send_add(c->connection, path_utf8); + mpd_send_add(connection, path_utf8); return mpdclient_finish_command(c); } -gint +bool mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song) { - gint retval = 0; + struct mpd_connection *connection = mpdclient_get_connection(c); + struct mpd_status *status; + struct mpd_song *new_song; + + assert(c != NULL); + assert(song != NULL); + + if (connection == NULL || c->status == NULL) + return false; + + /* send the add command to mpd; at the same time, get the new + status (to verify the new playlist id) and the last song + (we hope that's the song we just added) */ + + if (!mpd_command_list_begin(connection, true) || + !mpd_send_add(connection, mpd_song_get_uri(song)) || + !mpd_send_status(connection) || + !mpd_send_get_queue_song_pos(connection, + playlist_length(&c->playlist)) || + !mpd_command_list_end(connection) || + !mpd_response_next(connection)) + return mpdclient_handle_error(c); + + c->events |= MPD_IDLE_QUEUE; + + status = mpdclient_recv_status(c); + if (status == NULL) + return false; - if (MPD_ERROR(c)) - return -1; + if (!mpd_response_next(connection)) + return mpdclient_handle_error(c); - if (song == NULL) - return -1; + new_song = mpd_recv_song(connection); + if (!mpd_response_finish(connection) || new_song == NULL) { + if (new_song != NULL) + mpd_song_free(new_song); - /* send the add command to mpd */ - mpd_send_add(c->connection, mpd_song_get_uri(song)); - if( (retval=mpdclient_finish_command(c)) ) - return retval; + return mpd_connection_clear_error(connection) || + mpdclient_handle_error(c); + } -#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD - /* add the song to playlist */ - playlist_append(&c->playlist, song); + if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 && + mpd_status_get_queue_version(status) == c->playlist.version + 1) { + /* the cheap route: match on the new playlist length + and its version, we can keep our local playlist + copy in sync */ + c->playlist.version = mpd_status_get_queue_version(status); - /* increment the playlist id, so we don't retrieve a new playlist */ - c->playlist.id++; + /* the song we just received has the correct id; + append it to the local playlist */ + playlist_append(&c->playlist, new_song); + } - /* call playlist updated callback */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song); -#endif + mpd_song_free(new_song); - return 0; + return true; } -gint +bool mpdclient_cmd_delete(struct mpdclient *c, gint idx) { - gint retval = 0; - struct mpd_song *song; + struct mpd_connection *connection = mpdclient_get_connection(c); + const struct mpd_song *song; + struct mpd_status *status; - if (MPD_ERROR(c)) - return -1; + if (connection == NULL || c->status == NULL) + return false; if (idx < 0 || (guint)idx >= playlist_length(&c->playlist)) - return -1; + return false; song = playlist_get(&c->playlist, idx); - /* send the delete command to mpd */ - mpd_send_delete_id(c->connection, mpd_song_get_id(song)); - if( (retval=mpdclient_finish_command(c)) ) - return retval; + /* send the delete command to mpd; at the same time, get the + new status (to verify the playlist id) */ -#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE - /* increment the playlist id, so we don't retrieve a new playlist */ - c->playlist.id++; - - /* remove the song from the playlist */ - playlist_remove_reuse(&c->playlist, idx); - - /* call playlist updated callback */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song); + if (!mpd_command_list_begin(connection, false) || + !mpd_send_delete_id(connection, mpd_song_get_id(song)) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) + return mpdclient_handle_error(c); - /* remove references to the song */ - if (c->song == song) - c->song = NULL; + c->events |= MPD_IDLE_QUEUE; - mpd_song_free(song); -#endif + status = mpdclient_recv_status(c); + if (status == NULL) + return false; - return 0; -} + if (!mpd_response_finish(connection)) + return mpdclient_handle_error(c); -gint -mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index) -{ - gint n; - struct mpd_song *song1, *song2; + if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 && + mpd_status_get_queue_version(status) == c->playlist.version + 1) { + /* the cheap route: match on the new playlist length + and its version, we can keep our local playlist + copy in sync */ + c->playlist.version = mpd_status_get_queue_version(status); - if (MPD_ERROR(c)) - return -1; + /* remove the song from the local playlist */ + playlist_remove(&c->playlist, idx); - if (old_index == new_index || new_index < 0 || - (guint)new_index >= c->playlist.list->len) - return -1; + /* remove references to the song */ + if (c->song == song) + c->song = NULL; + } - song1 = playlist_get(&c->playlist, old_index); - song2 = playlist_get(&c->playlist, new_index); + return true; +} - /* send the move command to mpd */ - mpd_send_swap_id(c->connection, - mpd_song_get_id(song1), mpd_song_get_id(song2)); - if( (n=mpdclient_finish_command(c)) ) - return n; +/** + * 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; -#ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE - /* update the playlist */ - playlist_swap(&c->playlist, old_index, new_index); + if (!mpd_command_list_begin(connection, false)) + return mpdclient_handle_error(c); - /* increment the playlist id, so we don't retrieve a new playlist */ - c->playlist.id++; -#endif + for (; start < end; --end) + mpd_send_delete(connection, start); - /* call playlist updated callback */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index); + if (!mpd_command_list_end(connection) || + !mpd_response_finish(connection)) + return mpdclient_handle_error(c); - return 0; + return true; } -gint -mpdclient_cmd_save_playlist(struct mpdclient *c, const gchar *filename_utf8) +bool +mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end) { - gint retval = 0; + struct mpd_connection *connection; + struct mpd_status *status; - if (MPD_ERROR(c)) - return -1; + 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); - mpd_send_save(c->connection, filename_utf8); - if ((retval = mpdclient_finish_command(c)) == 0) - mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL); - return retval; -} + connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; -gint -mpdclient_cmd_load_playlist(struct mpdclient *c, const gchar *filename_utf8) -{ - if (MPD_ERROR(c)) - return -1; + if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) + return mpdclient_cmd_delete_range_fallback(c, start, end); - mpd_send_load(c->connection, filename_utf8); - return mpdclient_finish_command(c); -} + /* MPD 0.16 supports "delete" with a range argument */ -gint -mpdclient_cmd_delete_playlist(struct mpdclient *c, const gchar *filename_utf8) -{ - gint retval = 0; + /* send the delete command to mpd; at the same time, get the + new status (to verify the playlist id) */ - if (MPD_ERROR(c)) - return -1; + if (!mpd_command_list_begin(connection, false) || + !mpd_send_delete_range(connection, start, end) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) + return mpdclient_handle_error(c); - mpd_send_rm(c->connection, filename_utf8); - if ((retval = mpdclient_finish_command(c)) == 0) - mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL); - return retval; -} + c->events |= MPD_IDLE_QUEUE; + status = mpdclient_recv_status(c); + if (status == NULL) + return false; -/****************************************************************************/ -/*** Callback management functions ******************************************/ -/****************************************************************************/ + if (!mpd_response_finish(connection)) + return mpdclient_handle_error(c); -static void -do_list_callbacks(struct mpdclient *c, GList *list, gint event, gpointer data) -{ - while (list) { - mpdc_list_cb_t fn = list->data; + if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) && + mpd_status_get_queue_version(status) == c->playlist.version + 1) { + /* the cheap route: match on the new playlist length + and its version, we can keep our local playlist + copy in sync */ + c->playlist.version = mpd_status_get_queue_version(status); + + /* remove the song from the local playlist */ + while (end > start) { + --end; - fn(c, event, data); - list = list->next; + /* remove references to the song */ + if (c->song == playlist_get(&c->playlist, end)) + c->song = NULL; + + playlist_remove(&c->playlist, end); + } } -} -void -mpdclient_playlist_callback(struct mpdclient *c, int event, gpointer data) -{ - do_list_callbacks(c, c->playlist_callbacks, event, data); + return true; } -void -mpdclient_install_playlist_callback(struct mpdclient *c,mpdc_list_cb_t cb) +bool +mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos) { - c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb); -} + struct mpd_connection *connection; + struct mpd_status *status; -void -mpdclient_remove_playlist_callback(struct mpdclient *c, mpdc_list_cb_t cb) -{ - c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb); -} + if (dest_pos == src_pos) + return true; -void -mpdclient_browse_callback(struct mpdclient *c, int event, gpointer data) -{ - do_list_callbacks(c, c->browse_callbacks, event, data); -} + connection = mpdclient_get_connection(c); + if (connection == NULL) + return false; + /* send the "move" command to MPD; at the same time, get the + new status (to verify the playlist id) */ -void -mpdclient_install_browse_callback(struct mpdclient *c,mpdc_list_cb_t cb) -{ - c->browse_callbacks = g_list_append(c->browse_callbacks, cb); -} + if (!mpd_command_list_begin(connection, false) || + !mpd_send_move(connection, src_pos, dest_pos) || + !mpd_send_status(connection) || + !mpd_command_list_end(connection)) + return mpdclient_handle_error(c); -void -mpdclient_remove_browse_callback(struct mpdclient *c, mpdc_list_cb_t cb) -{ - c->browse_callbacks = g_list_remove(c->browse_callbacks, cb); + c->events |= MPD_IDLE_QUEUE; + + status = mpdclient_recv_status(c); + if (status == NULL) + return false; + + if (!mpd_response_finish(connection)) + return mpdclient_handle_error(c); + + if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) && + mpd_status_get_queue_version(status) == c->playlist.version + 1) { + /* the cheap route: match on the new playlist length + and its version, we can keep our local playlist + copy in sync */ + c->playlist.version = mpd_status_get_queue_version(status); + + /* swap songs in the local playlist */ + playlist_move(&c->playlist, dest_pos, src_pos); + } + + return true; } @@ -588,43 +668,42 @@ mpdclient_remove_browse_callback(struct mpdclient *c, mpdc_list_cb_t cb) bool mpdclient_playlist_update(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct mpd_entity *entity; - if (MPD_ERROR(c)) + if (connection == NULL) return false; playlist_clear(&c->playlist); - mpd_send_list_queue_meta(c->connection); - while ((entity = mpd_recv_entity(c->connection))) { + mpd_send_list_queue_meta(connection); + 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)); mpd_entity_free(entity); } - c->playlist.id = mpd_status_get_queue_version(c->status); + c->playlist.version = mpd_status_get_queue_version(c->status); c->song = NULL; - /* call playlist updated callbacks */ - mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL); - - return mpdclient_finish_command(c) == 0; + return mpdclient_finish_command(c); } /* update playlist (plchanges) */ bool mpdclient_playlist_update_changes(struct mpdclient *c) { + struct mpd_connection *connection = mpdclient_get_connection(c); struct mpd_song *song; guint length; - if (MPD_ERROR(c)) + if (connection == NULL) return false; - mpd_send_queue_changes_meta(c->connection, c->playlist.id); + mpd_send_queue_changes_meta(connection, c->playlist.version); - while ((song = mpd_recv_song(c->connection)) != NULL) { + while ((song = mpd_recv_song(connection)) != NULL) { int pos = mpd_song_get_pos(song); if (pos >= 0 && (guint)pos < c->playlist.list->len) { @@ -649,11 +728,9 @@ mpdclient_playlist_update_changes(struct mpdclient *c) } c->song = NULL; - c->playlist.id = mpd_status_get_queue_version(c->status); - - mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL); + c->playlist.version = mpd_status_get_queue_version(c->status); - return mpdclient_finish_command(c) == 0; + return mpdclient_finish_command(c); } @@ -661,79 +738,19 @@ mpdclient_playlist_update_changes(struct mpdclient *c) /*** Filelist functions *****************************************************/ /****************************************************************************/ -struct filelist * -mpdclient_filelist_get(struct mpdclient *c, const gchar *path) -{ - struct filelist *filelist; - struct mpd_entity *entity; - - if (MPD_ERROR(c)) - return NULL; - - mpd_send_list_meta(c->connection, path); - filelist = filelist_new(); - - while ((entity = mpd_recv_entity(c->connection)) != NULL) - filelist_append(filelist, entity); - - if (mpdclient_finish_command(c)) { - filelist_free(filelist); - return NULL; - } - - filelist_sort_dir_play(filelist, compare_filelistentry); - - return filelist; -} - -static struct filelist * -mpdclient_recv_filelist_response(struct mpdclient *c) -{ - struct filelist *filelist; - struct mpd_entity *entity; - - filelist = filelist_new(); - - while ((entity = mpd_recv_entity(c->connection)) != NULL) - filelist_append(filelist, entity); - - if (mpdclient_finish_command(c)) { - filelist_free(filelist); - return NULL; - } - - return filelist; -} - -struct filelist * -mpdclient_filelist_search(struct mpdclient *c, - int exact_match, - enum mpd_tag_type tag, - gchar *filter_utf8) -{ - if (MPD_ERROR(c)) - return NULL; - - mpd_search_db_songs(c->connection, exact_match); - mpd_search_add_tag_constraint(c->connection, MPD_OPERATOR_DEFAULT, - tag, filter_utf8); - mpd_search_commit(c->connection); - - return mpdclient_recv_filelist_response(c); -} - -int +bool mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl) { + struct mpd_connection *connection = mpdclient_get_connection(c); guint i; - if (MPD_ERROR(c)) - return -1; + if (connection == NULL) + return false; if (filelist_is_empty(fl)) - return 0; + return true; - mpd_command_list_begin(c->connection, false); + mpd_command_list_begin(connection, false); for (i = 0; i < filelist_length(fl); ++i) { struct filelist_entry *entry = filelist_get(fl, i); @@ -743,65 +760,11 @@ mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl) mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) { const struct mpd_song *song = mpd_entity_get_song(entity); - const char *uri = mpd_song_get_uri(song); - if (uri != NULL) - mpd_send_add(c->connection, uri); + mpd_send_add(connection, mpd_song_get_uri(song)); } } - mpd_command_list_end(c->connection); + mpd_command_list_end(connection); return mpdclient_finish_command(c); } - -GList * -mpdclient_get_artists(struct mpdclient *c) -{ - GList *list = NULL; - struct mpd_pair *pair; - - if (MPD_ERROR(c)) - return NULL; - - mpd_search_db_tags(c->connection, MPD_TAG_ARTIST); - mpd_search_commit(c->connection); - - while ((pair = mpd_recv_pair_tag(c->connection, - MPD_TAG_ARTIST)) != NULL) { - list = g_list_append(list, g_strdup(pair->value)); - mpd_return_pair(c->connection, pair); - } - - if (mpdclient_finish_command(c)) - return string_list_free(list); - - return list; -} - -GList * -mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8) -{ - GList *list = NULL; - struct mpd_pair *pair; - - if (MPD_ERROR(c)) - return NULL; - - mpd_search_db_tags(c->connection, MPD_TAG_ALBUM); - if (artist_utf8 != NULL) - mpd_search_add_tag_constraint(c->connection, - MPD_OPERATOR_DEFAULT, - MPD_TAG_ARTIST, artist_utf8); - mpd_search_commit(c->connection); - - while ((pair = mpd_recv_pair_tag(c->connection, - MPD_TAG_ALBUM)) != NULL) { - list = g_list_append(list, g_strdup(pair->value)); - mpd_return_pair(c->connection, pair); - } - - if (mpdclient_finish_command(c)) - return string_list_free(list); - - return list; -}