Code

use the new mpd shufflerange command.
[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_dir(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 &&
57             e1->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
58             e2->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
59                 n = g_utf8_collate(e1->info.directory->path,
60                                    e2->info.directory->path);
62         return n;
63 }
65 /* sort by list-format */
66 gint
67 compare_filelistentry_format(gconstpointer filelist_entry1,
68                              gconstpointer filelist_entry2)
69 {
70         const mpd_InfoEntity *e1, *e2;
71         char key1[BUFSIZE], key2[BUFSIZE];
72         int n = 0;
74         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
75         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
77         if (e1 && e2 &&
78             e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
79             e2->type == MPD_INFO_ENTITY_TYPE_SONG) {
80                 strfsong(key1, BUFSIZE, options.list_format, e1->info.song);
81                 strfsong(key2, BUFSIZE, options.list_format, e2->info.song);
82                 n = strcmp(key1,key2);
83         }
85         return n;
86 }
89 /* Error callbacks */
90 static gint
91 error_cb(mpdclient_t *c, gint error, gchar *msg)
92 {
93         GList *list = c->error_callbacks;
95         if (list == NULL)
96                 fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
98         while (list) {
99                 mpdc_error_cb_t cb = list->data;
100                 if (cb)
101                         cb(c, error, msg);
102                 list = list->next;
103         }
105         mpd_clearError(c->connection);
106         return error;
110 /****************************************************************************/
111 /*** mpdclient functions ****************************************************/
112 /****************************************************************************/
114 gint
115 mpdclient_finish_command(mpdclient_t *c)
117         mpd_finishCommand(c->connection);
119         if (c->connection->error) {
120                 gint error = c->connection->error;
122                 if (error == MPD_ERROR_ACK &&
123                     c->connection->errorCode == MPD_ACK_ERROR_PERMISSION &&
124                     screen_auth(c) == 0)
125                         return 0;
127                 if (error == MPD_ERROR_ACK)
128                         error = error | (c->connection->errorCode << 8);
130                 error_cb(c, error, c->connection->errorStr);
131                 return error;
132         }
134         return 0;
137 mpdclient_t *
138 mpdclient_new(void)
140         mpdclient_t *c;
142         c = g_malloc0(sizeof(mpdclient_t));
143         playlist_init(&c->playlist);
145         return c;
148 void
149 mpdclient_free(mpdclient_t *c)
151         mpdclient_disconnect(c);
153         mpdclient_playlist_free(&c->playlist);
155         g_list_free(c->error_callbacks);
156         g_list_free(c->playlist_callbacks);
157         g_list_free(c->browse_callbacks);
158         g_free(c);
161 gint
162 mpdclient_disconnect(mpdclient_t *c)
164         if (c->connection)
165                 mpd_closeConnection(c->connection);
166         c->connection = NULL;
168         if (c->status)
169                 mpd_freeStatus(c->status);
170         c->status = NULL;
172         playlist_clear(&c->playlist);
174         if (c->song)
175                 c->song = NULL;
177         return 0;
180 gint
181 mpdclient_connect(mpdclient_t *c,
182                   gchar *host,
183                   gint port,
184                   gfloat _timeout,
185                   gchar *password)
187         gint retval = 0;
189         /* close any open connection */
190         if( c->connection )
191                 mpdclient_disconnect(c);
193         /* connect to MPD */
194         c->connection = mpd_newConnection(host, port, _timeout);
195         if( c->connection->error )
196                 return error_cb(c, c->connection->error,
197                                 c->connection->errorStr);
199         /* send password */
200         if( password ) {
201                 mpd_sendPasswordCommand(c->connection, password);
202                 retval = mpdclient_finish_command(c);
203         }
204         c->need_update = TRUE;
206         return retval;
209 gint
210 mpdclient_update(mpdclient_t *c)
212         gint retval = 0;
214         if (MPD_ERROR(c))
215                 return -1;
217         /* free the old status */
218         if (c->status)
219                 mpd_freeStatus(c->status);
221         /* retreive new status */
222         mpd_sendStatusCommand(c->connection);
223         c->status = mpd_getStatus(c->connection);
224         if ((retval=mpdclient_finish_command(c)))
225                 return retval;
227         /* check if the playlist needs an update */
228         if (c->playlist.id != c->status->playlist) {
229                 if (playlist_is_empty(&c->playlist))
230                         retval = mpdclient_playlist_update_changes(c);
231                 else
232                         retval = mpdclient_playlist_update(c);
233         }
235         /* update the current song */
236         if (!c->song || c->status->songid != c->song->id) {
237                 c->song = playlist_get_song(c, c->status->song);
238         }
240         c->need_update = FALSE;
242         return retval;
246 /****************************************************************************/
247 /*** MPD Commands  **********************************************************/
248 /****************************************************************************/
250 gint
251 mpdclient_cmd_play(mpdclient_t *c, gint idx)
253 #ifdef ENABLE_SONG_ID
254         struct mpd_song *song = playlist_get_song(c, idx);
256         if (song)
257                 mpd_sendPlayIdCommand(c->connection, song->id);
258         else
259                 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
260 #else
261         mpd_sendPlayCommand(c->connection, idx);
262 #endif
263         c->need_update = TRUE;
264         return mpdclient_finish_command(c);
267 gint
268 mpdclient_cmd_pause(mpdclient_t *c, gint value)
270         mpd_sendPauseCommand(c->connection, value);
271         return mpdclient_finish_command(c);
274 gint
275 mpdclient_cmd_crop(mpdclient_t *c)
277         mpd_Status *status;
278         int length;
280         mpd_sendStatusCommand(c->connection);
281         status = mpd_getStatus(c->connection);
282         length = status->playlistLength - 1;
284         if (length <= 0) {
285                 mpd_freeStatus(status);
286         } else if (status->state == 3 || status->state == 2) {
287                 /* If playing or paused */
289                 mpd_sendCommandListBegin( c->connection );
291                 while (length >= 0) {
292                         if (length != status->song)
293                                 mpd_sendDeleteCommand(c->connection, length);
295                         length--;
296                 }
298                 mpd_sendCommandListEnd(c->connection);
299                 mpd_freeStatus(status);
300         } else {
301                 mpd_freeStatus(status);
302         }
304         return mpdclient_finish_command(c);
307 gint
308 mpdclient_cmd_stop(mpdclient_t *c)
310         mpd_sendStopCommand(c->connection);
311         return mpdclient_finish_command(c);
314 gint
315 mpdclient_cmd_next(mpdclient_t *c)
317         mpd_sendNextCommand(c->connection);
318         c->need_update = TRUE;
319         return mpdclient_finish_command(c);
322 gint
323 mpdclient_cmd_prev(mpdclient_t *c)
325         mpd_sendPrevCommand(c->connection);
326         c->need_update = TRUE;
327         return mpdclient_finish_command(c);
330 gint
331 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
333         mpd_sendSeekIdCommand(c->connection, id, pos);
334         return mpdclient_finish_command(c);
337 gint
338 mpdclient_cmd_shuffle(mpdclient_t *c)
340         mpd_sendShuffleCommand(c->connection);
341         c->need_update = TRUE;
342         return mpdclient_finish_command(c);
345 gint
346 mpdclient_cmd_shuffle_range(mpdclient_t *c, gint start, gint end)
348         mpd_sendShuffleRangeCommand(c->connection, start, end);
349         c->need_update = TRUE;
350         return mpdclient_finish_command(c);
353 gint
354 mpdclient_cmd_clear(mpdclient_t *c)
356         gint retval = 0;
358         mpd_sendClearCommand(c->connection);
359         retval = mpdclient_finish_command(c);
360         /* call playlist updated callback */
361         mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
362         c->need_update = TRUE;
363         return retval;
366 gint
367 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
369         mpd_sendRepeatCommand(c->connection, value);
370         return mpdclient_finish_command(c);
373 gint
374 mpdclient_cmd_random(mpdclient_t *c, gint value)
376         mpd_sendRandomCommand(c->connection, value);
377         return mpdclient_finish_command(c);
380 gint
381 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
383         mpd_sendCrossfadeCommand(c->connection, value);
384         return mpdclient_finish_command(c);
387 gint
388 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
390         mpd_sendUpdateCommand(c->connection, path ? path : "");
391         return mpdclient_finish_command(c);
394 gint
395 mpdclient_cmd_volume(mpdclient_t *c, gint value)
397         mpd_sendSetvolCommand(c->connection, value);
398         return mpdclient_finish_command(c);
401 gint
402 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
404         mpd_sendAddCommand(c->connection, path_utf8);
405         return mpdclient_finish_command(c);
408 gint
409 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
411         gint retval = 0;
413         if( !song || !song->file )
414                 return -1;
416         /* send the add command to mpd */
417         mpd_sendAddCommand(c->connection, song->file);
418         if( (retval=mpdclient_finish_command(c)) )
419                 return retval;
421 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
422         /* add the song to playlist */
423         playlist_append(&c->playlist, song);
425         /* increment the playlist id, so we dont retrives a new playlist */
426         c->playlist.id++;
428         /* call playlist updated callback */
429         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
430 #else
431         c->need_update = TRUE;
432 #endif
434         return 0;
437 gint
438 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
440         gint retval = 0;
441         struct mpd_song *song;
443         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
444                 return -1;
446         song = playlist_get(&c->playlist, idx);
448         /* send the delete command to mpd */
449 #ifdef ENABLE_SONG_ID
450         mpd_sendDeleteIdCommand(c->connection, song->id);
451 #else
452         mpd_sendDeleteCommand(c->connection, idx);
453 #endif
454         if( (retval=mpdclient_finish_command(c)) )
455                 return retval;
457 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
458         /* increment the playlist id, so we dont retrive a new playlist */
459         c->playlist.id++;
461         /* remove the song from the playlist */
462         playlist_remove_reuse(&c->playlist, idx);
464         /* call playlist updated callback */
465         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
467         /* remove references to the song */
468         if (c->song == song) {
469                 c->song = NULL;
470                 c->need_update = TRUE;
471         }
473         mpd_freeSong(song);
475 #else
476         c->need_update = TRUE;
477 #endif
479         return 0;
482 gint
483 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
485         gint n;
486         struct mpd_song *song1, *song2;
488         if (old_index == new_index || new_index < 0 ||
489             (guint)new_index >= c->playlist.list->len)
490                 return -1;
492         song1 = playlist_get(&c->playlist, old_index);
493         song2 = playlist_get(&c->playlist, new_index);
495         /* send the move command to mpd */
496 #ifdef ENABLE_SONG_ID
497         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
498 #else
499         mpd_sendMoveCommand(c->connection, old_index, new_index);
500 #endif
501         if( (n=mpdclient_finish_command(c)) )
502                 return n;
504 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
505         /* update the playlist */
506         playlist_swap(&c->playlist, old_index, new_index);
508         /* increment the playlist id, so we dont retrives a new playlist */
509         c->playlist.id++;
511 #else
512         c->need_update = TRUE;
513 #endif
515         /* call playlist updated callback */
516         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
518         return 0;
521 gint
522 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
524         gint retval = 0;
526         mpd_sendSaveCommand(c->connection, filename_utf8);
527         if ((retval = mpdclient_finish_command(c)) == 0)
528                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
529         return retval;
532 gint
533 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
535         mpd_sendLoadCommand(c->connection, filename_utf8);
536         c->need_update = TRUE;
537         return mpdclient_finish_command(c);
540 gint
541 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
543         gint retval = 0;
545         mpd_sendRmCommand(c->connection, filename_utf8);
546         if ((retval = mpdclient_finish_command(c)) == 0)
547                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
548         return retval;
552 /****************************************************************************/
553 /*** Callback managment functions *******************************************/
554 /****************************************************************************/
556 static void
557 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
559         while (list) {
560                 mpdc_list_cb_t fn = list->data;
562                 fn(c, event, data);
563                 list = list->next;
564         }
567 void
568 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
570         do_list_callbacks(c, c->playlist_callbacks, event, data);
573 void
574 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
576         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
579 void
580 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
582         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
585 void
586 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
588         do_list_callbacks(c, c->browse_callbacks, event, data);
592 void
593 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
595         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
598 void
599 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
601         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
604 void
605 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
607         c->error_callbacks = g_list_append(c->error_callbacks, cb);
610 void
611 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
613         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
617 /****************************************************************************/
618 /*** Playlist managment functions *******************************************/
619 /****************************************************************************/
621 /* update playlist */
622 gint
623 mpdclient_playlist_update(mpdclient_t *c)
625         mpd_InfoEntity *entity;
627         if (MPD_ERROR(c))
628                 return -1;
630         playlist_clear(&c->playlist);
632         mpd_sendPlaylistInfoCommand(c->connection,-1);
633         while ((entity = mpd_getNextInfoEntity(c->connection))) {
634                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
635                         playlist_append(&c->playlist, entity->info.song);
637                 mpd_freeInfoEntity(entity);
638         }
640         c->playlist.id = c->status->playlist;
641         c->song = NULL;
643         /* call playlist updated callbacks */
644         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
646         return mpdclient_finish_command(c);
649 #ifdef ENABLE_PLCHANGES
651 /* update playlist (plchanges) */
652 gint
653 mpdclient_playlist_update_changes(mpdclient_t *c)
655         mpd_InfoEntity *entity;
657         if (MPD_ERROR(c))
658                 return -1;
660         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
662         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
663                 struct mpd_song *song = entity->info.song;
665                 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
666                         /* update song */
667                         playlist_replace(&c->playlist, song->pos, song);
668                 } else {
669                         /* add a new song */
670                         playlist_append(&c->playlist, song);
671                 }
673                 mpd_freeInfoEntity(entity);
674         }
676         /* remove trailing songs */
677         while ((guint)c->status->playlistLength < c->playlist.list->len) {
678                 guint pos = c->playlist.list->len - 1;
680                 /* Remove the last playlist entry */
681                 playlist_remove(&c->playlist, pos);
682         }
684         c->song = NULL;
685         c->playlist.id = c->status->playlist;
687         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
689         return 0;
692 #else
693 gint
694 mpdclient_playlist_update_changes(mpdclient_t *c)
696         return mpdclient_playlist_update(c);
698 #endif
701 /****************************************************************************/
702 /*** Filelist functions *****************************************************/
703 /****************************************************************************/
705 mpdclient_filelist_t *
706 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
708         mpdclient_filelist_t *filelist;
709         mpd_InfoEntity *entity;
710         gboolean has_dirs_only = TRUE;
712         mpd_sendLsInfoCommand(c->connection, path);
713         filelist = filelist_new(path);
714         if (path && path[0] && strcmp(path, "/"))
715                 /* add a dummy entry for ./.. */
716                 filelist_append(filelist, NULL);
718         while ((entity=mpd_getNextInfoEntity(c->connection))) {
719                 filelist_append(filelist, entity);
721                 if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
722                         has_dirs_only = FALSE;
723                 }
724         }
726         /* If there's an error, ignore it.  We'll return an empty filelist. */
727         mpdclient_finish_command(c);
729         // If there are only directory entities in the filelist, we sort it
730         if (has_dirs_only)
731                 filelist_sort(filelist, compare_filelistentry_dir);
733         return filelist;
736 mpdclient_filelist_t *
737 mpdclient_filelist_search(mpdclient_t *c,
738                           int exact_match,
739                           int table,
740                           gchar *filter_utf8)
742         mpdclient_filelist_t *filelist;
743         mpd_InfoEntity *entity;
745         if (exact_match)
746                 mpd_sendFindCommand(c->connection, table, filter_utf8);
747         else
748                 mpd_sendSearchCommand(c->connection, table, filter_utf8);
749         filelist = filelist_new(NULL);
751         while ((entity=mpd_getNextInfoEntity(c->connection)))
752                 filelist_append(filelist, entity);
754         if (mpdclient_finish_command(c)) {
755                 filelist_free(filelist);
756                 return NULL;
757         }
759         return filelist;
762 mpdclient_filelist_t *
763 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
765         if (filelist != NULL) {
766                 gchar *path = g_strdup(filelist->path);
768                 filelist_free(filelist);
769                 filelist = mpdclient_filelist_get(c, path);
770                 g_free(path);
771                 return filelist;
772         }
773         return NULL;
776 int
777 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
779         guint i;
781         if (filelist_is_empty(fl))
782                 return 0;
784         mpd_sendCommandListBegin(c->connection);
786         for (i = 0; i < filelist_length(fl); ++i) {
787                 filelist_entry_t *entry = filelist_get(fl, i);
788                 mpd_InfoEntity *entity  = entry->entity;
790                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
791                         struct mpd_song *song = entity->info.song;
793                         mpd_sendAddCommand(c->connection, song->file);
794                 }
795         }
797         mpd_sendCommandListEnd(c->connection);
798         return mpdclient_finish_command(c);
801 GList *
802 mpdclient_get_artists(mpdclient_t *c)
804         gchar *str = NULL;
805         GList *list = NULL;
807         mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
808         while ((str = mpd_getNextArtist(c->connection)))
809                 list = g_list_append(list, (gpointer) str);
811         if (mpdclient_finish_command(c))
812                 return string_list_free(list);
814         return list;
817 GList *
818 mpdclient_get_albums(mpdclient_t *c, gchar *artist_utf8)
820         gchar *str = NULL;
821         GList *list = NULL;
823         mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
824         while ((str = mpd_getNextAlbum(c->connection)))
825                 list = g_list_append(list, (gpointer) str);
827         if (mpdclient_finish_command(c))
828                 return string_list_free(list);
830         return list;