Code

dbe931c7d66e8bdc61a09e7007661eb7966ec875
[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);
180         bool retval;
182         c->volume = -1;
184         if (connection == NULL)
185                 return false;
187         /* always announce these options as long as we don't have
188            "idle" support */
189         if (c->source == NULL)
190                 c->events |= MPD_IDLE_PLAYER|MPD_IDLE_OPTIONS;
192         /* free the old status */
193         if (c->status)
194                 mpd_status_free(c->status);
196         /* retrieve new status */
197         c->status = mpd_run_status(connection);
198         if (c->status == NULL)
199                 return mpdclient_handle_error(c);
201         if (c->source == NULL &&
202             c->update_id != mpd_status_get_update_id(c->status)) {
203                 c->events |= MPD_IDLE_UPDATE;
205                 if (c->update_id > 0)
206                         c->events |= MPD_IDLE_DATABASE;
207         }
209         c->update_id = mpd_status_get_update_id(c->status);
211         if (c->source == NULL &&
212             c->volume != mpd_status_get_volume(c->status))
213                 c->events |= MPD_IDLE_MIXER;
215         c->volume = mpd_status_get_volume(c->status);
217         /* check if the playlist needs an update */
218         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
219                 if (c->source == NULL)
220                         c->events |= MPD_IDLE_PLAYLIST;
222                 if (!playlist_is_empty(&c->playlist))
223                         retval = mpdclient_playlist_update_changes(c);
224                 else
225                         retval = mpdclient_playlist_update(c);
226         } else
227                 retval = true;
229         /* update the current song */
230         if (!c->song || mpd_status_get_song_id(c->status)) {
231                 c->song = playlist_get_song(&c->playlist,
232                                             mpd_status_get_song_pos(c->status));
233         }
235         return retval;
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         }
260 /****************************************************************************/
261 /*** MPD Commands  **********************************************************/
262 /****************************************************************************/
264 bool
265 mpdclient_cmd_crop(struct mpdclient *c)
267         struct mpd_connection *connection = mpdclient_get_connection(c);
268         struct mpd_status *status;
269         bool playing;
270         int length, current;
272         if (connection == NULL)
273                 return false;
275         status = mpd_run_status(connection);
276         if (status == NULL)
277                 return mpdclient_handle_error(c);
279         playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
280                 mpd_status_get_state(status) == MPD_STATE_PAUSE;
281         length = mpd_status_get_queue_length(status);
282         current = mpd_status_get_song_pos(status);
284         mpd_status_free(status);
286         if (!playing || length < 2)
287                 return true;
289         mpd_command_list_begin(connection, false);
291         while (--length >= 0)
292                 if (length != current)
293                         mpd_send_delete(connection, length);
295         mpd_command_list_end(connection);
297         return mpdclient_finish_command(c);
300 bool
301 mpdclient_cmd_clear(struct mpdclient *c)
303         struct mpd_connection *connection = mpdclient_get_connection(c);
304         bool retval;
306         if (connection == NULL)
307                 return false;
309         mpd_send_clear(connection);
310         retval = mpdclient_finish_command(c);
312         if (retval)
313                 c->events |= MPD_IDLE_PLAYLIST;
315         return retval;
318 bool
319 mpdclient_cmd_volume(struct mpdclient *c, gint value)
321         struct mpd_connection *connection = mpdclient_get_connection(c);
322         if (connection == NULL)
323                 return false;
325         mpd_send_set_volume(connection, value);
326         return mpdclient_finish_command(c);
329 bool
330 mpdclient_cmd_volume_up(struct mpdclient *c)
332         struct mpd_connection *connection = mpdclient_get_connection(c);
333         if (connection == NULL)
334                 return false;
336         if (c->status == NULL ||
337             mpd_status_get_volume(c->status) == -1)
338                 return true;
340         if (c->volume < 0)
341                 c->volume = mpd_status_get_volume(c->status);
343         if (c->volume >= 100)
344                 return true;
346         return mpdclient_cmd_volume(c, ++c->volume);
349 bool
350 mpdclient_cmd_volume_down(struct mpdclient *c)
352         struct mpd_connection *connection = mpdclient_get_connection(c);
353         if (connection == NULL)
354                 return false;
356         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
357                 return true;
359         if (c->volume < 0)
360                 c->volume = mpd_status_get_volume(c->status);
362         if (c->volume <= 0)
363                 return true;
365         return mpdclient_cmd_volume(c, --c->volume);
368 bool
369 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
371         struct mpd_connection *connection = mpdclient_get_connection(c);
372         if (connection == NULL)
373                 return false;
375         mpd_send_add(connection, path_utf8);
376         return mpdclient_finish_command(c);
379 bool
380 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
382         struct mpd_connection *connection = mpdclient_get_connection(c);
383         struct mpd_status *status;
384         struct mpd_song *new_song;
386         assert(c != NULL);
387         assert(song != NULL);
389         if (connection == NULL || c->status == NULL)
390                 return false;
392         /* send the add command to mpd; at the same time, get the new
393            status (to verify the new playlist id) and the last song
394            (we hope that's the song we just added) */
396         if (!mpd_command_list_begin(connection, true) ||
397             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
398             !mpd_send_status(connection) ||
399             !mpd_send_get_queue_song_pos(connection,
400                                          playlist_length(&c->playlist)) ||
401             !mpd_command_list_end(connection) ||
402             !mpd_response_next(connection))
403                 return mpdclient_handle_error(c);
405         c->events |= MPD_IDLE_PLAYLIST;
407         status = mpd_recv_status(connection);
408         if (status != NULL) {
409                 if (c->status != NULL)
410                         mpd_status_free(c->status);
411                 c->status = status;
412         }
414         if (!mpd_response_next(connection))
415                 return mpdclient_handle_error(c);
417         new_song = mpd_recv_song(connection);
418         if (!mpd_response_finish(connection) || new_song == NULL) {
419                 if (new_song != NULL)
420                         mpd_song_free(new_song);
422                 return mpd_connection_clear_error(connection) ||
423                         mpdclient_handle_error(c);
424         }
426         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
427             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
428                 /* the cheap route: match on the new playlist length
429                    and its version, we can keep our local playlist
430                    copy in sync */
431                 c->playlist.version = mpd_status_get_queue_version(status);
433                 /* the song we just received has the correct id;
434                    append it to the local playlist */
435                 playlist_append(&c->playlist, new_song);
436         }
438         mpd_song_free(new_song);
440         return true;
443 bool
444 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
446         struct mpd_connection *connection = mpdclient_get_connection(c);
447         const struct mpd_song *song;
448         struct mpd_status *status;
450         if (connection == NULL || c->status == NULL)
451                 return false;
453         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
454                 return false;
456         song = playlist_get(&c->playlist, idx);
458         /* send the delete command to mpd; at the same time, get the
459            new status (to verify the playlist id) */
461         if (!mpd_command_list_begin(connection, false) ||
462             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
463             !mpd_send_status(connection) ||
464             !mpd_command_list_end(connection))
465                 return mpdclient_handle_error(c);
467         c->events |= MPD_IDLE_PLAYLIST;
469         status = mpd_recv_status(connection);
470         if (status != NULL) {
471                 if (c->status != NULL)
472                         mpd_status_free(c->status);
473                 c->status = status;
474         }
476         if (!mpd_response_finish(connection))
477                 return mpdclient_handle_error(c);
479         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
480             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
481                 /* the cheap route: match on the new playlist length
482                    and its version, we can keep our local playlist
483                    copy in sync */
484                 c->playlist.version = mpd_status_get_queue_version(status);
486                 /* remove the song from the local playlist */
487                 playlist_remove(&c->playlist, idx);
489                 /* remove references to the song */
490                 if (c->song == song)
491                         c->song = NULL;
492         }
494         return true;
497 /**
498  * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
499  * It emulates the "delete range" command with a list of simple
500  * "delete" commands.
501  */
502 static bool
503 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
504                                     unsigned start, unsigned end)
506         struct mpd_connection *connection = mpdclient_get_connection(c);
507         if (connection == NULL)
508                 return false;
510         if (!mpd_command_list_begin(connection, false))
511                 return mpdclient_handle_error(c);
513         for (; start < end; --end)
514                 mpd_send_delete(connection, start);
516         if (!mpd_command_list_end(connection) ||
517             !mpd_response_finish(connection))
518                 return mpdclient_handle_error(c);
520         return true;
523 bool
524 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
526         struct mpd_connection *connection;
527         struct mpd_status *status;
529         if (end == start + 1)
530                 /* if that's not really a range, we choose to use the
531                    safer "deleteid" version */
532                 return mpdclient_cmd_delete(c, start);
534         connection = mpdclient_get_connection(c);
535         if (connection == NULL)
536                 return false;
538         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
539                 return mpdclient_cmd_delete_range_fallback(c, start, end);
541         /* MPD 0.16 supports "delete" with a range argument */
543         /* send the delete command to mpd; at the same time, get the
544            new status (to verify the playlist id) */
546         if (!mpd_command_list_begin(connection, false) ||
547             !mpd_send_delete_range(connection, start, end) ||
548             !mpd_send_status(connection) ||
549             !mpd_command_list_end(connection))
550                 return mpdclient_handle_error(c);
552         c->events |= MPD_IDLE_PLAYLIST;
554         status = mpd_recv_status(connection);
555         if (status != NULL) {
556                 if (c->status != NULL)
557                         mpd_status_free(c->status);
558                 c->status = status;
559         }
561         if (!mpd_response_finish(connection))
562                 return mpdclient_handle_error(c);
564         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
565             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
566                 /* the cheap route: match on the new playlist length
567                    and its version, we can keep our local playlist
568                    copy in sync */
569                 c->playlist.version = mpd_status_get_queue_version(status);
571                 /* remove the song from the local playlist */
572                 while (end > start) {
573                         --end;
575                         /* remove references to the song */
576                         if (c->song == playlist_get(&c->playlist, end))
577                                 c->song = NULL;
579                         playlist_remove(&c->playlist, end);
580                 }
581         }
583         return true;
586 bool
587 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
589         struct mpd_connection *connection = mpdclient_get_connection(c);
590         const struct mpd_song *song1, *song2;
591         struct mpd_status *status;
593         if (connection == NULL)
594                 return false;
596         if (old_index == new_index || new_index < 0 ||
597             (guint)new_index >= c->playlist.list->len)
598                 return false;
600         song1 = playlist_get(&c->playlist, old_index);
601         song2 = playlist_get(&c->playlist, new_index);
603         /* send the delete command to mpd; at the same time, get the
604            new status (to verify the playlist id) */
606         if (!mpd_command_list_begin(connection, false) ||
607             !mpd_send_swap_id(connection, mpd_song_get_id(song1),
608                               mpd_song_get_id(song2)) ||
609             !mpd_send_status(connection) ||
610             !mpd_command_list_end(connection))
611                 return mpdclient_handle_error(c);
613         c->events |= MPD_IDLE_PLAYLIST;
615         status = mpd_recv_status(connection);
616         if (status != NULL) {
617                 if (c->status != NULL)
618                         mpd_status_free(c->status);
619                 c->status = status;
620         }
622         if (!mpd_response_finish(connection))
623                 return mpdclient_handle_error(c);
625         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
626             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
627                 /* the cheap route: match on the new playlist length
628                    and its version, we can keep our local playlist
629                    copy in sync */
630                 c->playlist.version = mpd_status_get_queue_version(status);
632                 /* swap songs in the local playlist */
633                 playlist_swap(&c->playlist, old_index, new_index);
634         }
636         return true;
640 /****************************************************************************/
641 /*** Playlist management functions ******************************************/
642 /****************************************************************************/
644 /* update playlist */
645 bool
646 mpdclient_playlist_update(struct mpdclient *c)
648         struct mpd_connection *connection = mpdclient_get_connection(c);
649         struct mpd_entity *entity;
651         if (connection == NULL)
652                 return false;
654         playlist_clear(&c->playlist);
656         mpd_send_list_queue_meta(connection);
657         while ((entity = mpd_recv_entity(connection))) {
658                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
659                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
661                 mpd_entity_free(entity);
662         }
664         c->playlist.version = mpd_status_get_queue_version(c->status);
665         c->song = NULL;
667         return mpdclient_finish_command(c);
670 /* update playlist (plchanges) */
671 bool
672 mpdclient_playlist_update_changes(struct mpdclient *c)
674         struct mpd_connection *connection = mpdclient_get_connection(c);
675         struct mpd_song *song;
676         guint length;
678         if (connection == NULL)
679                 return false;
681         mpd_send_queue_changes_meta(connection, c->playlist.version);
683         while ((song = mpd_recv_song(connection)) != NULL) {
684                 int pos = mpd_song_get_pos(song);
686                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
687                         /* update song */
688                         playlist_replace(&c->playlist, pos, song);
689                 } else {
690                         /* add a new song */
691                         playlist_append(&c->playlist, song);
692                 }
694                 mpd_song_free(song);
695         }
697         /* remove trailing songs */
699         length = mpd_status_get_queue_length(c->status);
700         while (length < c->playlist.list->len) {
701                 guint pos = c->playlist.list->len - 1;
703                 /* Remove the last playlist entry */
704                 playlist_remove(&c->playlist, pos);
705         }
707         c->song = NULL;
708         c->playlist.version = mpd_status_get_queue_version(c->status);
710         return mpdclient_finish_command(c);
714 /****************************************************************************/
715 /*** Filelist functions *****************************************************/
716 /****************************************************************************/
718 bool
719 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
721         struct mpd_connection *connection = mpdclient_get_connection(c);
722         guint i;
724         if (connection == NULL)
725                 return false;
727         if (filelist_is_empty(fl))
728                 return true;
730         mpd_command_list_begin(connection, false);
732         for (i = 0; i < filelist_length(fl); ++i) {
733                 struct filelist_entry *entry = filelist_get(fl, i);
734                 struct mpd_entity *entity  = entry->entity;
736                 if (entity != NULL &&
737                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
738                         const struct mpd_song *song =
739                                 mpd_entity_get_song(entity);
741                         mpd_send_add(connection, mpd_song_get_uri(song));
742                 }
743         }
745         mpd_command_list_end(connection);
746         return mpdclient_finish_command(c);