reshuffle only songs in the UpNext queue

This commit is contained in:
chme 2014-07-13 09:59:37 +02:00
parent b195e76f11
commit 9527e06f71

View File

@ -655,6 +655,8 @@ player_queue_make(struct query_params *qp, const char *sort)
q_head->pl_prev = q_tail; q_head->pl_prev = q_tail;
q_tail->pl_next = q_head; q_tail->pl_next = q_head;
q_head->shuffle_prev = q_tail;
q_tail->shuffle_next = q_head;
return q_head; return q_head;
} }
@ -971,8 +973,11 @@ source_stop(struct player_source *ps)
} }
} }
/*
* Shuffles the items between head and tail (excluding head and tail)
*/
static struct player_source * static struct player_source *
source_shuffle(struct player_source *head) source_shuffle(struct player_source *head, struct player_source *tail)
{ {
struct player_source *ps; struct player_source *ps;
struct player_source **ps_array; struct player_source **ps_array;
@ -982,15 +987,31 @@ source_shuffle(struct player_source *head)
if (!head) if (!head)
return NULL; return NULL;
ps = head; if (!tail)
return NULL;
if (!shuffle)
{
ps = head;
do
{
ps->shuffle_next = ps->pl_next;
ps->shuffle_prev = ps->pl_prev;
ps = ps->pl_next;
}
while (ps != head);
}
// Count items in queue (excluding head and tail)
ps = head->shuffle_next;
nitems = 0; nitems = 0;
do while (ps != tail)
{ {
nitems++; nitems++;
ps = ps->pl_next; ps = ps->shuffle_next;
} }
while (ps != head);
// Construct array for number of items in queue
ps_array = (struct player_source **)malloc(nitems * sizeof(struct player_source *)); ps_array = (struct player_source **)malloc(nitems * sizeof(struct player_source *));
if (!ps_array) if (!ps_array)
{ {
@ -998,16 +1019,17 @@ source_shuffle(struct player_source *head)
return NULL; return NULL;
} }
ps = head; // Fill array with items in queue (excluding head and tail)
ps = head->shuffle_next;
i = 0; i = 0;
do do
{ {
ps_array[i] = ps; ps_array[i] = ps;
ps = ps->pl_next; ps = ps->shuffle_next;
i++; i++;
} }
while (ps != head); while (ps != tail);
shuffle_ptr(&shuffle_rng, (void **)ps_array, nitems); shuffle_ptr(&shuffle_rng, (void **)ps_array, nitems);
@ -1022,8 +1044,10 @@ source_shuffle(struct player_source *head)
ps->shuffle_next = ps_array[i + 1]; ps->shuffle_next = ps_array[i + 1];
} }
ps_array[0]->shuffle_prev = ps_array[nitems - 1]; ps_array[0]->shuffle_prev = head;
ps_array[nitems - 1]->shuffle_next = ps_array[0]; ps_array[nitems - 1]->shuffle_next = tail;
head->shuffle_next = ps_array[0];
tail->shuffle_prev = ps_array[nitems - 1];
ps = ps_array[0]; ps = ps_array[0];
@ -1036,14 +1060,27 @@ static void
source_reshuffle(void) source_reshuffle(void)
{ {
struct player_source *ps; struct player_source *ps;
struct player_source *head;
struct player_source *tail;
if (cur_streaming)
head = cur_streaming;
else
head = source_head;
if (repeat == REPEAT_ALL)
tail = head;
else if (shuffle)
tail = shuffle_head;
else
tail = source_head;
ps = source_shuffle(head, tail);
ps = source_shuffle(source_head);
if (!ps) if (!ps)
return; return;
if (cur_streaming) if (repeat == REPEAT_ALL)
shuffle_head = cur_streaming;
else
shuffle_head = ps; shuffle_head = ps;
} }
@ -3378,10 +3415,7 @@ queue_add(struct player_command *cmd)
struct player_source *ps_tail; struct player_source *ps_tail;
ps = cmd->arg.ps; ps = cmd->arg.ps;
ps_shuffle = ps;
ps_shuffle = source_shuffle(ps);
if (!ps_shuffle)
ps_shuffle = ps;
if (source_head) if (source_head)
{ {
@ -3411,6 +3445,9 @@ queue_add(struct player_command *cmd)
shuffle_head = ps_shuffle; shuffle_head = ps_shuffle;
} }
if (shuffle)
source_reshuffle();
if (cur_plid != 0) if (cur_plid != 0)
cur_plid = 0; cur_plid = 0;
@ -3425,10 +3462,7 @@ queue_add_next(struct player_command *cmd)
struct player_source *ps_playing; struct player_source *ps_playing;
ps = cmd->arg.ps; ps = cmd->arg.ps;
ps_shuffle = ps;
ps_shuffle = source_shuffle(ps);
if (!ps_shuffle)
ps_shuffle = ps;
if (source_head && cur_streaming) if (source_head && cur_streaming)
{ {
@ -3452,6 +3486,9 @@ queue_add_next(struct player_command *cmd)
shuffle_head = ps_shuffle; shuffle_head = ps_shuffle;
} }
if (shuffle)
source_reshuffle();
if (cur_plid != 0) if (cur_plid != 0)
cur_plid = 0; cur_plid = 0;