Commit f78cddb4 authored by Max Kellermann's avatar Max Kellermann

playlist: moved code to queue.c

Attempt to untie the playlist.c knot: moved the playlist storage code to queue.c, struct queue.
parent d5dcd0ed
......@@ -92,6 +92,7 @@ mpd_headers = \
player_control.h \
playlist.h \
playlist_save.h \
queue.h \
replay_gain.h \
sig_handlers.h \
song.h \
......@@ -175,6 +176,7 @@ mpd_SOURCES = \
player_control.c \
playlist.c \
playlist_save.c \
queue.c \
replay_gain.c \
sig_handlers.c \
song.c \
......
......@@ -20,6 +20,7 @@
#define MPD_PLAYLIST_H
#include "locate.h"
#include "queue.h"
#include <stdbool.h>
#include <stdio.h>
......@@ -43,18 +44,10 @@ enum playlist_result {
};
typedef struct _Playlist {
struct song **songs;
/* holds version a song was modified on */
uint32_t *songMod;
unsigned *order;
unsigned *positionToId;
int *idToPosition;
unsigned length;
struct queue queue;
int current;
int queued;
bool repeat;
bool random;
uint32_t version;
} Playlist;
extern bool playlist_saveAbsolutePaths;
......
/*
* Copyright (C) 2003-2009 The Music Player Daemon Project
* 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 "queue.h"
#include "song.h"
unsigned
queue_generate_id(const struct queue *queue)
{
static unsigned cur = (unsigned)-1;
do {
cur++;
if (cur >= queue->max_length * QUEUE_HASH_MULT)
cur = 0;
} while (queue->idToPosition[cur] != -1);
return cur;
}
int
queue_next_order(const struct queue *queue, unsigned order)
{
assert(order < queue->length);
if (order + 1 < queue->length)
return order + 1;
else if (queue->repeat)
/* restart at first song */
return 0;
else
/* end of queue */
return -1;
}
void
queue_increment_version(struct queue *queue)
{
static unsigned long max = ((uint32_t) 1 << 31) - 1;
queue->version++;
if (queue->version >= max) {
for (unsigned i = 0; i < queue->length; i++)
queue->songMod[i] = 0;
queue->version = 1;
}
}
void
queue_modify(struct queue *queue, unsigned order)
{
unsigned position;
assert(order < queue->length);
position = queue->order[order];
queue->songMod[position] = queue->version;
queue_increment_version(queue);
}
void
queue_modify_all(struct queue *queue)
{
for (unsigned i = 0; i < queue->length; i++)
queue->songMod[i] = queue->version;
queue_increment_version(queue);
}
unsigned
queue_append(struct queue *queue, struct song *song)
{
unsigned id = queue_generate_id(queue);
assert(!queue_is_full(queue));
queue->songs[queue->length] = song;
queue->songMod[queue->length] = queue->version;
queue->order[queue->length] = queue->length;
queue->positionToId[queue->length] = id;
queue->idToPosition[queue->positionToId[queue->length]] =
queue->length;
++queue->length;
return id;
}
void
queue_swap(struct queue *queue, unsigned position1, unsigned position2)
{
struct song *sTemp;
unsigned iTemp;
sTemp = queue->songs[position1];
queue->songs[position1] = queue->songs[position2];
queue->songs[position2] = sTemp;
queue->songMod[position1] = queue->version;
queue->songMod[position2] = queue->version;
queue->idToPosition[queue->positionToId[position1]] = position2;
queue->idToPosition[queue->positionToId[position2]] = position1;
iTemp = queue->positionToId[position1];
queue->positionToId[position1] = queue->positionToId[position2];
queue->positionToId[position2] = iTemp;
}
static void
queue_move_song_to(struct queue *queue, unsigned from, unsigned to)
{
unsigned from_id = queue->positionToId[from];
queue->idToPosition[from_id] = to;
queue->positionToId[to] = from_id;
queue->songs[to] = queue->songs[from];
queue->songMod[to] = queue->version;
}
void
queue_move(struct queue *queue, unsigned from, unsigned to)
{
struct song *song;
unsigned id;
song = queue_get(queue, from);
id = queue_position_to_id(queue, from);
/* move songs to one less in from->to */
for (unsigned i = from; i < to; i++)
queue_move_song_to(queue, i + 1, i);
/* move songs to one more in to->from */
for (unsigned i = from; i > to; i--)
queue_move_song_to(queue, i - 1, i);
/* put song at _to_ */
queue->idToPosition[id] = to;
queue->positionToId[to] = id;
queue->songs[to] = song;
queue->songMod[to] = queue->version;
}
void
queue_delete(struct queue *queue, unsigned position)
{
struct song *song;
unsigned id, order;
assert(position < queue->length);
song = queue_get(queue, position);
if (!song_in_database(song))
song_free(song);
id = queue_position_to_id(queue, position);
order = queue_position_to_order(queue, position);
--queue->length;
/* release the song id */
queue->idToPosition[id] = -1;
/* delete song from songs array */
for (unsigned i = position; i < queue->length; i++)
queue_move_song_to(queue, i + 1, i);
/* delete the entry from the order array */
for (unsigned i = order; i < queue->length; i++)
queue->order[i] = queue->order[i + 1];
/* readjust values in the order array */
for (unsigned i = 0; i < queue->length; i++)
if (queue->order[i] > position)
--queue->order[i];
}
void
queue_clear(struct queue *queue)
{
for (unsigned i = 0; i < queue->length; i++) {
if (!song_in_database(queue->songs[i]))
song_free(queue->songs[i]);
queue->idToPosition[queue->positionToId[i]] = -1;
}
queue->length = 0;
}
void
queue_init(struct queue *queue, unsigned max_length)
{
queue->max_length = max_length;
queue->length = 0;
queue->version = 1;
queue->repeat = false;
queue->random = false;
queue->songs = g_malloc(sizeof(queue->songs[0]) * max_length);
queue->songMod = g_malloc(sizeof(queue->songMod[0]) *
max_length);
queue->order = g_malloc(sizeof(queue->order[0]) *
max_length);
queue->idToPosition = g_malloc(sizeof(queue->idToPosition[0]) *
max_length * QUEUE_HASH_MULT);
queue->positionToId = g_malloc(sizeof(queue->positionToId[0]) *
max_length);
for (unsigned i = 0; i < max_length * QUEUE_HASH_MULT; ++i)
queue->idToPosition[i] = -1;
queue->rand = g_rand_new();
}
void
queue_finish(struct queue *queue)
{
queue_clear(queue);
g_free(queue->songs);
g_free(queue->songMod);
g_free(queue->order);
g_free(queue->idToPosition);
g_free(queue->positionToId);
g_rand_free(queue->rand);
}
void
queue_shuffle_range(struct queue *queue, unsigned start, unsigned end)
{
assert(start <= end);
assert(end <= queue->length);
for (unsigned i = start; i < end; i++) {
unsigned ri = g_rand_int_range(queue->rand, i, end);
queue_swap(queue, i, ri);
}
}
/*
* Copyright (C) 2003-2009 The Music Player Daemon Project
* 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
*/
#ifndef QUEUE_H
#define QUEUE_H
#include <glib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
enum {
/**
* reserve max_length * QUEUE_HASH_MULT elements in the id
* number space
*/
QUEUE_HASH_MULT = 4,
};
/**
* 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 */
struct song **songs;
/** holds version a song was modified on */
uint32_t *songMod;
/** map order numbers to positions */
unsigned *order;
/** map positions to song ids */
unsigned *positionToId;
/** map song ids to posiitons */
int *idToPosition;
/** repeat playback when the end of the queue has been
reached? */
bool repeat;
/** play back songs in random order? */
bool random;
/** random number generator for shuffle and random mode */
GRand *rand;
};
static inline unsigned
queue_length(const struct queue *queue)
{
assert(queue->length <= queue->max_length);
return queue->length;
}
/**
* Determine if the queue is empty, i.e. there are no songs.
*/
static inline bool
queue_is_empty(const struct queue *queue)
{
return queue->length == 0;
}
/**
* Determine if the maximum number of songs has been reached.
*/
static inline bool
queue_is_full(const struct queue *queue)
{
assert(queue->length <= queue->max_length);
return queue->length >= queue->max_length;
}
/**
* Is that a valid position number?
*/
static inline bool
queue_valid_position(const struct queue *queue, unsigned position)
{
return position < queue->length;
}
/**
* Is that a valid order number?
*/
static inline bool
queue_valid_order(const struct queue *queue, unsigned order)
{
return order < queue->length;
}
static inline int
queue_id_to_position(const struct queue *queue, unsigned id)
{
if (id >= queue->max_length * QUEUE_HASH_MULT)
return -1;
assert(queue->idToPosition[id] >= -1);
assert(queue->idToPosition[id] < (int)queue->length);
return queue->idToPosition[id];
}
static inline int
queue_position_to_id(const struct queue *queue, unsigned position)
{
assert(position < queue->length);
return queue->positionToId[position];
}
static inline unsigned
queue_order_to_position(const struct queue *queue, unsigned order)
{
assert(order < queue->length);
return queue->order[order];
}
static inline unsigned
queue_position_to_order(const struct queue *queue, unsigned position)
{
assert(position < queue->length);
for (unsigned i = 0;; ++i) {
assert(i < queue->length);
if (queue->order[i] == position)
return i;
}
}
/**
* Returns the song at the specified position.
*/
static inline struct song *
queue_get(const struct queue *queue, unsigned position)
{
assert(position < queue->length);
return queue->songs[position];
}
/**
* Returns the song at the specified order number.
*/
static inline struct song *
queue_get_order(const struct queue *queue, unsigned order)
{
return queue_get(queue, queue_order_to_position(queue, order));
}
/**
* Is the song at the specified position newer than the specified
* version?
*/
static inline bool
queue_song_newer(const struct queue *queue, unsigned position,
uint32_t version)
{
assert(position < queue->length);
return version > queue->version ||
queue->songMod[position] >= version ||
queue->songMod[position] == 0;
}
/**
* Initialize a queue object.
*/
void
queue_init(struct queue *queue, unsigned max_length);
/**
* Deinitializes a queue object. It does not free the queue pointer
* itself.
*/
void
queue_finish(struct queue *queue);
/**
* Generate a non-existing id number.
*/
unsigned
queue_generate_id(const struct queue *queue);
/**
* 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
*/
int
queue_next_order(const struct queue *queue, unsigned order);
/**
* Increments the queue's version number. This handles integer
* overflow well.
*/
void
queue_increment_version(struct queue *queue);
/**
* Marks the specified song as "modified" and increments the version
* number.
*/
void
queue_modify(struct queue *queue, unsigned order);
/**
* Marks all songs as "modified" and increments the version number.
*/
void
queue_modify_all(struct queue *queue);
/**
* 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.
*/
unsigned
queue_append(struct queue *queue, struct song *song);
/**
* Swaps two songs, addressed by their position.
*/
void
queue_swap(struct queue *queue, unsigned position1, unsigned position2);
/**
* Swaps two songs, addressed by their order number.
*/
static inline void
queue_swap_order(struct queue *queue, unsigned order1, unsigned order2)
{
unsigned tmp = queue->order[order1];
queue->order[order1] = queue->order[order2];
queue->order[order2] = tmp;
}
/**
* Moves a song to a new position.
*/
void
queue_move(struct queue *queue, unsigned from, unsigned to);
/**
* Removes a song from the playlist.
*/
void
queue_delete(struct queue *queue, unsigned position);
/**
* Removes all songs from the playlist.
*/
void
queue_clear(struct queue *queue);
/**
* Initializes the "order" array, and restores "normal" order.
*/
static inline void
queue_restore_order(struct queue *queue)
{
for (unsigned i = 0; i < queue->length; ++i)
queue->order[i] = i;
}
/**
* Shuffles a (position) range in the queue. The songs are physically
* shuffled, not by using the "order" mapping.
*/
void
queue_shuffle_range(struct queue *queue, unsigned start, unsigned end);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment