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;
107 }
110 /****************************************************************************/
111 /*** mpdclient functions ****************************************************/
112 /****************************************************************************/
114 gint
115 mpdclient_finish_command(mpdclient_t *c)
116 {
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;
135 }
137 mpdclient_t *
138 mpdclient_new(void)
139 {
140 mpdclient_t *c;
142 c = g_malloc0(sizeof(mpdclient_t));
143 playlist_init(&c->playlist);
145 return c;
146 }
148 void
149 mpdclient_free(mpdclient_t *c)
150 {
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);
159 }
161 gint
162 mpdclient_disconnect(mpdclient_t *c)
163 {
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;
178 }
180 gint
181 mpdclient_connect(mpdclient_t *c,
182 gchar *host,
183 gint port,
184 gfloat _timeout,
185 gchar *password)
186 {
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;
207 }
209 gint
210 mpdclient_update(mpdclient_t *c)
211 {
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;
243 }
246 /****************************************************************************/
247 /*** MPD Commands **********************************************************/
248 /****************************************************************************/
250 gint
251 mpdclient_cmd_play(mpdclient_t *c, gint idx)
252 {
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);
265 }
267 gint
268 mpdclient_cmd_pause(mpdclient_t *c, gint value)
269 {
270 mpd_sendPauseCommand(c->connection, value);
271 return mpdclient_finish_command(c);
272 }
274 gint
275 mpdclient_cmd_crop(mpdclient_t *c)
276 {
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);
305 }
307 gint
308 mpdclient_cmd_stop(mpdclient_t *c)
309 {
310 mpd_sendStopCommand(c->connection);
311 return mpdclient_finish_command(c);
312 }
314 gint
315 mpdclient_cmd_next(mpdclient_t *c)
316 {
317 mpd_sendNextCommand(c->connection);
318 c->need_update = TRUE;
319 return mpdclient_finish_command(c);
320 }
322 gint
323 mpdclient_cmd_prev(mpdclient_t *c)
324 {
325 mpd_sendPrevCommand(c->connection);
326 c->need_update = TRUE;
327 return mpdclient_finish_command(c);
328 }
330 gint
331 mpdclient_cmd_seek(mpdclient_t *c, gint id, gint pos)
332 {
333 mpd_sendSeekIdCommand(c->connection, id, pos);
334 return mpdclient_finish_command(c);
335 }
337 gint
338 mpdclient_cmd_shuffle(mpdclient_t *c)
339 {
340 mpd_sendShuffleCommand(c->connection);
341 c->need_update = TRUE;
342 return mpdclient_finish_command(c);
343 }
345 gint
346 mpdclient_cmd_clear(mpdclient_t *c)
347 {
348 gint retval = 0;
350 mpd_sendClearCommand(c->connection);
351 retval = mpdclient_finish_command(c);
352 /* call playlist updated callback */
353 mpdclient_playlist_callback(c, PLAYLIST_EVENT_CLEAR, NULL);
354 c->need_update = TRUE;
355 return retval;
356 }
358 gint
359 mpdclient_cmd_repeat(mpdclient_t *c, gint value)
360 {
361 mpd_sendRepeatCommand(c->connection, value);
362 return mpdclient_finish_command(c);
363 }
365 gint
366 mpdclient_cmd_random(mpdclient_t *c, gint value)
367 {
368 mpd_sendRandomCommand(c->connection, value);
369 return mpdclient_finish_command(c);
370 }
372 gint
373 mpdclient_cmd_crossfade(mpdclient_t *c, gint value)
374 {
375 mpd_sendCrossfadeCommand(c->connection, value);
376 return mpdclient_finish_command(c);
377 }
379 gint
380 mpdclient_cmd_db_update(mpdclient_t *c, gchar *path)
381 {
382 mpd_sendUpdateCommand(c->connection, path ? path : "");
383 return mpdclient_finish_command(c);
384 }
386 gint
387 mpdclient_cmd_volume(mpdclient_t *c, gint value)
388 {
389 mpd_sendSetvolCommand(c->connection, value);
390 return mpdclient_finish_command(c);
391 }
393 gint
394 mpdclient_cmd_add_path(mpdclient_t *c, gchar *path_utf8)
395 {
396 mpd_sendAddCommand(c->connection, path_utf8);
397 return mpdclient_finish_command(c);
398 }
400 gint
401 mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
402 {
403 gint retval = 0;
405 if( !song || !song->file )
406 return -1;
408 /* send the add command to mpd */
409 mpd_sendAddCommand(c->connection, song->file);
410 if( (retval=mpdclient_finish_command(c)) )
411 return retval;
413 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_ADD
414 /* add the song to playlist */
415 playlist_append(&c->playlist, song);
417 /* increment the playlist id, so we dont retrives a new playlist */
418 c->playlist.id++;
420 /* call playlist updated callback */
421 mpdclient_playlist_callback(c, PLAYLIST_EVENT_ADD, (gpointer) song);
422 #else
423 c->need_update = TRUE;
424 #endif
426 return 0;
427 }
429 gint
430 mpdclient_cmd_delete(mpdclient_t *c, gint idx)
431 {
432 gint retval = 0;
433 struct mpd_song *song;
435 if (idx < 0 || (guint)idx >= playlist_length(&c->playlist))
436 return -1;
438 song = playlist_get(&c->playlist, idx);
440 /* send the delete command to mpd */
441 #ifdef ENABLE_SONG_ID
442 mpd_sendDeleteIdCommand(c->connection, song->id);
443 #else
444 mpd_sendDeleteCommand(c->connection, idx);
445 #endif
446 if( (retval=mpdclient_finish_command(c)) )
447 return retval;
449 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_DELETE
450 /* increment the playlist id, so we dont retrive a new playlist */
451 c->playlist.id++;
453 /* remove the song from the playlist */
454 playlist_remove_reuse(&c->playlist, idx);
456 /* call playlist updated callback */
457 mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
459 /* remove references to the song */
460 if (c->song == song) {
461 c->song = NULL;
462 c->need_update = TRUE;
463 }
465 mpd_freeSong(song);
467 #else
468 c->need_update = TRUE;
469 #endif
471 return 0;
472 }
474 gint
475 mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
476 {
477 gint n;
478 struct mpd_song *song1, *song2;
480 if (old_index == new_index || new_index < 0 ||
481 (guint)new_index >= c->playlist.list->len)
482 return -1;
484 song1 = playlist_get(&c->playlist, old_index);
485 song2 = playlist_get(&c->playlist, new_index);
487 /* send the move command to mpd */
488 #ifdef ENABLE_SONG_ID
489 mpd_sendSwapIdCommand(c->connection, song1->id, song2->id);
490 #else
491 mpd_sendMoveCommand(c->connection, old_index, new_index);
492 #endif
493 if( (n=mpdclient_finish_command(c)) )
494 return n;
496 #ifdef ENABLE_FANCY_PLAYLIST_MANAGMENT_CMD_MOVE
497 /* update the playlist */
498 playlist_swap(&c->playlist, old_index, new_index);
500 /* increment the playlist id, so we dont retrives a new playlist */
501 c->playlist.id++;
503 #else
504 c->need_update = TRUE;
505 #endif
507 /* call playlist updated callback */
508 mpdclient_playlist_callback(c, PLAYLIST_EVENT_MOVE, (gpointer) &new_index);
510 return 0;
511 }
513 gint
514 mpdclient_cmd_save_playlist(mpdclient_t *c, gchar *filename_utf8)
515 {
516 gint retval = 0;
518 mpd_sendSaveCommand(c->connection, filename_utf8);
519 if ((retval = mpdclient_finish_command(c)) == 0)
520 mpdclient_browse_callback(c, BROWSE_PLAYLIST_SAVED, NULL);
521 return retval;
522 }
524 gint
525 mpdclient_cmd_load_playlist(mpdclient_t *c, gchar *filename_utf8)
526 {
527 mpd_sendLoadCommand(c->connection, filename_utf8);
528 c->need_update = TRUE;
529 return mpdclient_finish_command(c);
530 }
532 gint
533 mpdclient_cmd_delete_playlist(mpdclient_t *c, gchar *filename_utf8)
534 {
535 gint retval = 0;
537 mpd_sendRmCommand(c->connection, filename_utf8);
538 if ((retval = mpdclient_finish_command(c)) == 0)
539 mpdclient_browse_callback(c, BROWSE_PLAYLIST_DELETED, NULL);
540 return retval;
541 }
544 /****************************************************************************/
545 /*** Callback managment functions *******************************************/
546 /****************************************************************************/
548 static void
549 do_list_callbacks(mpdclient_t *c, GList *list, gint event, gpointer data)
550 {
551 while (list) {
552 mpdc_list_cb_t fn = list->data;
554 fn(c, event, data);
555 list = list->next;
556 }
557 }
559 void
560 mpdclient_playlist_callback(mpdclient_t *c, int event, gpointer data)
561 {
562 do_list_callbacks(c, c->playlist_callbacks, event, data);
563 }
565 void
566 mpdclient_install_playlist_callback(mpdclient_t *c,mpdc_list_cb_t cb)
567 {
568 c->playlist_callbacks = g_list_append(c->playlist_callbacks, cb);
569 }
571 void
572 mpdclient_remove_playlist_callback(mpdclient_t *c, mpdc_list_cb_t cb)
573 {
574 c->playlist_callbacks = g_list_remove(c->playlist_callbacks, cb);
575 }
577 void
578 mpdclient_browse_callback(mpdclient_t *c, int event, gpointer data)
579 {
580 do_list_callbacks(c, c->browse_callbacks, event, data);
581 }
584 void
585 mpdclient_install_browse_callback(mpdclient_t *c,mpdc_list_cb_t cb)
586 {
587 c->browse_callbacks = g_list_append(c->browse_callbacks, cb);
588 }
590 void
591 mpdclient_remove_browse_callback(mpdclient_t *c, mpdc_list_cb_t cb)
592 {
593 c->browse_callbacks = g_list_remove(c->browse_callbacks, cb);
594 }
596 void
597 mpdclient_install_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
598 {
599 c->error_callbacks = g_list_append(c->error_callbacks, cb);
600 }
602 void
603 mpdclient_remove_error_callback(mpdclient_t *c, mpdc_error_cb_t cb)
604 {
605 c->error_callbacks = g_list_remove(c->error_callbacks, cb);
606 }
609 /****************************************************************************/
610 /*** Playlist managment functions *******************************************/
611 /****************************************************************************/
613 /* update playlist */
614 gint
615 mpdclient_playlist_update(mpdclient_t *c)
616 {
617 mpd_InfoEntity *entity;
619 if (MPD_ERROR(c))
620 return -1;
622 playlist_clear(&c->playlist);
624 mpd_sendPlaylistInfoCommand(c->connection,-1);
625 while ((entity = mpd_getNextInfoEntity(c->connection))) {
626 if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
627 playlist_append(&c->playlist, entity->info.song);
629 mpd_freeInfoEntity(entity);
630 }
632 c->playlist.id = c->status->playlist;
633 c->song = NULL;
635 /* call playlist updated callbacks */
636 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
638 return mpdclient_finish_command(c);
639 }
641 #ifdef ENABLE_PLCHANGES
643 /* update playlist (plchanges) */
644 gint
645 mpdclient_playlist_update_changes(mpdclient_t *c)
646 {
647 mpd_InfoEntity *entity;
649 if (MPD_ERROR(c))
650 return -1;
652 mpd_sendPlChangesCommand(c->connection, c->playlist.id);
654 while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
655 struct mpd_song *song = entity->info.song;
657 if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
658 /* update song */
659 playlist_replace(&c->playlist, song->pos, song);
660 } else {
661 /* add a new song */
662 playlist_append(&c->playlist, song);
663 }
665 mpd_freeInfoEntity(entity);
666 }
668 /* remove trailing songs */
669 while ((guint)c->status->playlistLength < c->playlist.list->len) {
670 guint pos = c->playlist.list->len - 1;
672 /* Remove the last playlist entry */
673 playlist_remove(&c->playlist, pos);
674 }
676 c->song = NULL;
677 c->playlist.id = c->status->playlist;
679 mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
681 return 0;
682 }
684 #else
685 gint
686 mpdclient_playlist_update_changes(mpdclient_t *c)
687 {
688 return mpdclient_playlist_update(c);
689 }
690 #endif
693 /****************************************************************************/
694 /*** Filelist functions *****************************************************/
695 /****************************************************************************/
697 mpdclient_filelist_t *
698 mpdclient_filelist_get(mpdclient_t *c, const gchar *path)
699 {
700 mpdclient_filelist_t *filelist;
701 mpd_InfoEntity *entity;
702 gboolean has_dirs_only = TRUE;
704 mpd_sendLsInfoCommand(c->connection, path);
705 filelist = filelist_new(path);
706 if (path && path[0] && strcmp(path, "/"))
707 /* add a dummy entry for ./.. */
708 filelist_append(filelist, NULL);
710 while ((entity=mpd_getNextInfoEntity(c->connection))) {
711 filelist_append(filelist, entity);
713 if (has_dirs_only && entity->type != MPD_INFO_ENTITY_TYPE_DIRECTORY) {
714 has_dirs_only = FALSE;
715 }
716 }
718 /* If there's an error, ignore it. We'll return an empty filelist. */
719 mpdclient_finish_command(c);
721 // If there are only directory entities in the filelist, we sort it
722 if (has_dirs_only)
723 filelist_sort(filelist, compare_filelistentry_dir);
725 return filelist;
726 }
728 mpdclient_filelist_t *
729 mpdclient_filelist_search(mpdclient_t *c,
730 int exact_match,
731 int table,
732 gchar *filter_utf8)
733 {
734 mpdclient_filelist_t *filelist;
735 mpd_InfoEntity *entity;
737 if (exact_match)
738 mpd_sendFindCommand(c->connection, table, filter_utf8);
739 else
740 mpd_sendSearchCommand(c->connection, table, filter_utf8);
741 filelist = filelist_new(NULL);
743 while ((entity=mpd_getNextInfoEntity(c->connection)))
744 filelist_append(filelist, entity);
746 if (mpdclient_finish_command(c)) {
747 filelist_free(filelist);
748 return NULL;
749 }
751 return filelist;
752 }
754 mpdclient_filelist_t *
755 mpdclient_filelist_update(mpdclient_t *c, mpdclient_filelist_t *filelist)
756 {
757 if (filelist != NULL) {
758 gchar *path = g_strdup(filelist->path);
760 filelist_free(filelist);
761 filelist = mpdclient_filelist_get(c, path);
762 g_free(path);
763 return filelist;
764 }
765 return NULL;
766 }
768 int
769 mpdclient_filelist_add_all(mpdclient_t *c, mpdclient_filelist_t *fl)
770 {
771 guint i;
773 if (filelist_is_empty(fl))
774 return 0;
776 mpd_sendCommandListBegin(c->connection);
778 for (i = 0; i < filelist_length(fl); ++i) {
779 filelist_entry_t *entry = filelist_get(fl, i);
780 mpd_InfoEntity *entity = entry->entity;
782 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
783 struct mpd_song *song = entity->info.song;
785 mpd_sendAddCommand(c->connection, song->file);
786 }
787 }
789 mpd_sendCommandListEnd(c->connection);
790 return mpdclient_finish_command(c);
791 }
793 GList *
794 mpdclient_get_artists(mpdclient_t *c)
795 {
796 gchar *str = NULL;
797 GList *list = NULL;
799 mpd_sendListCommand(c->connection, MPD_TABLE_ARTIST, NULL);
800 while ((str = mpd_getNextArtist(c->connection)))
801 list = g_list_append(list, (gpointer) str);
803 if (mpdclient_finish_command(c))
804 return string_list_free(list);
806 return list;
807 }
809 GList *
810 mpdclient_get_albums(mpdclient_t *c, gchar *artist_utf8)
811 {
812 gchar *str = NULL;
813 GList *list = NULL;
815 mpd_sendListCommand(c->connection, MPD_TABLE_ALBUM, artist_utf8);
816 while ((str = mpd_getNextAlbum(c->connection)))
817 list = g_list_append(list, (gpointer) str);
819 if (mpdclient_finish_command(c))
820 return string_list_free(list);
822 return list;
823 }