Code

mpdclient: replaced error_callback with imported function
[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"
28 #include <mpd/client.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <time.h>
33 #include <string.h>
35 #undef  ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
36 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
37 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
39 #define BUFSIZE 1024
41 static bool
42 MPD_ERROR(const struct mpdclient *client)
43 {
44         return client->connection == NULL ||
45                 mpd_connection_get_error(client->connection) != MPD_ERROR_SUCCESS;
46 }
48 /* filelist sorting functions */
49 static gint
50 compare_filelistentry(gconstpointer filelist_entry1,
51                           gconstpointer filelist_entry2)
52 {
53         const struct mpd_entity *e1, *e2;
54         int n = 0;
56         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
57         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
59         if (e1 != NULL && e2 != NULL &&
60             mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
61                 switch (mpd_entity_get_type(e1)) {
62                 case MPD_ENTITY_TYPE_UNKNOWN:
63                         break;
64                 case MPD_ENTITY_TYPE_DIRECTORY:
65                         n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
66                                            mpd_directory_get_path(mpd_entity_get_directory(e2)));
67                         break;
68                 case MPD_ENTITY_TYPE_SONG:
69                         break;
70                 case MPD_ENTITY_TYPE_PLAYLIST:
71                         n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
72                                            mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
73                 }
74         }
75         return n;
76 }
78 /* sort by list-format */
79 gint
80 compare_filelistentry_format(gconstpointer filelist_entry1,
81                              gconstpointer filelist_entry2)
82 {
83         const struct mpd_entity *e1, *e2;
84         char key1[BUFSIZE], key2[BUFSIZE];
85         int n = 0;
87         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
88         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
90         if (e1 && e2 &&
91             mpd_entity_get_type(e1) == MPD_ENTITY_TYPE_SONG &&
92             mpd_entity_get_type(e2) == MPD_ENTITY_TYPE_SONG) {
93                 strfsong(key1, BUFSIZE, options.list_format, mpd_entity_get_song(e1));
94                 strfsong(key2, BUFSIZE, options.list_format, mpd_entity_get_song(e2));
95                 n = strcmp(key1,key2);
96         }
98         return n;
99 }
102 /****************************************************************************/
103 /*** mpdclient functions ****************************************************/
104 /****************************************************************************/
106 gint
107 mpdclient_handle_error(struct mpdclient *c)
109         enum mpd_error error = mpd_connection_get_error(c->connection);
111         assert(error != MPD_ERROR_SUCCESS);
113         if (error == MPD_ERROR_SERVER &&
114             mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_PERMISSION &&
115             screen_auth(c))
116                 return 0;
118         if (error == MPD_ERROR_SERVER)
119                 error = error | (mpd_connection_get_server_error(c->connection) << 8);
121         mpdclient_ui_error(mpd_connection_get_error_message(c->connection));
123         if (!mpd_connection_clear_error(c->connection))
124                 mpdclient_disconnect(c);
126         return error;
129 static gint
130 mpdclient_finish_command(struct mpdclient *c)
132         return mpd_response_finish(c->connection)
133                 ? 0 : mpdclient_handle_error(c);
136 struct mpdclient *
137 mpdclient_new(void)
139         struct mpdclient *c;
141         c = g_new0(struct mpdclient, 1);
142         playlist_init(&c->playlist);
143         c->volume = -1;
145         return c;
148 void
149 mpdclient_free(struct mpdclient *c)
151         mpdclient_disconnect(c);
153         mpdclient_playlist_free(&c->playlist);
155         g_list_free(c->playlist_callbacks);
156         g_list_free(c->browse_callbacks);
157         g_free(c);
160 void
161 mpdclient_disconnect(struct mpdclient *c)
163         if (c->connection)
164                 mpd_connection_free(c->connection);
165         c->connection = NULL;
167         if (c->status)
168                 mpd_status_free(c->status);
169         c->status = NULL;
171         playlist_clear(&c->playlist);
173         if (c->song)
174                 c->song = NULL;
177 bool
178 mpdclient_connect(struct mpdclient *c,
179                   const gchar *host,
180                   gint port,
181                   gfloat _timeout,
182                   const gchar *password)
184         /* close any open connection */
185         if( c->connection )
186                 mpdclient_disconnect(c);
188         /* connect to MPD */
189         c->connection = mpd_connection_new(host, port, _timeout * 1000);
190         if (c->connection == NULL)
191                 g_error("Out of memory");
193         if (mpd_connection_get_error(c->connection) != MPD_ERROR_SUCCESS) {
194                 mpdclient_handle_error(c);
195                 mpdclient_disconnect(c);
196                 return false;
197         }
199         /* send password */
200         if (password != NULL && !mpd_run_password(c->connection, password)) {
201                 mpdclient_handle_error(c);
202                 mpdclient_disconnect(c);
203                 return false;
204         }
206         return true;
209 bool
210 mpdclient_update(struct mpdclient *c)
212         bool retval;
214         c->volume = -1;
216         if (MPD_ERROR(c))
217                 return false;
219         /* free the old status */
220         if (c->status)
221                 mpd_status_free(c->status);
223         /* retrieve new status */
224         c->status = mpd_run_status(c->connection);
225         if (c->status == NULL)
226                 return mpdclient_handle_error(c) == 0;
228         if (c->update_id > 0 &&
229             c->update_id != mpd_status_get_update_id(c->status))
230                 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
232         c->update_id = mpd_status_get_update_id(c->status);
233         c->volume = mpd_status_get_volume(c->status);
235         /* check if the playlist needs an update */
236         if (c->playlist.id != mpd_status_get_queue_version(c->status)) {
237                 if (!playlist_is_empty(&c->playlist))
238                         retval = mpdclient_playlist_update_changes(c);
239                 else
240                         retval = mpdclient_playlist_update(c);
241         } else
242                 retval = true;
244         /* update the current song */
245         if (!c->song || mpd_status_get_song_id(c->status)) {
246                 c->song = playlist_get_song(&c->playlist,
247                                             mpd_status_get_song_pos(c->status));
248         }
250         return retval;
254 /****************************************************************************/
255 /*** MPD Commands  **********************************************************/
256 /****************************************************************************/
258 gint
259 mpdclient_cmd_play(struct mpdclient *c, gint idx)
261         const struct mpd_song *song = playlist_get_song(&c->playlist, idx);
263         if (MPD_ERROR(c))
264                 return -1;
266         if (song)
267                 mpd_send_play_id(c->connection, mpd_song_get_id(song));
268         else
269                 mpd_send_play(c->connection);
271         return mpdclient_finish_command(c);
274 gint
275 mpdclient_cmd_crop(struct mpdclient *c)
277         struct mpd_status *status;
278         bool playing;
279         int length, current;
281         if (MPD_ERROR(c))
282                 return -1;
284         status = mpd_run_status(c->connection);
285         if (status == NULL)
286                 return mpdclient_handle_error(c);
288         playing = mpd_status_get_state(status) == MPD_STATE_PLAY ||
289                 mpd_status_get_state(status) == MPD_STATE_PAUSE;
290         length = mpd_status_get_queue_length(status);
291         current = mpd_status_get_song_pos(status);
293         mpd_status_free(status);
295         if (!playing || length < 2)
296                 return 0;
298         mpd_command_list_begin(c->connection, false);
300         while (--length >= 0)
301                 if (length != current)
302                         mpd_send_delete(c->connection, length);
304         mpd_command_list_end(c->connection);
306         return mpdclient_finish_command(c);
309 gint
310 mpdclient_cmd_shuffle_range(struct mpdclient *c, guint start, guint end)
312         mpd_send_shuffle_range(c->connection, start, end);
313         return mpdclient_finish_command(c);
316 gint
317 mpdclient_cmd_clear(struct mpdclient *c)
319         gint retval = 0;
321         if (MPD_ERROR(c))
322                 return -1;
324         mpd_send_clear(c->connection);
325         retval = mpdclient_finish_command(c);
326         /* call playlist updated callback */
327         mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
328         return retval;
331 gint
332 mpdclient_cmd_volume(struct mpdclient *c, gint value)
334         if (MPD_ERROR(c))
335                 return -1;
337         mpd_send_set_volume(c->connection, value);
338         return mpdclient_finish_command(c);
341 gint mpdclient_cmd_volume_up(struct mpdclient *c)
343         if (MPD_ERROR(c))
344                 return -1;
346         if (c->status == NULL ||
347             mpd_status_get_volume(c->status) == -1)
348                 return 0;
350         if (c->volume < 0)
351                 c->volume = mpd_status_get_volume(c->status);
353         if (c->volume >= 100)
354                 return 0;
356         return mpdclient_cmd_volume(c, ++c->volume);
359 gint mpdclient_cmd_volume_down(struct mpdclient *c)
361         if (MPD_ERROR(c))
362                 return -1;
364         if (c->status == NULL || mpd_status_get_volume(c->status) < 0)
365                 return 0;
367         if (c->volume < 0)
368                 c->volume = mpd_status_get_volume(c->status);
370         if (c->volume <= 0)
371                 return 0;
373         return mpdclient_cmd_volume(c, --c->volume);
376 gint
377 mpdclient_cmd_add_path(struct mpdclient *c, const gchar *path_utf8)
379         if (MPD_ERROR(c))
380                 return -1;
382         mpd_send_add(c->connection, path_utf8);
383         return mpdclient_finish_command(c);
386 gint
387 mpdclient_cmd_add(struct mpdclient *c, const struct mpd_song *song)
389         gint retval = 0;
391         if (MPD_ERROR(c))
392                 return -1;
394         if (song == NULL)
395                 return -1;
397         /* send the add command to mpd */
398         mpd_send_add(c->connection, mpd_song_get_uri(song));
399         if( (retval=mpdclient_finish_command(c)) )
400                 return retval;
402 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
403         /* add the song to playlist */
404         playlist_append(&c->playlist, song);
406         /* increment the playlist id, so we don't retrieve a new playlist */
407         c->playlist.id++;
409         /* call playlist updated callback */
410         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
411 #endif
413         return 0;
416 gint
417 mpdclient_cmd_delete(struct mpdclient *c, gint idx)
419         gint retval = 0;
420         struct mpd_song *song;
422         if (MPD_ERROR(c))
423                 return -1;
425         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
426                 return -1;
428         song = playlist_get(&c->playlist, idx);
430         /* send the delete command to mpd */
431         mpd_send_delete_id(c->connection, mpd_song_get_id(song));
432         if( (retval=mpdclient_finish_command(c)) )
433                 return retval;
435 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
436         /* increment the playlist id, so we don't retrieve a new playlist */
437         c->playlist.id++;
439         /* remove the song from the playlist */
440         playlist_remove_reuse(&c->playlist, idx);
442         /* call playlist updated callback */
443         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
445         /* remove references to the song */
446         if (c->song == song)
447                 c->song = NULL;
449         mpd_song_free(song);
450 #endif
452         return 0;
455 gint
456 mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index)
458         gint n;
459         struct mpd_song *song1, *song2;
461         if (MPD_ERROR(c))
462                 return -1;
464         if (old_index == new_index || new_index < 0 ||
465             (guint)new_index >= c->playlist.list->len)
466                 return -1;
468         song1 = playlist_get(&c->playlist, old_index);
469         song2 = playlist_get(&c->playlist, new_index);
471         /* send the move command to mpd */
472         mpd_send_swap_id(c->connection,
473                          mpd_song_get_id(song1), mpd_song_get_id(song2));
474         if( (n=mpdclient_finish_command(c)) )
475                 return n;
477 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
478         /* update the playlist */
479         playlist_swap(&c->playlist, old_index, new_index);
481         /* increment the playlist id, so we don't retrieve a new playlist */
482         c->playlist.id++;
483 #endif
485         /* call playlist updated callback */
486         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
488         return 0;
491 gint
492 mpdclient_cmd_save_playlist(struct mpdclient *c, const gchar *filename_utf8)
494         gint retval = 0;
496         if (MPD_ERROR(c))
497                 return -1;
499         mpd_send_save(c->connection, filename_utf8);
500         if ((retval = mpdclient_finish_command(c)) == 0)
501                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
502         return retval;
505 gint
506 mpdclient_cmd_load_playlist(struct mpdclient *c, const gchar *filename_utf8)
508         if (MPD_ERROR(c))
509                 return -1;
511         mpd_send_load(c->connection, filename_utf8);
512         return mpdclient_finish_command(c);
515 gint
516 mpdclient_cmd_delete_playlist(struct mpdclient *c, const gchar *filename_utf8)
518         gint retval = 0;
520         if (MPD_ERROR(c))
521                 return -1;
523         mpd_send_rm(c->connection, filename_utf8);
524         if ((retval = mpdclient_finish_command(c)) == 0)
525                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
526         return retval;
530 /****************************************************************************/
531 /*** Callback management functions ******************************************/
532 /****************************************************************************/
534 static void
535 do_list_callbacks(struct mpdclient *c, GList *list, gint event, gpointer data)
537         while (list) {
538                 mpdc_list_cb_t fn = list->data;
540                 fn(c, event, data);
541                 list = list->next;
542         }
545 void
546 mpdclient_playlist_callback(struct mpdclient *c, int event, gpointer data)
548         do_list_callbacks(c, c->playlist_callbacks, event, data);
551 void
552 mpdclient_install_playlist_callback(struct mpdclient *c,mpdc_list_cb_t cb)
554         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
557 void
558 mpdclient_remove_playlist_callback(struct mpdclient *c, mpdc_list_cb_t cb)
560         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
563 void
564 mpdclient_browse_callback(struct mpdclient *c, int event, gpointer data)
566         do_list_callbacks(c, c->browse_callbacks, event, data);
570 void
571 mpdclient_install_browse_callback(struct mpdclient *c,mpdc_list_cb_t cb)
573         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
576 void
577 mpdclient_remove_browse_callback(struct mpdclient *c, mpdc_list_cb_t cb)
579         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
583 /****************************************************************************/
584 /*** Playlist management functions ******************************************/
585 /****************************************************************************/
587 /* update playlist */
588 bool
589 mpdclient_playlist_update(struct mpdclient *c)
591         struct mpd_entity *entity;
593         if (MPD_ERROR(c))
594                 return false;
596         playlist_clear(&c->playlist);
598         mpd_send_list_queue_meta(c->connection);
599         while ((entity = mpd_recv_entity(c->connection))) {
600                 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
601                         playlist_append(&c->playlist, mpd_entity_get_song(entity));
603                 mpd_entity_free(entity);
604         }
606         c->playlist.id = mpd_status_get_queue_version(c->status);
607         c->song = NULL;
609         /* call playlist updated callbacks */
610         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
612         return mpdclient_finish_command(c) == 0;
615 /* update playlist (plchanges) */
616 bool
617 mpdclient_playlist_update_changes(struct mpdclient *c)
619         struct mpd_song *song;
620         guint length;
622         if (MPD_ERROR(c))
623                 return false;
625         mpd_send_queue_changes_meta(c->connection, c->playlist.id);
627         while ((song = mpd_recv_song(c->connection)) != NULL) {
628                 int pos = mpd_song_get_pos(song);
630                 if (pos >= 0 && (guint)pos < c->playlist.list->len) {
631                         /* update song */
632                         playlist_replace(&c->playlist, pos, song);
633                 } else {
634                         /* add a new song */
635                         playlist_append(&c->playlist, song);
636                 }
638                 mpd_song_free(song);
639         }
641         /* remove trailing songs */
643         length = mpd_status_get_queue_length(c->status);
644         while (length < c->playlist.list->len) {
645                 guint pos = c->playlist.list->len - 1;
647                 /* Remove the last playlist entry */
648                 playlist_remove(&c->playlist, pos);
649         }
651         c->song = NULL;
652         c->playlist.id = mpd_status_get_queue_version(c->status);
654         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
656         return mpdclient_finish_command(c) == 0;
660 /****************************************************************************/
661 /*** Filelist functions *****************************************************/
662 /****************************************************************************/
664 struct filelist *
665 mpdclient_filelist_get(struct mpdclient *c, const gchar *path)
667         struct filelist *filelist;
668         struct mpd_entity *entity;
670         if (MPD_ERROR(c))
671                 return NULL;
673         mpd_send_list_meta(c->connection, path);
674         filelist = filelist_new();
676         while ((entity = mpd_recv_entity(c->connection)) != NULL)
677                 filelist_append(filelist, entity);
679         if (mpdclient_finish_command(c)) {
680                 filelist_free(filelist);
681                 return NULL;
682         }
684         filelist_sort_dir_play(filelist, compare_filelistentry);
686         return filelist;
689 static struct filelist *
690 mpdclient_recv_filelist_response(struct mpdclient *c)
692         struct filelist *filelist;
693         struct mpd_entity *entity;
695         filelist = filelist_new();
697         while ((entity = mpd_recv_entity(c->connection)) != NULL)
698                 filelist_append(filelist, entity);
700         if (mpdclient_finish_command(c)) {
701                 filelist_free(filelist);
702                 return NULL;
703         }
705         return filelist;
708 struct filelist *
709 mpdclient_filelist_search(struct mpdclient *c,
710                           int exact_match,
711                           enum mpd_tag_type tag,
712                           gchar *filter_utf8)
714         if (MPD_ERROR(c))
715                 return NULL;
717         mpd_search_db_songs(c->connection, exact_match);
718         mpd_search_add_tag_constraint(c->connection, MPD_OPERATOR_DEFAULT,
719                                       tag, filter_utf8);
720         mpd_search_commit(c->connection);
722         return mpdclient_recv_filelist_response(c);
725 int
726 mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl)
728         guint i;
730         if (MPD_ERROR(c))
731                 return -1;
733         if (filelist_is_empty(fl))
734                 return 0;
736         mpd_command_list_begin(c->connection, false);
738         for (i = 0; i < filelist_length(fl); ++i) {
739                 struct filelist_entry *entry = filelist_get(fl, i);
740                 struct mpd_entity *entity  = entry->entity;
742                 if (entity != NULL &&
743                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
744                         const struct mpd_song *song =
745                                 mpd_entity_get_song(entity);
746                         const char *uri = mpd_song_get_uri(song);
748                         if (uri != NULL)
749                                 mpd_send_add(c->connection, uri);
750                 }
751         }
753         mpd_command_list_end(c->connection);
754         return mpdclient_finish_command(c);
757 GList *
758 mpdclient_get_artists(struct mpdclient *c)
760         GList *list = NULL;
761         struct mpd_pair *pair;
763         if (MPD_ERROR(c))
764                return NULL;
766         mpd_search_db_tags(c->connection, MPD_TAG_ARTIST);
767         mpd_search_commit(c->connection);
769         while ((pair = mpd_recv_pair_tag(c->connection,
770                                          MPD_TAG_ARTIST)) != NULL) {
771                 list = g_list_append(list, g_strdup(pair->value));
772                 mpd_return_pair(c->connection, pair);
773         }
775         if (mpdclient_finish_command(c))
776                 return string_list_free(list);
778         return list;
781 GList *
782 mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8)
784         GList *list = NULL;
785         struct mpd_pair *pair;
787         if (MPD_ERROR(c))
788                return NULL;
790         mpd_search_db_tags(c->connection, MPD_TAG_ALBUM);
791         if (artist_utf8 != NULL)
792                 mpd_search_add_tag_constraint(c->connection,
793                                               MPD_OPERATOR_DEFAULT,
794                                               MPD_TAG_ARTIST, artist_utf8);
795         mpd_search_commit(c->connection);
797         while ((pair = mpd_recv_pair_tag(c->connection,
798                                          MPD_TAG_ALBUM)) != NULL) {
799                 list = g_list_append(list, g_strdup(pair->value));
800                 mpd_return_pair(c->connection, pair);
801         }
803         if (mpdclient_finish_command(c))
804                 return string_list_free(list);
806         return list;