summaryrefslogtreecommitdiff
path: root/apps/playlist.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/playlist.c')
-rw-r--r--apps/playlist.c84
1 files changed, 70 insertions, 14 deletions
diff --git a/apps/playlist.c b/apps/playlist.c
index 8c9d6d11c7..ce2818eded 100644
--- a/apps/playlist.c
+++ b/apps/playlist.c
@@ -149,7 +149,8 @@ static int randomise_playlist(struct playlist_info* playlist,
149 bool write); 149 bool write);
150static int sort_playlist(struct playlist_info* playlist, bool start_current, 150static int sort_playlist(struct playlist_info* playlist, bool start_current,
151 bool write); 151 bool write);
152static int get_next_index(const struct playlist_info* playlist, int steps); 152static int get_next_index(struct playlist_info* playlist, int steps,
153 int repeat_mode);
153static void find_and_set_playlist_index(struct playlist_info* playlist, 154static void find_and_set_playlist_index(struct playlist_info* playlist,
154 unsigned int seek); 155 unsigned int seek);
155static int compare(const void* p1, const void* p2); 156static int compare(const void* p1, const void* p2);
@@ -399,13 +400,15 @@ static int add_indices_to_playlist(struct playlist_info* playlist,
399/* 400/*
400 * Add track to playlist at specified position. There are three special 401 * Add track to playlist at specified position. There are three special
401 * positions that can be specified: 402 * positions that can be specified:
402 * PLAYLIST_PREPEND - Add track at beginning of playlist 403 * PLAYLIST_PREPEND - Add track at beginning of playlist
403 * PLAYLIST_INSERT - Add track after current song. NOTE: If there 404 * PLAYLIST_INSERT - Add track after current song. NOTE: If
404 * are already inserted tracks then track is added 405 * there are already inserted tracks then track
405 * to the end of the insertion list. 406 * is added to the end of the insertion list
406 * PLAYLIST_INSERT_FIRST - Add track immediately after current song, no 407 * PLAYLIST_INSERT_FIRST - Add track immediately after current song, no
407 * matter what other tracks have been inserted. 408 * matter what other tracks have been inserted
408 * PLAYLIST_INSERT_LAST - Add track to end of playlist 409 * PLAYLIST_INSERT_LAST - Add track to end of playlist
410 * PLAYLIST_INSERT_SHUFFLED - Add track at some random point between the
411 * current playing track and end of playlist
409 */ 412 */
410static int add_track_to_playlist(struct playlist_info* playlist, 413static int add_track_to_playlist(struct playlist_info* playlist,
411 const char *filename, int position, 414 const char *filename, int position,
@@ -459,6 +462,24 @@ static int add_track_to_playlist(struct playlist_info* playlist,
459 462
460 flags = PLAYLIST_INSERT_TYPE_APPEND; 463 flags = PLAYLIST_INSERT_TYPE_APPEND;
461 break; 464 break;
465 case PLAYLIST_INSERT_SHUFFLED:
466 {
467 int offset;
468 int n = playlist->amount -
469 rotate_index(playlist, playlist->index);
470
471 if (n > 0)
472 offset = rand() % n;
473 else
474 offset = 0;
475
476 position = playlist->index + offset + 1;
477 if (position >= playlist->amount)
478 position -= playlist->amount;
479
480 insert_position = position;
481 break;
482 }
462 } 483 }
463 484
464 if (queue) 485 if (queue)
@@ -807,7 +828,8 @@ static int sort_playlist(struct playlist_info* playlist, bool start_current,
807 * returns the index of the track that is "steps" away from current playing 828 * returns the index of the track that is "steps" away from current playing
808 * track. 829 * track.
809 */ 830 */
810static int get_next_index(const struct playlist_info* playlist, int steps) 831static int get_next_index(struct playlist_info* playlist, int steps,
832 int repeat_mode)
811{ 833{
812 int current_index = playlist->index; 834 int current_index = playlist->index;
813 int next_index = -1; 835 int next_index = -1;
@@ -815,8 +837,18 @@ static int get_next_index(const struct playlist_info* playlist, int steps)
815 if (playlist->amount <= 0) 837 if (playlist->amount <= 0)
816 return -1; 838 return -1;
817 839
818 switch (global_settings.repeat_mode) 840 if (repeat_mode == -1)
841 repeat_mode = global_settings.repeat_mode;
842
843 if (repeat_mode == REPEAT_SHUFFLE &&
844 (!global_settings.playlist_shuffle || playlist->amount <= 1))
845 repeat_mode = REPEAT_ALL;
846
847 switch (repeat_mode)
819 { 848 {
849 case REPEAT_SHUFFLE:
850 /* Treat repeat shuffle just like repeat off. At end of playlist,
851 play will be resumed in playlist_next() */
820 case REPEAT_OFF: 852 case REPEAT_OFF:
821 { 853 {
822 current_index = rotate_index(playlist, current_index); 854 current_index = rotate_index(playlist, current_index);
@@ -1634,7 +1666,13 @@ int playlist_start(int start_index, int offset)
1634bool playlist_check(int steps) 1666bool playlist_check(int steps)
1635{ 1667{
1636 struct playlist_info* playlist = &current_playlist; 1668 struct playlist_info* playlist = &current_playlist;
1637 int index = get_next_index(playlist, steps); 1669 int index = get_next_index(playlist, steps, -1);
1670
1671 if (index < 0 && steps >= 0 &&
1672 global_settings.repeat_mode == REPEAT_SHUFFLE)
1673 /* shuffle repeat is the same as repeat all for check purposes */
1674 index = get_next_index(playlist, steps, REPEAT_ALL);
1675
1638 return (index >= 0); 1676 return (index >= 0);
1639} 1677}
1640 1678
@@ -1649,7 +1687,7 @@ char* playlist_peek(int steps)
1649 int index; 1687 int index;
1650 bool control_file; 1688 bool control_file;
1651 1689
1652 index = get_next_index(playlist, steps); 1690 index = get_next_index(playlist, steps, -1);
1653 if (index < 0) 1691 if (index < 0)
1654 return NULL; 1692 return NULL;
1655 1693
@@ -1705,7 +1743,7 @@ int playlist_next(int steps)
1705 /* We need to delete all the queued songs */ 1743 /* We need to delete all the queued songs */
1706 for (i=0, j=steps; i<j; i++) 1744 for (i=0, j=steps; i<j; i++)
1707 { 1745 {
1708 index = get_next_index(playlist, i); 1746 index = get_next_index(playlist, i, -1);
1709 1747
1710 if (playlist->indices[index] & PLAYLIST_QUEUE_MASK) 1748 if (playlist->indices[index] & PLAYLIST_QUEUE_MASK)
1711 { 1749 {
@@ -1715,7 +1753,25 @@ int playlist_next(int steps)
1715 } 1753 }
1716 } 1754 }
1717 1755
1718 index = get_next_index(playlist, steps); 1756 index = get_next_index(playlist, steps, -1);
1757
1758 if (index < 0)
1759 {
1760 /* end of playlist... or is it */
1761 if (global_settings.repeat_mode == REPEAT_SHUFFLE &&
1762 global_settings.playlist_shuffle &&
1763 playlist->amount > 1)
1764 {
1765 /* Repeat shuffle mode. Re-shuffle playlist and resume play */
1766 playlist->first_index = global_settings.resume_first_index = 0;
1767 sort_playlist(playlist, false, false);
1768 randomise_playlist(playlist, current_tick, false, true);
1769 playlist_start(0, 0);
1770 }
1771
1772 return index;
1773 }
1774
1719 playlist->index = index; 1775 playlist->index = index;
1720 1776
1721 if (playlist->last_insert_pos >= 0 && steps > 0) 1777 if (playlist->last_insert_pos >= 0 && steps > 0)