Code

34d26df69e30a81965f3df091af98aad267a48cc
[ncmpc.git] / src / mpdclient.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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_ALL;
142 bool
143 mpdclient_connect(struct mpdclient *c,
144                   const gchar *host,
145                   gint port,
146                   unsigned timeout_ms,
147                   const gchar *password)
149         /* close any open connection */
150         if (c->connection)
151                 mpdclient_disconnect(c);
153         /* connect to MPD */
154         c->connection = mpd_connection_new(host, port, timeout_ms);
155         if (c->connection == NULL)
156                 g_error("Out of memory");
158         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
159                 mpdclient_handle_error(c);
160                 mpdclient_disconnect(c);
161                 return false;
162         }
164         /* send password */
165         if (password != NULL && !mpd_run_password(c->connection, password)) {
166                 mpdclient_handle_error(c);
167                 mpdclient_disconnect(c);
168                 return false;
169         }
171         return true;
174 bool
175 mpdclient_update(struct mpdclient *c)
177         struct mpd_connection *connection = mpdclient_get_connection(c);
179         c->volume = -1;
181         if (connection == NULL)
182                 return false;
184         /* always announce these options as long as we don't have
185            "idle" support */
186         if (c->source == NULL)
187                 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
189         /* free the old status */
190         if (c->status)
191                 mpd_status_free(c->status);
193         /* retrieve new status */
194         c->status = mpd_run_status(connection);
195         if (c->status == NULL)
196                 return mpdclient_handle_error(c);
198         if (c->source == NULL &&
199             c->update_id != mpd_status_get_update_id(c->status)) {
200                 c->events |= MPD_IDLE_UPDATE;
202                 if (c->update_id > 0)
203                         c->events |= MPD_IDLE_DATABASE;
204         }
206         c->update_id = mpd_status_get_update_id(c->status);
208         if (c->source == NULL &&
209             c->volume != mpd_status_get_volume(c->status))
210                 c->events |= MPD_IDLE_MIXER;
212         c->volume = mpd_status_get_volume(c->status);
214         /* check if the playlist needs an update */
215         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
216                 bool retval;
218                 if (c->source == NULL)
219                         c->events |= MPD_IDLE_QUEUE;
221                 if (!playlist_is_empty(&c->playlist))
222                         retval = mpdclient_playlist_update_changes(c);
223                 else
224                         retval = mpdclient_playlist_update(c);
225                 if (!retval)
226                         return false;
227         }
229         /* update the current song */
230         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
231                 c->song = playlist_get_song(&c->playlist,
232                                             mpd_status_get_song_pos(c->status));
233         }
235         return true;
238 struct mpd_connection *
239 mpdclient_get_connection(struct mpdclient *c)
241         if (c->source != NULL && c->idle) {
242                 c->idle = false;
243                 mpd_glib_leave(c->source);
244         }
246         return c->connection;
249 void
250 mpdclient_put_connection(struct mpdclient *c)
252         assert(c->source == NULL || c->connection != NULL);
254         if (c->source != NULL && !c->idle) {
255                 c->idle = mpd_glib_enter(c->source);
256         }
259 static struct mpd_status *
260 mpdclient_recv_status(struct mpdclient *c)
262         struct mpd_status *status;
264         assert(c->connection != NULL);
266         status = mpd_recv_status(c->connection);
267         if (status == NULL) {
268                 mpdclient_handle_error(c);
269                 return NULL;
270         }
272         if (c->status != NULL)
273                 mpd_status_free(c->status);
274         return c->status = status;
277 /****************************************************************************/
278 /*** MPD Commands  **********************************************************/
279 /****************************************************************************/
281 bool
282 mpdclient_cmd_crop(struct mpdclient *c)
284         struct mpd_connection *connection;
285         int length, current;
287         if (!mpdclient_is_playing(c))
288                 return false;
290         length = mpd_status_get_queue_length(c->status);
291         current = mpd_status_get_song_pos(c->status);
292         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
293                 return true;
295         connection = mpdclient_get_connection(c);
296         if (connection == NULL)
297                 return false;
299         mpd_command_list_begin(connection, false);
301         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
302                 if (current < length - 1)
303                         mpd_send_delete_range(connection, current + 1, length);
304                 if (current > 0)
305                         mpd_send_delete_range(connection, 0, current);
306         } else
307                 while (--length >= 0)
308                         if (length != current)
309                                 mpd_send_delete(connection, length);
311         mpd_command_list_end(connection);
313         return mpdclient_finish_command(c);
316 bool
317 mpdclient_cmd_clear(struct mpdclient *c)
319         struct mpd_connection *connection = mpdclient_get_connection(c);
320         struct mpd_status *status;
322         if (connection == NULL)
323                 return false;
325         /* send "clear" and "status" */
326         if (!mpd_command_list_begin(connection, false) ||
327             !mpd_send_clear(connection) ||
328             !mpd_send_status(connection) ||
329             !mpd_command_list_end(connection))
330                 return mpdclient_handle_error(c);
332         /* receive the new status, store it in the mpdclient struct */
334         status = mpdclient_recv_status(c);
335         if (status == NULL)
336                 return false;
338         if (!mpd_response_finish(connection))
339                 return mpdclient_handle_error(c);
341         /* update mpdclient.playlist */
343         if (mpd_status_get_queue_length(status) == 0) {
344                 /* after the "clear" command, the queue is really
345                    empty - this means we can clear it locally,
346                    reducing the UI latency */
347                 playlist_clear(&c->playlist);
348                 c->playlist.version = mpd_status_get_queue_version(status);
349                 c->song = NULL;
350         }
352         c->events |= MPD_IDLE_QUEUE;
353         return true;
356 bool
357 mpdclient_cmd_volume(struct mpdclient *c, gint value)
359         struct mpd_connection *connection = mpdclient_get_connection(c);
360         if (connection == NULL)
361                 return false;
363         mpd_send_set_volume(connection, value);
364         return mpdclient_finish_command(c);
367 bool
368 mpdclient_cmd_volume_up(struct mpdclient *c)
370         struct mpd_connection *connection = mpdclient_get_connection(c);
371         if (connection == NULL)
372                 return false;
374         if (c->status == NULL ||
375             mpd_status_get_volume(c->status) == -1)
376                 return true;
378         if (c->volume < 0)
379                 c->volume = mpd_status_get_volume(c->status);
381         if (c->volume >= 100)
382                 return true;
384         return mpdclient_cmd_volume(c, ++c->volume);
387 bool
388 mpdclient_cmd_volume_down(struct mpdclient *c)
390         struct mpd_connection *connection = mpdclient_get_connection(c);
391         if (connection == NULL)
392                 return false;
394         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
395                 return true;
397         if (c->volume < 0)
398                 c->volume = mpd_status_get_volume(c->status);
400         if (c->volume <= 0)
401                 return true;
403         return mpdclient_cmd_volume(c, --c->volume);
406 bool
407 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
409         struct mpd_connection *connection = mpdclient_get_connection(c);
410         if (connection == NULL)
411                 return false;
413         return mpd_send_add(connection, path_utf8)?
414                 mpdclient_finish_command(c) : false;
417 bool
418 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
420         struct mpd_connection *connection = mpdclient_get_connection(c);
421         struct mpd_status *status;
422         struct mpd_song *new_song;
424         assert(c != NULL);
425         assert(song != NULL);
427         if (connection == NULL || c->status == NULL)
428                 return false;
430         /* send the add command to mpd; at the same time, get the new
431            status (to verify the new playlist id) and the last song
432            (we hope that's the song we just added) */
434         if (!mpd_command_list_begin(connection, true) ||
435             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
436             !mpd_send_status(connection) ||
437             !mpd_send_get_queue_song_pos(connection,
438                                          playlist_length(&c->playlist)) ||
439             !mpd_command_list_end(connection) ||
440             !mpd_response_next(connection))
441                 return mpdclient_handle_error(c);
443         c->events |= MPD_IDLE_QUEUE;
445         status = mpdclient_recv_status(c);
446         if (status == NULL)
447                 return false;
449         if (!mpd_response_next(connection))
450                 return mpdclient_handle_error(c);
452         new_song = mpd_recv_song(connection);
453         if (!mpd_response_finish(connection) || new_song == NULL) {
454                 if (new_song != NULL)
455                         mpd_song_free(new_song);
457                 return mpd_connection_clear_error(connection) ||
458                         mpdclient_handle_error(c);
459         }
461         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
462             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
463                 /* the cheap route: match on the new playlist length
464                    and its version, we can keep our local playlist
465                    copy in sync */
466                 c->playlist.version = mpd_status_get_queue_version(status);
468                 /* the song we just received has the correct id;
469                    append it to the local playlist */
470                 playlist_append(&c->playlist, new_song);
471         }
473         mpd_song_free(new_song);
475         return true;
478 bool
479 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
481         struct mpd_connection *connection = mpdclient_get_connection(c);
482         const struct mpd_song *song;
483         struct mpd_status *status;
485         if (connection == NULL || c->status == NULL)
486                 return false;
488         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
489                 return false;
491         song = playlist_get(&c->playlist, idx);
493         /* send the delete command to mpd; at the same time, get the
494            new status (to verify the playlist id) */
496         if (!mpd_command_list_begin(connection, false) ||
497             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
498             !mpd_send_status(connection) ||
499             !mpd_command_list_end(connection))
500                 return mpdclient_handle_error(c);
502         c->events |= MPD_IDLE_QUEUE;
504         status = mpdclient_recv_status(c);
505         if (status == NULL)
506                 return false;
508         if (!mpd_response_finish(connection))
509                 return mpdclient_handle_error(c);
511         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
512             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
513                 /* the cheap route: match on the new playlist length
514                    and its version, we can keep our local playlist
515                    copy in sync */
516                 c->playlist.version = mpd_status_get_queue_version(status);
518                 /* remove the song from the local playlist */
519                 playlist_remove(&c->playlist, idx);
521                 /* remove references to the song */
522                 if (c->song == song)
523                         c->song = NULL;
524         }
526         return true;
529 /**
530  * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
531  * It emulates the "delete range" command with a list of simple
532  * "delete" commands.
533  */
534 static bool
535 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
536                                     unsigned start, unsigned end)
538         struct mpd_connection *connection = mpdclient_get_connection(c);
539         if (connection == NULL)
540                 return false;
542         if (!mpd_command_list_begin(connection, false))
543                 return mpdclient_handle_error(c);
545         for (; start < end; --end)
546                 mpd_send_delete(connection, start);
548         if (!mpd_command_list_end(connection) ||
549             !mpd_response_finish(connection))
550                 return mpdclient_handle_error(c);
552         return true;
555 bool
556 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
558         struct mpd_connection *connection;
559         struct mpd_status *status;
561         if (end == start + 1)
562                 /* if that's not really a range, we choose to use the
563                    safer "deleteid" version */
564                 return mpdclient_cmd_delete(c, start);
566         connection = mpdclient_get_connection(c);
567         if (connection == NULL)
568                 return false;
570         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
571                 return mpdclient_cmd_delete_range_fallback(c, start, end);
573         /* MPD 0.16 supports "delete" with a range argument */
575         /* send the delete command to mpd; at the same time, get the
576            new status (to verify the playlist id) */
578         if (!mpd_command_list_begin(connection, false) ||
579             !mpd_send_delete_range(connection, start, end) ||
580             !mpd_send_status(connection) ||
581             !mpd_command_list_end(connection))
582                 return mpdclient_handle_error(c);
584         c->events |= MPD_IDLE_QUEUE;
586         status = mpdclient_recv_status(c);
587         if (status == NULL)
588                 return false;
590         if (!mpd_response_finish(connection))
591                 return mpdclient_handle_error(c);
593         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
594             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
595                 /* the cheap route: match on the new playlist length
596                    and its version, we can keep our local playlist
597                    copy in sync */
598                 c->playlist.version = mpd_status_get_queue_version(status);
600                 /* remove the song from the local playlist */
601                 while (end > start) {
602                         --end;
604                         /* remove references to the song */
605                         if (c->song == playlist_get(&c->playlist, end))
606                                 c->song = NULL;
608                         playlist_remove(&c->playlist, end);
609                 }
610         }
612         return true;
615 bool
616 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
618         struct mpd_connection *connection;
619         struct mpd_status *status;
621         if (dest_pos == src_pos)
622                 return true;
624         connection = mpdclient_get_connection(c);
625         if (connection == NULL)
626                 return false;
628         /* send the "move" command to MPD; at the same time, get the
629            new status (to verify the playlist id) */
631         if (!mpd_command_list_begin(connection, false) ||
632             !mpd_send_move(connection, src_pos, dest_pos) ||
633             !mpd_send_status(connection) ||
634             !mpd_command_list_end(connection))
635                 return mpdclient_handle_error(c);
637         c->events |= MPD_IDLE_QUEUE;
639         status = mpdclient_recv_status(c);
640         if (status == NULL)
641                 return false;
643         if (!mpd_response_finish(connection))
644                 return mpdclient_handle_error(c);
646         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
647             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
648                 /* the cheap route: match on the new playlist length
649                    and its version, we can keep our local playlist
650                    copy in sync */
651                 c->playlist.version = mpd_status_get_queue_version(status);
653                 /* swap songs in the local playlist */
654                 playlist_move(&c->playlist, dest_pos, src_pos);
655         }
657         return true;
661 /****************************************************************************/
662 /*** Playlist management functions ******************************************/
663 /****************************************************************************/
665 /* update playlist */
666 bool
667 mpdclient_playlist_update(struct mpdclient *c)
669         struct mpd_connection *connection = mpdclient_get_connection(c);
670         struct mpd_entity *entity;
672         if (connection == NULL)
673                 return false;
675         playlist_clear(&c->playlist);
677         mpd_send_list_queue_meta(connection);
678         while ((entity = mpd_recv_entity(connection))) {
679                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
680                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
682                 mpd_entity_free(entity);
683         }
685         c->playlist.version = mpd_status_get_queue_version(c->status);
686         c->song = NULL;
688         return mpdclient_finish_command(c);
691 /* update playlist (plchanges) */
692 bool
693 mpdclient_playlist_update_changes(struct mpdclient *c)
695         struct mpd_connection *connection = mpdclient_get_connection(c);
696         struct mpd_song *song;
697         guint length;
699         if (connection == NULL)
700                 return false;
702         mpd_send_queue_changes_meta(connection, c->playlist.version);
704         while ((song = mpd_recv_song(connection)) != NULL) {
705                 int pos = mpd_song_get_pos(song);
707                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
708                         /* update song */
709                         playlist_replace(&c->playlist, pos, song);
710                 } else {
711                         /* add a new song */
712                         playlist_append(&c->playlist, song);
713                 }
715                 mpd_song_free(song);
716         }
718         /* remove trailing songs */
720         length = mpd_status_get_queue_length(c->status);
721         while (length < c->playlist.list->len) {
722                 guint pos = c->playlist.list->len - 1;
724                 /* Remove the last playlist entry */
725                 playlist_remove(&c->playlist, pos);
726         }
728         c->song = NULL;
729         c->playlist.version = mpd_status_get_queue_version(c->status);
731         return mpdclient_finish_command(c);
735 /****************************************************************************/
736 /*** Filelist functions *****************************************************/
737 /****************************************************************************/
739 bool
740 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
742         struct mpd_connection *connection = mpdclient_get_connection(c);
743         guint i;
745         if (connection == NULL)
746                 return false;
748         if (filelist_is_empty(fl))
749                 return true;
751         mpd_command_list_begin(connection, false);
753         for (i = 0; i < filelist_length(fl); ++i) {
754                 struct filelist_entry *entry = filelist_get(fl, i);
755                 struct mpd_entity *entity  = entry->entity;
757                 if (entity != NULL &&
758                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
759                         const struct mpd_song *song =
760                                 mpd_entity_get_song(entity);
762                         mpd_send_add(connection, mpd_song_get_uri(song));
763                 }
764         }
766         mpd_command_list_end(connection);
767         return mpdclient_finish_command(c);