Code

support the new 'single' state feature
[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 "screen_utils.h"
22 #include "config.h"
23 #include "options.h"
24 #include "strfsong.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <time.h>
29 #include <string.h>
31 #undef  ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
32 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
33 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
34 #define ENABLE_SONG_ID
35 #define ENABLE_PLCHANGES
37 #define BUFSIZE 1024
39 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
41 /* from utils.c */
42 extern GList *string_list_free(GList *string_list);
45 /* filelist sorting functions */
46 static gint
47 compare_filelistentry(gconstpointer filelist_entry1,
48                           gconstpointer filelist_entry2)
49 {
50         const mpd_InfoEntity *e1, *e2;
51         int n = 0;
53         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
54         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
56         if (e1 && e2 && e1->type == e2->type) {
57                 switch (e1->type) {
58                 case MPD_INFO_ENTITY_TYPE_DIRECTORY:
59                         n = g_utf8_collate(e1->info.directory->path,
60                                         e2->info.directory->path);
61                         break;
62                 case MPD_INFO_ENTITY_TYPE_SONG:
63                         break;
64                 case MPD_INFO_ENTITY_TYPE_PLAYLISTFILE:
65                         n = g_utf8_collate(e1->info.playlistFile->path,
66                                         e2->info.playlistFile->path);
67                 }
68         }
69         return n;
70 }
72 /* sort by list-format */
73 gint
74 compare_filelistentry_format(gconstpointer filelist_entry1,
75                              gconstpointer filelist_entry2)
76 {
77         const mpd_InfoEntity *e1, *e2;
78         char key1[BUFSIZE], key2[BUFSIZE];
79         int n = 0;
81         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
82         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
84         if (e1 && e2 &&
85             e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
86             e2->type == MPD_INFO_ENTITY_TYPE_SONG) {
87                 strfsong(key1, BUFSIZE, options.list_format, e1->info.song);
88                 strfsong(key2, BUFSIZE, options.list_format, e2->info.song);
89                 n = strcmp(key1,key2);
90         }
92         return n;
93 }
96 /* Error callbacks */
97 static gint
98 error_cb(mpdclient_t *c, gint error, gchar *msg)
99 {
100         GList *list = c->error_callbacks;
102         if (list == NULL)
103                 fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
105         while (list) {
106                 mpdc_error_cb_t cb = list->data;
107                 if (cb)
108                         cb(c, error, msg);
109                 list = list->next;
110         }
112         mpd_clearError(c->connection);
113         return error;
117 /****************************************************************************/
118 /*** mpdclient functions ****************************************************/
119 /****************************************************************************/
121 gint
122 mpdclient_finish_command(mpdclient_t *c)
124         mpd_finishCommand(c->connection);
126         if (c->connection->error) {
127                 gint error = c->connection->error;
129                 if (error == MPD_ERROR_ACK &&
130                     c->connection->errorCode == MPD_ACK_ERROR_PERMISSION &&
131                     screen_auth(c) == 0)
132                         return 0;
134                 if (error == MPD_ERROR_ACK)
135                         error = error | (c->connection->errorCode << 8);
137                 error_cb(c, error, c->connection->errorStr);
138                 return error;
139         }
141         return 0;
144 mpdclient_t *
145 mpdclient_new(void)
147         mpdclient_t *c;
149         c = g_malloc0(sizeof(mpdclient_t));
150         playlist_init(&c->playlist);
152         return c;
155 void
156 mpdclient_free(mpdclient_t *c)
158         mpdclient_disconnect(c);
160         mpdclient_playlist_free(&c->playlist);
162         g_list_free(c->error_callbacks);
163         g_list_free(c->playlist_callbacks);
164         g_list_free(c->browse_callbacks);
165         g_free(c);
168 gint
169 mpdclient_disconnect(mpdclient_t *c)
171         if (c->connection)
172                 mpd_closeConnection(c->connection);
173         c->connection = NULL;
175         if (c->status)
176                 mpd_freeStatus(c->status);
177         c->status = NULL;
179         playlist_clear(&c->playlist);
181         if (c->song)
182                 c->song = NULL;
184         return 0;
187 gint
188 mpdclient_connect(mpdclient_t *c,
189                   gchar *host,
190                   gint port,
191                   gfloat _timeout,
192                   gchar *password)
194         gint retval = 0;
196         /* close any open connection */
197         if( c->connection )
198                 mpdclient_disconnect(c);
200         /* connect to MPD */
201         c->connection = mpd_newConnection(host, port, _timeout);
202         if( c->connection->error )
203                 return error_cb(c, c->connection->error,
204                                 c->connection->errorStr);
206         /* send password */
207         if( password ) {
208                 mpd_sendPasswordCommand(c->connection, password);
209                 retval = mpdclient_finish_command(c);
210         }
211         c->need_update = TRUE;
213         return retval;
216 gint
217 mpdclient_update(mpdclient_t *c)
219         gint retval = 0;
221         if (MPD_ERROR(c))
222                 return -1;
224         /* free the old status */
225         if (c->status)
226                 mpd_freeStatus(c->status);
228         /* retrieve new status */
229         mpd_sendStatusCommand(c->connection);
230         c->status = mpd_getStatus(c->connection);
231         if ((retval=mpdclient_finish_command(c)))
232                 return retval;
234         /* check if the playlist needs an update */
235         if (c->playlist.id != c->status->playlist) {
236                 if (playlist_is_empty(&c->playlist))
237                         retval = mpdclient_playlist_update_changes(c);
238                 else
239                         retval = mpdclient_playlist_update(c);
240         }
242         /* update the current song */
243         if (!c->song || c->status->songid != c->song->id) {
244                 c->song = playlist_get_song(c, c->status->song);
245         }
247         c->need_update = FALSE;
249         return retval;
253 /****************************************************************************/
254 /*** MPD Commands  **********************************************************/
255 /****************************************************************************/
257 gint
258 mpdclient_cmd_play(mpdclient_t *c, gint idx)
260 #ifdef ENABLE_SONG_ID
261         struct mpd_song *song = playlist_get_song(c, idx);
263         if (song)
264                 mpd_sendPlayIdCommand(c->connection, song->id);
265         else
266                 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
267 #else
268         mpd_sendPlayCommand(c->connection, idx);
269 #endif
270         c->need_update = TRUE;
271         return mpdclient_finish_command(c);
274 gint
275 mpdclient_cmd_pause(mpdclient_t *c, gint value)
277         mpd_sendPauseCommand(c->connection, value);
278         return mpdclient_finish_command(c);
281 gint
282 mpdclient_cmd_crop(mpdclient_t *c)
284         mpd_Status *status;
285         int length;
287         mpd_sendStatusCommand(c->connection);
288         status = mpd_getStatus(c->connection);
289         length = status->playlistLength - 1;
291         if (length <= 0) {
292                 mpd_freeStatus(status);
293         } else if (status->state == 3 || status->state == 2) {
294                 /* If playing or paused */
296                 mpd_sendCommandListBegin( c->connection );
298                 while (length >= 0) {
299                         if (length != status->song)
300                                 mpd_sendDeleteCommand(c->connection, length);
302                         length--;
303                 }
305                 mpd_sendCommandListEnd(c->connection);
306                 mpd_freeStatus(status);
307         } else {
308                 mpd_freeStatus(status);
309         }
311         return mpdclient_finish_command(c);
314 gint
315 mpdclient_cmd_stop(mpdclient_t *c)
317         mpd_sendStopCommand(c->connection);
318         return mpdclient_finish_command(c);
321 gint
322 mpdclient_cmd_next(mpdclient_t *c)
324         mpd_sendNextCommand(c->connection);
325         c->need_update = TRUE;
326         return mpdclient_finish_command(c);
329 gint
330 mpdclient_cmd_prev(mpdclient_t *c)
332         mpd_sendPrevCommand(c->connection);
333         c->need_update = TRUE;
334         return mpdclient_finish_command(c);
337 gint
338 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
340         mpd_sendSeekIdCommand(c->connection, id, pos);
341         return mpdclient_finish_command(c);
344 gint
345 mpdclient_cmd_shuffle(mpdclient_t *c)
347         mpd_sendShuffleCommand(c->connection);
348         c->need_update = TRUE;
349         return mpdclient_finish_command(c);
352 gint
353 mpdclient_cmd_shuffle_range(mpdclient_t *c, guint start, guint end)
355         mpd_sendShuffleRangeCommand(c->connection, start, end);
356         c->need_update = TRUE;
357         return mpdclient_finish_command(c);
360 gint
361 mpdclient_cmd_clear(mpdclient_t *c)
363         gint retval = 0;
365         mpd_sendClearCommand(c->connection);
366         retval = mpdclient_finish_command(c);
367         /* call playlist updated callback */
368         mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
369         c->need_update = TRUE;
370         return retval;
373 gint
374 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
376         mpd_sendRepeatCommand(c->connection, value);
377         return mpdclient_finish_command(c);
380 gint
381 mpdclient_cmd_random(mpdclient_t *c, gint value)
383         mpd_sendRandomCommand(c->connection, value);
384         return mpdclient_finish_command(c);
387 gint
388 mpdclient_cmd_single(mpdclient_t *c, gint value)
390         mpd_sendSingleCommand(c->connection, value);
391         return mpdclient_finish_command(c);
394 gint
395 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
397         mpd_sendCrossfadeCommand(c->connection, value);
398         return mpdclient_finish_command(c);
401 gint
402 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
404         mpd_sendUpdateCommand(c->connection, path ? path : "");
405         return mpdclient_finish_command(c);
408 gint
409 mpdclient_cmd_volume(mpdclient_t *c, gint value)
411         mpd_sendSetvolCommand(c->connection, value);
412         return mpdclient_finish_command(c);
415 gint
416 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
418         mpd_sendAddCommand(c->connection, path_utf8);
419         return mpdclient_finish_command(c);
422 gint
423 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
425         gint retval = 0;
427         if( !song || !song->file )
428                 return -1;
430         /* send the add command to mpd */
431         mpd_sendAddCommand(c->connection, song->file);
432         if( (retval=mpdclient_finish_command(c)) )
433                 return retval;
435 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
436         /* add the song to playlist */
437         playlist_append(&c->playlist, song);
439         /* increment the playlist id, so we don't retrieve a new playlist */
440         c->playlist.id++;
442         /* call playlist updated callback */
443         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
444 #else
445         c->need_update = TRUE;
446 #endif
448         return 0;
451 gint
452 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
454         gint retval = 0;
455         struct mpd_song *song;
457         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
458                 return -1;
460         song = playlist_get(&c->playlist, idx);
462         /* send the delete command to mpd */
463 #ifdef ENABLE_SONG_ID
464         mpd_sendDeleteIdCommand(c->connection, song->id);
465 #else
466         mpd_sendDeleteCommand(c->connection, idx);
467 #endif
468         if( (retval=mpdclient_finish_command(c)) )
469                 return retval;
471 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
472         /* increment the playlist id, so we don't retrieve a new playlist */
473         c->playlist.id++;
475         /* remove the song from the playlist */
476         playlist_remove_reuse(&c->playlist, idx);
478         /* call playlist updated callback */
479         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
481         /* remove references to the song */
482         if (c->song == song) {
483                 c->song = NULL;
484                 c->need_update = TRUE;
485         }
487         mpd_freeSong(song);
489 #else
490         c->need_update = TRUE;
491 #endif
493         return 0;
496 gint
497 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
499         gint n;
500         struct mpd_song *song1, *song2;
502         if (old_index == new_index || new_index < 0 ||
503             (guint)new_index >= c->playlist.list->len)
504                 return -1;
506         song1 = playlist_get(&c->playlist, old_index);
507         song2 = playlist_get(&c->playlist, new_index);
509         /* send the move command to mpd */
510 #ifdef ENABLE_SONG_ID
511         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
512 #else
513         mpd_sendMoveCommand(c->connection, old_index, new_index);
514 #endif
515         if( (n=mpdclient_finish_command(c)) )
516                 return n;
518 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
519         /* update the playlist */
520         playlist_swap(&c->playlist, old_index, new_index);
522         /* increment the playlist id, so we don't retrieve a new playlist */
523         c->playlist.id++;
525 #else
526         c->need_update = TRUE;
527 #endif
529         /* call playlist updated callback */
530         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
532         return 0;
535 gint
536 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
538         gint retval = 0;
540         mpd_sendSaveCommand(c->connection, filename_utf8);
541         if ((retval = mpdclient_finish_command(c)) == 0)
542                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
543         return retval;
546 gint
547 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
549         mpd_sendLoadCommand(c->connection, filename_utf8);
550         c->need_update = TRUE;
551         return mpdclient_finish_command(c);
554 gint
555 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
557         gint retval = 0;
559         mpd_sendRmCommand(c->connection, filename_utf8);
560         if ((retval = mpdclient_finish_command(c)) == 0)
561                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
562         return retval;
566 /****************************************************************************/
567 /*** Callback management functions ******************************************/
568 /****************************************************************************/
570 static void
571 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
573         while (list) {
574                 mpdc_list_cb_t fn = list->data;
576                 fn(c, event, data);
577                 list = list->next;
578         }
581 void
582 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
584         do_list_callbacks(c, c->playlist_callbacks, event, data);
587 void
588 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
590         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
593 void
594 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
596         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
599 void
600 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
602         do_list_callbacks(c, c->browse_callbacks, event, data);
606 void
607 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
609         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
612 void
613 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
615         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
618 void
619 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
621         c->error_callbacks = g_list_append(c->error_callbacks, cb);
624 void
625 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
627         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
631 /****************************************************************************/
632 /*** Playlist management functions ******************************************/
633 /****************************************************************************/
635 /* update playlist */
636 gint
637 mpdclient_playlist_update(mpdclient_t *c)
639         mpd_InfoEntity *entity;
641         if (MPD_ERROR(c))
642                 return -1;
644         playlist_clear(&c->playlist);
646         mpd_sendPlaylistInfoCommand(c->connection,-1);
647         while ((entity = mpd_getNextInfoEntity(c->connection))) {
648                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
649                         playlist_append(&c->playlist, entity->info.song);
651                 mpd_freeInfoEntity(entity);
652         }
654         c->playlist.id = c->status->playlist;
655         c->song = NULL;
657         /* call playlist updated callbacks */
658         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
660         return mpdclient_finish_command(c);
663 #ifdef ENABLE_PLCHANGES
665 /* update playlist (plchanges) */
666 gint
667 mpdclient_playlist_update_changes(mpdclient_t *c)
669         mpd_InfoEntity *entity;
671         if (MPD_ERROR(c))
672                 return -1;
674         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
676         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
677                 struct mpd_song *song = entity->info.song;
679                 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
680                         /* update song */
681                         playlist_replace(&c->playlist, song->pos, song);
682                 } else {
683                         /* add a new song */
684                         playlist_append(&c->playlist, song);
685                 }
687                 mpd_freeInfoEntity(entity);
688         }
690         /* remove trailing songs */
691         while ((guint)c->status->playlistLength < c->playlist.list->len) {
692                 guint pos = c->playlist.list->len - 1;
694                 /* Remove the last playlist entry */
695                 playlist_remove(&c->playlist, pos);
696         }
698         c->song = NULL;
699         c->playlist.id = c->status->playlist;
701         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
703         return 0;
706 #else
707 gint
708 mpdclient_playlist_update_changes(mpdclient_t *c)
710         return mpdclient_playlist_update(c);
712 #endif
715 /****************************************************************************/
716 /*** Filelist functions *****************************************************/
717 /****************************************************************************/
719 mpdclient_filelist_t *
720 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
722         mpdclient_filelist_t *filelist;
723         mpd_InfoEntity *entity;
725         mpd_sendLsInfoCommand(c->connection, path);
726         filelist = filelist_new(path);
727         if (path && path[0] && strcmp(path, "/"))
728                 /* add a dummy entry for ./.. */
729                 filelist_append(filelist, NULL);
731         while ((entity=mpd_getNextInfoEntity(c->connection))) {
732                 filelist_append(filelist, entity);
733         }
735         /* If there's an error, ignore it.  We'll return an empty filelist. */
736         mpdclient_finish_command(c);
738         filelist_sort_dir_play(filelist, compare_filelistentry);
740         return filelist;
743 mpdclient_filelist_t *
744 mpdclient_filelist_search(mpdclient_t *c,
745                           int exact_match,
746                           int table,
747                           gchar *filter_utf8)
749         mpdclient_filelist_t *filelist;
750         mpd_InfoEntity *entity;
752         if (exact_match)
753                 mpd_sendFindCommand(c->connection, table, filter_utf8);
754         else
755                 mpd_sendSearchCommand(c->connection, table, filter_utf8);
756         filelist = filelist_new(NULL);
758         while ((entity=mpd_getNextInfoEntity(c->connection)))
759                 filelist_append(filelist, entity);
761         if (mpdclient_finish_command(c)) {
762                 filelist_free(filelist);
763                 return NULL;
764         }
766         return filelist;
769 mpdclient_filelist_t *
770 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
772         if (filelist != NULL) {
773                 gchar *path = g_strdup(filelist->path);
775                 filelist_free(filelist);
776                 filelist = mpdclient_filelist_get(c, path);
777                 g_free(path);
778                 return filelist;
779         }
780         return NULL;
783 int
784 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
786         guint i;
788         if (filelist_is_empty(fl))
789                 return 0;
791         mpd_sendCommandListBegin(c->connection);
793         for (i = 0; i < filelist_length(fl); ++i) {
794                 filelist_entry_t *entry = filelist_get(fl, i);
795                 mpd_InfoEntity *entity  = entry->entity;
797                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
798                         struct mpd_song *song = entity->info.song;
800                         mpd_sendAddCommand(c->connection, song->file);
801                 }
802         }
804         mpd_sendCommandListEnd(c->connection);
805         return mpdclient_finish_command(c);
808 GList *
809 mpdclient_get_artists(mpdclient_t *c)
811         gchar *str = NULL;
812         GList *list = NULL;
814         mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
815         while ((str = mpd_getNextArtist(c->connection)))
816                 list = g_list_append(list, (gpointer) str);
818         if (mpdclient_finish_command(c))
819                 return string_list_free(list);
821         return list;
824 GList *
825 mpdclient_get_albums(mpdclient_t *c, gchar *artist_utf8)
827         gchar *str = NULL;
828         GList *list = NULL;
830         mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
831         while ((str = mpd_getNextAlbum(c->connection)))
832                 list = g_list_append(list, (gpointer) str);
834         if (mpdclient_finish_command(c))
835                 return string_list_free(list);
837         return list;