Code

mpdclient: tweak updatingdb in mpdclient.c
[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"
25 #include "utils.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <time.h>
30 #include <string.h>
32 #undef  ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD /* broken with song id's */
33 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
34 #define ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
35 #define ENABLE_SONG_ID
36 #define ENABLE_PLCHANGES
38 #define BUFSIZE 1024
40 static bool
41 MPD_ERROR(const struct mpdclient *client)
42 {
43         return client == NULL || client->connection==NULL ||
44                 client->connection->error != MPD_ERROR_SUCCESS;
45 }
47 /* filelist sorting functions */
48 static gint
49 compare_filelistentry(gconstpointer filelist_entry1,
50                           gconstpointer filelist_entry2)
51 {
52         const mpd_InfoEntity *e1, *e2;
53         int n = 0;
55         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
56         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
58         if (e1 && e2 && e1->type == e2->type) {
59                 switch (e1->type) {
60                 case MPD_INFO_ENTITY_TYPE_DIRECTORY:
61                         n = g_utf8_collate(e1->info.directory->path,
62                                         e2->info.directory->path);
63                         break;
64                 case MPD_INFO_ENTITY_TYPE_SONG:
65                         break;
66                 case MPD_INFO_ENTITY_TYPE_PLAYLISTFILE:
67                         n = g_utf8_collate(e1->info.playlistFile->path,
68                                         e2->info.playlistFile->path);
69                 }
70         }
71         return n;
72 }
74 /* sort by list-format */
75 gint
76 compare_filelistentry_format(gconstpointer filelist_entry1,
77                              gconstpointer filelist_entry2)
78 {
79         const mpd_InfoEntity *e1, *e2;
80         char key1[BUFSIZE], key2[BUFSIZE];
81         int n = 0;
83         e1 = ((const filelist_entry_t *)filelist_entry1)->entity;
84         e2 = ((const filelist_entry_t *)filelist_entry2)->entity;
86         if (e1 && e2 &&
87             e1->type == MPD_INFO_ENTITY_TYPE_SONG &&
88             e2->type == MPD_INFO_ENTITY_TYPE_SONG) {
89                 strfsong(key1, BUFSIZE, options.list_format, e1->info.song);
90                 strfsong(key2, BUFSIZE, options.list_format, e2->info.song);
91                 n = strcmp(key1,key2);
92         }
94         return n;
95 }
98 /* Error callbacks */
99 static gint
100 error_cb(mpdclient_t *c, gint error, const gchar *msg)
102         GList *list = c->error_callbacks;
104         if (list == NULL)
105                 fprintf(stderr, "error [%d]: %s\n", (error & 0xFF), msg);
107         while (list) {
108                 mpdc_error_cb_t cb = list->data;
109                 if (cb)
110                         cb(c, error, msg);
111                 list = list->next;
112         }
114         mpd_clearError(c->connection);
115         return error;
119 /****************************************************************************/
120 /*** mpdclient functions ****************************************************/
121 /****************************************************************************/
123 gint
124 mpdclient_finish_command(mpdclient_t *c)
126         mpd_finishCommand(c->connection);
128         if (c->connection->error) {
129                 gint error = c->connection->error;
131                 if (error == MPD_ERROR_ACK &&
132                     c->connection->errorCode == MPD_ACK_ERROR_PERMISSION &&
133                     screen_auth(c) == 0)
134                         return 0;
136                 if (error == MPD_ERROR_ACK)
137                         error = error | (c->connection->errorCode << 8);
139                 error_cb(c, error, c->connection->errorStr);
140                 return error;
141         }
143         return 0;
146 mpdclient_t *
147 mpdclient_new(void)
149         mpdclient_t *c;
151         c = g_malloc0(sizeof(mpdclient_t));
152         playlist_init(&c->playlist);
154         return c;
157 void
158 mpdclient_free(mpdclient_t *c)
160         mpdclient_disconnect(c);
162         mpdclient_playlist_free(&c->playlist);
164         g_list_free(c->error_callbacks);
165         g_list_free(c->playlist_callbacks);
166         g_list_free(c->browse_callbacks);
167         g_free(c);
170 gint
171 mpdclient_disconnect(mpdclient_t *c)
173         if (c->connection)
174                 mpd_closeConnection(c->connection);
175         c->connection = NULL;
177         if (c->status)
178                 mpd_freeStatus(c->status);
179         c->status = NULL;
181         playlist_clear(&c->playlist);
183         if (c->song)
184                 c->song = NULL;
186         return 0;
189 gint
190 mpdclient_connect(mpdclient_t *c,
191                   gchar *host,
192                   gint port,
193                   gfloat _timeout,
194                   gchar *password)
196         gint retval = 0;
198         /* close any open connection */
199         if( c->connection )
200                 mpdclient_disconnect(c);
202         /* connect to MPD */
203         c->connection = mpd_newConnection(host, port, _timeout);
204         if( c->connection->error )
205                 return error_cb(c, c->connection->error,
206                                 c->connection->errorStr);
208         /* send password */
209         if( password ) {
210                 mpd_sendPasswordCommand(c->connection, password);
211                 retval = mpdclient_finish_command(c);
212         }
213         c->need_update = TRUE;
215         return retval;
218 gint
219 mpdclient_update(mpdclient_t *c)
221         gint retval = 0;
223         if (MPD_ERROR(c))
224                 return -1;
226         /* free the old status */
227         if (c->status)
228                 mpd_freeStatus(c->status);
230         /* retrieve new status */
231         mpd_sendStatusCommand(c->connection);
232         c->status = mpd_getStatus(c->connection);
233         if ((retval=mpdclient_finish_command(c)))
234                 return retval;
236         /* check if the playlist needs an update */
237         if (c->playlist.id != c->status->playlist) {
238                 if (playlist_is_empty(&c->playlist))
239                         retval = mpdclient_playlist_update_changes(c);
240                 else
241                         retval = mpdclient_playlist_update(c);
242         }
244         /* update the current song */
245         if (!c->song || c->status->songid != c->song->id) {
246                 c->song = playlist_get_song(c, c->status->song);
247         }
249         c->need_update = FALSE;
251         return retval;
255 /****************************************************************************/
256 /*** MPD Commands  **********************************************************/
257 /****************************************************************************/
259 gint
260 mpdclient_cmd_play(mpdclient_t *c, gint idx)
262 #ifdef ENABLE_SONG_ID
263         struct mpd_song *song = playlist_get_song(c, idx);
265         if (song)
266                 mpd_sendPlayIdCommand(c->connection, song->id);
267         else
268                 mpd_sendPlayIdCommand(c->connection, MPD_PLAY_AT_BEGINNING);
269 #else
270         mpd_sendPlayCommand(c->connection, idx);
271 #endif
272         c->need_update = TRUE;
273         return mpdclient_finish_command(c);
276 gint
277 mpdclient_cmd_pause(mpdclient_t *c, gint value)
279         mpd_sendPauseCommand(c->connection, value);
280         return mpdclient_finish_command(c);
283 gint
284 mpdclient_cmd_crop(mpdclient_t *c)
286         gint error;
287         mpd_Status *status;
288         bool playing;
289         int length, current;
291         mpd_sendStatusCommand(c->connection);
292         status = mpd_getStatus(c->connection);
293         error = mpdclient_finish_command(c);
294         if (error)
295                 return error;
297         playing = status->state == MPD_STATUS_STATE_PLAY ||
298                 status->state == MPD_STATUS_STATE_PAUSE;
299         length = status->playlistLength;
300         current = status->song;
302         mpd_freeStatus(status);
304         if (!playing || length < 2)
305                 return 0;
307         mpd_sendCommandListBegin( c->connection );
309         while (--length >= 0)
310                 if (length != current)
311                         mpd_sendDeleteCommand(c->connection, length);
313         mpd_sendCommandListEnd(c->connection);
315         return mpdclient_finish_command(c);
318 gint
319 mpdclient_cmd_stop(mpdclient_t *c)
321         mpd_sendStopCommand(c->connection);
322         return mpdclient_finish_command(c);
325 gint
326 mpdclient_cmd_next(mpdclient_t *c)
328         mpd_sendNextCommand(c->connection);
329         c->need_update = TRUE;
330         return mpdclient_finish_command(c);
333 gint
334 mpdclient_cmd_prev(mpdclient_t *c)
336         mpd_sendPrevCommand(c->connection);
337         c->need_update = TRUE;
338         return mpdclient_finish_command(c);
341 gint
342 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
344         mpd_sendSeekIdCommand(c->connection, id, pos);
345         return mpdclient_finish_command(c);
348 gint
349 mpdclient_cmd_shuffle(mpdclient_t *c)
351         mpd_sendShuffleCommand(c->connection);
352         c->need_update = TRUE;
353         return mpdclient_finish_command(c);
356 gint
357 mpdclient_cmd_shuffle_range(mpdclient_t *c, guint start, guint end)
359         mpd_sendShuffleRangeCommand(c->connection, start, end);
360         c->need_update = TRUE;
361         return mpdclient_finish_command(c);
364 gint
365 mpdclient_cmd_clear(mpdclient_t *c)
367         gint retval = 0;
369         mpd_sendClearCommand(c->connection);
370         retval = mpdclient_finish_command(c);
371         /* call playlist updated callback */
372         mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
373         c->need_update = TRUE;
374         return retval;
377 gint
378 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
380         mpd_sendRepeatCommand(c->connection, value);
381         return mpdclient_finish_command(c);
384 gint
385 mpdclient_cmd_random(mpdclient_t *c, gint value)
387         mpd_sendRandomCommand(c->connection, value);
388         return mpdclient_finish_command(c);
391 gint
392 mpdclient_cmd_single(mpdclient_t *c, gint value)
394         mpd_sendSingleCommand(c->connection, value);
395         return mpdclient_finish_command(c);
398 gint
399 mpdclient_cmd_consume(mpdclient_t *c, gint value)
401         mpd_sendConsumeCommand(c->connection, value);
402         return mpdclient_finish_command(c);
405 gint
406 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
408         mpd_sendCrossfadeCommand(c->connection, value);
409         return mpdclient_finish_command(c);
412 gint
413 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
415         gint ret;
417         mpd_sendUpdateCommand(c->connection, path ? path : "");
418         ret = mpdclient_finish_command(c);
420         if (ret == 0)
421                 /* set updatingDb to make sure the browse callback
422                    gets called even if the update has finished before
423                    status is updated */
424                 c->status->updatingDb = 1;
426         return ret;
429 gint
430 mpdclient_cmd_volume(mpdclient_t *c, gint value)
432         mpd_sendSetvolCommand(c->connection, value);
433         return mpdclient_finish_command(c);
436 gint
437 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
439         mpd_sendAddCommand(c->connection, path_utf8);
440         return mpdclient_finish_command(c);
443 gint
444 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
446         gint retval = 0;
448         if( !song || !song->file )
449                 return -1;
451         /* send the add command to mpd */
452         mpd_sendAddCommand(c->connection, song->file);
453         if( (retval=mpdclient_finish_command(c)) )
454                 return retval;
456 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
457         /* add the song to playlist */
458         playlist_append(&c->playlist, song);
460         /* increment the playlist id, so we don't retrieve a new playlist */
461         c->playlist.id++;
463         /* call playlist updated callback */
464         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
465 #else
466         c->need_update = TRUE;
467 #endif
469         return 0;
472 gint
473 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
475         gint retval = 0;
476         struct mpd_song *song;
478         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
479                 return -1;
481         song = playlist_get(&c->playlist, idx);
483         /* send the delete command to mpd */
484 #ifdef ENABLE_SONG_ID
485         mpd_sendDeleteIdCommand(c->connection, song->id);
486 #else
487         mpd_sendDeleteCommand(c->connection, idx);
488 #endif
489         if( (retval=mpdclient_finish_command(c)) )
490                 return retval;
492 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
493         /* increment the playlist id, so we don't retrieve a new playlist */
494         c->playlist.id++;
496         /* remove the song from the playlist */
497         playlist_remove_reuse(&c->playlist, idx);
499         /* call playlist updated callback */
500         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
502         /* remove references to the song */
503         if (c->song == song) {
504                 c->song = NULL;
505                 c->need_update = TRUE;
506         }
508         mpd_freeSong(song);
510 #else
511         c->need_update = TRUE;
512 #endif
514         return 0;
517 gint
518 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
520         gint n;
521         struct mpd_song *song1, *song2;
523         if (old_index == new_index || new_index < 0 ||
524             (guint)new_index >= c->playlist.list->len)
525                 return -1;
527         song1 = playlist_get(&c->playlist, old_index);
528         song2 = playlist_get(&c->playlist, new_index);
530         /* send the move command to mpd */
531 #ifdef ENABLE_SONG_ID
532         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
533 #else
534         mpd_sendMoveCommand(c->connection, old_index, new_index);
535 #endif
536         if( (n=mpdclient_finish_command(c)) )
537                 return n;
539 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
540         /* update the playlist */
541         playlist_swap(&c->playlist, old_index, new_index);
543         /* increment the playlist id, so we don't retrieve a new playlist */
544         c->playlist.id++;
546 #else
547         c->need_update = TRUE;
548 #endif
550         /* call playlist updated callback */
551         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
553         return 0;
556 gint
557 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
559         gint retval = 0;
561         mpd_sendSaveCommand(c->connection, filename_utf8);
562         if ((retval = mpdclient_finish_command(c)) == 0)
563                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
564         return retval;
567 gint
568 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
570         mpd_sendLoadCommand(c->connection, filename_utf8);
571         c->need_update = TRUE;
572         return mpdclient_finish_command(c);
575 gint
576 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
578         gint retval = 0;
580         mpd_sendRmCommand(c->connection, filename_utf8);
581         if ((retval = mpdclient_finish_command(c)) == 0)
582                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
583         return retval;
587 /****************************************************************************/
588 /*** Callback management functions ******************************************/
589 /****************************************************************************/
591 static void
592 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
594         while (list) {
595                 mpdc_list_cb_t fn = list->data;
597                 fn(c, event, data);
598                 list = list->next;
599         }
602 void
603 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
605         do_list_callbacks(c, c->playlist_callbacks, event, data);
608 void
609 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
611         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
614 void
615 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
617         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
620 void
621 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
623         do_list_callbacks(c, c->browse_callbacks, event, data);
627 void
628 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
630         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
633 void
634 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
636         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
639 void
640 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
642         c->error_callbacks = g_list_append(c->error_callbacks, cb);
645 void
646 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
648         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
652 /****************************************************************************/
653 /*** Playlist management functions ******************************************/
654 /****************************************************************************/
656 /* update playlist */
657 gint
658 mpdclient_playlist_update(mpdclient_t *c)
660         mpd_InfoEntity *entity;
662         if (MPD_ERROR(c))
663                 return -1;
665         playlist_clear(&c->playlist);
667         mpd_sendPlaylistInfoCommand(c->connection,-1);
668         while ((entity = mpd_getNextInfoEntity(c->connection))) {
669                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
670                         playlist_append(&c->playlist, entity->info.song);
672                 mpd_freeInfoEntity(entity);
673         }
675         c->playlist.id = c->status->playlist;
676         c->song = NULL;
678         /* call playlist updated callbacks */
679         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
681         return mpdclient_finish_command(c);
684 #ifdef ENABLE_PLCHANGES
686 /* update playlist (plchanges) */
687 gint
688 mpdclient_playlist_update_changes(mpdclient_t *c)
690         mpd_InfoEntity *entity;
692         if (MPD_ERROR(c))
693                 return -1;
695         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
697         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
698                 struct mpd_song *song = entity->info.song;
700                 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
701                         /* update song */
702                         playlist_replace(&c->playlist, song->pos, song);
703                 } else {
704                         /* add a new song */
705                         playlist_append(&c->playlist, song);
706                 }
708                 mpd_freeInfoEntity(entity);
709         }
711         /* remove trailing songs */
712         while ((guint)c->status->playlistLength < c->playlist.list->len) {
713                 guint pos = c->playlist.list->len - 1;
715                 /* Remove the last playlist entry */
716                 playlist_remove(&c->playlist, pos);
717         }
719         c->song = NULL;
720         c->playlist.id = c->status->playlist;
722         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
724         return 0;
727 #else
728 gint
729 mpdclient_playlist_update_changes(mpdclient_t *c)
731         return mpdclient_playlist_update(c);
733 #endif
736 /****************************************************************************/
737 /*** Filelist functions *****************************************************/
738 /****************************************************************************/
740 mpdclient_filelist_t *
741 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
743         mpdclient_filelist_t *filelist;
744         mpd_InfoEntity *entity;
746         mpd_sendLsInfoCommand(c->connection, path);
747         filelist = filelist_new(path);
748         if (path && path[0] && strcmp(path, "/"))
749                 /* add a dummy entry for ./.. */
750                 filelist_append(filelist, NULL);
752         while ((entity=mpd_getNextInfoEntity(c->connection))) {
753                 filelist_append(filelist, entity);
754         }
756         /* If there's an error, ignore it.  We'll return an empty filelist. */
757         mpdclient_finish_command(c);
759         filelist_sort_dir_play(filelist, compare_filelistentry);
761         return filelist;
764 mpdclient_filelist_t *
765 mpdclient_filelist_search(mpdclient_t *c,
766                           int exact_match,
767                           int table,
768                           gchar *filter_utf8)
770         mpdclient_filelist_t *filelist;
771         mpd_InfoEntity *entity;
773         if (exact_match)
774                 mpd_sendFindCommand(c->connection, table, filter_utf8);
775         else
776                 mpd_sendSearchCommand(c->connection, table, filter_utf8);
777         filelist = filelist_new(NULL);
779         while ((entity=mpd_getNextInfoEntity(c->connection)))
780                 filelist_append(filelist, entity);
782         if (mpdclient_finish_command(c)) {
783                 filelist_free(filelist);
784                 return NULL;
785         }
787         return filelist;
790 mpdclient_filelist_t *
791 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
793         if (filelist != NULL) {
794                 gchar *path = g_strdup(filelist->path);
796                 filelist_free(filelist);
797                 filelist = mpdclient_filelist_get(c, path);
798                 g_free(path);
799                 return filelist;
800         }
801         return NULL;
804 int
805 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
807         guint i;
809         if (filelist_is_empty(fl))
810                 return 0;
812         mpd_sendCommandListBegin(c->connection);
814         for (i = 0; i < filelist_length(fl); ++i) {
815                 filelist_entry_t *entry = filelist_get(fl, i);
816                 mpd_InfoEntity *entity  = entry->entity;
818                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
819                         struct mpd_song *song = entity->info.song;
821                         mpd_sendAddCommand(c->connection, song->file);
822                 }
823         }
825         mpd_sendCommandListEnd(c->connection);
826         return mpdclient_finish_command(c);
829 GList *
830 mpdclient_get_artists(mpdclient_t *c)
832         gchar *str = NULL;
833         GList *list = NULL;
835         mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
836         while ((str = mpd_getNextArtist(c->connection)))
837                 list = g_list_append(list, (gpointer) str);
839         if (mpdclient_finish_command(c))
840                 return string_list_free(list);
842         return list;
845 GList *
846 mpdclient_get_albums(mpdclient_t *c, gchar *artist_utf8)
848         gchar *str = NULL;
849         GList *list = NULL;
851         mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
852         while ((str = mpd_getNextAlbum(c->connection)))
853                 list = g_list_append(list, (gpointer) str);
855         if (mpdclient_finish_command(c))
856                 return string_list_free(list);
858         return list;