Code

mpdclient: return after playlist_update() error
[ncmpc.git] / src / mpdclient.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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.
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.
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 "filelist.h"
22 #include "screen_client.h"
23 #include "config.h"
24 #include "options.h"
25 #include "strfsong.h"
26 #include "utils.h"
27 #include "gidle.h"
29 #include <mpd/client.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <time.h>
34 #include <string.h>
36 #define BUFSIZE 1024
38 /* sort by list-format */
39 gint
40 compare_filelistentry_format(gconstpointer filelist_entry1,
41                              gconstpointer filelist_entry2)
42 {
43         const struct mpd_entity *e1, *e2;
44         char key1[BUFSIZE], key2[BUFSIZE];
45         int n = 0;
47         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
48         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
50         if (e1 && e2 &&
51             mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
52             mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
53                 strfsong(key1, BUFSIZE, options.list_format, mpd_entity_get_song(e1));
54                 strfsong(key2, BUFSIZE, options.list_format, mpd_entity_get_song(e2));
55                 n = strcmp(key1,key2);
56         }
58         return n;
59 }
62 /****************************************************************************/
63 /*** mpdclient functions ****************************************************/
64 /****************************************************************************/
66 bool
67 mpdclient_handle_error(struct mpdclient *c)
68 {
69         enum mpd_error error = mpd_connection_get_error(c->connection);
71         assert(error != MPD_ERROR_SUCCESS);
73         if (error == MPD_ERROR_SERVER &&
74             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
75             screen_auth(c))
76                 return true;
78         mpdclient_ui_error(mpd_connection_get_error_message(c->connection));
80         if (!mpd_connection_clear_error(c->connection))
81                 mpdclient_disconnect(c);
83         return false;
84 }
86 static bool
87 mpdclient_finish_command(struct mpdclient *c)
88 {
89         return mpd_response_finish(c->connection)
90                 ? true : mpdclient_handle_error(c);
91 }
93 struct mpdclient *
94 mpdclient_new(void)
95 {
96         struct mpdclient *c;
98         c = g_new0(struct mpdclient, 1);
99         playlist_init(&c->playlist);
100         c->volume = -1;
101         c->events = 0;
103         return c;
106 void
107 mpdclient_free(struct mpdclient *c)
109         mpdclient_disconnect(c);
111         mpdclient_playlist_free(&c->playlist);
113         g_free(c);
116 void
117 mpdclient_disconnect(struct mpdclient *c)
119         if (c->source != NULL) {
120                 mpd_glib_free(c->source);
121                 c->source = NULL;
122                 c->idle = false;
123         }
125         if (c->connection)
126                 mpd_connection_free(c->connection);
127         c->connection = NULL;
129         if (c->status)
130                 mpd_status_free(c->status);
131         c->status = NULL;
133         playlist_clear(&c->playlist);
135         if (c->song)
136                 c->song = NULL;
138         /* everything has changed after a disconnect */
139         c->events |= MPD_IDLE_DATABASE|MPD_IDLE_STORED_PLAYLIST|
140                 MPD_IDLE_QUEUE|MPD_IDLE_PLAYER|MPD_IDLE_MIXER|MPD_IDLE_OUTPUT|
141                 MPD_IDLE_OPTIONS|MPD_IDLE_UPDATE;
144 bool
145 mpdclient_connect(struct mpdclient *c,
146                   const gchar *host,
147                   gint port,
148                   gfloat _timeout,
149                   const gchar *password)
151         /* close any open connection */
152         if( c->connection )
153                 mpdclient_disconnect(c);
155         /* connect to MPD */
156         c->connection = mpd_connection_new(host, port, _timeout * 1000);
157         if (c->connection == NULL)
158                 g_error("Out of memory");
160         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
161                 mpdclient_handle_error(c);
162                 mpdclient_disconnect(c);
163                 return false;
164         }
166         /* send password */
167         if (password != NULL && !mpd_run_password(c->connection, password)) {
168                 mpdclient_handle_error(c);
169                 mpdclient_disconnect(c);
170                 return false;
171         }
173         return true;
176 bool
177 mpdclient_update(struct mpdclient *c)
179         struct mpd_connection *connection = mpdclient_get_connection(c);
181         c->volume = -1;
183         if (connection == NULL)
184                 return false;
186         /* always announce these options as long as we don't have
187            "idle" support */
188         if (c->source == NULL)
189                 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
191         /* free the old status */
192         if (c->status)
193                 mpd_status_free(c->status);
195         /* retrieve new status */
196         c->status = mpd_run_status(connection);
197         if (c->status == NULL)
198                 return mpdclient_handle_error(c);
200         if (c->source == NULL &&
201             c->update_id != mpd_status_get_update_id(c->status)) {
202                 c->events |= MPD_IDLE_UPDATE;
204                 if (c->update_id > 0)
205                         c->events |= MPD_IDLE_DATABASE;
206         }
208         c->update_id = mpd_status_get_update_id(c->status);
210         if (c->source == NULL &&
211             c->volume != mpd_status_get_volume(c->status))
212                 c->events |= MPD_IDLE_MIXER;
214         c->volume = mpd_status_get_volume(c->status);
216         /* check if the playlist needs an update */
217         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
218                 bool retval;
220                 if (c->source == NULL)
221                         c->events |= MPD_IDLE_PLAYLIST;
223                 if (!playlist_is_empty(&c->playlist))
224                         retval = mpdclient_playlist_update_changes(c);
225                 else
226                         retval = mpdclient_playlist_update(c);
227                 if (!retval)
228                         return false;
229         }
231         /* update the current song */
232         if (!c->song || mpd_status_get_song_id(c->status)) {
233                 c->song = playlist_get_song(&c->playlist,
234                                             mpd_status_get_song_pos(c->status));
235         }
237         return true;
240 struct mpd_connection *
241 mpdclient_get_connection(struct mpdclient *c)
243         if (c->source != NULL && c->idle) {
244                 c->idle = false;
245                 mpd_glib_leave(c->source);
246         }
248         return c->connection;
251 void
252 mpdclient_put_connection(struct mpdclient *c)
254         assert(c->source == NULL || c->connection != NULL);
256         if (c->source != NULL && !c->idle) {
257                 c->idle = mpd_glib_enter(c->source);
258         }
261 static struct mpd_status *
262 mpdclient_recv_status(struct mpdclient *c)
264         struct mpd_status *status;
266         assert(c->connection != NULL);
268         status = mpd_recv_status(c->connection);
269         if (status == NULL) {
270                 mpdclient_handle_error(c);
271                 return NULL;
272         }
274         if (c->status != NULL)
275                 mpd_status_free(c->status);
276         return c->status = status;
279 /****************************************************************************/
280 /*** MPD Commands  **********************************************************/
281 /****************************************************************************/
283 bool
284 mpdclient_cmd_crop(struct mpdclient *c)
286         struct mpd_connection *connection;
287         int length, current;
289         if (c->status == NULL)
290                 return false;
292         length = mpd_status_get_queue_length(c->status);
293         current = mpd_status_get_song_pos(c->status);
294         if (current < 0 ||
295             (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
296              mpd_status_get_state(c->status) != MPD_STATE_PAUSE) ||
297             mpd_status_get_queue_length(c->status) < 2)
298                 return true;
300         connection = mpdclient_get_connection(c);
301         if (connection == NULL)
302                 return false;
304         mpd_command_list_begin(connection, false);
306         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
307                 if (current < length - 1)
308                         mpd_send_delete_range(connection, current + 1, length);
309                 if (current > 0)
310                         mpd_send_delete_range(connection, 0, current);
311         } else
312                 while (--length >= 0)
313                         if (length != current)
314                                 mpd_send_delete(connection, length);
316         mpd_command_list_end(connection);
318         return mpdclient_finish_command(c);
321 bool
322 mpdclient_cmd_clear(struct mpdclient *c)
324         struct mpd_connection *connection = mpdclient_get_connection(c);
325         struct mpd_status *status;
327         if (connection == NULL)
328                 return false;
330         /* send "clear" and "status" */
331         if (!mpd_command_list_begin(connection, false) ||
332             !mpd_send_clear(connection) ||
333             !mpd_send_status(connection) ||
334             !mpd_command_list_end(connection))
335                 return mpdclient_handle_error(c);
337         /* receive the new status, store it in the mpdclient struct */
339         status = mpdclient_recv_status(c);
340         if (status == NULL)
341                 return false;
343         if (!mpd_response_finish(connection))
344                 return mpdclient_handle_error(c);
346         /* update mpdclient.playlist */
348         if (mpd_status_get_queue_length(status) == 0) {
349                 /* after the "clear" command, the queue is really
350                    empty - this means we can clear it locally,
351                    reducing the UI latency */
352                 playlist_clear(&c->playlist);
353                 c->playlist.version = mpd_status_get_queue_version(status);
354         }
356         c->events |= MPD_IDLE_QUEUE;
357         return true;
360 bool
361 mpdclient_cmd_volume(struct mpdclient *c, gint value)
363         struct mpd_connection *connection = mpdclient_get_connection(c);
364         if (connection == NULL)
365                 return false;
367         mpd_send_set_volume(connection, value);
368         return mpdclient_finish_command(c);
371 bool
372 mpdclient_cmd_volume_up(struct mpdclient *c)
374         struct mpd_connection *connection = mpdclient_get_connection(c);
375         if (connection == NULL)
376                 return false;
378         if (c->status == NULL ||
379             mpd_status_get_volume(c->status) == -1)
380                 return true;
382         if (c->volume < 0)
383                 c->volume = mpd_status_get_volume(c->status);
385         if (c->volume >= 100)
386                 return true;
388         return mpdclient_cmd_volume(c, ++c->volume);
391 bool
392 mpdclient_cmd_volume_down(struct mpdclient *c)
394         struct mpd_connection *connection = mpdclient_get_connection(c);
395         if (connection == NULL)
396                 return false;
398         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
399                 return true;
401         if (c->volume < 0)
402                 c->volume = mpd_status_get_volume(c->status);
404         if (c->volume <= 0)
405                 return true;
407         return mpdclient_cmd_volume(c, --c->volume);
410 bool
411 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
413         struct mpd_connection *connection = mpdclient_get_connection(c);
414         if (connection == NULL)
415                 return false;
417         mpd_send_add(connection, path_utf8);
418         return mpdclient_finish_command(c);
421 bool
422 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
424         struct mpd_connection *connection = mpdclient_get_connection(c);
425         struct mpd_status *status;
426         struct mpd_song *new_song;
428         assert(c != NULL);
429         assert(song != NULL);
431         if (connection == NULL || c->status == NULL)
432                 return false;
434         /* send the add command to mpd; at the same time, get the new
435            status (to verify the new playlist id) and the last song
436            (we hope that's the song we just added) */
438         if (!mpd_command_list_begin(connection, true) ||
439             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
440             !mpd_send_status(connection) ||
441             !mpd_send_get_queue_song_pos(connection,
442                                          playlist_length(&c->playlist)) ||
443             !mpd_command_list_end(connection) ||
444             !mpd_response_next(connection))
445                 return mpdclient_handle_error(c);
447         c->events |= MPD_IDLE_PLAYLIST;
449         status = mpdclient_recv_status(c);
450         if (status == NULL)
451                 return false;
453         if (!mpd_response_next(connection))
454                 return mpdclient_handle_error(c);
456         new_song = mpd_recv_song(connection);
457         if (!mpd_response_finish(connection) || new_song == NULL) {
458                 if (new_song != NULL)
459                         mpd_song_free(new_song);
461                 return mpd_connection_clear_error(connection) ||
462                         mpdclient_handle_error(c);
463         }
465         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
466             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
467                 /* the cheap route: match on the new playlist length
468                    and its version, we can keep our local playlist
469                    copy in sync */
470                 c->playlist.version = mpd_status_get_queue_version(status);
472                 /* the song we just received has the correct id;
473                    append it to the local playlist */
474                 playlist_append(&c->playlist, new_song);
475         }
477         mpd_song_free(new_song);
479         return true;
482 bool
483 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
485         struct mpd_connection *connection = mpdclient_get_connection(c);
486         const struct mpd_song *song;
487         struct mpd_status *status;
489         if (connection == NULL || c->status == NULL)
490                 return false;
492         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
493                 return false;
495         song = playlist_get(&c->playlist, idx);
497         /* send the delete command to mpd; at the same time, get the
498            new status (to verify the playlist id) */
500         if (!mpd_command_list_begin(connection, false) ||
501             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
502             !mpd_send_status(connection) ||
503             !mpd_command_list_end(connection))
504                 return mpdclient_handle_error(c);
506         c->events |= MPD_IDLE_PLAYLIST;
508         status = mpdclient_recv_status(c);
509         if (status == NULL)
510                 return false;
512         if (!mpd_response_finish(connection))
513                 return mpdclient_handle_error(c);
515         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
516             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
517                 /* the cheap route: match on the new playlist length
518                    and its version, we can keep our local playlist
519                    copy in sync */
520                 c->playlist.version = mpd_status_get_queue_version(status);
522                 /* remove the song from the local playlist */
523                 playlist_remove(&c->playlist, idx);
525                 /* remove references to the song */
526                 if (c->song == song)
527                         c->song = NULL;
528         }
530         return true;
533 /**
534  * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
535  * It emulates the "delete range" command with a list of simple
536  * "delete" commands.
537  */
538 static bool
539 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
540                                     unsigned start, unsigned end)
542         struct mpd_connection *connection = mpdclient_get_connection(c);
543         if (connection == NULL)
544                 return false;
546         if (!mpd_command_list_begin(connection, false))
547                 return mpdclient_handle_error(c);
549         for (; start < end; --end)
550                 mpd_send_delete(connection, start);
552         if (!mpd_command_list_end(connection) ||
553             !mpd_response_finish(connection))
554                 return mpdclient_handle_error(c);
556         return true;
559 bool
560 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
562         struct mpd_connection *connection;
563         struct mpd_status *status;
565         if (end == start + 1)
566                 /* if that's not really a range, we choose to use the
567                    safer "deleteid" version */
568                 return mpdclient_cmd_delete(c, start);
570         connection = mpdclient_get_connection(c);
571         if (connection == NULL)
572                 return false;
574         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
575                 return mpdclient_cmd_delete_range_fallback(c, start, end);
577         /* MPD 0.16 supports "delete" with a range argument */
579         /* send the delete command to mpd; at the same time, get the
580            new status (to verify the playlist id) */
582         if (!mpd_command_list_begin(connection, false) ||
583             !mpd_send_delete_range(connection, start, end) ||
584             !mpd_send_status(connection) ||
585             !mpd_command_list_end(connection))
586                 return mpdclient_handle_error(c);
588         c->events |= MPD_IDLE_PLAYLIST;
590         status = mpdclient_recv_status(c);
591         if (status == NULL)
592                 return false;
594         if (!mpd_response_finish(connection))
595                 return mpdclient_handle_error(c);
597         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
598             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
599                 /* the cheap route: match on the new playlist length
600                    and its version, we can keep our local playlist
601                    copy in sync */
602                 c->playlist.version = mpd_status_get_queue_version(status);
604                 /* remove the song from the local playlist */
605                 while (end > start) {
606                         --end;
608                         /* remove references to the song */
609                         if (c->song == playlist_get(&c->playlist, end))
610                                 c->song = NULL;
612                         playlist_remove(&c->playlist, end);
613                 }
614         }
616         return true;
619 bool
620 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
622         struct mpd_connection *connection;
623         struct mpd_status *status;
625         if (dest_pos == src_pos)
626                 return true;
628         connection = mpdclient_get_connection(c);
629         if (connection == NULL)
630                 return false;
632         /* send the "move" command to MPD; at the same time, get the
633            new status (to verify the playlist id) */
635         if (!mpd_command_list_begin(connection, false) ||
636             !mpd_send_move(connection, src_pos, dest_pos) ||
637             !mpd_send_status(connection) ||
638             !mpd_command_list_end(connection))
639                 return mpdclient_handle_error(c);
641         c->events |= MPD_IDLE_QUEUE;
643         status = mpdclient_recv_status(c);
644         if (status == NULL)
645                 return false;
647         if (!mpd_response_finish(connection))
648                 return mpdclient_handle_error(c);
650         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
651             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
652                 /* the cheap route: match on the new playlist length
653                    and its version, we can keep our local playlist
654                    copy in sync */
655                 c->playlist.version = mpd_status_get_queue_version(status);
657                 /* swap songs in the local playlist */
658                 playlist_move(&c->playlist, dest_pos, src_pos);
659         }
661         return true;
665 /****************************************************************************/
666 /*** Playlist management functions ******************************************/
667 /****************************************************************************/
669 /* update playlist */
670 bool
671 mpdclient_playlist_update(struct mpdclient *c)
673         struct mpd_connection *connection = mpdclient_get_connection(c);
674         struct mpd_entity *entity;
676         if (connection == NULL)
677                 return false;
679         playlist_clear(&c->playlist);
681         mpd_send_list_queue_meta(connection);
682         while ((entity = mpd_recv_entity(connection))) {
683                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
684                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
686                 mpd_entity_free(entity);
687         }
689         c->playlist.version = mpd_status_get_queue_version(c->status);
690         c->song = NULL;
692         return mpdclient_finish_command(c);
695 /* update playlist (plchanges) */
696 bool
697 mpdclient_playlist_update_changes(struct mpdclient *c)
699         struct mpd_connection *connection = mpdclient_get_connection(c);
700         struct mpd_song *song;
701         guint length;
703         if (connection == NULL)
704                 return false;
706         mpd_send_queue_changes_meta(connection, c->playlist.version);
708         while ((song = mpd_recv_song(connection)) != NULL) {
709                 int pos = mpd_song_get_pos(song);
711                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
712                         /* update song */
713                         playlist_replace(&c->playlist, pos, song);
714                 } else {
715                         /* add a new song */
716                         playlist_append(&c->playlist, song);
717                 }
719                 mpd_song_free(song);
720         }
722         /* remove trailing songs */
724         length = mpd_status_get_queue_length(c->status);
725         while (length < c->playlist.list->len) {
726                 guint pos = c->playlist.list->len - 1;
728                 /* Remove the last playlist entry */
729                 playlist_remove(&c->playlist, pos);
730         }
732         c->song = NULL;
733         c->playlist.version = mpd_status_get_queue_version(c->status);
735         return mpdclient_finish_command(c);
739 /****************************************************************************/
740 /*** Filelist functions *****************************************************/
741 /****************************************************************************/
743 bool
744 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
746         struct mpd_connection *connection = mpdclient_get_connection(c);
747         guint i;
749         if (connection == NULL)
750                 return false;
752         if (filelist_is_empty(fl))
753                 return true;
755         mpd_command_list_begin(connection, false);
757         for (i = 0; i < filelist_length(fl); ++i) {
758                 struct filelist_entry *entry = filelist_get(fl, i);
759                 struct mpd_entity *entity  = entry->entity;
761                 if (entity != NULL &&
762                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
763                         const struct mpd_song *song =
764                                 mpd_entity_get_song(entity);
766                         mpd_send_add(connection, mpd_song_get_uri(song));
767                 }
768         }
770         mpd_command_list_end(connection);
771         return mpdclient_finish_command(c);