2009-01-22 23:40:11 +01:00
|
|
|
/*
|
2013-01-04 20:50:00 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-01-22 23:40:11 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-01-22 23:40:11 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-04 20:50:00 +01:00
|
|
|
#include "Queue.hxx"
|
2009-01-22 23:40:11 +01:00
|
|
|
#include "song.h"
|
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::queue(unsigned _max_length)
|
|
|
|
:max_length(_max_length), length(0),
|
|
|
|
version(1),
|
|
|
|
items(g_new(struct queue_item, max_length)),
|
|
|
|
order((unsigned *)g_malloc(sizeof(order[0]) * max_length)),
|
|
|
|
id_to_position((int *)g_malloc(sizeof(id_to_position[0]) *
|
|
|
|
max_length * QUEUE_HASH_MULT)),
|
|
|
|
repeat(false),
|
|
|
|
single(false),
|
|
|
|
consume(false),
|
|
|
|
random(false),
|
|
|
|
rand(g_rand_new())
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i)
|
|
|
|
id_to_position[i] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue::~queue()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
g_free(items);
|
|
|
|
g_free(order);
|
|
|
|
g_free(id_to_position);
|
|
|
|
|
|
|
|
g_rand_free(rand);
|
|
|
|
}
|
|
|
|
|
2009-03-29 19:58:24 +02:00
|
|
|
/**
|
|
|
|
* Generate a non-existing id number.
|
|
|
|
*/
|
|
|
|
static unsigned
|
2009-01-22 23:40:11 +01:00
|
|
|
queue_generate_id(const struct queue *queue)
|
|
|
|
{
|
|
|
|
static unsigned cur = (unsigned)-1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
cur++;
|
|
|
|
|
|
|
|
if (cur >= queue->max_length * QUEUE_HASH_MULT)
|
|
|
|
cur = 0;
|
2009-10-13 16:43:06 +02:00
|
|
|
} while (queue->id_to_position[cur] != -1);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::GetNextOrder(unsigned _order) const
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(_order < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (single && repeat && !consume)
|
|
|
|
return _order;
|
|
|
|
else if (_order + 1 < length)
|
|
|
|
return _order + 1;
|
|
|
|
else if (repeat && (_order > 0 || !consume))
|
2009-01-22 23:40:11 +01:00
|
|
|
/* restart at first song */
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
/* end of queue */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::IncrementVersion()
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
|
|
|
static unsigned long max = ((uint32_t) 1 << 31) - 1;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
version++;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (version >= max) {
|
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
items[i].version = 0;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
version = 1;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ModifyAtOrder(unsigned _order)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(_order < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned position = order[_order];
|
|
|
|
items[position].version = version;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
IncrementVersion();
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ModifyAll()
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
items[i].version = version;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
IncrementVersion();
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::Append(struct song *song, uint8_t priority)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned id = queue_generate_id(this);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(!IsFull());
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
auto &item = items[length];
|
2013-01-04 20:50:00 +01:00
|
|
|
item.song = song_dup_detached(song);
|
|
|
|
item.id = id;
|
2013-01-06 21:33:58 +01:00
|
|
|
item.version = version;
|
2013-01-04 20:50:00 +01:00
|
|
|
item.priority = priority;
|
2009-01-23 00:06:38 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
order[length] = length;
|
|
|
|
id_to_position[id] = length;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
++length;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::SwapPositions(unsigned position1, unsigned position2)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2009-01-23 00:06:38 +01:00
|
|
|
struct queue_item tmp;
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned id1 = items[position1].id;
|
|
|
|
unsigned id2 = items[position2].id;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
tmp = items[position1];
|
|
|
|
items[position1] = items[position2];
|
|
|
|
items[position2] = tmp;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
items[position1].version = version;
|
|
|
|
items[position2].version = version;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
id_to_position[id1] = position2;
|
|
|
|
id_to_position[id2] = position1;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_move_song_to(struct queue *queue, unsigned from, unsigned to)
|
|
|
|
{
|
2009-01-23 00:06:38 +01:00
|
|
|
unsigned from_id = queue->items[from].id;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2009-01-23 00:06:38 +01:00
|
|
|
queue->items[to] = queue->items[from];
|
2009-01-23 16:17:16 +01:00
|
|
|
queue->items[to].version = queue->version;
|
2009-10-13 16:43:06 +02:00
|
|
|
queue->id_to_position[from_id] = to;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::MovePostion(unsigned from, unsigned to)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
struct queue_item item = items[from];
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* move songs to one less in from->to */
|
|
|
|
|
|
|
|
for (unsigned i = from; i < to; i++)
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_move_song_to(this, i + 1, i);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* move songs to one more in to->from */
|
|
|
|
|
|
|
|
for (unsigned i = from; i > to; i--)
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_move_song_to(this, i - 1, i);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* put song at _to_ */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
id_to_position[item.id] = to;
|
|
|
|
items[to] = item;
|
|
|
|
items[to].version = version;
|
2009-02-09 22:47:23 +01:00
|
|
|
|
|
|
|
/* now deal with order */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (random) {
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
if (order[i] > from && order[i] <= to)
|
|
|
|
order[i]--;
|
|
|
|
else if (order[i] < from &&
|
|
|
|
order[i] >= to)
|
|
|
|
order[i]++;
|
|
|
|
else if (from == order[i])
|
|
|
|
order[i] = to;
|
2009-02-09 22:47:23 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2009-03-26 22:02:56 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::MoveRange(unsigned start, unsigned end, unsigned to)
|
2009-03-26 22:02:56 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
struct queue_item tmp[end - start];
|
2009-03-26 22:02:56 +01:00
|
|
|
// Copy the original block [start,end-1]
|
|
|
|
for (unsigned i = start; i < end; i++)
|
2013-01-06 21:33:58 +01:00
|
|
|
tmp[i - start] = items[i];
|
2009-03-26 22:02:56 +01:00
|
|
|
|
|
|
|
// If to > start, we need to move to-start items to start, starting from end
|
|
|
|
for (unsigned i = end; i < end + to - start; i++)
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_move_song_to(this, i, start + i - end);
|
2009-03-26 22:02:56 +01:00
|
|
|
|
|
|
|
// If to < start, we need to move start-to items to newend (= end + to - start), starting from to
|
|
|
|
// This is the same as moving items from start-1 to to (decreasing), with start-1 going to end-1
|
|
|
|
// We have to iterate in this order to avoid writing over something we haven't yet moved
|
|
|
|
for (unsigned i = start - 1; i >= to && i != G_MAXUINT; i--)
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_move_song_to(this, i, i + end - start);
|
2009-03-26 22:02:56 +01:00
|
|
|
|
|
|
|
// Copy the original block back in, starting at to.
|
|
|
|
for (unsigned i = start; i< end; i++)
|
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
id_to_position[tmp[i-start].id] = to + i - start;
|
|
|
|
items[to + i - start] = tmp[i-start];
|
|
|
|
items[to + i - start].version = version;
|
2009-03-26 22:02:56 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (random) {
|
2009-03-26 22:02:56 +01:00
|
|
|
// Update the positions in the queue.
|
|
|
|
// Note that the ranges for these cases are the same as the ranges of
|
|
|
|
// the loops above.
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
if (order[i] >= end && order[i] < to + end - start)
|
|
|
|
order[i] -= end - start;
|
|
|
|
else if (order[i] < start &&
|
|
|
|
order[i] >= to)
|
|
|
|
order[i] += end - start;
|
|
|
|
else if (start <= order[i] && order[i] < end)
|
|
|
|
order[i] += to - start;
|
2009-03-26 22:02:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
/**
|
|
|
|
* Moves a song to a new position in the "order" list.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
queue_move_order(struct queue *queue, unsigned from_order, unsigned to_order)
|
|
|
|
{
|
|
|
|
assert(queue != NULL);
|
|
|
|
assert(from_order < queue->length);
|
|
|
|
assert(to_order <= queue->length);
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
const unsigned from_position = queue->OrderToPosition(from_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
if (from_order < to_order) {
|
|
|
|
for (unsigned i = from_order; i < to_order; ++i)
|
|
|
|
queue->order[i] = queue->order[i + 1];
|
|
|
|
} else {
|
|
|
|
for (unsigned i = from_order; i > to_order; --i)
|
|
|
|
queue->order[i] = queue->order[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
queue->order[to_order] = from_position;
|
|
|
|
}
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::DeletePosition(unsigned position)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(position < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
struct song *song = Get(position);
|
2012-08-15 19:52:32 +02:00
|
|
|
assert(!song_in_database(song) || song_is_detached(song));
|
|
|
|
song_free(song);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
const unsigned id = PositionToId(position);
|
|
|
|
const unsigned _order = PositionToOrder(position);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
--length;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* release the song id */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
id_to_position[id] = -1;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* delete song from songs array */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = position; i < length; i++)
|
|
|
|
queue_move_song_to(this, i + 1, i);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* delete the entry from the order array */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = _order; i < length; i++)
|
|
|
|
order[i] = order[i + 1];
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/* readjust values in the order array */
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++)
|
|
|
|
if (order[i] > position)
|
|
|
|
--order[i];
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::Clear()
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
struct queue_item *item = &items[i];
|
2009-01-23 00:06:38 +01:00
|
|
|
|
2012-08-15 19:52:32 +02:00
|
|
|
assert(!song_in_database(item->song) ||
|
|
|
|
song_is_detached(item->song));
|
|
|
|
song_free(item->song);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
id_to_position[item->id] = -1;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
length = 0;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
static const struct queue_item *
|
|
|
|
queue_get_order_item_const(const struct queue *queue, unsigned order)
|
|
|
|
{
|
|
|
|
assert(queue != NULL);
|
|
|
|
assert(order < queue->length);
|
|
|
|
|
|
|
|
return &queue->items[queue->order[order]];
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
queue_get_order_priority(const struct queue *queue, unsigned order)
|
|
|
|
{
|
|
|
|
return queue_get_order_item_const(queue, order)->priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
queue_item_compare_order_priority(gconstpointer av, gconstpointer bv,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2013-01-04 20:50:00 +01:00
|
|
|
const struct queue *queue = (const struct queue *)user_data;
|
|
|
|
const unsigned *const ap = (const unsigned *)av;
|
|
|
|
const unsigned *const bp = (const unsigned *)bv;
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(ap >= queue->order && ap < queue->order + queue->length);
|
|
|
|
assert(bp >= queue->order && bp < queue->order + queue->length);
|
|
|
|
uint8_t a = queue->items[*ap].priority;
|
|
|
|
uint8_t b = queue->items[*bp].priority;
|
|
|
|
|
|
|
|
if (G_LIKELY(a == b))
|
|
|
|
return 0;
|
|
|
|
else if (a > b)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_sort_order_by_priority(struct queue *queue, unsigned start, unsigned end)
|
2009-01-23 00:08:40 +01:00
|
|
|
{
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(queue != NULL);
|
2009-01-23 00:08:40 +01:00
|
|
|
assert(queue->random);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(start <= end);
|
|
|
|
assert(end <= queue->length);
|
2009-01-23 00:08:40 +01:00
|
|
|
|
2011-07-19 00:34:33 +02:00
|
|
|
g_qsort_with_data(&queue->order[start], end - start,
|
|
|
|
sizeof(queue->order[0]),
|
|
|
|
queue_item_compare_order_priority,
|
|
|
|
queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffle the order of items in the specified range, ignoring their
|
|
|
|
* priorities.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
queue_shuffle_order_range(struct queue *queue, unsigned start, unsigned end)
|
|
|
|
{
|
|
|
|
assert(queue != NULL);
|
|
|
|
assert(queue->random);
|
|
|
|
assert(start <= end);
|
|
|
|
assert(end <= queue->length);
|
|
|
|
|
|
|
|
for (unsigned i = start; i < end; ++i)
|
2013-01-06 21:33:58 +01:00
|
|
|
queue->SwapOrders(i, g_rand_int_range(queue->rand, i, end));
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort the "order" of items by priority, and then shuffle each
|
|
|
|
* priority group.
|
|
|
|
*/
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleOrderRangeWithPriority(unsigned start, unsigned end)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(random);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(start <= end);
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(end <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
if (start == end)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* first group the range by priority */
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_sort_order_by_priority(this, start, end);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/* now shuffle each priority group */
|
|
|
|
unsigned group_start = start;
|
2013-01-06 21:33:58 +01:00
|
|
|
uint8_t group_priority = queue_get_order_priority(this, start);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
for (unsigned i = start + 1; i < end; ++i) {
|
2013-01-06 21:33:58 +01:00
|
|
|
uint8_t priority = queue_get_order_priority(this, i);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(priority <= group_priority);
|
|
|
|
|
|
|
|
if (priority != group_priority) {
|
|
|
|
/* start of a new group - shuffle the one that
|
|
|
|
has just ended */
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_shuffle_order_range(this, group_start, i);
|
2011-07-19 00:34:33 +02:00
|
|
|
group_start = i;
|
|
|
|
group_priority = priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shuffle the last group */
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_shuffle_order_range(this, group_start, end);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleOrder()
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
ShuffleOrderRangeWithPriority(0, length);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_shuffle_order_first(struct queue *queue, unsigned start, unsigned end)
|
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
queue->SwapOrders(start, g_rand_int_range(queue->rand, start, end));
|
2009-01-23 00:08:40 +01:00
|
|
|
}
|
|
|
|
|
2009-01-25 14:00:51 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleOrderLast(unsigned start, unsigned end)
|
2009-01-25 14:00:51 +01:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
SwapOrders(end - 1, g_rand_int_range(rand, start, end));
|
2009-01-25 14:00:51 +01:00
|
|
|
}
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
void
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::ShuffleRange(unsigned start, unsigned end)
|
2009-01-22 23:40:11 +01:00
|
|
|
{
|
|
|
|
assert(start <= end);
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(end <= length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
for (unsigned i = start; i < end; i++) {
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned ri = g_rand_int_range(rand, i, end);
|
|
|
|
SwapPositions(i, ri);
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the first item that has this specified priority or higher.
|
|
|
|
*/
|
|
|
|
G_GNUC_PURE
|
|
|
|
static unsigned
|
|
|
|
queue_find_priority_order(const struct queue *queue, unsigned start_order,
|
|
|
|
uint8_t priority, unsigned exclude_order)
|
|
|
|
{
|
|
|
|
assert(queue != NULL);
|
|
|
|
assert(queue->random);
|
|
|
|
assert(start_order <= queue->length);
|
|
|
|
|
|
|
|
for (unsigned order = start_order; order < queue->length; ++order) {
|
2013-01-06 21:33:58 +01:00
|
|
|
const unsigned position = queue->OrderToPosition(order);
|
2011-07-19 00:34:33 +02:00
|
|
|
const struct queue_item *item = &queue->items[position];
|
|
|
|
if (item->priority <= priority && order != exclude_order)
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_GNUC_PURE
|
|
|
|
static unsigned
|
|
|
|
queue_count_same_priority(const struct queue *queue, unsigned start_order,
|
|
|
|
uint8_t priority)
|
|
|
|
{
|
|
|
|
assert(queue != NULL);
|
|
|
|
assert(queue->random);
|
|
|
|
assert(start_order <= queue->length);
|
|
|
|
|
|
|
|
for (unsigned order = start_order; order < queue->length; ++order) {
|
2013-01-06 21:33:58 +01:00
|
|
|
const unsigned position = queue->OrderToPosition(order);
|
2011-07-19 00:34:33 +02:00
|
|
|
const struct queue_item *item = &queue->items[position];
|
|
|
|
if (item->priority != priority)
|
|
|
|
return order - start_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue->length - start_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::SetPriority(unsigned position, uint8_t priority, int after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(position < length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
struct queue_item *item = &items[position];
|
2011-07-19 00:34:33 +02:00
|
|
|
uint8_t old_priority = item->priority;
|
|
|
|
if (old_priority == priority)
|
|
|
|
return false;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
item->version = version;
|
2011-07-19 00:34:33 +02:00
|
|
|
item->priority = priority;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (!random)
|
2011-07-19 00:34:33 +02:00
|
|
|
/* don't reorder if not in random mode */
|
|
|
|
return true;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned _order = PositionToOrder(position);
|
2011-07-19 00:34:33 +02:00
|
|
|
if (after_order >= 0) {
|
2013-01-06 21:33:58 +01:00
|
|
|
if (_order == (unsigned)after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
/* don't reorder the current song */
|
|
|
|
return true;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (_order < (unsigned)after_order) {
|
2011-07-19 00:34:33 +02:00
|
|
|
/* the specified song has been played already
|
|
|
|
- enqueue it only if its priority has just
|
|
|
|
become bigger than the current one's */
|
|
|
|
|
|
|
|
const unsigned after_position =
|
2013-01-06 21:33:58 +01:00
|
|
|
OrderToPosition(after_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
const struct queue_item *after_item =
|
2013-01-06 21:33:58 +01:00
|
|
|
&items[after_position];
|
2011-07-19 00:34:33 +02:00
|
|
|
if (old_priority > after_item->priority ||
|
|
|
|
priority <= after_item->priority)
|
|
|
|
/* priority hasn't become bigger */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move the item to the beginning of the priority group (or
|
|
|
|
create a new priority group) */
|
|
|
|
|
|
|
|
const unsigned before_order =
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_find_priority_order(this, after_order + 1, priority,
|
|
|
|
_order);
|
|
|
|
const unsigned new_order = before_order > _order
|
2011-07-19 00:34:33 +02:00
|
|
|
? before_order - 1
|
|
|
|
: before_order;
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_move_order(this, _order, new_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/* shuffle the song within that priority group */
|
|
|
|
|
|
|
|
const unsigned priority_count =
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_count_same_priority(this, new_order, priority);
|
2011-07-19 00:34:33 +02:00
|
|
|
assert(priority_count >= 1);
|
2013-01-06 21:33:58 +01:00
|
|
|
queue_shuffle_order_first(this, new_order,
|
2011-07-19 00:34:33 +02:00
|
|
|
new_order + priority_count);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-06 21:33:58 +01:00
|
|
|
queue::SetPriorityRange(unsigned start_position, unsigned end_position,
|
|
|
|
uint8_t priority, int after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
{
|
|
|
|
assert(start_position <= end_position);
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(end_position <= length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
bool modified = false;
|
|
|
|
int after_position = after_order >= 0
|
2013-01-06 21:33:58 +01:00
|
|
|
? (int)OrderToPosition(after_order)
|
2011-07-19 00:34:33 +02:00
|
|
|
: -1;
|
|
|
|
for (unsigned i = start_position; i < end_position; ++i) {
|
|
|
|
after_order = after_position >= 0
|
2013-01-06 21:33:58 +01:00
|
|
|
? (int)PositionToOrder(after_position)
|
2011-07-19 00:34:33 +02:00
|
|
|
: -1;
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
modified |= SetPriority(i, priority, after_order);
|
2011-07-19 00:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return modified;
|
|
|
|
}
|