Code

mpdclient: pass const message to error callback
[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, const 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_consume(mpdclient_t *c, gint value)
397         mpd_sendConsumeCommand(c->connection, value);
398         return mpdclient_finish_command(c);
401 gint
402 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
404         mpd_sendCrossfadeCommand(c->connection, value);
405         return mpdclient_finish_command(c);
408 gint
409 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
411         mpd_sendUpdateCommand(c->connection, path ? path : "");
412         return mpdclient_finish_command(c);
415 gint
416 mpdclient_cmd_volume(mpdclient_t *c, gint value)
418         mpd_sendSetvolCommand(c->connection, value);
419         return mpdclient_finish_command(c);
422 gint
423 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
425         mpd_sendAddCommand(c->connection, path_utf8);
426         return mpdclient_finish_command(c);
429 gint
430 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
432         gint retval = 0;
434         if( !song || !song->file )
435                 return -1;
437         /* send the add command to mpd */
438         mpd_sendAddCommand(c->connection, song->file);
439         if( (retval=mpdclient_finish_command(c)) )
440                 return retval;
442 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
443         /* add the song to playlist */
444         playlist_append(&c->playlist, song);
446         /* increment the playlist id, so we don't retrieve a new playlist */
447         c->playlist.id++;
449         /* call playlist updated callback */
450         mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
451 #else
452         c->need_update = TRUE;
453 #endif
455         return 0;
458 gint
459 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
461         gint retval = 0;
462         struct mpd_song *song;
464         if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
465                 return -1;
467         song = playlist_get(&c->playlist, idx);
469         /* send the delete command to mpd */
470 #ifdef ENABLE_SONG_ID
471         mpd_sendDeleteIdCommand(c->connection, song->id);
472 #else
473         mpd_sendDeleteCommand(c->connection, idx);
474 #endif
475         if( (retval=mpdclient_finish_command(c)) )
476                 return retval;
478 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
479         /* increment the playlist id, so we don't retrieve a new playlist */
480         c->playlist.id++;
482         /* remove the song from the playlist */
483         playlist_remove_reuse(&c->playlist, idx);
485         /* call playlist updated callback */
486         mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
488         /* remove references to the song */
489         if (c->song == song) {
490                 c->song = NULL;
491                 c->need_update = TRUE;
492         }
494         mpd_freeSong(song);
496 #else
497         c->need_update = TRUE;
498 #endif
500         return 0;
503 gint
504 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
506         gint n;
507         struct mpd_song *song1, *song2;
509         if (old_index == new_index || new_index < 0 ||
510             (guint)new_index >= c->playlist.list->len)
511                 return -1;
513         song1 = playlist_get(&c->playlist, old_index);
514         song2 = playlist_get(&c->playlist, new_index);
516         /* send the move command to mpd */
517 #ifdef ENABLE_SONG_ID
518         mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
519 #else
520         mpd_sendMoveCommand(c->connection, old_index, new_index);
521 #endif
522         if( (n=mpdclient_finish_command(c)) )
523                 return n;
525 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
526         /* update the playlist */
527         playlist_swap(&c->playlist, old_index, new_index);
529         /* increment the playlist id, so we don't retrieve a new playlist */
530         c->playlist.id++;
532 #else
533         c->need_update = TRUE;
534 #endif
536         /* call playlist updated callback */
537         mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
539         return 0;
542 gint
543 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
545         gint retval = 0;
547         mpd_sendSaveCommand(c->connection, filename_utf8);
548         if ((retval = mpdclient_finish_command(c)) == 0)
549                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
550         return retval;
553 gint
554 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
556         mpd_sendLoadCommand(c->connection, filename_utf8);
557         c->need_update = TRUE;
558         return mpdclient_finish_command(c);
561 gint
562 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
564         gint retval = 0;
566         mpd_sendRmCommand(c->connection, filename_utf8);
567         if ((retval = mpdclient_finish_command(c)) == 0)
568                 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
569         return retval;
573 /****************************************************************************/
574 /*** Callback management functions ******************************************/
575 /****************************************************************************/
577 static void
578 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
580         while (list) {
581                 mpdc_list_cb_t fn = list->data;
583                 fn(c, event, data);
584                 list = list->next;
585         }
588 void
589 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
591         do_list_callbacks(c, c->playlist_callbacks, event, data);
594 void
595 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
597         c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
600 void
601 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
603         c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
606 void
607 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
609         do_list_callbacks(c, c->browse_callbacks, event, data);
613 void
614 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
616         c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
619 void
620 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
622         c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
625 void
626 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
628         c->error_callbacks = g_list_append(c->error_callbacks, cb);
631 void
632 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
634         c->error_callbacks = g_list_remove(c->error_callbacks, cb);
638 /****************************************************************************/
639 /*** Playlist management functions ******************************************/
640 /****************************************************************************/
642 /* update playlist */
643 gint
644 mpdclient_playlist_update(mpdclient_t *c)
646         mpd_InfoEntity *entity;
648         if (MPD_ERROR(c))
649                 return -1;
651         playlist_clear(&c->playlist);
653         mpd_sendPlaylistInfoCommand(c->connection,-1);
654         while ((entity = mpd_getNextInfoEntity(c->connection))) {
655                 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
656                         playlist_append(&c->playlist, entity->info.song);
658                 mpd_freeInfoEntity(entity);
659         }
661         c->playlist.id = c->status->playlist;
662         c->song = NULL;
664         /* call playlist updated callbacks */
665         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
667         return mpdclient_finish_command(c);
670 #ifdef ENABLE_PLCHANGES
672 /* update playlist (plchanges) */
673 gint
674 mpdclient_playlist_update_changes(mpdclient_t *c)
676         mpd_InfoEntity *entity;
678         if (MPD_ERROR(c))
679                 return -1;
681         mpd_sendPlChangesCommand(c->connection, c->playlist.id);
683         while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
684                 struct mpd_song *song = entity->info.song;
686                 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
687                         /* update song */
688                         playlist_replace(&c->playlist, song->pos, song);
689                 } else {
690                         /* add a new song */
691                         playlist_append(&c->playlist, song);
692                 }
694                 mpd_freeInfoEntity(entity);
695         }
697         /* remove trailing songs */
698         while ((guint)c->status->playlistLength < c->playlist.list->len) {
699                 guint pos = c->playlist.list->len - 1;
701                 /* Remove the last playlist entry */
702                 playlist_remove(&c->playlist, pos);
703         }
705         c->song = NULL;
706         c->playlist.id = c->status->playlist;
708         mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
710         return 0;
713 #else
714 gint
715 mpdclient_playlist_update_changes(mpdclient_t *c)
717         return mpdclient_playlist_update(c);
719 #endif
722 /****************************************************************************/
723 /*** Filelist functions *****************************************************/
724 /****************************************************************************/
726 mpdclient_filelist_t *
727 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
729         mpdclient_filelist_t *filelist;
730         mpd_InfoEntity *entity;
732         mpd_sendLsInfoCommand(c->connection, path);
733         filelist = filelist_new(path);
734         if (path && path[0] && strcmp(path, "/"))
735                 /* add a dummy entry for ./.. */
736                 filelist_append(filelist, NULL);
738         while ((entity=mpd_getNextInfoEntity(c->connection))) {
739                 filelist_append(filelist, entity);
740         }
742         /* If there's an error, ignore it.  We'll return an empty filelist. */
743         mpdclient_finish_command(c);
745         filelist_sort_dir_play(filelist, compare_filelistentry);
747         return filelist;
750 mpdclient_filelist_t *
751 mpdclient_filelist_search(mpdclient_t *c,
752                           int exact_match,
753                           int table,
754                           gchar *filter_utf8)
756         mpdclient_filelist_t *filelist;
757         mpd_InfoEntity *entity;
759         if (exact_match)
760                 mpd_sendFindCommand(c->connection, table, filter_utf8);
761         else
762                 mpd_sendSearchCommand(c->connection, table, filter_utf8);
763         filelist = filelist_new(NULL);
765         while ((entity=mpd_getNextInfoEntity(c->connection)))
766                 filelist_append(filelist, entity);
768         if (mpdclient_finish_command(c)) {
769                 filelist_free(filelist);
770                 return NULL;
771         }
773         return filelist;
776 mpdclient_filelist_t *
777 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
779         if (filelist != NULL) {
780                 gchar *path = g_strdup(filelist->path);
782                 filelist_free(filelist);
783                 filelist = mpdclient_filelist_get(c, path);
784                 g_free(path);
785                 return filelist;
786         }
787         return NULL;
790 int
791 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
793         guint i;
795         if (filelist_is_empty(fl))
796                 return 0;
798         mpd_sendCommandListBegin(c->connection);
800         for (i = 0; i < filelist_length(fl); ++i) {
801                 filelist_entry_t *entry = filelist_get(fl, i);
802                 mpd_InfoEntity *entity  = entry->entity;
804                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
805                         struct mpd_song *song = entity->info.song;
807                         mpd_sendAddCommand(c->connection, song->file);
808                 }
809         }
811         mpd_sendCommandListEnd(c->connection);
812         return mpdclient_finish_command(c);
815 GList *
816 mpdclient_get_artists(mpdclient_t *c)
818         gchar *str = NULL;
819         GList *list = NULL;
821         mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
822         while ((str = mpd_getNextArtist(c->connection)))
823                 list = g_list_append(list, (gpointer) str);
825         if (mpdclient_finish_command(c))
826                 return string_list_free(list);
828         return list;
831 GList *
832 mpdclient_get_albums(mpdclient_t *c, gchar *artist_utf8)
834         gchar *str = NULL;
835         GList *list = NULL;
837         mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
838         while ((str = mpd_getNextAlbum(c->connection)))
839                 list = g_list_append(list, (gpointer) str);
841         if (mpdclient_finish_command(c))
842                 return string_list_free(list);
844         return list;