Code

6e6dd9948f6fa218e261246c7510ead26adac537
[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 "gidle.h"
25 #include "charset.h"
27 #include <mpd/client.h>
29 #include <assert.h>
31 static gboolean
32 mpdclient_enter_idle_callback(gpointer user_data)
33 {
34         struct mpdclient *c = user_data;
35         assert(c->enter_idle_source_id != 0);
36         assert(c->source != NULL);
37         assert(!c->idle);
39         c->enter_idle_source_id = 0;
40         c->idle = mpd_glib_enter(c->source);
41         return false;
42 }
44 static void
45 mpdclient_schedule_enter_idle(struct mpdclient *c)
46 {
47         assert(c != NULL);
48         assert(c->source != NULL);
50         if (c->enter_idle_source_id == 0)
51                 /* automatically re-enter MPD "idle" mode */
52                 c->enter_idle_source_id =
53                         g_idle_add(mpdclient_enter_idle_callback, c);
54 }
56 static void
57 mpdclient_cancel_enter_idle(struct mpdclient *c)
58 {
59         if (c->enter_idle_source_id != 0) {
60                 g_source_remove(c->enter_idle_source_id);
61                 c->enter_idle_source_id = 0;
62         }
63 }
65 static void
66 mpdclient_invoke_error_callback(enum mpd_error error,
67                                 const char *message)
68 {
69         char *allocated;
70         if (error == MPD_ERROR_SERVER)
71                 /* server errors are UTF-8, the others are locale */
72                 message = allocated = utf8_to_locale(message);
73         else
74                 allocated = NULL;
76         mpdclient_error_callback(message);
77         g_free(allocated);
78 }
80 static void
81 mpdclient_invoke_error_callback1(struct mpdclient *c)
82 {
83         assert(c != NULL);
84         assert(c->connection != NULL);
86         struct mpd_connection *connection = c->connection;
88         enum mpd_error error = mpd_connection_get_error(connection);
89         assert(error != MPD_ERROR_SUCCESS);
91         mpdclient_invoke_error_callback(error,
92                                         mpd_connection_get_error_message(connection));
93 }
95 static void
96 mpdclient_gidle_callback(enum mpd_error error,
97                          gcc_unused enum mpd_server_error server_error,
98                          const char *message, enum mpd_idle events,
99                          void *ctx)
101         struct mpdclient *c = ctx;
103         c->idle = false;
105         assert(mpdclient_is_connected(c));
107         if (error != MPD_ERROR_SUCCESS) {
108                 mpdclient_invoke_error_callback(error, message);
109                 mpdclient_disconnect(c);
110                 mpdclient_lost_callback();
111                 return;
112         }
114         c->events |= events;
115         mpdclient_update(c);
117         mpdclient_idle_callback(c->events);
119         c->events = 0;
121         if (c->source != NULL)
122                 mpdclient_schedule_enter_idle(c);
125 /****************************************************************************/
126 /*** mpdclient functions ****************************************************/
127 /****************************************************************************/
129 bool
130 mpdclient_handle_error(struct mpdclient *c)
132         enum mpd_error error = mpd_connection_get_error(c->connection);
134         assert(error != MPD_ERROR_SUCCESS);
136         if (error == MPD_ERROR_SERVER &&
137             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
138             mpdclient_auth_callback(c))
139                 return true;
141         mpdclient_invoke_error_callback(error,
142                                         mpd_connection_get_error_message(c->connection));
144         if (!mpd_connection_clear_error(c->connection))
145                 mpdclient_disconnect(c);
147         return false;
150 struct mpdclient *
151 mpdclient_new(const gchar *host, unsigned port,
152               unsigned timeout_ms, const gchar *password)
154         struct mpdclient *c = g_new0(struct mpdclient, 1);
156         c->host = host;
157         c->port = port;
158         c->timeout_ms = timeout_ms;
159         c->password = password;
161         playlist_init(&c->playlist);
162         c->volume = -1;
163         c->events = 0;
164         c->playing = false;
166         return c;
169 void
170 mpdclient_free(struct mpdclient *c)
172         mpdclient_disconnect(c);
174         mpdclient_playlist_free(&c->playlist);
176         g_free(c);
179 static void
180 mpdclient_status_free(struct mpdclient *c)
182         if (c->status == NULL)
183                 return;
185         mpd_status_free(c->status);
186         c->status = NULL;
188         c->volume = -1;
189         c->playing = false;
192 void
193 mpdclient_disconnect(struct mpdclient *c)
195         mpdclient_cancel_enter_idle(c);
197         if (c->source != NULL) {
198                 mpd_glib_free(c->source);
199                 c->source = NULL;
200                 c->idle = false;
201         }
203         if (c->connection) {
204                 mpd_connection_free(c->connection);
205                 ++c->connection_id;
206         }
207         c->connection = NULL;
209         mpdclient_status_free(c);
211         playlist_clear(&c->playlist);
213         if (c->song)
214                 c->song = NULL;
216         /* everything has changed after a disconnect */
217         c->events |= MPD_IDLE_ALL;
220 bool
221 mpdclient_connect(struct mpdclient *c)
223         /* close any open connection */
224         mpdclient_disconnect(c);
226         /* connect to MPD */
227         c->connection = mpd_connection_new(c->host, c->port, c->timeout_ms);
228         if (c->connection == NULL)
229                 g_error("Out of memory");
231         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
232                 mpdclient_invoke_error_callback1(c);
233                 mpdclient_disconnect(c);
234                 mpdclient_failed_callback();
235                 return false;
236         }
238         /* send password */
239         if (c->password != NULL &&
240             !mpd_run_password(c->connection, c->password)) {
241                 mpdclient_invoke_error_callback1(c);
242                 mpdclient_disconnect(c);
243                 mpdclient_failed_callback();
244                 return false;
245         }
247         c->source = mpd_glib_new(c->connection,
248                                  mpdclient_gidle_callback, c);
249         mpdclient_schedule_enter_idle(c);
251         ++c->connection_id;
253         mpdclient_connected_callback();
255         return true;
258 bool
259 mpdclient_update(struct mpdclient *c)
261         struct mpd_connection *connection = mpdclient_get_connection(c);
263         if (connection == NULL)
264                 return false;
266         /* free the old status */
267         mpdclient_status_free(c);
269         /* retrieve new status */
270         c->status = mpd_run_status(connection);
271         if (c->status == NULL)
272                 return mpdclient_handle_error(c);
274         c->volume = mpd_status_get_volume(c->status);
275         c->playing = mpd_status_get_state(c->status) == MPD_STATE_PLAY;
277         /* check if the playlist needs an update */
278         if (c->playlist.version != mpd_status_get_queue_version(c->status)) {
279                 bool retval;
281                 if (!playlist_is_empty(&c->playlist))
282                         retval = mpdclient_playlist_update_changes(c);
283                 else
284                         retval = mpdclient_playlist_update(c);
285                 if (!retval)
286                         return false;
287         }
289         /* update the current song */
290         if (!c->song || mpd_status_get_song_id(c->status) >= 0) {
291                 c->song = playlist_get_song(&c->playlist,
292                                             mpd_status_get_song_pos(c->status));
293         }
295         return true;
298 struct mpd_connection *
299 mpdclient_get_connection(struct mpdclient *c)
301         if (c->source != NULL && c->idle) {
302                 c->idle = false;
303                 mpd_glib_leave(c->source);
305                 mpdclient_schedule_enter_idle(c);
306         }
308         return c->connection;
311 static struct mpd_status *
312 mpdclient_recv_status(struct mpdclient *c)
314         assert(c->connection != NULL);
316         struct mpd_status *status = mpd_recv_status(c->connection);
317         if (status == NULL) {
318                 mpdclient_handle_error(c);
319                 return NULL;
320         }
322         if (c->status != NULL)
323                 mpd_status_free(c->status);
324         return c->status = status;
327 /****************************************************************************/
328 /*** MPD Commands  **********************************************************/
329 /****************************************************************************/
331 bool
332 mpdclient_cmd_crop(struct mpdclient *c)
334         if (!mpdclient_is_playing(c))
335                 return false;
337         int length = mpd_status_get_queue_length(c->status);
338         int current = mpd_status_get_song_pos(c->status);
339         if (current < 0 || mpd_status_get_queue_length(c->status) < 2)
340                 return true;
342         struct mpd_connection *connection = mpdclient_get_connection(c);
343         if (connection == NULL)
344                 return false;
346         mpd_command_list_begin(connection, false);
348         if (current < length - 1)
349                 mpd_send_delete_range(connection, current + 1, length);
350         if (current > 0)
351                 mpd_send_delete_range(connection, 0, current);
353         mpd_command_list_end(connection);
355         return mpdclient_finish_command(c);
358 bool
359 mpdclient_cmd_clear(struct mpdclient *c)
361         struct mpd_connection *connection = mpdclient_get_connection(c);
362         if (connection == NULL)
363                 return false;
365         /* send "clear" and "status" */
366         if (!mpd_command_list_begin(connection, false) ||
367             !mpd_send_clear(connection) ||
368             !mpd_send_status(connection) ||
369             !mpd_command_list_end(connection))
370                 return mpdclient_handle_error(c);
372         /* receive the new status, store it in the mpdclient struct */
374         struct mpd_status *status = mpdclient_recv_status(c);
375         if (status == NULL)
376                 return false;
378         if (!mpd_response_finish(connection))
379                 return mpdclient_handle_error(c);
381         /* update mpdclient.playlist */
383         if (mpd_status_get_queue_length(status) == 0) {
384                 /* after the "clear" command, the queue is really
385                    empty - this means we can clear it locally,
386                    reducing the UI latency */
387                 playlist_clear(&c->playlist);
388                 c->playlist.version = mpd_status_get_queue_version(status);
389                 c->song = NULL;
390         }
392         c->events |= MPD_IDLE_QUEUE;
393         return true;
396 bool
397 mpdclient_cmd_volume(struct mpdclient *c, gint value)
399         struct mpd_connection *connection = mpdclient_get_connection(c);
400         if (connection == NULL)
401                 return false;
403         mpd_send_set_volume(connection, value);
404         return mpdclient_finish_command(c);
407 bool
408 mpdclient_cmd_volume_up(struct mpdclient *c)
410         struct mpd_connection *connection = mpdclient_get_connection(c);
411         if (connection == NULL)
412                 return false;
414         if (c->status == NULL ||
415             mpd_status_get_volume(c->status) == -1)
416                 return true;
418         if (c->volume < 0)
419                 c->volume = mpd_status_get_volume(c->status);
421         if (c->volume >= 100)
422                 return true;
424         return mpdclient_cmd_volume(c, ++c->volume);
427 bool
428 mpdclient_cmd_volume_down(struct mpdclient *c)
430         struct mpd_connection *connection = mpdclient_get_connection(c);
431         if (connection == NULL)
432                 return false;
434         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
435                 return true;
437         if (c->volume < 0)
438                 c->volume = mpd_status_get_volume(c->status);
440         if (c->volume <= 0)
441                 return true;
443         return mpdclient_cmd_volume(c, --c->volume);
446 bool
447 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
449         struct mpd_connection *connection = mpdclient_get_connection(c);
450         if (connection == NULL)
451                 return false;
453         return mpd_send_add(connection, path_utf8)?
454                 mpdclient_finish_command(c) : false;
457 bool
458 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
460         assert(c != NULL);
461         assert(song != NULL);
463         struct mpd_connection *connection = mpdclient_get_connection(c);
464         if (connection == NULL || c->status == NULL)
465                 return false;
467         /* send the add command to mpd; at the same time, get the new
468            status (to verify the new playlist id) and the last song
469            (we hope that's the song we just added) */
471         if (!mpd_command_list_begin(connection, true) ||
472             !mpd_send_add(connection, mpd_song_get_uri(song)) ||
473             !mpd_send_status(connection) ||
474             !mpd_send_get_queue_song_pos(connection,
475                                          playlist_length(&c->playlist)) ||
476             !mpd_command_list_end(connection) ||
477             !mpd_response_next(connection))
478                 return mpdclient_handle_error(c);
480         c->events |= MPD_IDLE_QUEUE;
482         struct mpd_status *status = mpdclient_recv_status(c);
483         if (status == NULL)
484                 return false;
486         if (!mpd_response_next(connection))
487                 return mpdclient_handle_error(c);
489         struct mpd_song *new_song = mpd_recv_song(connection);
490         if (!mpd_response_finish(connection) || new_song == NULL) {
491                 if (new_song != NULL)
492                         mpd_song_free(new_song);
494                 return mpd_connection_clear_error(connection) ||
495                         mpdclient_handle_error(c);
496         }
498         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) + 1 &&
499             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
500                 /* the cheap route: match on the new playlist length
501                    and its version, we can keep our local playlist
502                    copy in sync */
503                 c->playlist.version = mpd_status_get_queue_version(status);
505                 /* the song we just received has the correct id;
506                    append it to the local playlist */
507                 playlist_append(&c->playlist, new_song);
508         }
510         mpd_song_free(new_song);
512         return true;
515 bool
516 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
518         struct mpd_connection *connection = mpdclient_get_connection(c);
520         if (connection == NULL || c->status == NULL)
521                 return false;
523         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
524                 return false;
526         const struct mpd_song *song = playlist_get(&c->playlist, idx);
528         /* send the delete 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_delete_id(connection, mpd_song_get_id(song)) ||
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) - 1 &&
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                 /* remove the song from the local playlist */
554                 playlist_remove(&c->playlist, idx);
556                 /* remove references to the song */
557                 if (c->song == song)
558                         c->song = NULL;
559         }
561         return true;
564 bool
565 mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end)
567         if (end == start + 1)
568                 /* if that's not really a range, we choose to use the
569                    safer "deleteid" version */
570                 return mpdclient_cmd_delete(c, start);
572         struct mpd_connection *connection = mpdclient_get_connection(c);
573         if (connection == NULL)
574                 return false;
576         /* send the delete command to mpd; at the same time, get the
577            new status (to verify the playlist id) */
579         if (!mpd_command_list_begin(connection, false) ||
580             !mpd_send_delete_range(connection, start, end) ||
581             !mpd_send_status(connection) ||
582             !mpd_command_list_end(connection))
583                 return mpdclient_handle_error(c);
585         c->events |= MPD_IDLE_QUEUE;
587         struct mpd_status *status = mpdclient_recv_status(c);
588         if (status == NULL)
589                 return false;
591         if (!mpd_response_finish(connection))
592                 return mpdclient_handle_error(c);
594         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) - (end - start) &&
595             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
596                 /* the cheap route: match on the new playlist length
597                    and its version, we can keep our local playlist
598                    copy in sync */
599                 c->playlist.version = mpd_status_get_queue_version(status);
601                 /* remove the song from the local playlist */
602                 while (end > start) {
603                         --end;
605                         /* remove references to the song */
606                         if (c->song == playlist_get(&c->playlist, end))
607                                 c->song = NULL;
609                         playlist_remove(&c->playlist, end);
610                 }
611         }
613         return true;
616 bool
617 mpdclient_cmd_move(struct mpdclient *c, unsigned dest_pos, unsigned src_pos)
619         if (dest_pos == src_pos)
620                 return true;
622         struct mpd_connection *connection = mpdclient_get_connection(c);
623         if (connection == NULL)
624                 return false;
626         /* send the "move" command to MPD; at the same time, get the
627            new status (to verify the playlist id) */
629         if (!mpd_command_list_begin(connection, false) ||
630             !mpd_send_move(connection, src_pos, dest_pos) ||
631             !mpd_send_status(connection) ||
632             !mpd_command_list_end(connection))
633                 return mpdclient_handle_error(c);
635         c->events |= MPD_IDLE_QUEUE;
637         struct mpd_status *status = mpdclient_recv_status(c);
638         if (status == NULL)
639                 return false;
641         if (!mpd_response_finish(connection))
642                 return mpdclient_handle_error(c);
644         if (mpd_status_get_queue_length(status) == playlist_length(&c->playlist) &&
645             mpd_status_get_queue_version(status) == c->playlist.version + 1) {
646                 /* the cheap route: match on the new playlist length
647                    and its version, we can keep our local playlist
648                    copy in sync */
649                 c->playlist.version = mpd_status_get_queue_version(status);
651                 /* swap songs in the local playlist */
652                 playlist_move(&c->playlist, dest_pos, src_pos);
653         }
655         return true;
658 /* The client-to-client protocol (MPD 0.17.0) */
660 bool
661 mpdclient_cmd_subscribe(struct mpdclient *c, const char *channel)
663         struct mpd_connection *connection = mpdclient_get_connection(c);
665         if (connection == NULL)
666                 return false;
668         if (!mpd_send_subscribe(connection, channel))
669                 return mpdclient_handle_error(c);
671         return mpdclient_finish_command(c);
674 bool
675 mpdclient_cmd_unsubscribe(struct mpdclient *c, const char *channel)
677         struct mpd_connection *connection = mpdclient_get_connection(c);
678         if (connection == NULL)
679                 return false;
681         if (!mpd_send_unsubscribe(connection, channel))
682                 return mpdclient_handle_error(c);
684         return mpdclient_finish_command(c);
687 bool
688 mpdclient_cmd_send_message(struct mpdclient *c, const char *channel,
689                            const char *text)
691         struct mpd_connection *connection = mpdclient_get_connection(c);
692         if (connection == NULL)
693                 return false;
695         if (!mpd_send_send_message(connection, channel, text))
696                 return mpdclient_handle_error(c);
698         return mpdclient_finish_command(c);
701 bool
702 mpdclient_send_read_messages(struct mpdclient *c)
704         struct mpd_connection *connection = mpdclient_get_connection(c);
705         if (connection == NULL)
706                 return false;
708         return mpd_send_read_messages(connection)?
709                 true : mpdclient_handle_error(c);
712 struct mpd_message *
713 mpdclient_recv_message(struct mpdclient *c)
715         struct mpd_connection *connection = mpdclient_get_connection(c);
716         if (connection == NULL)
717                 return false;
719         struct mpd_message *message = mpd_recv_message(connection);
720         if (message == NULL &&
721             mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS)
722                 mpdclient_handle_error(c);
724         return message;
727 /****************************************************************************/
728 /*** Playlist management functions ******************************************/
729 /****************************************************************************/
731 /* update playlist */
732 bool
733 mpdclient_playlist_update(struct mpdclient *c)
735         struct mpd_connection *connection = mpdclient_get_connection(c);
736         if (connection == NULL)
737                 return false;
739         playlist_clear(&c->playlist);
741         mpd_send_list_queue_meta(connection);
743         struct mpd_entity *entity;
744         while ((entity = mpd_recv_entity(connection))) {
745                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
746                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
748                 mpd_entity_free(entity);
749         }
751         c->playlist.version = mpd_status_get_queue_version(c->status);
752         c->song = NULL;
754         return mpdclient_finish_command(c);
757 /* update playlist (plchanges) */
758 bool
759 mpdclient_playlist_update_changes(struct mpdclient *c)
761         struct mpd_connection *connection = mpdclient_get_connection(c);
763         if (connection == NULL)
764                 return false;
766         mpd_send_queue_changes_meta(connection, c->playlist.version);
768         struct mpd_song *song;
769         while ((song = mpd_recv_song(connection)) != NULL) {
770                 int pos = mpd_song_get_pos(song);
772                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
773                         /* update song */
774                         playlist_replace(&c->playlist, pos, song);
775                 } else {
776                         /* add a new song */
777                         playlist_append(&c->playlist, song);
778                 }
780                 mpd_song_free(song);
781         }
783         /* remove trailing songs */
785         unsigned length = mpd_status_get_queue_length(c->status);
786         while (length < c->playlist.list->len) {
787                 guint pos = c->playlist.list->len - 1;
789                 /* Remove the last playlist entry */
790                 playlist_remove(&c->playlist, pos);
791         }
793         c->song = NULL;
794         c->playlist.version = mpd_status_get_queue_version(c->status);
796         return mpdclient_finish_command(c);
800 /****************************************************************************/
801 /*** Filelist functions *****************************************************/
802 /****************************************************************************/
804 bool
805 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
807         struct mpd_connection *connection = mpdclient_get_connection(c);
808         if (connection == NULL)
809                 return false;
811         if (filelist_is_empty(fl))
812                 return true;
814         mpd_command_list_begin(connection, false);
816         for (unsigned i = 0; i < filelist_length(fl); ++i) {
817                 struct filelist_entry *entry = filelist_get(fl, i);
818                 struct mpd_entity *entity  = entry->entity;
820                 if (entity != NULL &&
821                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
822                         const struct mpd_song *song =
823                                 mpd_entity_get_song(entity);
825                         mpd_send_add(connection, mpd_song_get_uri(song));
826                 }
827         }
829         mpd_command_list_end(connection);
830         return mpdclient_finish_command(c);