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
|
|
|
*/
|
|
|
|
|
2013-01-04 20:50:00 +01:00
|
|
|
#ifndef MPD_QUEUE_HXX
|
|
|
|
#define MPD_QUEUE_HXX
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
#include "gcc.h"
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
/**
|
|
|
|
* reserve max_length * QUEUE_HASH_MULT elements in the id
|
|
|
|
* number space
|
|
|
|
*/
|
|
|
|
QUEUE_HASH_MULT = 4,
|
|
|
|
};
|
|
|
|
|
2009-01-23 00:06:38 +01:00
|
|
|
/**
|
|
|
|
* One element of the queue: basically a song plus some queue specific
|
|
|
|
* information attached.
|
|
|
|
*/
|
|
|
|
struct queue_item {
|
|
|
|
struct song *song;
|
|
|
|
|
|
|
|
/** the unique id of this item in the queue */
|
|
|
|
unsigned id;
|
|
|
|
|
|
|
|
/** when was this item last changed? */
|
|
|
|
uint32_t version;
|
2011-07-19 00:34:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The priority of this item, between 0 and 255. High
|
|
|
|
* priority value means that this song gets played first in
|
|
|
|
* "random" mode.
|
|
|
|
*/
|
|
|
|
uint8_t priority;
|
2009-01-23 00:06:38 +01:00
|
|
|
};
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
/**
|
|
|
|
* A queue of songs. This is the backend of the playlist: it contains
|
|
|
|
* an ordered list of songs.
|
|
|
|
*
|
|
|
|
* Songs can be addressed in three possible ways:
|
|
|
|
*
|
|
|
|
* - the position in the queue
|
|
|
|
* - the unique id (which stays the same, regardless of moves)
|
|
|
|
* - the order number (which only differs from "position" in random mode)
|
|
|
|
*/
|
|
|
|
struct queue {
|
|
|
|
/** configured maximum length of the queue */
|
|
|
|
unsigned max_length;
|
|
|
|
|
|
|
|
/** number of songs in the queue */
|
|
|
|
unsigned length;
|
|
|
|
|
|
|
|
/** the current version number */
|
|
|
|
uint32_t version;
|
|
|
|
|
|
|
|
/** all songs in "position" order */
|
2009-01-23 00:06:38 +01:00
|
|
|
struct queue_item *items;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/** map order numbers to positions */
|
|
|
|
unsigned *order;
|
|
|
|
|
2009-10-13 16:43:06 +02:00
|
|
|
/** map song ids to positions */
|
|
|
|
int *id_to_position;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
|
|
|
/** repeat playback when the end of the queue has been
|
|
|
|
reached? */
|
|
|
|
bool repeat;
|
|
|
|
|
2009-03-27 15:28:49 +01:00
|
|
|
/** play only current song. */
|
|
|
|
bool single;
|
2009-03-27 14:42:55 +01:00
|
|
|
|
2009-03-30 00:01:02 +02:00
|
|
|
/** remove each played files. */
|
|
|
|
bool consume;
|
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
/** play back songs in random order? */
|
|
|
|
bool random;
|
|
|
|
|
|
|
|
/** random number generator for shuffle and random mode */
|
|
|
|
GRand *rand;
|
2013-01-06 14:58:54 +01:00
|
|
|
|
|
|
|
queue(unsigned max_length);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deinitializes a queue object. It does not free the queue
|
|
|
|
* pointer itself.
|
|
|
|
*/
|
|
|
|
~queue();
|
|
|
|
|
|
|
|
queue(const queue &other) = delete;
|
|
|
|
queue &operator=(const queue &other) = delete;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
unsigned GetLength() const {
|
|
|
|
assert(length <= max_length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return length;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Determine if the queue is empty, i.e. there are no songs.
|
|
|
|
*/
|
|
|
|
bool IsEmpty() const {
|
|
|
|
return length == 0;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Determine if the maximum number of songs has been reached.
|
|
|
|
*/
|
|
|
|
bool IsFull() const {
|
|
|
|
assert(length <= max_length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return length >= max_length;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Is that a valid position number?
|
|
|
|
*/
|
|
|
|
bool IsValidPosition(unsigned position) const {
|
|
|
|
return position < length;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Is that a valid order number?
|
|
|
|
*/
|
|
|
|
bool IsValidOrder(unsigned _order) const {
|
|
|
|
return _order < length;
|
2009-01-22 23:40:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
int IdToPosition(unsigned id) const {
|
|
|
|
if (id >= max_length * QUEUE_HASH_MULT)
|
|
|
|
return -1;
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
assert(id_to_position[id] >= -1);
|
|
|
|
assert(id_to_position[id] < (int)length);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return id_to_position[id];
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
int PositionToId(unsigned position) const
|
|
|
|
{
|
|
|
|
assert(position < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return items[position].id;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
gcc_pure
|
|
|
|
unsigned OrderToPosition(unsigned _order) const {
|
|
|
|
assert(_order < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return order[_order];
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
gcc_pure
|
|
|
|
unsigned PositionToOrder(unsigned position) const {
|
|
|
|
assert(position < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
for (unsigned i = 0;; ++i) {
|
|
|
|
assert(i < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
if (order[i] == position)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
G_GNUC_PURE
|
|
|
|
uint8_t GetPriorityAtPosition(unsigned position) const {
|
|
|
|
assert(position < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return items[position].priority;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Returns the song at the specified position.
|
|
|
|
*/
|
|
|
|
struct song *Get(unsigned position) const {
|
|
|
|
assert(position < length);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return items[position].song;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Returns the song at the specified order number.
|
|
|
|
*/
|
|
|
|
struct song *GetOrder(unsigned _order) const {
|
|
|
|
return Get(OrderToPosition(_order));
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Is the song at the specified position newer than the specified
|
|
|
|
* version?
|
|
|
|
*/
|
|
|
|
bool IsNewerAtPosition(unsigned position, uint32_t _version) const {
|
|
|
|
assert(position < length);
|
2009-03-26 22:02:56 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
return _version > version ||
|
|
|
|
items[position].version >= _version ||
|
|
|
|
items[position].version == 0;
|
|
|
|
}
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Returns the order number following the specified one. This takes
|
|
|
|
* end of queue and "repeat" mode into account.
|
|
|
|
*
|
|
|
|
* @return the next order number, or -1 to stop playback
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
int GetNextOrder(unsigned order) const;
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Increments the queue's version number. This handles integer
|
|
|
|
* overflow well.
|
|
|
|
*/
|
|
|
|
void IncrementVersion();
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Marks the specified song as "modified" and increments the version
|
|
|
|
* number.
|
|
|
|
*/
|
|
|
|
void ModifyAtOrder(unsigned order);
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Marks all songs as "modified" and increments the version number.
|
|
|
|
*/
|
|
|
|
void ModifyAll();
|
2009-01-23 00:08:40 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Appends a song to the queue and returns its position. Prior to
|
|
|
|
* that, the caller must check if the queue is already full.
|
|
|
|
*
|
|
|
|
* If a song is not in the database (determined by
|
|
|
|
* song_in_database()), it is freed when removed from the queue.
|
|
|
|
*
|
|
|
|
* @param priority the priority of this new queue item
|
|
|
|
*/
|
|
|
|
unsigned Append(struct song *song, uint8_t priority);
|
2009-01-25 14:00:51 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Swaps two songs, addressed by their position.
|
|
|
|
*/
|
|
|
|
void SwapPositions(unsigned position1, unsigned position2);
|
2009-01-22 23:40:11 +01:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Swaps two songs, addressed by their order number.
|
|
|
|
*/
|
|
|
|
void SwapOrders(unsigned order1, unsigned order2) {
|
|
|
|
unsigned tmp = order[order1];
|
|
|
|
order[order1] = order[order2];
|
|
|
|
order[order2] = tmp;
|
|
|
|
}
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2013-01-06 21:33:58 +01:00
|
|
|
/**
|
|
|
|
* Moves a song to a new position.
|
|
|
|
*/
|
|
|
|
void MovePostion(unsigned from, unsigned to);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves a range of songs to a new position.
|
|
|
|
*/
|
|
|
|
void MoveRange(unsigned start, unsigned end, unsigned to);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a song from the playlist.
|
|
|
|
*/
|
|
|
|
void DeletePosition(unsigned position);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all songs from the playlist.
|
|
|
|
*/
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the "order" array, and restores "normal" order.
|
|
|
|
*/
|
|
|
|
void RestoreOrder() {
|
|
|
|
for (unsigned i = 0; i < length; ++i)
|
|
|
|
order[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffle the order of items in the specified range, taking their
|
|
|
|
* priorities into account.
|
|
|
|
*/
|
|
|
|
void ShuffleOrderRangeWithPriority(unsigned start, unsigned end);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffles the virtual order of songs, but does not move them
|
|
|
|
* physically. This is used in random mode.
|
|
|
|
*/
|
|
|
|
void ShuffleOrder();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffles the virtual order of the last song in the specified
|
|
|
|
* (order) range. This is used in random mode after a song has been
|
|
|
|
* appended by queue_append().
|
|
|
|
*/
|
|
|
|
void ShuffleOrderLast(unsigned start, unsigned end);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuffles a (position) range in the queue. The songs are physically
|
|
|
|
* shuffled, not by using the "order" mapping.
|
|
|
|
*/
|
|
|
|
void ShuffleRange(unsigned start, unsigned end);
|
|
|
|
|
|
|
|
bool SetPriority(unsigned position, uint8_t priority, int after_order);
|
|
|
|
|
|
|
|
bool SetPriorityRange(unsigned start_position, unsigned end_position,
|
|
|
|
uint8_t priority, int after_order);
|
|
|
|
};
|
2011-07-19 00:34:33 +02:00
|
|
|
|
2009-01-22 23:40:11 +01:00
|
|
|
#endif
|