1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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.
27 #include "debug_printf.h"
29 #include "libmpd-internal.h"
32 int mpd_playlist_get_playlist_length(MpdObj
*mi
)
34 if(!mpd_check_connected(mi
))
36 debug_printf(DEBUG_WARNING
,"not connected\n");
37 return MPD_NOT_CONNECTED
;
39 if(mpd_status_check(mi
) != MPD_OK
)
41 debug_printf(DEBUG_ERROR
,"Failed grabbing status\n");
42 return MPD_STATUS_FAILED
;
44 return mi
->status
->playlistLength
;
47 long long mpd_playlist_get_old_playlist_id(MpdObj
*mi
)
49 return mi
->OldState
.playlistid
;
52 long long mpd_playlist_get_playlist_id(MpdObj
*mi
)
54 if(!mpd_check_connected(mi
))
56 debug_printf(DEBUG_WARNING
,"not connected\n");
57 return MPD_NOT_CONNECTED
;
59 if(mpd_status_check(mi
) != MPD_OK
)
61 debug_printf(DEBUG_WARNING
,"Failed grabbing status\n");
62 return MPD_STATUS_FAILED
;
64 return mi
->status
->playlist
;
66 int mpd_playlist_add(MpdObj
*mi
,const char *path
)
68 int retv
= mpd_playlist_queue_add(mi
, path
);
69 if(retv
!= MPD_OK
) return retv
;
70 return mpd_playlist_queue_commit(mi
);
73 int mpd_playlist_delete_id(MpdObj
*mi
, int songid
)
75 int retv
= mpd_playlist_queue_delete_id(mi
, songid
);
76 if(retv
!= MPD_OK
) return retv
;
77 return mpd_playlist_queue_commit(mi
);
80 int mpd_playlist_delete_pos(MpdObj
*mi
, int songpos
)
82 int retv
= mpd_playlist_queue_delete_pos(mi
, songpos
);
83 if(retv
!= MPD_OK
) return retv
;
84 return mpd_playlist_queue_commit(mi
);
86 /*******************************************************************************
89 mpd_Song
* mpd_playlist_get_song(MpdObj
*mi
, int songid
)
91 mpd_Song
*song
= NULL
;
92 mpd_InfoEntity
*ent
= NULL
;
94 debug_printf(DEBUG_ERROR
, "songid < 0 Failed");
97 if(!mpd_check_connected(mi
))
99 debug_printf(DEBUG_ERROR
, "Not Connected\n");
103 if(mpd_lock_conn(mi
))
107 debug_printf(DEBUG_INFO
, "Trying to grab song with id: %i\n", songid
);
108 mpd_sendPlaylistIdCommand(mi
->connection
, songid
);
109 ent
= mpd_getNextInfoEntity(mi
->connection
);
110 mpd_finishCommand(mi
->connection
);
112 if(mpd_unlock_conn(mi
))
114 if(ent
) mpd_freeInfoEntity(ent
);
120 debug_printf(DEBUG_ERROR
, "Failed to grab song from mpd\n");
124 if(ent
->type
!= MPD_INFO_ENTITY_TYPE_SONG
)
126 mpd_freeInfoEntity(ent
);
127 debug_printf(DEBUG_ERROR
, "Failed to grab correct song type from mpd\n");
130 song
= ent
->info
.song
;
131 ent
->info
.song
= NULL
;
133 mpd_freeInfoEntity(ent
);
138 mpd_Song
* mpd_playlist_get_song_from_pos(MpdObj
*mi
, int songpos
)
140 mpd_Song
*song
= NULL
;
141 mpd_InfoEntity
*ent
= NULL
;
143 debug_printf(DEBUG_ERROR
, "songpos < 0 Failed");
146 if(!mpd_check_connected(mi
))
148 debug_printf(DEBUG_ERROR
, "Not Connected\n");
152 if(mpd_lock_conn(mi
))
156 debug_printf(DEBUG_INFO
, "Trying to grab song with id: %i\n", songpos
);
157 mpd_sendPlaylistInfoCommand(mi
->connection
, songpos
);
158 ent
= mpd_getNextInfoEntity(mi
->connection
);
159 mpd_finishCommand(mi
->connection
);
161 if(mpd_unlock_conn(mi
))
163 /*TODO free entity. for now this can never happen */
169 debug_printf(DEBUG_ERROR
, "Failed to grab song from mpd\n");
173 if(ent
->type
!= MPD_INFO_ENTITY_TYPE_SONG
)
175 mpd_freeInfoEntity(ent
);
176 debug_printf(DEBUG_ERROR
, "Failed to grab corect song type from mpd\n");
179 song
= ent
->info
.song
;
180 ent
->info
.song
= NULL
;
182 mpd_freeInfoEntity(ent
);
187 MpdData
* mpd_playlist_get_song_from_pos_range(MpdObj
*mi
, int start
, int stop
)
189 MpdData
*data
= NULL
;
191 mpd_InfoEntity
*ent
= NULL
;
192 if(!mpd_check_connected(mi
))
194 debug_printf(DEBUG_ERROR
, "Not Connected\n");
197 if(mpd_status_check(mi
) != MPD_OK
)
199 debug_printf(DEBUG_ERROR
,"Failed grabbing status\n");
203 if(mpd_lock_conn(mi
))
207 /* Don't check outside playlist length */
208 if(!(stop
< mi
->status
->playlistLength
)) {
209 stop
= mi
->status
->playlistLength
-1;
211 mpd_sendCommandListBegin(mi
->connection
);
212 for(i
=start
; i
<= stop
; i
++){
213 mpd_sendPlaylistInfoCommand(mi
->connection
, i
);
215 mpd_sendCommandListEnd(mi
->connection
);
216 while (( ent
= mpd_getNextInfoEntity(mi
->connection
)) != NULL
)
218 if(ent
->type
== MPD_INFO_ENTITY_TYPE_SONG
)
220 data
= mpd_new_data_struct_append(data
);
221 data
->type
= MPD_DATA_TYPE_SONG
;
222 data
->song
= ent
->info
.song
;
223 ent
->info
.song
= NULL
;
225 mpd_freeInfoEntity(ent
);
227 mpd_finishCommand(mi
->connection
);
229 if(mpd_unlock_conn(mi
))
231 /*TODO free entity. for now this can never happen */
237 mpd_Song
* mpd_playlist_get_current_song(MpdObj
*mi
)
239 if(!mpd_check_connected(mi
))
241 debug_printf(DEBUG_WARNING
, "Not Connected\n");
245 if(mpd_status_check(mi
) != MPD_OK
)
247 debug_printf(DEBUG_ERROR
, "Failed to check status\n");
251 if(mi
->CurrentSong
!= NULL
&& mi
->CurrentSong
->id
!= mi
->status
->songid
)
253 debug_printf(DEBUG_WARNING
, "Current song not up2date, updating\n");
254 mpd_freeSong(mi
->CurrentSong
);
255 mi
->CurrentSong
= NULL
;
257 /* only update song when playing/pasing */
258 if(mi
->CurrentSong
== NULL
&&
259 (mpd_player_get_state(mi
) != MPD_PLAYER_STOP
&& mpd_player_get_state(mi
) != MPD_PLAYER_UNKNOWN
))
261 /* TODO: this to use the geT_current_song_id function */
262 mi
->CurrentSong
= mpd_playlist_get_song(mi
, mpd_player_get_current_song_id(mi
));
263 if(mi
->CurrentSong
== NULL
)
265 debug_printf(DEBUG_ERROR
, "Failed to grab song\n");
269 return mi
->CurrentSong
;
272 int mpd_playlist_clear(MpdObj
*mi
)
274 if(!mpd_check_connected(mi
))
276 debug_printf(DEBUG_WARNING
,"not connected\n");
277 return MPD_NOT_CONNECTED
;
279 if(mpd_lock_conn(mi
))
281 debug_printf(DEBUG_WARNING
,"lock failed\n");
282 return MPD_LOCK_FAILED
;
285 mpd_sendClearCommand(mi
->connection
);
286 mpd_finishCommand(mi
->connection
);
287 /* hack to make it update correctly when replacing 1 song */
288 mi
->CurrentState
.songid
= -1;
291 mpd_status_update(mi
);
295 int mpd_playlist_shuffle(MpdObj
*mi
)
297 if(!mpd_check_connected(mi
))
299 debug_printf(DEBUG_WARNING
,"not connected\n");
300 return MPD_NOT_CONNECTED
;
302 if(mpd_lock_conn(mi
))
304 debug_printf(DEBUG_ERROR
,"lock failed\n");
305 return MPD_LOCK_FAILED
;
308 mpd_sendShuffleCommand(mi
->connection
);
309 mpd_finishCommand(mi
->connection
);
318 int mpd_playlist_move_id(MpdObj
*mi
, int old_id
, int new_id
)
320 if(!mpd_check_connected(mi
))
322 debug_printf(DEBUG_WARNING
,"not connected\n");
323 return MPD_NOT_CONNECTED
;
325 if(mpd_lock_conn(mi
))
327 debug_printf(DEBUG_ERROR
,"lock failed\n");
328 return MPD_LOCK_FAILED
;
331 mpd_sendMoveIdCommand(mi
->connection
,old_id
, new_id
);
332 mpd_finishCommand(mi
->connection
);
339 int mpd_playlist_move_pos(MpdObj
*mi
, int old_pos
, int new_pos
)
341 if(!mpd_check_connected(mi
))
343 debug_printf(DEBUG_WARNING
,"not connected\n");
344 return MPD_NOT_CONNECTED
;
346 if(mpd_lock_conn(mi
))
348 debug_printf(DEBUG_ERROR
,"lock failed\n");
349 return MPD_LOCK_FAILED
;
352 mpd_sendMoveCommand(mi
->connection
,old_pos
, new_pos
);
353 mpd_finishCommand(mi
->connection
);
360 MpdData
* mpd_playlist_get_changes(MpdObj
*mi
,int old_playlist_id
)
362 MpdData
*data
= NULL
;
363 mpd_InfoEntity
*ent
= NULL
;
364 if(!mpd_check_connected(mi
))
366 debug_printf(DEBUG_WARNING
,"not connected\n");
369 if(mpd_lock_conn(mi
))
371 debug_printf(DEBUG_WARNING
,"lock failed\n");
375 if(old_playlist_id
== -1)
377 debug_printf(DEBUG_INFO
,"get fresh playlist\n");
378 mpd_sendPlChangesCommand (mi
->connection
, 0);
379 /* mpd_sendPlaylistIdCommand(mi->connection, -1); */
383 mpd_sendPlChangesCommand (mi
->connection
, old_playlist_id
);
386 while (( ent
= mpd_getNextInfoEntity(mi
->connection
)) != NULL
)
388 if(ent
->type
== MPD_INFO_ENTITY_TYPE_SONG
)
390 data
= mpd_new_data_struct_append(data
);
391 data
->type
= MPD_DATA_TYPE_SONG
;
392 data
->song
= ent
->info
.song
;
393 ent
->info
.song
= NULL
;
395 mpd_freeInfoEntity(ent
);
397 mpd_finishCommand(mi
->connection
);
400 if(mpd_unlock_conn(mi
))
402 debug_printf(DEBUG_WARNING
,"mpd_playlist_get_changes: unlock failed.\n");
410 return mpd_data_get_first(data
);
415 MpdData
* mpd_playlist_get_changes_posid(MpdObj
*mi
,int old_playlist_id
)
417 MpdData
*data
= NULL
;
418 mpd_InfoEntity
*ent
= NULL
;
419 debug_printf(DEBUG_INFO
, "Fetching using new plchangesposid command");
420 if(!mpd_check_connected(mi
))
422 debug_printf(DEBUG_WARNING
,"not connected\n");
425 if(mpd_lock_conn(mi
))
427 debug_printf(DEBUG_WARNING
,"lock failed\n");
431 if(old_playlist_id
== -1)
433 debug_printf(DEBUG_INFO
,"get fresh playlist\n");
434 mpd_sendPlChangesPosIdCommand (mi
->connection
, 0);
435 /* mpd_sendPlaylistIdCommand(mi->connection, -1); */
439 mpd_sendPlChangesPosIdCommand (mi
->connection
, old_playlist_id
);
442 while (( ent
= mpd_getNextInfoEntity(mi
->connection
)) != NULL
)
444 if(ent
->type
== MPD_INFO_ENTITY_TYPE_SONG
)
446 data
= mpd_new_data_struct_append(data
);
447 data
->type
= MPD_DATA_TYPE_SONG
;
448 data
->song
= ent
->info
.song
;
449 ent
->info
.song
= NULL
;
451 mpd_freeInfoEntity(ent
);
453 mpd_finishCommand(mi
->connection
);
456 if(mpd_unlock_conn(mi
))
458 debug_printf(DEBUG_WARNING
,"mpd_playlist_get_changes: unlock failed.\n");
466 return mpd_data_get_first(data
);
469 int mpd_playlist_queue_add(MpdObj
*mi
,const char *path
)
471 if(!mpd_check_connected(mi
))
473 debug_printf(DEBUG_WARNING
,"not connected\n");
474 return MPD_NOT_CONNECTED
;
478 debug_printf(DEBUG_ERROR
, "path != NULL Failed");
479 return MPD_ARGS_ERROR
;
482 if(mi
->queue
== NULL
)
484 mi
->queue
= mpd_new_queue_struct();
485 mi
->queue
->first
= mi
->queue
;
486 mi
->queue
->next
= NULL
;
487 mi
->queue
->prev
= NULL
;
491 mi
->queue
->next
= mpd_new_queue_struct();
492 mi
->queue
->next
->first
= mi
->queue
->first
;
493 mi
->queue
->next
->prev
= mi
->queue
;
494 mi
->queue
= mi
->queue
->next
;
495 mi
->queue
->next
= NULL
;
497 mi
->queue
->type
= MPD_QUEUE_ADD
;
498 mi
->queue
->path
= strdup(path
);
502 int mpd_playlist_queue_load(MpdObj
*mi
,const char *path
)
504 if(!mpd_check_connected(mi
))
506 debug_printf(DEBUG_WARNING
,"not connected\n");
507 return MPD_NOT_CONNECTED
;
511 debug_printf(DEBUG_ERROR
, "path != NULL Failed");
512 return MPD_ARGS_ERROR
;
515 if(mi
->queue
== NULL
)
517 mi
->queue
= mpd_new_queue_struct();
518 mi
->queue
->first
= mi
->queue
;
519 mi
->queue
->next
= NULL
;
520 mi
->queue
->prev
= NULL
;
524 mi
->queue
->next
= mpd_new_queue_struct();
525 mi
->queue
->next
->first
= mi
->queue
->first
;
526 mi
->queue
->next
->prev
= mi
->queue
;
527 mi
->queue
= mi
->queue
->next
;
528 mi
->queue
->next
= NULL
;
530 mi
->queue
->type
= MPD_QUEUE_LOAD
;
531 mi
->queue
->path
= strdup(path
);
536 int mpd_playlist_queue_commit(MpdObj
*mi
)
538 if(!mpd_check_connected(mi
))
540 debug_printf(DEBUG_WARNING
,"not connected\n");
541 return MPD_NOT_CONNECTED
;
543 if(mi
->queue
== NULL
)
545 debug_printf(DEBUG_WARNING
,"mi->queue is empty");
546 return MPD_PLAYLIST_QUEUE_EMPTY
;
548 if(mpd_lock_conn(mi
))
550 debug_printf(DEBUG_WARNING
,"lock failed\n");
551 return MPD_LOCK_FAILED
;
553 mpd_sendCommandListBegin(mi
->connection
);
555 mi
->queue
= mi
->queue
->first
;
556 while(mi
->queue
!= NULL
)
558 if(mi
->queue
->type
== MPD_QUEUE_ADD
)
560 if(mi
->queue
->path
!= NULL
)
562 mpd_sendAddCommand(mi
->connection
, mi
->queue
->path
);
565 else if(mi
->queue
->type
== MPD_QUEUE_LOAD
)
567 if(mi
->queue
->path
!= NULL
)
569 mpd_sendLoadCommand(mi
->connection
, mi
->queue
->path
);
572 else if (mi
->queue
->type
== MPD_QUEUE_DELETE_ID
)
574 if(mi
->queue
->id
>= 0)
576 mpd_sendDeleteIdCommand(mi
->connection
, mi
->queue
->id
);
579 else if (mi
->queue
->type
== MPD_QUEUE_DELETE_POS
)
581 if(mi
->queue
->id
>= 0)
583 mpd_sendDeleteCommand(mi
->connection
, mi
->queue
->id
);
587 mpd_queue_get_next(mi
);
589 mpd_sendCommandListEnd(mi
->connection
);
590 mpd_finishCommand(mi
->connection
);
595 mpd_status_update(mi
);
598 int mpd_playlist_queue_delete_id(MpdObj
*mi
,int id
)
600 if(!mpd_check_connected(mi
))
602 debug_printf(DEBUG_WARNING
,"not connected\n");
603 return MPD_NOT_CONNECTED
;
606 if(mi
->queue
== NULL
)
608 mi
->queue
= mpd_new_queue_struct();
609 mi
->queue
->first
= mi
->queue
;
610 mi
->queue
->next
= NULL
;
611 mi
->queue
->prev
= NULL
;
615 mi
->queue
->next
= mpd_new_queue_struct();
616 mi
->queue
->next
->first
= mi
->queue
->first
;
617 mi
->queue
->next
->prev
= mi
->queue
;
618 mi
->queue
= mi
->queue
->next
;
619 mi
->queue
->next
= NULL
;
621 mi
->queue
->type
= MPD_QUEUE_DELETE_ID
;
623 mi
->queue
->path
= NULL
;
627 int mpd_playlist_queue_delete_pos(MpdObj
*mi
,int songpos
)
629 if(!mpd_check_connected(mi
))
631 debug_printf(DEBUG_WARNING
,"mpd_playlist_add: not connected\n");
632 return MPD_NOT_CONNECTED
;
635 if(mi
->queue
== NULL
)
637 mi
->queue
= mpd_new_queue_struct();
638 mi
->queue
->first
= mi
->queue
;
639 mi
->queue
->next
= NULL
;
640 mi
->queue
->prev
= NULL
;
644 mi
->queue
->next
= mpd_new_queue_struct();
645 mi
->queue
->next
->first
= mi
->queue
->first
;
646 mi
->queue
->next
->prev
= mi
->queue
;
647 mi
->queue
= mi
->queue
->next
;
648 mi
->queue
->next
= NULL
;
650 mi
->queue
->type
= MPD_QUEUE_DELETE_POS
;
651 mi
->queue
->id
= songpos
;
652 mi
->queue
->path
= NULL
;
656 int mpd_playlist_add_get_id(MpdObj
*mi
,const char *path
)
659 if(mi
== NULL
|| path
== NULL
)
661 debug_printf(DEBUG_ERROR
, "mi == NULL || path == NULL failed");
662 return MPD_ARGS_ERROR
;
664 if(!mpd_check_connected(mi
))
666 debug_printf(DEBUG_WARNING
,"mpd_playlist_add: not connected\n");
667 return MPD_NOT_CONNECTED
;
669 if(mpd_lock_conn(mi
))
671 debug_printf(DEBUG_WARNING
,"lock failed\n");
672 return MPD_LOCK_FAILED
;
674 songid
= mpd_sendAddIdCommand(mi
->connection
, path
);
675 mpd_finishCommand(mi
->connection
);
681 void mpd_playlist_search_start(MpdObj
*mi
, int exact
)
686 if(mi
== NULL
|| exact
> 1 || exact
< 0)
688 debug_printf(DEBUG_ERROR
, "Argument error");
691 if(!mpd_check_connected(mi
))
693 debug_printf(DEBUG_ERROR
, "Not Connected\n");
696 if(!mpd_server_check_version(mi
, 0,12,1))
698 debug_printf(DEBUG_ERROR
, "Advanced search requires mpd 0.12.2 or higher");
701 /* lock, so we can work on mi->connection */
702 if(mpd_lock_conn(mi
) != MPD_OK
)
704 debug_printf(DEBUG_ERROR
, "Failed to lock connection");
707 mpd_startPlaylistSearch(mi
->connection
, exact
);
708 /* Set search type */
709 mi
->search_type
= (exact
)? MPD_SEARCH_TYPE_PLAYLIST_FIND
:MPD_SEARCH_TYPE_PLAYLIST_SEARCH
;
710 /* unlock, let the error handler handle any possible error.
716 void mpd_playlist_search_add_constraint(MpdObj
*mi
, mpd_TagItems field
,const char *value
)
718 mpd_database_search_add_constraint(mi
, field
, value
);
720 MpdData
* mpd_playlist_search_commit(MpdObj
*mi
)
722 mpd_InfoEntity
*ent
= NULL
;
723 MpdData
*data
= NULL
;
724 if(!mpd_check_connected(mi
))
726 debug_printf(DEBUG_WARNING
,"not connected\n");
729 if(mi
->search_type
< MPD_SEARCH_TYPE_PLAYLIST_FIND
)
731 debug_printf(DEBUG_ERROR
, "no or wrong search in progress to commit");
734 if(mpd_lock_conn(mi
))
736 debug_printf(DEBUG_ERROR
,"lock failed\n");
739 mpd_commitSearch(mi
->connection
);
740 while (( ent
= mpd_getNextInfoEntity(mi
->connection
)) != NULL
)
742 if(ent
->type
== MPD_INFO_ENTITY_TYPE_SONG
)
744 data
= mpd_new_data_struct_append(data
);
745 data
->type
= MPD_DATA_TYPE_SONG
;
746 data
->song
= ent
->info
.song
;
747 ent
->info
.song
= NULL
;
749 mpd_freeInfoEntity(ent
);
751 mpd_finishCommand(mi
->connection
);
755 mi
->search_type
= MPD_SEARCH_TYPE_NONE
;
756 mi
->search_field
= MPD_TAG_ITEM_ARTIST
;
758 if(mpd_unlock_conn(mi
))
760 debug_printf(DEBUG_ERROR
, "Failed to unlock connection");
761 if(data
)mpd_data_free(data
);
768 return mpd_data_get_first(data
);
772 int mpd_playlist_load(MpdObj
*mi
, const char *path
)
775 if(!mpd_check_connected(mi
))
777 debug_printf(DEBUG_WARNING
,"mpd_playlist_load: not connected\n");
778 return MPD_NOT_CONNECTED
;
780 if(mpd_lock_conn(mi
))
782 debug_printf(DEBUG_ERROR
,"lock failed\n");
785 mpd_sendLoadCommand(mi
->connection
,path
);
786 mpd_finishCommand(mi
->connection
);
787 if(mi
->connection
->errorCode
== MPD_ACK_ERROR_NO_EXIST
)
789 debug_printf(DEBUG_WARNING
, "mpd_playlist_load: failed to load playlist\n");
790 mpd_clearError(mi
->connection
);
791 retv
= MPD_PLAYLIST_LOAD_FAILED
;
794 if(mpd_unlock_conn(mi
))
796 debug_printf(DEBUG_ERROR
, "Failed to unlock connection");
797 return MPD_LOCK_FAILED
;