Code

mpdclient: remove unused function compare_filelistentry_format()
[ncmpc.git] / src / mpdclient.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "mpdclient.h"
21 #include "callbacks.h"
22 #include "filelist.h"
23 #include "config.h"
24 #include "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 /****************************************************************************/
39 /*** mpdclient functions ****************************************************/
40 /****************************************************************************/
42 bool
43 mpdclient_handle_error(struct mpdclient *c)
44 {
45         enum mpd_error error = mpd_connection_get_error(c->connection);
47         assert(error != MPD_ERROR_SUCCESS);
49         if (error == MPD_ERROR_SERVER &&
50             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
51             mpdclient_auth_callback(c))
52                 return true;
54         mpdclient_error_callback(mpd_connection_get_error_message(c->connection));
56         if (!mpd_connection_clear_error(c->connection))
57                 mpdclient_disconnect(c);
59         return false;
60 }
62 struct mpdclient *
63 mpdclient_new(void)
64 {
65         struct mpdclient *c = g_new0(struct mpdclient, 1);
66         playlist_init(&c->playlist);
67         c->volume = -1;
68         c->events = 0;
70         return c;
71 }
73 void
74 mpdclient_free(struct mpdclient *c)
75 {
76         mpdclient_disconnect(c);
78         mpdclient_playlist_free(&c->playlist);
80         g_free(c);
81 }
83 void
84 mpdclient_disconnect(struct mpdclient *c)
85 {
86         if (c->source != NULL) {
87                 mpd_glib_free(c->source);
88                 c->source = NULL;
89                 c->idle = false;
90         }
92         if (c->connection) {
93                 mpd_connection_free(c->connection);
94                 ++c->connection_id;
95         }
96         c->connection = NULL;
98         if (c->status)
99                 mpd_status_free(c->status);
100         c->status = NULL;
102         playlist_clear(&c->playlist);
104         if (c->song)
105                 c->song = NULL;
107         /* everything has changed after a disconnect */
108         c->events |= MPD_IDLE_ALL;
111 bool
112 mpdclient_connect(struct mpdclient *c,
113                   const gchar *host,
114                   gint port,
115                   unsigned timeout_ms,
116                   const gchar *password)
118         /* close any open connection */
119         if (c->connection)
120                 mpdclient_disconnect(c);
122         /* connect to MPD */
123         c->connection = mpd_connection_new(host, port, timeout_ms);
124         if (c->connection == NULL)
125                 g_error("Out of memory");
127         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
128                 mpdclient_handle_error(c);
129                 mpdclient_disconnect(c);
130                 return false;
131         }
133         /* send password */
134         if (password != NULL && !mpd_run_password(c->connection, password)) {
135                 mpdclient_handle_error(c);
136                 mpdclient_disconnect(c);
137                 return false;
138         }
140         c->source = mpd_glib_new(c->connection,
141                                  mpdclient_idle_callback, c);
143         ++c->connection_id;
145         return true;
148 bool
149 mpdclient_update(struct mpdclient *c)
151         struct mpd_connection *connection = mpdclient_get_connection(c);
153         c->volume = -1;
155         if (connection == NULL)
156                 return false;
158         /* free the old status */
159         if (c->status)
160                 mpd_status_free(c->status);
162         /* retrieve new status */
163         c->status = mpd_run_status(connection);
164         if (c->status == NULL)
165                 return mpdclient_handle_error(c);
167         c->update_id = mpd_status_get_update_id(c->status);
169         c->volume = mpd_status_get_volume(c->status);
171         /* check if the playlist needs an update */
172         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
173                 bool retval;
175                 if (!playlist_is_empty(&c->playlist))
176                         retval = mpdclient_playlist_update_changes(c);
177                 else
178                         retval = mpdclient_playlist_update(c);
179                 if (!retval)
180                         return false;
181         }
183         /* update the current song */
184         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
185                 c->song = playlist_get_song(&c->playlist,
186                                             mpd_status_get_song_pos(c->status));
187         }
189         return true;
192 struct mpd_connection *
193 mpdclient_get_connection(struct mpdclient *c)
195         if (c->source != NULL && c->idle) {
196                 c->idle = false;
197                 mpd_glib_leave(c->source);
198         }
200         return c->connection;
203 void
204 mpdclient_put_connection(struct mpdclient *c)
206         assert(c->source == NULL || c->connection != NULL);
208         if (c->source != NULL && !c->idle) {
209                 c->idle = mpd_glib_enter(c->source);
210         }
213 static struct mpd_status *
214 mpdclient_recv_status(struct mpdclient *c)
216         assert(c->connection != NULL);
218         struct mpd_status *status = mpd_recv_status(c->connection);
219         if (status == NULL) {
220                 mpdclient_handle_error(c);
221                 return NULL;
222         }
224         if (c->status != NULL)
225                 mpd_status_free(c->status);
226         return c->status = status;
229 /****************************************************************************/
230 /*** MPD Commands  **********************************************************/
231 /****************************************************************************/
233 bool
234 mpdclient_cmd_crop(struct mpdclient *c)
236         if (!mpdclient_is_playing(c))
237                 return false;
239         int length = mpd_status_get_queue_length(c->status);
240         int current = mpd_status_get_song_pos(c->status);
241         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
242                 return true;
244         struct mpd_connection *connection = mpdclient_get_connection(c);
245         if (connection == NULL)
246                 return false;
248         mpd_command_list_begin(connection, false);
250         if (current < length - 1)
251                 mpd_send_delete_range(connection, current + 1, length);
252         if (current > 0)
253                 mpd_send_delete_range(connection, 0, current);
255         mpd_command_list_end(connection);
257         return mpdclient_finish_command(c);
260 bool
261 mpdclient_cmd_clear(struct mpdclient *c)
263         struct mpd_connection *connection = mpdclient_get_connection(c);
264         if (connection == NULL)
265                 return false;
267         /* send "clear" and "status" */
268         if (!mpd_command_list_begin(connection, false) ||
269             !mpd_send_clear(connection) ||
270             !mpd_send_status(connection) ||
271             !mpd_command_list_end(connection))
272                 return mpdclient_handle_error(c);
274         /* receive the new status, store it in the mpdclient struct */
276         struct mpd_status *status = mpdclient_recv_status(c);
277         if (status == NULL)
278                 return false;
280         if (!mpd_response_finish(connection))
281                 return mpdclient_handle_error(c);
283         /* update mpdclient.playlist */
285         if (mpd_status_get_queue_length(status) == 0) {
286                 /* after the "clear" command, the queue is really
287                    empty - this means we can clear it locally,
288                    reducing the UI latency */
289                 playlist_clear(&c->playlist);
290                 c->playlist.version = mpd_status_get_queue_version(status);
291                 c->song = NULL;
292         }
294         c->events |= MPD_IDLE_QUEUE;
295         return true;
298 bool
299 mpdclient_cmd_volume(struct mpdclient *c, gint value)
301         struct mpd_connection *connection = mpdclient_get_connection(c);
302         if (connection == NULL)
303                 return false;
305         mpd_send_set_volume(connection, value);
306         return mpdclient_finish_command(c);
309 bool
310 mpdclient_cmd_volume_up(struct mpdclient *c)
312         struct mpd_connection *connection = mpdclient_get_connection(c);
313         if (connection == NULL)
314                 return false;
316         if (c->status == NULL ||
317             mpd_status_get_volume(c->status) == -1)
318                 return true;
320         if (c->volume < 0)
321                 c->volume = mpd_status_get_volume(c->status);
323         if (c->volume >= 100)
324                 return true;
326         return mpdclient_cmd_volume(c, ++c->volume);
329 bool
330 mpdclient_cmd_volume_down(struct mpdclient *c)
332         struct mpd_connection *connection = mpdclient_get_connection(c);
333         if (connection == NULL)
334                 return false;
336         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
337                 return true;
339         if (c->volume < 0)
340                 c->volume = mpd_status_get_volume(c->status);
342         if (c->volume <= 0)
343                 return true;
345         return mpdclient_cmd_volume(c, --c->volume);
348 bool
349 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
351         struct mpd_connection *connection = mpdclient_get_connection(c);
352         if (connection == NULL)
353                 return false;
355         return mpd_send_add(connection, path_utf8)?
356                 mpdclient_finish_command(c) : false;
359 bool
360 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
362         assert(c != NULL);
363         assert(song != NULL);
365         struct mpd_connection *connection = mpdclient_get_connection(c);
366         if (connection == NULL || c->status == NULL)
367                 return false;
369         /* send the add command to mpd; at the same time, get the new
370            status (to verify the new playlist id) and the last song
371            (we hope that's the song we just added) */
373         if (!mpd_command_list_begin(connection, true) ||
374             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
375             !mpd_send_status(connection) ||
376             !mpd_send_get_queue_song_pos(connection,
377                                          playlist_length(&c->playlist)) ||
378             !mpd_command_list_end(connection) ||
379             !mpd_response_next(connection))
380                 return mpdclient_handle_error(c);
382         c->events |= MPD_IDLE_QUEUE;
384         struct mpd_status *status = mpdclient_recv_status(c);
385         if (status == NULL)
386                 return false;
388         if (!mpd_response_next(connection))
389                 return mpdclient_handle_error(c);
391         struct mpd_song *new_song = mpd_recv_song(connection);
392         if (!mpd_response_finish(connection) || new_song == NULL) {
393                 if (new_song != NULL)
394                         mpd_song_free(new_song);
396                 return mpd_connection_clear_error(connection) ||
397                         mpdclient_handle_error(c);
398         }
400         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
401             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
402                 /* the cheap route: match on the new playlist length
403                    and its version, we can keep our local playlist
404                    copy in sync */
405                 c->playlist.version = mpd_status_get_queue_version(status);
407                 /* the song we just received has the correct id;
408                    append it to the local playlist */
409                 playlist_append(&c->playlist, new_song);
410         }
412         mpd_song_free(new_song);
414         return true;
417 bool
418 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
420         struct mpd_connection *connection = mpdclient_get_connection(c);
422         if (connection == NULL || c->status == NULL)
423                 return false;
425         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
426                 return false;
428         const struct mpd_song *song = playlist_get(&c->playlist, idx);
430         /* send the delete command to mpd; at the same time, get the
431            new status (to verify the playlist id) */
433         if (!mpd_command_list_begin(connection, false) ||
434             !mpd_send_delete_id(connection, mpd_song_get_id(song)) ||
435             !mpd_send_status(connection) ||
436             !mpd_command_list_end(connection))
437                 return mpdclient_handle_error(c);
439         c->events |= MPD_IDLE_QUEUE;
441         struct mpd_status *status = mpdclient_recv_status(c);
442         if (status == NULL)
443                 return false;
445         if (!mpd_response_finish(connection))
446                 return mpdclient_handle_error(c);
448         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - 1 &&
449             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
450                 /* the cheap route: match on the new playlist length
451                    and its version, we can keep our local playlist
452                    copy in sync */
453                 c->playlist.version = mpd_status_get_queue_version(status);
455                 /* remove the song from the local playlist */
456                 playlist_remove(&c->playlist, idx);
458                 /* remove references to the song */
459                 if (c->song == song)
460                         c->song = NULL;
461         }
463         return true;
466 bool
467 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
469         if (end == start + 1)
470                 /* if that's not really a range, we choose to use the
471                    safer "deleteid" version */
472                 return mpdclient_cmd_delete(c, start);
474         struct mpd_connection *connection = mpdclient_get_connection(c);
475         if (connection == NULL)
476                 return false;
478         /* send the delete command to mpd; at the same time, get the
479            new status (to verify the playlist id) */
481         if (!mpd_command_list_begin(connection, false) ||
482             !mpd_send_delete_range(connection, start, end) ||
483             !mpd_send_status(connection) ||
484             !mpd_command_list_end(connection))
485                 return mpdclient_handle_error(c);
487         c->events |= MPD_IDLE_QUEUE;
489         struct mpd_status *status = mpdclient_recv_status(c);
490         if (status == NULL)
491                 return false;
493         if (!mpd_response_finish(connection))
494                 return mpdclient_handle_error(c);
496         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
497             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
498                 /* the cheap route: match on the new playlist length
499                    and its version, we can keep our local playlist
500                    copy in sync */
501                 c->playlist.version = mpd_status_get_queue_version(status);
503                 /* remove the song from the local playlist */
504                 while (end > start) {
505                         --end;
507                         /* remove references to the song */
508                         if (c->song == playlist_get(&c->playlist, end))
509                                 c->song = NULL;
511                         playlist_remove(&c->playlist, end);
512                 }
513         }
515         return true;
518 bool
519 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
521         if (dest_pos == src_pos)
522                 return true;
524         struct mpd_connection *connection = mpdclient_get_connection(c);
525         if (connection == NULL)
526                 return false;
528         /* send the "move" command to MPD; at the same time, get the
529            new status (to verify the playlist id) */
531         if (!mpd_command_list_begin(connection, false) ||
532             !mpd_send_move(connection, src_pos, dest_pos) ||
533             !mpd_send_status(connection) ||
534             !mpd_command_list_end(connection))
535                 return mpdclient_handle_error(c);
537         c->events |= MPD_IDLE_QUEUE;
539         struct mpd_status *status = mpdclient_recv_status(c);
540         if (status == NULL)
541                 return false;
543         if (!mpd_response_finish(connection))
544                 return mpdclient_handle_error(c);
546         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
547             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
548                 /* the cheap route: match on the new playlist length
549                    and its version, we can keep our local playlist
550                    copy in sync */
551                 c->playlist.version = mpd_status_get_queue_version(status);
553                 /* swap songs in the local playlist */
554                 playlist_move(&c->playlist, dest_pos, src_pos);
555         }
557         return true;
560 /* The client-to-client protocol (MPD 0.17.0) */
562 bool
563 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
565         struct mpd_connection *connection = mpdclient_get_connection(c);
567         if (connection == NULL)
568                 return false;
570         if (!mpd_send_subscribe(connection, channel))
571                 return mpdclient_handle_error(c);
573         return mpdclient_finish_command(c);
576 bool
577 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
579         struct mpd_connection *connection = mpdclient_get_connection(c);
580         if (connection == NULL)
581                 return false;
583         if (!mpd_send_unsubscribe(connection, channel))
584                 return mpdclient_handle_error(c);
586         return mpdclient_finish_command(c);
589 bool
590 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
591                            const char *text)
593         struct mpd_connection *connection = mpdclient_get_connection(c);
594         if (connection == NULL)
595                 return false;
597         if (!mpd_send_send_message(connection, channel, text))
598                 return mpdclient_handle_error(c);
600         return mpdclient_finish_command(c);
603 bool
604 mpdclient_send_read_messages(struct mpdclient *c)
606         struct mpd_connection *connection = mpdclient_get_connection(c);
607         if (connection == NULL)
608                 return false;
610         return mpd_send_read_messages(connection)?
611                 true : mpdclient_handle_error(c);
614 struct mpd_message *
615 mpdclient_recv_message(struct mpdclient *c)
617         struct mpd_connection *connection = mpdclient_get_connection(c);
618         if (connection == NULL)
619                 return false;
621         struct mpd_message *message = mpd_recv_message(connection);
622         if (message == NULL &&
623             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
624                 mpdclient_handle_error(c);
626         return message;
629 /****************************************************************************/
630 /*** Playlist management functions ******************************************/
631 /****************************************************************************/
633 /* update playlist */
634 bool
635 mpdclient_playlist_update(struct mpdclient *c)
637         struct mpd_connection *connection = mpdclient_get_connection(c);
638         if (connection == NULL)
639                 return false;
641         playlist_clear(&c->playlist);
643         mpd_send_list_queue_meta(connection);
645         struct mpd_entity *entity;
646         while ((entity = mpd_recv_entity(connection))) {
647                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
648                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
650                 mpd_entity_free(entity);
651         }
653         c->playlist.version = mpd_status_get_queue_version(c->status);
654         c->song = NULL;
656         return mpdclient_finish_command(c);
659 /* update playlist (plchanges) */
660 bool
661 mpdclient_playlist_update_changes(struct mpdclient *c)
663         struct mpd_connection *connection = mpdclient_get_connection(c);
665         if (connection == NULL)
666                 return false;
668         mpd_send_queue_changes_meta(connection, c->playlist.version);
670         struct mpd_song *song;
671         while ((song = mpd_recv_song(connection)) != NULL) {
672                 int pos = mpd_song_get_pos(song);
674                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
675                         /* update song */
676                         playlist_replace(&c->playlist, pos, song);
677                 } else {
678                         /* add a new song */
679                         playlist_append(&c->playlist, song);
680                 }
682                 mpd_song_free(song);
683         }
685         /* remove trailing songs */
687         unsigned length = mpd_status_get_queue_length(c->status);
688         while (length < c->playlist.list->len) {
689                 guint pos = c->playlist.list->len - 1;
691                 /* Remove the last playlist entry */
692                 playlist_remove(&c->playlist, pos);
693         }
695         c->song = NULL;
696         c->playlist.version = mpd_status_get_queue_version(c->status);
698         return mpdclient_finish_command(c);
702 /****************************************************************************/
703 /*** Filelist functions *****************************************************/
704 /****************************************************************************/
706 bool
707 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
709         struct mpd_connection *connection = mpdclient_get_connection(c);
710         if (connection == NULL)
711                 return false;
713         if (filelist_is_empty(fl))
714                 return true;
716         mpd_command_list_begin(connection, false);
718         for (unsigned i = 0; i < filelist_length(fl); ++i) {
719                 struct filelist_entry *entry = filelist_get(fl, i);
720                 struct mpd_entity *entity  = entry->entity;
722                 if (entity != NULL &&
723                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
724                         const struct mpd_song *song =
725                                 mpd_entity_get_song(entity);
727                         mpd_send_add(connection, mpd_song_get_uri(song));
728                 }
729         }
731         mpd_command_list_end(connection);
732         return mpdclient_finish_command(c);