Code

637aef76b8d0ffea6d8d4682c632eba3e67be154
[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_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                   unsigned timeout_ms,
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_ms);
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_QUEUE;
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) >= 0) {
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 (!mpdclient_is_playing(c))
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 || mpd_status_get_queue_length(c->status) < 2)
295                 return true;
297         connection = mpdclient_get_connection(c);
298         if (connection == NULL)
299                 return false;
301         mpd_command_list_begin(connection, false);
303         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) >= 0) {
304                 if (current < length - 1)
305                         mpd_send_delete_range(connection, current + 1, length);
306                 if (current > 0)
307                         mpd_send_delete_range(connection, 0, current);
308         } else
309                 while (--length >= 0)
310                         if (length != current)
311                                 mpd_send_delete(connection, length);
313         mpd_command_list_end(connection);
315         return mpdclient_finish_command(c);
318 bool
319 mpdclient_cmd_clear(struct mpdclient *c)
321         struct mpd_connection *connection = mpdclient_get_connection(c);
322         struct mpd_status *status;
324         if (connection == NULL)
325                 return false;
327         /* send "clear" and "status" */
328         if (!mpd_command_list_begin(connection, false) ||
329             !mpd_send_clear(connection) ||
330             !mpd_send_status(connection) ||
331             !mpd_command_list_end(connection))
332                 return mpdclient_handle_error(c);
334         /* receive the new status, store it in the mpdclient struct */
336         status = mpdclient_recv_status(c);
337         if (status == NULL)
338                 return false;
340         if (!mpd_response_finish(connection))
341                 return mpdclient_handle_error(c);
343         /* update mpdclient.playlist */
345         if (mpd_status_get_queue_length(status) == 0) {
346                 /* after the "clear" command, the queue is really
347                    empty - this means we can clear it locally,
348                    reducing the UI latency */
349                 playlist_clear(&c->playlist);
350                 c->playlist.version = mpd_status_get_queue_version(status);
351                 c->song = NULL;
352         }
354         c->events |= MPD_IDLE_QUEUE;
355         return true;
358 bool
359 mpdclient_cmd_volume(struct mpdclient *c, gint value)
361         struct mpd_connection *connection = mpdclient_get_connection(c);
362         if (connection == NULL)
363                 return false;
365         mpd_send_set_volume(connection, value);
366         return mpdclient_finish_command(c);
369 bool
370 mpdclient_cmd_volume_up(struct mpdclient *c)
372         struct mpd_connection *connection = mpdclient_get_connection(c);
373         if (connection == NULL)
374                 return false;
376         if (c->status == NULL ||
377             mpd_status_get_volume(c->status) == -1)
378                 return true;
380         if (c->volume < 0)
381                 c->volume = mpd_status_get_volume(c->status);
383         if (c->volume >= 100)
384                 return true;
386         return mpdclient_cmd_volume(c, ++c->volume);
389 bool
390 mpdclient_cmd_volume_down(struct mpdclient *c)
392         struct mpd_connection *connection = mpdclient_get_connection(c);
393         if (connection == NULL)
394                 return false;
396         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
397                 return true;
399         if (c->volume < 0)
400                 c->volume = mpd_status_get_volume(c->status);
402         if (c->volume <= 0)
403                 return true;
405         return mpdclient_cmd_volume(c, --c->volume);
408 bool
409 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
411         struct mpd_connection *connection = mpdclient_get_connection(c);
412         if (connection == NULL)
413                 return false;
415         return mpd_send_add(connection, path_utf8)?
416                 mpdclient_finish_command(c) : false;
419 bool
420 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
422         struct mpd_connection *connection = mpdclient_get_connection(c);
423         struct mpd_status *status;
424         struct mpd_song *new_song;
426         assert(c != NULL);
427         assert(song != NULL);
429         if (connection == NULL || c->status == NULL)
430                 return false;
432         /* send the add command to mpd; at the same time, get the new
433            status (to verify the new playlist id) and the last song
434            (we hope that's the song we just added) */
436         if (!mpd_command_list_begin(connection, true) ||
437             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
438             !mpd_send_status(connection) ||
439             !mpd_send_get_queue_song_pos(connection,
440                                          playlist_length(&c->playlist)) ||
441             !mpd_command_list_end(connection) ||
442             !mpd_response_next(connection))
443                 return mpdclient_handle_error(c);
445         c->events |= MPD_IDLE_QUEUE;
447         status = mpdclient_recv_status(c);
448         if (status == NULL)
449                 return false;
451         if (!mpd_response_next(connection))
452                 return mpdclient_handle_error(c);
454         new_song = mpd_recv_song(connection);
455         if (!mpd_response_finish(connection) || new_song == NULL) {
456                 if (new_song != NULL)
457                         mpd_song_free(new_song);
459                 return mpd_connection_clear_error(connection) ||
460                         mpdclient_handle_error(c);
461         }
463         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
464             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
465                 /* the cheap route: match on the new playlist length
466                    and its version, we can keep our local playlist
467                    copy in sync */
468                 c->playlist.version = mpd_status_get_queue_version(status);
470                 /* the song we just received has the correct id;
471                    append it to the local playlist */
472                 playlist_append(&c->playlist, new_song);
473         }
475         mpd_song_free(new_song);
477         return true;
480 bool
481 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
483         struct mpd_connection *connection = mpdclient_get_connection(c);
484         const struct mpd_song *song;
485         struct mpd_status *status;
487         if (connection == NULL || c->status == NULL)
488                 return false;
490         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
491                 return false;
493         song = playlist_get(&c->playlist, idx);
495         /* send the delete command to mpd; at the same time, get the
496            new status (to verify the playlist id) */
498         if (!mpd_command_list_begin(connection, false) ||
499             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
500             !mpd_send_status(connection) ||
501             !mpd_command_list_end(connection))
502                 return mpdclient_handle_error(c);
504         c->events |= MPD_IDLE_QUEUE;
506         status = mpdclient_recv_status(c);
507         if (status == NULL)
508                 return false;
510         if (!mpd_response_finish(connection))
511                 return mpdclient_handle_error(c);
513         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
514             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
515                 /* the cheap route: match on the new playlist length
516                    and its version, we can keep our local playlist
517                    copy in sync */
518                 c->playlist.version = mpd_status_get_queue_version(status);
520                 /* remove the song from the local playlist */
521                 playlist_remove(&c->playlist, idx);
523                 /* remove references to the song */
524                 if (c->song == song)
525                         c->song = NULL;
526         }
528         return true;
531 /**
532  * Fallback for mpdclient_cmd_delete_range() on MPD older than 0.16.
533  * It emulates the "delete range" command with a list of simple
534  * "delete" commands.
535  */
536 static bool
537 mpdclient_cmd_delete_range_fallback(struct mpdclient *c,
538                                     unsigned start, unsigned end)
540         struct mpd_connection *connection = mpdclient_get_connection(c);
541         if (connection == NULL)
542                 return false;
544         if (!mpd_command_list_begin(connection, false))
545                 return mpdclient_handle_error(c);
547         for (; start < end; --end)
548                 mpd_send_delete(connection, start);
550         if (!mpd_command_list_end(connection) ||
551             !mpd_response_finish(connection))
552                 return mpdclient_handle_error(c);
554         return true;
557 bool
558 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
560         struct mpd_connection *connection;
561         struct mpd_status *status;
563         if (end == start + 1)
564                 /* if that's not really a range, we choose to use the
565                    safer "deleteid" version */
566                 return mpdclient_cmd_delete(c, start);
568         connection = mpdclient_get_connection(c);
569         if (connection == NULL)
570                 return false;
572         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0)
573                 return mpdclient_cmd_delete_range_fallback(c, start, end);
575         /* MPD 0.16 supports "delete" with a range argument */
577         /* send the delete command to mpd; at the same time, get the
578            new status (to verify the playlist id) */
580         if (!mpd_command_list_begin(connection, false) ||
581             !mpd_send_delete_range(connection, start, end) ||
582             !mpd_send_status(connection) ||
583             !mpd_command_list_end(connection))
584                 return mpdclient_handle_error(c);
586         c->events |= MPD_IDLE_QUEUE;
588         status = mpdclient_recv_status(c);
589         if (status == NULL)
590                 return false;
592         if (!mpd_response_finish(connection))
593                 return mpdclient_handle_error(c);
595         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
596             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
597                 /* the cheap route: match on the new playlist length
598                    and its version, we can keep our local playlist
599                    copy in sync */
600                 c->playlist.version = mpd_status_get_queue_version(status);
602                 /* remove the song from the local playlist */
603                 while (end > start) {
604                         --end;
606                         /* remove references to the song */
607                         if (c->song == playlist_get(&c->playlist, end))
608                                 c->song = NULL;
610                         playlist_remove(&c->playlist, end);
611                 }
612         }
614         return true;
617 bool
618 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
620         struct mpd_connection *connection;
621         struct mpd_status *status;
623         if (dest_pos == src_pos)
624                 return true;
626         connection = mpdclient_get_connection(c);
627         if (connection == NULL)
628                 return false;
630         /* send the "move" command to MPD; at the same time, get the
631            new status (to verify the playlist id) */
633         if (!mpd_command_list_begin(connection, false) ||
634             !mpd_send_move(connection, src_pos, dest_pos) ||
635             !mpd_send_status(connection) ||
636             !mpd_command_list_end(connection))
637                 return mpdclient_handle_error(c);
639         c->events |= MPD_IDLE_QUEUE;
641         status = mpdclient_recv_status(c);
642         if (status == NULL)
643                 return false;
645         if (!mpd_response_finish(connection))
646                 return mpdclient_handle_error(c);
648         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
649             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
650                 /* the cheap route: match on the new playlist length
651                    and its version, we can keep our local playlist
652                    copy in sync */
653                 c->playlist.version = mpd_status_get_queue_version(status);
655                 /* swap songs in the local playlist */
656                 playlist_move(&c->playlist, dest_pos, src_pos);
657         }
659         return true;
663 /****************************************************************************/
664 /*** Playlist management functions ******************************************/
665 /****************************************************************************/
667 /* update playlist */
668 bool
669 mpdclient_playlist_update(struct mpdclient *c)
671         struct mpd_connection *connection = mpdclient_get_connection(c);
672         struct mpd_entity *entity;
674         if (connection == NULL)
675                 return false;
677         playlist_clear(&c->playlist);
679         mpd_send_list_queue_meta(connection);
680         while ((entity = mpd_recv_entity(connection))) {
681                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
682                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
684                 mpd_entity_free(entity);
685         }
687         c->playlist.version = mpd_status_get_queue_version(c->status);
688         c->song = NULL;
690         return mpdclient_finish_command(c);
693 /* update playlist (plchanges) */
694 bool
695 mpdclient_playlist_update_changes(struct mpdclient *c)
697         struct mpd_connection *connection = mpdclient_get_connection(c);
698         struct mpd_song *song;
699         guint length;
701         if (connection == NULL)
702                 return false;
704         mpd_send_queue_changes_meta(connection, c->playlist.version);
706         while ((song = mpd_recv_song(connection)) != NULL) {
707                 int pos = mpd_song_get_pos(song);
709                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
710                         /* update song */
711                         playlist_replace(&c->playlist, pos, song);
712                 } else {
713                         /* add a new song */
714                         playlist_append(&c->playlist, song);
715                 }
717                 mpd_song_free(song);
718         }
720         /* remove trailing songs */
722         length = mpd_status_get_queue_length(c->status);
723         while (length < c->playlist.list->len) {
724                 guint pos = c->playlist.list->len - 1;
726                 /* Remove the last playlist entry */
727                 playlist_remove(&c->playlist, pos);
728         }
730         c->song = NULL;
731         c->playlist.version = mpd_status_get_queue_version(c->status);
733         return mpdclient_finish_command(c);
737 /****************************************************************************/
738 /*** Filelist functions *****************************************************/
739 /****************************************************************************/
741 bool
742 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
744         struct mpd_connection *connection = mpdclient_get_connection(c);
745         guint i;
747         if (connection == NULL)
748                 return false;
750         if (filelist_is_empty(fl))
751                 return true;
753         mpd_command_list_begin(connection, false);
755         for (i = 0; i < filelist_length(fl); ++i) {
756                 struct filelist_entry *entry = filelist_get(fl, i);
757                 struct mpd_entity *entity  = entry->entity;
759                 if (entity != NULL &&
760                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
761                         const struct mpd_song *song =
762                                 mpd_entity_get_song(entity);
764                         mpd_send_add(connection, mpd_song_get_uri(song));
765                 }
766         }
768         mpd_command_list_end(connection);
769         return mpdclient_finish_command(c);