1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
|
/* the Music Player Daemon (MPD)
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
* This project's homepage is: http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "playlist.h"
#include "playlist_save.h"
#include "queue_print.h"
#include "locate.h"
#include "player_control.h"
#include "command.h"
#include "ls.h"
#include "tag.h"
#include "song.h"
#include "conf.h"
#include "database.h"
#include "mapper.h"
#include "stored_playlist.h"
#include "ack.h"
#include "idle.h"
#include <glib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
/**
* When the "prev" command is received, rewind the current track if
* this number of seconds has already elapsed.
*/
#define PLAYLIST_PREV_UNLESS_ELAPSED 10
static void
playPlaylistOrderNumber(struct playlist *playlist, int orderNum);
static void incrPlaylistVersion(struct playlist *playlist)
{
queue_increment_version(&playlist->queue);
idle_add(IDLE_PLAYLIST);
}
void playlistVersionChange(struct playlist *playlist)
{
queue_modify_all(&playlist->queue);
idle_add(IDLE_PLAYLIST);
}
void
playlist_tag_changed(struct playlist *playlist)
{
if (!playlist->playing)
return;
assert(playlist->current >= 0);
queue_modify(&playlist->queue, playlist->current);
idle_add(IDLE_PLAYLIST);
}
void
playlist_init(struct playlist *playlist)
{
queue_init(&playlist->queue,
config_get_positive(CONF_MAX_PLAYLIST_LENGTH,
DEFAULT_PLAYLIST_MAX_LENGTH));
playlist->queued = -1;
playlist->current = -1;
}
void
playlist_finish(struct playlist *playlist)
{
queue_finish(&playlist->queue);
}
void clearPlaylist(struct playlist *playlist)
{
stopPlaylist(playlist);
/* make sure there are no references to allocated songs
anymore */
for (unsigned i = 0; i < queue_length(&playlist->queue); i++) {
const struct song *song = queue_get(&playlist->queue, i);
if (!song_in_database(song))
pc_song_deleted(song);
}
queue_clear(&playlist->queue);
playlist->current = -1;
incrPlaylistVersion(playlist);
}
/**
* Queue a song, addressed by its order number.
*/
static void
playlist_queue_song_order(struct playlist *playlist, unsigned order)
{
struct song *song;
char *uri;
assert(queue_valid_order(&playlist->queue, order));
playlist->queued = order;
song = queue_get_order(&playlist->queue, order);
uri = song_get_uri(song);
g_debug("playlist: queue song %i:\"%s\"",
playlist->queued, uri);
g_free(uri);
queueSong(song);
}
/**
* Check if the player thread has already started playing the "queued"
* song.
*/
static void syncPlaylistWithQueue(struct playlist *playlist)
{
if (pc.next_song == NULL && playlist->queued != -1) {
/* queued song has started: copy queued to current,
and notify the clients */
playlist->current = playlist->queued;
playlist->queued = -1;
idle_add(IDLE_PLAYER);
}
}
/**
* Returns the song object which is currently queued. Returns none if
* there is none (yet?) or if MPD isn't playing.
*/
static const struct song *
playlist_get_queued_song(struct playlist *playlist)
{
if (!playlist->playing || playlist->queued < 0)
return NULL;
return queue_get_order(&playlist->queue, playlist->queued);
}
/**
* Updates the "queued song". Calculates the next song according to
* the current one (if MPD isn't playing, it takes the first song),
* and queues this song. Clears the old queued song if there was one.
*
* @param prev the song which was previously queued, as determined by
* playlist_get_queued_song()
*/
static void
playlist_update_queued_song(struct playlist *playlist, const struct song *prev)
{
int next_order;
const struct song *next_song;
if (!playlist->playing)
return;
assert(!queue_is_empty(&playlist->queue));
assert((playlist->queued < 0) == (prev == NULL));
next_order = playlist->current >= 0
? queue_next_order(&playlist->queue, playlist->current)
: 0;
if (next_order == 0 && playlist->queue.random) {
/* shuffle the song order again, so we get a different
order each time the playlist is played
completely */
unsigned current_position =
queue_order_to_position(&playlist->queue,
playlist->current);
queue_shuffle_order(&playlist->queue);
/* make sure that the playlist->current still points to
the current song, after the song order has been
shuffled */
playlist->current =
queue_position_to_order(&playlist->queue,
current_position);
}
if (next_order >= 0)
next_song = queue_get_order(&playlist->queue, next_order);
else
next_song = NULL;
if (prev != NULL && next_song != prev) {
/* clear the currently queued song */
pc_cancel();
playlist->queued = -1;
}
if (next_order >= 0) {
if (next_song != prev)
playlist_queue_song_order(playlist, next_order);
else
playlist->queued = next_order;
}
}
#ifndef WIN32
enum playlist_result
playlist_append_file(struct playlist *playlist, const char *path, int uid,
unsigned *added_id)
{
int ret;
struct stat st;
struct song *song;
if (uid <= 0)
/* unauthenticated client */
return PLAYLIST_RESULT_DENIED;
ret = stat(path, &st);
if (ret < 0)
return PLAYLIST_RESULT_ERRNO;
if (st.st_uid != (uid_t)uid && (st.st_mode & 0444) != 0444)
/* client is not owner */
return PLAYLIST_RESULT_DENIED;
song = song_file_load(path, NULL);
if (song == NULL)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return addSongToPlaylist(playlist, song, added_id);
}
#endif
static struct song *
song_by_url(const char *url)
{
struct song *song;
song = db_get_song(url);
if (song != NULL)
return song;
if (uri_has_scheme(url))
return song_remote_new(url);
return NULL;
}
enum playlist_result
addToPlaylist(struct playlist *playlist, const char *url, unsigned *added_id)
{
struct song *song;
g_debug("add to playlist: %s", url);
song = song_by_url(url);
if (song == NULL)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return addSongToPlaylist(playlist, song, added_id);
}
enum playlist_result
addSongToPlaylist(struct playlist *playlist,
struct song *song, unsigned *added_id)
{
const struct song *queued;
unsigned id;
if (queue_is_full(&playlist->queue))
return PLAYLIST_RESULT_TOO_LARGE;
queued = playlist_get_queued_song(playlist);
id = queue_append(&playlist->queue, song);
if (playlist->queue.random) {
/* shuffle the new song into the list of remaining
songs to play */
unsigned start;
if (playlist->queued >= 0)
start = playlist->queued + 1;
else
start = playlist->current + 1;
if (start < queue_length(&playlist->queue))
queue_shuffle_order_last(&playlist->queue, start,
queue_length(&playlist->queue));
}
incrPlaylistVersion(playlist);
playlist_update_queued_song(playlist, queued);
if (added_id)
*added_id = id;
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
swapSongsInPlaylist(struct playlist *playlist, unsigned song1, unsigned song2)
{
const struct song *queued;
if (!queue_valid_position(&playlist->queue, song1) ||
!queue_valid_position(&playlist->queue, song2))
return PLAYLIST_RESULT_BAD_RANGE;
queued = playlist_get_queued_song(playlist);
queue_swap(&playlist->queue, song1, song2);
if (playlist->queue.random) {
/* update the queue order, so that playlist->current
still points to the current song order */
queue_swap_order(&playlist->queue,
queue_position_to_order(&playlist->queue,
song1),
queue_position_to_order(&playlist->queue,
song2));
} else {
/* correct the "current" song order */
if (playlist->current == (int)song1)
playlist->current = song2;
else if (playlist->current == (int)song2)
playlist->current = song1;
}
incrPlaylistVersion(playlist);
playlist_update_queued_song(playlist, queued);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
swapSongsInPlaylistById(struct playlist *playlist, unsigned id1, unsigned id2)
{
int song1 = queue_id_to_position(&playlist->queue, id1);
int song2 = queue_id_to_position(&playlist->queue, id2);
if (song1 < 0 || song2 < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return swapSongsInPlaylist(playlist, song1, song2);
}
enum playlist_result
deleteFromPlaylist(struct playlist *playlist, unsigned song)
{
const struct song *queued;
unsigned songOrder;
if (song >= queue_length(&playlist->queue))
return PLAYLIST_RESULT_BAD_RANGE;
queued = playlist_get_queued_song(playlist);
songOrder = queue_position_to_order(&playlist->queue, song);
if (playlist->playing && playlist->current == (int)songOrder) {
bool paused = getPlayerState() == PLAYER_STATE_PAUSE;
/* the current song is going to be deleted: stop the player */
playerWait();
playlist->playing = false;
/* see which song is going to be played instead */
playlist->current = queue_next_order(&playlist->queue,
playlist->current);
if (playlist->current == (int)songOrder)
playlist->current = -1;
if (playlist->current >= 0 && !paused)
/* play the song after the deleted one */
playPlaylistOrderNumber(playlist, playlist->current);
else
/* no songs left to play, stop playback
completely */
stopPlaylist(playlist);
queued = NULL;
}
/* now do it: remove the song */
if (!song_in_database(queue_get(&playlist->queue, song)))
pc_song_deleted(queue_get(&playlist->queue, song));
queue_delete(&playlist->queue, song);
incrPlaylistVersion(playlist);
/* update the "current" and "queued" variables */
if (playlist->current > (int)songOrder) {
playlist->current--;
}
playlist_update_queued_song(playlist, queued);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
deleteFromPlaylistById(struct playlist *playlist, unsigned id)
{
int song = queue_id_to_position(&playlist->queue, id);
if (song < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return deleteFromPlaylist(playlist, song);
}
void
deleteASongFromPlaylist(struct playlist *playlist, const struct song *song)
{
for (int i = queue_length(&playlist->queue) - 1; i >= 0; --i)
if (song == queue_get(&playlist->queue, i))
deleteFromPlaylist(playlist, i);
pc_song_deleted(song);
}
void stopPlaylist(struct playlist *playlist)
{
if (!playlist->playing)
return;
assert(playlist->current >= 0);
g_debug("playlist: stop");
playerWait();
playlist->queued = -1;
playlist->playing = false;
if (playlist->queue.random) {
/* shuffle the playlist, so the next playback will
result in a new random order */
unsigned current_position =
queue_order_to_position(&playlist->queue,
playlist->current);
queue_shuffle_order(&playlist->queue);
/* make sure that "current" stays valid, and the next
"play" command plays the same song again */
playlist->current =
queue_position_to_order(&playlist->queue,
current_position);
}
}
static void
playPlaylistOrderNumber(struct playlist *playlist, int orderNum)
{
struct song *song;
char *uri;
playlist->playing = true;
playlist->queued = -1;
song = queue_get_order(&playlist->queue, orderNum);
uri = song_get_uri(song);
g_debug("playlist: play %i:\"%s\"", orderNum, uri);
g_free(uri);
playerPlay(song);
playlist->current = orderNum;
}
enum playlist_result playPlaylist(struct playlist *playlist, int song)
{
unsigned i = song;
clearPlayerError();
if (song == -1) {
/* play any song ("current" song, or the first song */
if (queue_is_empty(&playlist->queue))
return PLAYLIST_RESULT_SUCCESS;
if (playlist->playing) {
/* already playing: unpause playback, just in
case it was paused, and return */
playerSetPause(0);
return PLAYLIST_RESULT_SUCCESS;
}
/* select a song: "current" song, or the first one */
i = playlist->current >= 0
? playlist->current
: 0;
} else if (!queue_valid_position(&playlist->queue, song))
return PLAYLIST_RESULT_BAD_RANGE;
if (playlist->queue.random) {
if (song >= 0)
/* "i" is currently the song position (which
would be equal to the order number in
no-random mode); convert it to a order
number, because random mode is enabled */
i = queue_position_to_order(&playlist->queue, song);
if (!playlist->playing)
playlist->current = 0;
/* swap the new song with the previous "current" one,
so playback continues as planned */
queue_swap_order(&playlist->queue,
i, playlist->current);
i = playlist->current;
}
playlist->stop_on_error = false;
playlist->error_count = 0;
playPlaylistOrderNumber(playlist, i);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
playPlaylistById(struct playlist *playlist, int id)
{
int song;
if (id == -1) {
return playPlaylist(playlist, id);
}
song = queue_id_to_position(&playlist->queue, id);
if (song < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return playPlaylist(playlist, song);
}
static void
playPlaylistIfPlayerStopped(struct playlist *playlist);
/**
* This is the "PLAYLIST" event handler. It is invoked by the player
* thread whenever it requests a new queued song, or when it exits.
*/
void syncPlayerAndPlaylist(struct playlist *playlist)
{
if (!playlist->playing)
/* this event has reached us out of sync: we aren't
playing anymore; ignore the event */
return;
if (getPlayerState() == PLAYER_STATE_STOP)
/* the player thread has stopped: check if playback
should be restarted with the next song. That can
happen if the playlist isn't filling the queue fast
enough */
playPlaylistIfPlayerStopped(playlist);
else {
/* check if the player thread has already started
playing the queued song */
syncPlaylistWithQueue(playlist);
/* make sure the queued song is always set (if
possible) */
if (pc.next_song == NULL)
playlist_update_queued_song(playlist, NULL);
}
}
void
nextSongInPlaylist(struct playlist *playlist)
{
int next_order;
if (!playlist->playing)
return;
assert(!queue_is_empty(&playlist->queue));
assert(queue_valid_order(&playlist->queue, playlist->current));
playlist->stop_on_error = false;
/* determine the next song from the queue's order list */
next_order = queue_next_order(&playlist->queue, playlist->current);
if (next_order < 0) {
/* no song after this one: stop playback */
stopPlaylist(playlist);
return;
}
if (next_order == 0 && playlist->queue.random) {
/* The queue told us that the next song is the first
song. This means we are in repeat mode. Shuffle
the queue order, so this time, the user hears the
songs in a different than before */
assert(playlist->queue.repeat);
queue_shuffle_order(&playlist->queue);
/* note that playlist->current and playlist->queued are
now invalid, but playPlaylistOrderNumber() will
discard them anyway */
}
playPlaylistOrderNumber(playlist, next_order);
}
/**
* The player has stopped for some reason. Check the error, and
* decide whether to re-start playback
*/
static void
playPlaylistIfPlayerStopped(struct playlist *playlist)
{
enum player_error error;
assert(playlist->playing);
assert(getPlayerState() == PLAYER_STATE_STOP);
error = getPlayerError();
if (error == PLAYER_ERROR_NOERROR)
playlist->error_count = 0;
else
++playlist->error_count;
if ((playlist->stop_on_error && error != PLAYER_ERROR_NOERROR) ||
error == PLAYER_ERROR_AUDIO || error == PLAYER_ERROR_SYSTEM ||
playlist->error_count >= queue_length(&playlist->queue))
/* too many errors, or critical error: stop
playback */
stopPlaylist(playlist);
else
/* continue playback at the next song */
nextSongInPlaylist(playlist);
}
bool
getPlaylistRepeatStatus(struct playlist *playlist)
{
return playlist->queue.repeat;
}
bool
getPlaylistRandomStatus(struct playlist *playlist)
{
return playlist->queue.random;
}
void setPlaylistRepeatStatus(struct playlist *playlist, bool status)
{
if (status == playlist->queue.repeat)
return;
playlist->queue.repeat = status;
/* if the last song is currently being played, the "next song"
might change when repeat mode is toggled */
playlist_update_queued_song(playlist,
playlist_get_queued_song(playlist));
idle_add(IDLE_OPTIONS);
}
enum playlist_result
moveSongInPlaylist(struct playlist *playlist, unsigned from, int to)
{
const struct song *queued;
int currentSong;
if (!queue_valid_position(&playlist->queue, from))
return PLAYLIST_RESULT_BAD_RANGE;
if ((to >= 0 && to >= (int)queue_length(&playlist->queue)) ||
(to < 0 && abs(to) > (int)queue_length(&playlist->queue)))
return PLAYLIST_RESULT_BAD_RANGE;
if ((int)from == to) /* no-op */
return PLAYLIST_RESULT_SUCCESS;
queued = playlist_get_queued_song(playlist);
/*
* (to < 0) => move to offset from current song
* (-playlist.length == to) => move to position BEFORE current song
*/
currentSong = playlist->current >= 0
? (int)queue_order_to_position(&playlist->queue,
playlist->current)
: -1;
if (to < 0 && playlist->current >= 0) {
if ((unsigned)currentSong == from)
/* no-op, can't be moved to offset of itself */
return PLAYLIST_RESULT_SUCCESS;
to = (currentSong + abs(to)) % queue_length(&playlist->queue);
}
queue_move(&playlist->queue, from, to);
if (!playlist->queue.random) {
/* update current/queued */
if (playlist->current == (int)from)
playlist->current = to;
else if (playlist->current > (int)from &&
playlist->current <= to) {
playlist->current--;
} else if (playlist->current >= to &&
playlist->current < (int)from) {
playlist->current++;
}
}
incrPlaylistVersion(playlist);
playlist_update_queued_song(playlist, queued);
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
moveSongInPlaylistById(struct playlist *playlist, unsigned id1, int to)
{
int song = queue_id_to_position(&playlist->queue, id1);
if (song < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return moveSongInPlaylist(playlist, song, to);
}
static void orderPlaylist(struct playlist *playlist)
{
if (playlist->current >= 0)
/* update playlist.current, order==position now */
playlist->current = queue_order_to_position(&playlist->queue,
playlist->current);
queue_restore_order(&playlist->queue);
}
void setPlaylistRandomStatus(struct playlist *playlist, bool status)
{
const struct song *queued;
if (status == playlist->queue.random)
return;
queued = playlist_get_queued_song(playlist);
playlist->queue.random = status;
if (playlist->queue.random) {
/* shuffle the queue order, but preserve
playlist->current */
int current_position = playlist->current >= 0
? (int)queue_order_to_position(&playlist->queue,
playlist->current)
: -1;
queue_shuffle_order(&playlist->queue);
if (current_position >= 0) {
/* make sure the current song is the first in
the order list, so the whole rest of the
playlist is played after that */
unsigned current_order =
queue_position_to_order(&playlist->queue,
current_position);
queue_swap_order(&playlist->queue, 0, current_order);
playlist->current = 0;
}
} else
orderPlaylist(playlist);
playlist_update_queued_song(playlist, queued);
idle_add(IDLE_OPTIONS);
}
void previousSongInPlaylist(struct playlist *playlist)
{
static time_t lastTime;
time_t diff = time(NULL) - lastTime;
lastTime += diff;
if (!playlist->playing)
return;
if (diff && getPlayerElapsedTime() > PLAYLIST_PREV_UNLESS_ELAPSED) {
/* re-start playing the current song (just like the
"prev" button on CD players) */
playPlaylistOrderNumber(playlist, playlist->current);
} else {
if (playlist->current > 0) {
/* play the preceding song */
playPlaylistOrderNumber(playlist,
playlist->current - 1);
} else if (playlist->queue.repeat) {
/* play the last song in "repeat" mode */
playPlaylistOrderNumber(playlist,
queue_length(&playlist->queue) - 1);
} else {
/* re-start playing the current song if it's
the first one */
playPlaylistOrderNumber(playlist, playlist->current);
}
}
}
void shufflePlaylist(struct playlist *playlist)
{
const struct song *queued;
unsigned i;
if (queue_length(&playlist->queue) <= 1)
return;
queued = playlist_get_queued_song(playlist);
if (playlist->playing) {
if (playlist->current >= 0)
/* put current playing song first */
queue_swap(&playlist->queue, 0,
queue_order_to_position(&playlist->queue,
playlist->current));
if (playlist->queue.random) {
playlist->current =
queue_position_to_order(&playlist->queue, 0);
} else
playlist->current = 0;
/* start shuffle after the current song */
i = 1;
} else {
/* no playback currently: shuffle everything, and
reset playlist->current */
i = 0;
playlist->current = -1;
}
/* shuffle the rest of the list */
queue_shuffle_range(&playlist->queue, i,
queue_length(&playlist->queue));
incrPlaylistVersion(playlist);
playlist_update_queued_song(playlist, queued);
}
int getPlaylistCurrentSong(struct playlist *playlist)
{
if (playlist->current >= 0)
return queue_order_to_position(&playlist->queue,
playlist->current);
return -1;
}
unsigned long
getPlaylistVersion(struct playlist *playlist)
{
return playlist->queue.version;
}
int
getPlaylistLength(struct playlist *playlist)
{
return queue_length(&playlist->queue);
}
enum playlist_result
seekSongInPlaylist(struct playlist *playlist, unsigned song, float seek_time)
{
const struct song *queued;
unsigned i;
int ret;
if (!queue_valid_position(&playlist->queue, song))
return PLAYLIST_RESULT_BAD_RANGE;
queued = playlist_get_queued_song(playlist);
if (playlist->queue.random)
i = queue_position_to_order(&playlist->queue, song);
else
i = song;
clearPlayerError();
playlist->stop_on_error = true;
playlist->error_count = 0;
if (!playlist->playing || (unsigned)playlist->current != i) {
/* seeking is not within the current song - first
start playing the new song */
playPlaylistOrderNumber(playlist, i);
queued = NULL;
}
playlist_update_queued_song(playlist, queued);
ret = playerSeek(queue_get_order(&playlist->queue, i), seek_time);
if (ret < 0)
return PLAYLIST_RESULT_NOT_PLAYING;
return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
seekSongInPlaylistById(struct playlist *playlist, unsigned id, float seek_time)
{
int song = queue_id_to_position(&playlist->queue, id);
if (song < 0)
return PLAYLIST_RESULT_NO_SUCH_SONG;
return seekSongInPlaylist(playlist, song, seek_time);
}
unsigned
getPlaylistSongId(struct playlist *playlist, unsigned song)
{
return queue_position_to_id(&playlist->queue, song);
}
|