Code

mpdclient: check source again after mpd_glib_leave()
[ncmpc.git] / src / mpdclient.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "mpdclient.h"
21 #include "callbacks.h"
22 #include "filelist.h"
23 #include "config.h"
24 #include "gidle.h"
25 #include "charset.h"
27 #ifdef ENABLE_ASYNC_CONNECT
28 #include "aconnect.h"
29 #endif
31 #include <mpd/client.h>
33 #include <assert.h>
35 static gboolean
36 mpdclient_enter_idle_callback(gpointer user_data)
37 {
38         struct mpdclient *c = user_data;
39         assert(c->enter_idle_source_id != 0);
40         assert(c->source != NULL);
41         assert(!c->idle);
43         c->enter_idle_source_id = 0;
44         c->idle = mpd_glib_enter(c->source);
45         return false;
46 }
48 static void
49 mpdclient_schedule_enter_idle(struct mpdclient *c)
50 {
51         assert(c != NULL);
52         assert(c->source != NULL);
54         if (c->enter_idle_source_id == 0)
55                 /* automatically re-enter MPD "idle" mode */
56                 c->enter_idle_source_id =
57                         g_idle_add(mpdclient_enter_idle_callback, c);
58 }
60 static void
61 mpdclient_cancel_enter_idle(struct mpdclient *c)
62 {
63         if (c->enter_idle_source_id != 0) {
64                 g_source_remove(c->enter_idle_source_id);
65                 c->enter_idle_source_id = 0;
66         }
67 }
69 static void
70 mpdclient_invoke_error_callback(enum mpd_error error,
71                                 const char *message)
72 {
73         char *allocated;
74         if (error == MPD_ERROR_SERVER)
75                 /* server errors are UTF-8, the others are locale */
76                 message = allocated = utf8_to_locale(message);
77         else
78                 allocated = NULL;
80         mpdclient_error_callback(message);
81         g_free(allocated);
82 }
84 static void
85 mpdclient_invoke_error_callback1(struct mpdclient *c)
86 {
87         assert(c != NULL);
88         assert(c->connection != NULL);
90         struct mpd_connection *connection = c->connection;
92         enum mpd_error error = mpd_connection_get_error(connection);
93         assert(error != MPD_ERROR_SUCCESS);
95         mpdclient_invoke_error_callback(error,
96                                         mpd_connection_get_error_message(connection));
97 }
99 static void
100 mpdclient_gidle_callback(enum mpd_error error,
101                          gcc_unused enum mpd_server_error server_error,
102                          const char *message, enum mpd_idle events,
103                          void *ctx)
105         struct mpdclient *c = ctx;
107         c->idle = false;
109         assert(mpdclient_is_connected(c));
111         if (error != MPD_ERROR_SUCCESS) {
112                 mpdclient_invoke_error_callback(error, message);
113                 mpdclient_disconnect(c);
114                 mpdclient_lost_callback();
115                 return;
116         }
118         c->events |= events;
119         mpdclient_update(c);
121         mpdclient_idle_callback(c->events);
123         c->events = 0;
125         if (c->source != NULL)
126                 mpdclient_schedule_enter_idle(c);
129 /****************************************************************************/
130 /*** mpdclient functions ****************************************************/
131 /****************************************************************************/
133 bool
134 mpdclient_handle_error(struct mpdclient *c)
136         enum mpd_error error = mpd_connection_get_error(c->connection);
138         assert(error != MPD_ERROR_SUCCESS);
140         if (error == MPD_ERROR_SERVER &&
141             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
142             mpdclient_auth_callback(c))
143                 return true;
145         mpdclient_invoke_error_callback(error,
146                                         mpd_connection_get_error_message(c->connection));
148         if (!mpd_connection_clear_error(c->connection)) {
149                 mpdclient_disconnect(c);
150                 mpdclient_lost_callback();
151         }
153         return false;
156 #ifdef ENABLE_ASYNC_CONNECT
157 #ifndef WIN32
159 static bool
160 is_local_socket(const char *host)
162         return *host == '/' || *host == '@';
165 static bool
166 settings_is_local_socket(const struct mpd_settings *settings)
168         const char *host = mpd_settings_get_host(settings);
169         return host != NULL && is_local_socket(host);
172 #endif
173 #endif
175 struct mpdclient *
176 mpdclient_new(const gchar *host, unsigned port,
177               unsigned timeout_ms, const gchar *password)
179         struct mpdclient *c = g_new0(struct mpdclient, 1);
181 #ifdef ENABLE_ASYNC_CONNECT
182         c->settings = mpd_settings_new(host, port, timeout_ms,
183                                        NULL, NULL);
184         if (c->settings == NULL)
185                 g_error("Out of memory");
187 #ifndef WIN32
188         c->settings2 = host == NULL && port == 0 &&
189                 settings_is_local_socket(c->settings)
190                 ? mpd_settings_new(host, 6600, timeout_ms, NULL, NULL)
191                 : NULL;
192 #endif
194 #else
195         c->host = host;
196         c->port = port;
197 #endif
199         c->timeout_ms = timeout_ms;
200         c->password = password;
202         playlist_init(&c->playlist);
203         c->volume = -1;
204         c->events = 0;
205         c->playing = false;
207         return c;
210 void
211 mpdclient_free(struct mpdclient *c)
213         mpdclient_disconnect(c);
215         mpdclient_playlist_free(&c->playlist);
217 #ifdef ENABLE_ASYNC_CONNECT
218         mpd_settings_free(c->settings);
220 #ifndef WIN32
221         if (c->settings2 != NULL)
222                 mpd_settings_free(c->settings2);
223 #endif
224 #endif
226         g_free(c);
229 static char *
230 settings_name(const struct mpd_settings *settings)
232         assert(settings != NULL);
234         const char *host = mpd_settings_get_host(settings);
235         if (host == NULL)
236                 host = "unknown";
238         if (host[0] == '/')
239                 return g_strdup(host);
241         unsigned port = mpd_settings_get_port(settings);
242         if (port == 0 || port == 6600)
243                 return g_strdup(host);
245         return g_strdup_printf("%s:%u", host, port);
248 char *
249 mpdclient_settings_name(const struct mpdclient *c)
251         assert(c != NULL);
253 #ifdef ENABLE_ASYNC_CONNECT
254         return settings_name(c->settings);
255 #else
256         struct mpd_settings *settings =
257                 mpd_settings_new(c->host, c->port, 0, NULL, NULL);
258         if (settings == NULL)
259                 return g_strdup("unknown");
261         char *name = settings_name(settings);
262         mpd_settings_free(settings);
263         return name;
264 #endif
267 static void
268 mpdclient_status_free(struct mpdclient *c)
270         if (c->status == NULL)
271                 return;
273         mpd_status_free(c->status);
274         c->status = NULL;
276         c->volume = -1;
277         c->playing = false;
280 void
281 mpdclient_disconnect(struct mpdclient *c)
283 #ifdef ENABLE_ASYNC_CONNECT
284         if (c->async_connect != NULL) {
285                 aconnect_cancel(c->async_connect);
286                 c->async_connect = NULL;
287         }
288 #endif
290         mpdclient_cancel_enter_idle(c);
292         if (c->source != NULL) {
293                 mpd_glib_free(c->source);
294                 c->source = NULL;
295                 c->idle = false;
296         }
298         if (c->connection) {
299                 mpd_connection_free(c->connection);
300                 ++c->connection_id;
301         }
302         c->connection = NULL;
304         mpdclient_status_free(c);
306         playlist_clear(&c->playlist);
308         if (c->song)
309                 c->song = NULL;
311         /* everything has changed after a disconnect */
312         c->events |= MPD_IDLE_ALL;
315 static bool
316 mpdclient_connected(struct mpdclient *c,
317                     struct mpd_connection *connection)
319         c->connection = connection;
321         if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
322                 mpdclient_invoke_error_callback1(c);
323                 mpdclient_disconnect(c);
324                 mpdclient_failed_callback();
325                 return false;
326         }
328 #ifdef ENABLE_ASYNC_CONNECT
329         if (c->timeout_ms > 0)
330                 mpd_connection_set_timeout(connection, c->timeout_ms);
331 #endif
333         /* send password */
334         if (c->password != NULL &&
335             !mpd_run_password(connection, c->password)) {
336                 mpdclient_invoke_error_callback1(c);
337                 mpdclient_disconnect(c);
338                 mpdclient_failed_callback();
339                 return false;
340         }
342         c->source = mpd_glib_new(connection,
343                                  mpdclient_gidle_callback, c);
344         mpdclient_schedule_enter_idle(c);
346         ++c->connection_id;
348         mpdclient_connected_callback();
349         return true;
352 #ifdef ENABLE_ASYNC_CONNECT
354 static void
355 mpdclient_aconnect_start(struct mpdclient *c,
356                          const struct mpd_settings *settings);
358 static const struct mpd_settings *
359 mpdclient_get_settings(const struct mpdclient *c)
361 #ifndef WIN32
362         if (c->connecting2)
363                 return c->settings2;
364 #endif
366         return c->settings;
369 static void
370 mpdclient_connect_success(struct mpd_connection *connection, void *ctx)
372         struct mpdclient *c = ctx;
373         assert(c->async_connect != NULL);
374         c->async_connect = NULL;
376         const char *password =
377                 mpd_settings_get_password(mpdclient_get_settings(c));
378         if (password != NULL && !mpd_run_password(connection, password)) {
379                 mpdclient_error_callback(mpd_connection_get_error_message(connection));
380                 mpd_connection_free(connection);
381                 mpdclient_failed_callback();
382                 return;
383         }
385         mpdclient_connected(c, connection);
388 static void
389 mpdclient_connect_error(const char *message, void *ctx)
391         struct mpdclient *c = ctx;
392         assert(c->async_connect != NULL);
393         c->async_connect = NULL;
395 #ifndef WIN32
396         if (!c->connecting2 && c->settings2 != NULL) {
397                 c->connecting2 = true;
398                 mpdclient_aconnect_start(c, c->settings2);
399                 return;
400         }
401 #endif
403         mpdclient_error_callback(message);
404         mpdclient_failed_callback();
407 static const struct aconnect_handler mpdclient_connect_handler = {
408         .success = mpdclient_connect_success,
409         .error = mpdclient_connect_error,
410 };
412 static void
413 mpdclient_aconnect_start(struct mpdclient *c,
414                          const struct mpd_settings *settings)
416         aconnect_start(&c->async_connect,
417                        mpd_settings_get_host(settings),
418                        mpd_settings_get_port(settings),
419                        &mpdclient_connect_handler, c);
422 #endif
424 void
425 mpdclient_connect(struct mpdclient *c)
427         /* close any open connection */
428         mpdclient_disconnect(c);
430 #ifdef ENABLE_ASYNC_CONNECT
431 #ifndef WIN32
432         c->connecting2 = false;
433 #endif
434         mpdclient_aconnect_start(c, c->settings);
435 #else
436         /* connect to MPD */
437         struct mpd_connection *connection =
438                 mpd_connection_new(c->host, c->port, c->timeout_ms);
439         if (connection == NULL)
440                 g_error("Out of memory");
442         mpdclient_connected(c, connection);
443 #endif
446 bool
447 mpdclient_update(struct mpdclient *c)
449         struct mpd_connection *connection = mpdclient_get_connection(c);
451         if (connection == NULL)
452                 return false;
454         /* free the old status */
455         mpdclient_status_free(c);
457         /* retrieve new status */
458         c->status = mpd_run_status(connection);
459         if (c->status == NULL)
460                 return mpdclient_handle_error(c);
462         c->volume = mpd_status_get_volume(c->status);
463         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
465         /* check if the playlist needs an update */
466         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
467                 bool retval;
469                 if (!playlist_is_empty(&c->playlist))
470                         retval = mpdclient_playlist_update_changes(c);
471                 else
472                         retval = mpdclient_playlist_update(c);
473                 if (!retval)
474                         return false;
475         }
477         /* update the current song */
478         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
479                 c->song = playlist_get_song(&c->playlist,
480                                             mpd_status_get_song_pos(c->status));
481         }
483         return true;
486 struct mpd_connection *
487 mpdclient_get_connection(struct mpdclient *c)
489         if (c->source != NULL && c->idle) {
490                 c->idle = false;
491                 mpd_glib_leave(c->source);
493                 if (c->source != NULL)
494                         mpdclient_schedule_enter_idle(c);
495         }
497         return c->connection;
500 static struct mpd_status *
501 mpdclient_recv_status(struct mpdclient *c)
503         assert(c->connection != NULL);
505         struct mpd_status *status = mpd_recv_status(c->connection);
506         if (status == NULL) {
507                 mpdclient_handle_error(c);
508                 return NULL;
509         }
511         if (c->status != NULL)
512                 mpd_status_free(c->status);
513         return c->status = status;
516 /****************************************************************************/
517 /*** MPD Commands  **********************************************************/
518 /****************************************************************************/
520 bool
521 mpdclient_cmd_crop(struct mpdclient *c)
523         if (!mpdclient_is_playing(c))
524                 return false;
526         int length = mpd_status_get_queue_length(c->status);
527         int current = mpd_status_get_song_pos(c->status);
528         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
529                 return true;
531         struct mpd_connection *connection = mpdclient_get_connection(c);
532         if (connection == NULL)
533                 return false;
535         mpd_command_list_begin(connection, false);
537         if (current < length - 1)
538                 mpd_send_delete_range(connection, current + 1, length);
539         if (current > 0)
540                 mpd_send_delete_range(connection, 0, current);
542         mpd_command_list_end(connection);
544         return mpdclient_finish_command(c);
547 bool
548 mpdclient_cmd_clear(struct mpdclient *c)
550         struct mpd_connection *connection = mpdclient_get_connection(c);
551         if (connection == NULL)
552                 return false;
554         /* send "clear" and "status" */
555         if (!mpd_command_list_begin(connection, false) ||
556             !mpd_send_clear(connection) ||
557             !mpd_send_status(connection) ||
558             !mpd_command_list_end(connection))
559                 return mpdclient_handle_error(c);
561         /* receive the new status, store it in the mpdclient struct */
563         struct mpd_status *status = mpdclient_recv_status(c);
564         if (status == NULL)
565                 return false;
567         if (!mpd_response_finish(connection))
568                 return mpdclient_handle_error(c);
570         /* update mpdclient.playlist */
572         if (mpd_status_get_queue_length(status) == 0) {
573                 /* after the "clear" command, the queue is really
574                    empty - this means we can clear it locally,
575                    reducing the UI latency */
576                 playlist_clear(&c->playlist);
577                 c->playlist.version = mpd_status_get_queue_version(status);
578                 c->song = NULL;
579         }
581         c->events |= MPD_IDLE_QUEUE;
582         return true;
585 bool
586 mpdclient_cmd_volume(struct mpdclient *c, gint value)
588         struct mpd_connection *connection = mpdclient_get_connection(c);
589         if (connection == NULL)
590                 return false;
592         mpd_send_set_volume(connection, value);
593         return mpdclient_finish_command(c);
596 bool
597 mpdclient_cmd_volume_up(struct mpdclient *c)
599         if (c->volume < 0 || c->volume >= 100)
600                 return true;
602         struct mpd_connection *connection = mpdclient_get_connection(c);
603         if (connection == NULL)
604                 return false;
606         return mpdclient_cmd_volume(c, ++c->volume);
609 bool
610 mpdclient_cmd_volume_down(struct mpdclient *c)
612         if (c->volume <= 0)
613                 return true;
615         struct mpd_connection *connection = mpdclient_get_connection(c);
616         if (connection == NULL)
617                 return false;
619         return mpdclient_cmd_volume(c, --c->volume);
622 bool
623 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
625         struct mpd_connection *connection = mpdclient_get_connection(c);
626         if (connection == NULL)
627                 return false;
629         return mpd_send_add(connection, path_utf8)?
630                 mpdclient_finish_command(c) : false;
633 bool
634 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
636         assert(c != NULL);
637         assert(song != NULL);
639         struct mpd_connection *connection = mpdclient_get_connection(c);
640         if (connection == NULL || c->status == NULL)
641                 return false;
643         /* send the add command to mpd; at the same time, get the new
644            status (to verify the new playlist id) and the last song
645            (we hope that's the song we just added) */
647         if (!mpd_command_list_begin(connection, true) ||
648             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
649             !mpd_send_status(connection) ||
650             !mpd_send_get_queue_song_pos(connection,
651                                          playlist_length(&c->playlist)) ||
652             !mpd_command_list_end(connection) ||
653             !mpd_response_next(connection))
654                 return mpdclient_handle_error(c);
656         c->events |= MPD_IDLE_QUEUE;
658         struct mpd_status *status = mpdclient_recv_status(c);
659         if (status == NULL)
660                 return false;
662         if (!mpd_response_next(connection))
663                 return mpdclient_handle_error(c);
665         struct mpd_song *new_song = mpd_recv_song(connection);
666         if (!mpd_response_finish(connection) || new_song == NULL) {
667                 if (new_song != NULL)
668                         mpd_song_free(new_song);
670                 return mpd_connection_clear_error(connection) ||
671                         mpdclient_handle_error(c);
672         }
674         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
675             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
676                 /* the cheap route: match on the new playlist length
677                    and its version, we can keep our local playlist
678                    copy in sync */
679                 c->playlist.version = mpd_status_get_queue_version(status);
681                 /* the song we just received has the correct id;
682                    append it to the local playlist */
683                 playlist_append(&c->playlist, new_song);
684         }
686         mpd_song_free(new_song);
688         return true;
691 bool
692 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
694         struct mpd_connection *connection = mpdclient_get_connection(c);
696         if (connection == NULL || c->status == NULL)
697                 return false;
699         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
700                 return false;
702         const struct mpd_song *song = playlist_get(&c->playlist, idx);
704         /* send the delete command to mpd; at the same time, get the
705            new status (to verify the playlist id) */
707         if (!mpd_command_list_begin(connection, false) ||
708             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
709             !mpd_send_status(connection) ||
710             !mpd_command_list_end(connection))
711                 return mpdclient_handle_error(c);
713         c->events |= MPD_IDLE_QUEUE;
715         struct mpd_status *status = mpdclient_recv_status(c);
716         if (status == NULL)
717                 return false;
719         if (!mpd_response_finish(connection))
720                 return mpdclient_handle_error(c);
722         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
723             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
724                 /* the cheap route: match on the new playlist length
725                    and its version, we can keep our local playlist
726                    copy in sync */
727                 c->playlist.version = mpd_status_get_queue_version(status);
729                 /* remove the song from the local playlist */
730                 playlist_remove(&c->playlist, idx);
732                 /* remove references to the song */
733                 if (c->song == song)
734                         c->song = NULL;
735         }
737         return true;
740 bool
741 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
743         if (end == start + 1)
744                 /* if that's not really a range, we choose to use the
745                    safer "deleteid" version */
746                 return mpdclient_cmd_delete(c, start);
748         struct mpd_connection *connection = mpdclient_get_connection(c);
749         if (connection == NULL)
750                 return false;
752         /* send the delete command to mpd; at the same time, get the
753            new status (to verify the playlist id) */
755         if (!mpd_command_list_begin(connection, false) ||
756             !mpd_send_delete_range(connection, start, end) ||
757             !mpd_send_status(connection) ||
758             !mpd_command_list_end(connection))
759                 return mpdclient_handle_error(c);
761         c->events |= MPD_IDLE_QUEUE;
763         struct mpd_status *status = mpdclient_recv_status(c);
764         if (status == NULL)
765                 return false;
767         if (!mpd_response_finish(connection))
768                 return mpdclient_handle_error(c);
770         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
771             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
772                 /* the cheap route: match on the new playlist length
773                    and its version, we can keep our local playlist
774                    copy in sync */
775                 c->playlist.version = mpd_status_get_queue_version(status);
777                 /* remove the song from the local playlist */
778                 while (end > start) {
779                         --end;
781                         /* remove references to the song */
782                         if (c->song == playlist_get(&c->playlist, end))
783                                 c->song = NULL;
785                         playlist_remove(&c->playlist, end);
786                 }
787         }
789         return true;
792 bool
793 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
795         if (dest_pos == src_pos)
796                 return true;
798         struct mpd_connection *connection = mpdclient_get_connection(c);
799         if (connection == NULL)
800                 return false;
802         /* send the "move" command to MPD; at the same time, get the
803            new status (to verify the playlist id) */
805         if (!mpd_command_list_begin(connection, false) ||
806             !mpd_send_move(connection, src_pos, dest_pos) ||
807             !mpd_send_status(connection) ||
808             !mpd_command_list_end(connection))
809                 return mpdclient_handle_error(c);
811         c->events |= MPD_IDLE_QUEUE;
813         struct mpd_status *status = mpdclient_recv_status(c);
814         if (status == NULL)
815                 return false;
817         if (!mpd_response_finish(connection))
818                 return mpdclient_handle_error(c);
820         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
821             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
822                 /* the cheap route: match on the new playlist length
823                    and its version, we can keep our local playlist
824                    copy in sync */
825                 c->playlist.version = mpd_status_get_queue_version(status);
827                 /* swap songs in the local playlist */
828                 playlist_move(&c->playlist, dest_pos, src_pos);
829         }
831         return true;
834 /* The client-to-client protocol (MPD 0.17.0) */
836 bool
837 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
839         struct mpd_connection *connection = mpdclient_get_connection(c);
841         if (connection == NULL)
842                 return false;
844         if (!mpd_send_subscribe(connection, channel))
845                 return mpdclient_handle_error(c);
847         return mpdclient_finish_command(c);
850 bool
851 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
853         struct mpd_connection *connection = mpdclient_get_connection(c);
854         if (connection == NULL)
855                 return false;
857         if (!mpd_send_unsubscribe(connection, channel))
858                 return mpdclient_handle_error(c);
860         return mpdclient_finish_command(c);
863 bool
864 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
865                            const char *text)
867         struct mpd_connection *connection = mpdclient_get_connection(c);
868         if (connection == NULL)
869                 return false;
871         if (!mpd_send_send_message(connection, channel, text))
872                 return mpdclient_handle_error(c);
874         return mpdclient_finish_command(c);
877 bool
878 mpdclient_send_read_messages(struct mpdclient *c)
880         struct mpd_connection *connection = mpdclient_get_connection(c);
881         if (connection == NULL)
882                 return false;
884         return mpd_send_read_messages(connection)?
885                 true : mpdclient_handle_error(c);
888 struct mpd_message *
889 mpdclient_recv_message(struct mpdclient *c)
891         struct mpd_connection *connection = mpdclient_get_connection(c);
892         if (connection == NULL)
893                 return false;
895         struct mpd_message *message = mpd_recv_message(connection);
896         if (message == NULL &&
897             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
898                 mpdclient_handle_error(c);
900         return message;
903 /****************************************************************************/
904 /*** Playlist management functions ******************************************/
905 /****************************************************************************/
907 /* update playlist */
908 bool
909 mpdclient_playlist_update(struct mpdclient *c)
911         struct mpd_connection *connection = mpdclient_get_connection(c);
912         if (connection == NULL)
913                 return false;
915         playlist_clear(&c->playlist);
917         mpd_send_list_queue_meta(connection);
919         struct mpd_entity *entity;
920         while ((entity = mpd_recv_entity(connection))) {
921                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
922                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
924                 mpd_entity_free(entity);
925         }
927         c->playlist.version = mpd_status_get_queue_version(c->status);
928         c->song = NULL;
930         return mpdclient_finish_command(c);
933 /* update playlist (plchanges) */
934 bool
935 mpdclient_playlist_update_changes(struct mpdclient *c)
937         struct mpd_connection *connection = mpdclient_get_connection(c);
939         if (connection == NULL)
940                 return false;
942         mpd_send_queue_changes_meta(connection, c->playlist.version);
944         struct mpd_song *song;
945         while ((song = mpd_recv_song(connection)) != NULL) {
946                 int pos = mpd_song_get_pos(song);
948                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
949                         /* update song */
950                         playlist_replace(&c->playlist, pos, song);
951                 } else {
952                         /* add a new song */
953                         playlist_append(&c->playlist, song);
954                 }
956                 mpd_song_free(song);
957         }
959         /* remove trailing songs */
961         unsigned length = mpd_status_get_queue_length(c->status);
962         while (length < c->playlist.list->len) {
963                 guint pos = c->playlist.list->len - 1;
965                 /* Remove the last playlist entry */
966                 playlist_remove(&c->playlist, pos);
967         }
969         c->song = NULL;
970         c->playlist.version = mpd_status_get_queue_version(c->status);
972         return mpdclient_finish_command(c);
976 /****************************************************************************/
977 /*** Filelist functions *****************************************************/
978 /****************************************************************************/
980 bool
981 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
983         struct mpd_connection *connection = mpdclient_get_connection(c);
984         if (connection == NULL)
985                 return false;
987         if (filelist_is_empty(fl))
988                 return true;
990         mpd_command_list_begin(connection, false);
992         for (unsigned i = 0; i < filelist_length(fl); ++i) {
993                 struct filelist_entry *entry = filelist_get(fl, i);
994                 struct mpd_entity *entity  = entry->entity;
996                 if (entity != NULL &&
997                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
998                         const struct mpd_song *song =
999                                 mpd_entity_get_song(entity);
1001                         mpd_send_add(connection, mpd_song_get_uri(song));
1002                 }
1003         }
1005         mpd_command_list_end(connection);
1006         return mpdclient_finish_command(c);