Code

mpdclient: Fixes sorting of the filelist.
[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_crossfade(mpdclient_t *c, gint value)
390         mpd_sendCrossfadeCommand(c->connection, value);
391         return mpdclient_finish_command(c);
394 gint
395 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
397         mpd_sendUpdateCommand(c->connection, path ? path : "");
398         return mpdclient_finish_command(c);
401 gint
402 mpdclient_cmd_volume(mpdclient_t *c, gint value)
404         mpd_sendSetvolCommand(c->connection, value);
405         return mpdclient_finish_command(c);
408 gint
409 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
411         mpd_sendAddCommand(c->connection, path_utf8);
412         return mpdclient_finish_command(c);
415 gint
416 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
418         gint retval = 0;
420         if( !song || !song->file )
421                 return -1;
423         /* send the add command to mpd */
424         mpd_sendAddCommand(c->connection, song->file);
425         if( (retval=mpdclient_finish_command(c)) )
426                 return retval;
428 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
429         /* add the song to playlist */
430         playlist_append(&c->playlist, song);
432         /* increment the playlist id, so we don't retrieve a new playlist */
433         c->playlist.id++;
435         /* call playlist updated callback */
436         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
437 #else
438         c->need_update = TRUE;
439 #endif
441         return 0;
444 gint
445 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
447         gint retval = 0;
448         struct mpd_song *song;
450         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
451                 return -1;
453         song = playlist_get(&c->playlist, idx);
455         /* send the delete command to mpd */
456 #ifdef ENABLE_SONG_ID
457         mpd_sendDeleteIdCommand(c->connection, song->id);
458 #else
459         mpd_sendDeleteCommand(c->connection, idx);
460 #endif
461         if( (retval=mpdclient_finish_command(c)) )
462                 return retval;
464 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
465         /* increment the playlist id, so we don't retrieve a new playlist */
466         c->playlist.id++;
468         /* remove the song from the playlist */
469         playlist_remove_reuse(&c->playlist, idx);
471         /* call playlist updated callback */
472         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
474         /* remove references to the song */
475         if (c->song == song) {
476                 c->song = NULL;
477                 c->need_update = TRUE;
478         }
480         mpd_freeSong(song);
482 #else
483         c->need_update = TRUE;
484 #endif
486         return 0;
489 gint
490 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
492         gint n;
493         struct mpd_song *song1, *song2;
495         if (old_index == new_index || new_index < 0 ||
496             (guint)new_index >= c->playlist.list->len)
497                 return -1;
499         song1 = playlist_get(&c->playlist, old_index);
500         song2 = playlist_get(&c->playlist, new_index);
502         /* send the move command to mpd */
503 #ifdef ENABLE_SONG_ID
504         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
505 #else
506         mpd_sendMoveCommand(c->connection, old_index, new_index);
507 #endif
508         if( (n=mpdclient_finish_command(c)) )
509                 return n;
511 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
512         /* update the playlist */
513         playlist_swap(&c->playlist, old_index, new_index);
515         /* increment the playlist id, so we don't retrieve a new playlist */
516         c->playlist.id++;
518 #else
519         c->need_update = TRUE;
520 #endif
522         /* call playlist updated callback */
523         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
525         return 0;
528 gint
529 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
531         gint retval = 0;
533         mpd_sendSaveCommand(c->connection, filename_utf8);
534         if ((retval = mpdclient_finish_command(c)) == 0)
535                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
536         return retval;
539 gint
540 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
542         mpd_sendLoadCommand(c->connection, filename_utf8);
543         c->need_update = TRUE;
544         return mpdclient_finish_command(c);
547 gint
548 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
550         gint retval = 0;
552         mpd_sendRmCommand(c->connection, filename_utf8);
553         if ((retval = mpdclient_finish_command(c)) == 0)
554                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
555         return retval;
559 /****************************************************************************/
560 /*** Callback management functions ******************************************/
561 /****************************************************************************/
563 static void
564 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
566         while (list) {
567                 mpdc_list_cb_t fn = list->data;
569                 fn(c, event, data);
570                 list = list->next;
571         }
574 void
575 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
577         do_list_callbacks(c, c->playlist_callbacks, event, data);
580 void
581 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
583         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
586 void
587 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
589         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
592 void
593 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
595         do_list_callbacks(c, c->browse_callbacks, event, data);
599 void
600 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
602         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
605 void
606 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
608         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
611 void
612 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
614         c->error_callbacks = g_list_append(c->error_callbacks, cb);
617 void
618 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
620         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
624 /****************************************************************************/
625 /*** Playlist management functions ******************************************/
626 /****************************************************************************/
628 /* update playlist */
629 gint
630 mpdclient_playlist_update(mpdclient_t *c)
632         mpd_InfoEntity *entity;
634         if (MPD_ERROR(c))
635                 return -1;
637         playlist_clear(&c->playlist);
639         mpd_sendPlaylistInfoCommand(c->connection,-1);
640         while ((entity = mpd_getNextInfoEntity(c->connection))) {
641                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
642                         playlist_append(&c->playlist, entity->info.song);
644                 mpd_freeInfoEntity(entity);
645         }
647         c->playlist.id = c->status->playlist;
648         c->song = NULL;
650         /* call playlist updated callbacks */
651         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
653         return mpdclient_finish_command(c);
656 #ifdef ENABLE_PLCHANGES
658 /* update playlist (plchanges) */
659 gint
660 mpdclient_playlist_update_changes(mpdclient_t *c)
662         mpd_InfoEntity *entity;
664         if (MPD_ERROR(c))
665                 return -1;
667         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
669         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
670                 struct mpd_song *song = entity->info.song;
672                 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
673                         /* update song */
674                         playlist_replace(&c->playlist, song->pos, song);
675                 } else {
676                         /* add a new song */
677                         playlist_append(&c->playlist, song);
678                 }
680                 mpd_freeInfoEntity(entity);
681         }
683         /* remove trailing songs */
684         while ((guint)c->status->playlistLength < c->playlist.list->len) {
685                 guint pos = c->playlist.list->len - 1;
687                 /* Remove the last playlist entry */
688                 playlist_remove(&c->playlist, pos);
689         }
691         c->song = NULL;
692         c->playlist.id = c->status->playlist;
694         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
696         return 0;
699 #else
700 gint
701 mpdclient_playlist_update_changes(mpdclient_t *c)
703         return mpdclient_playlist_update(c);
705 #endif
708 /****************************************************************************/
709 /*** Filelist functions *****************************************************/
710 /****************************************************************************/
712 mpdclient_filelist_t *
713 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
715         mpdclient_filelist_t *filelist;
716         mpd_InfoEntity *entity;
718         mpd_sendLsInfoCommand(c->connection, path);
719         filelist = filelist_new(path);
720         if (path && path[0] && strcmp(path, "/"))
721                 /* add a dummy entry for ./.. */
722                 filelist_append(filelist, NULL);
724         while ((entity=mpd_getNextInfoEntity(c->connection))) {
725                 filelist_append(filelist, entity);
726         }
728         /* If there's an error, ignore it.  We'll return an empty filelist. */
729         mpdclient_finish_command(c);
731         filelist_sort_dir_play(filelist, compare_filelistentry);
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;