2013-12-22 22:21:19 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-01-16 22:25:04 -05:00
|
|
|
#include <callback.h>
|
2013-12-22 22:21:19 -05:00
|
|
|
#include <deck.h>
|
2014-01-04 13:09:44 -05:00
|
|
|
#include <error.h>
|
2014-01-24 21:43:18 -05:00
|
|
|
#include <file.h>
|
2013-12-22 22:21:19 -05:00
|
|
|
#include <print.h>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
static std::list<Queue> playqueue_deck;
|
|
|
|
static Queue library_playqueue(Q_ENABLED);
|
2014-04-26 13:31:22 -04:00
|
|
|
static File deck_file("deck");
|
2014-01-16 22:25:04 -05:00
|
|
|
|
|
|
|
static void add_library_track(unsigned int id)
|
|
|
|
{
|
|
|
|
library_playqueue.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void del_library_track(unsigned int id)
|
|
|
|
{
|
|
|
|
library_playqueue.del_track(id);
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:45:01 -05:00
|
|
|
static void change_library_track(unsigned int id)
|
|
|
|
{
|
|
|
|
library_playqueue.track_updated(id);
|
|
|
|
}
|
|
|
|
|
2014-01-16 22:25:04 -05:00
|
|
|
void deck :: init()
|
|
|
|
{
|
2014-04-01 20:28:19 -04:00
|
|
|
library_playqueue.set_flag(Q_REPEAT);
|
|
|
|
library_playqueue.set_flag(Q_DISABLE_CHANGED_SIZE);
|
2014-01-26 12:12:01 -05:00
|
|
|
library_playqueue.add_sort(SORT_ARTIST);
|
|
|
|
library_playqueue.add_sort(SORT_YEAR);
|
|
|
|
library_playqueue.add_sort(SORT_TRACK);
|
2014-01-25 13:21:57 -05:00
|
|
|
read();
|
|
|
|
|
2014-02-02 14:11:38 -05:00
|
|
|
get_callbacks()->on_playlist_ban = del_library_track;
|
|
|
|
get_callbacks()->on_playlist_unban = add_library_track;
|
2014-01-16 22:25:04 -05:00
|
|
|
get_callbacks()->on_library_track_add = add_library_track;
|
|
|
|
get_callbacks()->on_library_track_del = del_library_track;
|
2014-01-29 22:45:01 -05:00
|
|
|
get_callbacks()->on_library_track_updated = change_library_track;
|
2014-01-24 21:43:18 -05:00
|
|
|
get_callbacks()->on_queue_changed = write;
|
2014-01-16 22:25:04 -05:00
|
|
|
}
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
void deck :: read()
|
2013-12-23 10:37:22 -05:00
|
|
|
{
|
|
|
|
unsigned int num;
|
2014-01-24 22:50:05 -05:00
|
|
|
int random;
|
2014-01-26 14:44:56 -05:00
|
|
|
unsigned int field;
|
|
|
|
bool ascending;
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it;
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
if (!deck_file.exists())
|
|
|
|
return;
|
|
|
|
|
|
|
|
deck_file.open(OPEN_READ);
|
|
|
|
deck_file >> random >> num;
|
2014-01-29 21:37:06 -05:00
|
|
|
library_playqueue.force_clear_sort();
|
2014-01-26 14:44:56 -05:00
|
|
|
for (unsigned int i = 0; i < num; i++) {
|
|
|
|
deck_file >> field >> ascending;
|
|
|
|
if (i == 0)
|
2014-01-29 21:37:06 -05:00
|
|
|
library_playqueue.reset_sort((sort_t)field, ascending);
|
2014-01-26 14:44:56 -05:00
|
|
|
else
|
2014-01-29 21:37:06 -05:00
|
|
|
library_playqueue.add_sort((sort_t)field, ascending);
|
2014-01-26 14:44:56 -05:00
|
|
|
}
|
2014-01-16 22:25:04 -05:00
|
|
|
|
2014-01-26 14:44:56 -05:00
|
|
|
deck_file >> num;
|
2014-01-16 22:25:04 -05:00
|
|
|
if (random)
|
2014-04-01 20:28:19 -04:00
|
|
|
library_playqueue.set_flag(Q_RANDOM);
|
2014-01-16 22:25:04 -05:00
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.resize(num);
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2014-01-30 22:31:36 -05:00
|
|
|
num = 0;
|
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
2014-01-24 21:43:18 -05:00
|
|
|
it->read(deck_file);
|
2014-01-30 22:31:36 -05:00
|
|
|
get_callbacks()->on_pq_created(&(*it), num);
|
|
|
|
num++;
|
|
|
|
}
|
2014-01-24 21:43:18 -05:00
|
|
|
deck_file.close();
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
void deck :: write()
|
2013-12-23 10:37:22 -05:00
|
|
|
{
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it;
|
2014-01-26 14:44:56 -05:00
|
|
|
std::list<sort_info>::iterator st;
|
|
|
|
std::list<sort_info> sort_order;
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
deck_file.open(OPEN_WRITE);
|
2014-01-26 14:44:56 -05:00
|
|
|
|
|
|
|
/* Save library playqueue */
|
|
|
|
sort_order = library_playqueue.get_sort_order();
|
2014-04-13 12:26:32 -04:00
|
|
|
deck_file << library_playqueue.has_flag(Q_RANDOM) << " ";
|
2014-01-26 14:44:56 -05:00
|
|
|
deck_file << sort_order.size() << " ";
|
|
|
|
for (st = sort_order.begin(); st != sort_order.end(); st++)
|
|
|
|
deck_file << st->field << " " << st->ascending << " ";
|
|
|
|
|
2014-01-24 21:43:18 -05:00
|
|
|
deck_file << playqueue_deck.size() << std :: endl;
|
2014-01-04 11:21:05 -05:00
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
2014-01-24 21:43:18 -05:00
|
|
|
it->write(deck_file);
|
|
|
|
deck_file << std::endl;
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
2014-01-24 21:43:18 -05:00
|
|
|
deck_file.close();
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
Queue *deck :: create(bool random)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-04-01 20:28:19 -04:00
|
|
|
Queue *pq;
|
|
|
|
playqueue_deck.push_back(Queue(Q_ENABLED));
|
2014-01-30 22:31:36 -05:00
|
|
|
pq = &playqueue_deck.back();
|
2014-01-30 22:53:10 -05:00
|
|
|
if (random == true)
|
2014-04-01 20:28:19 -04:00
|
|
|
pq->set_flag(Q_RANDOM);
|
2014-01-30 22:31:36 -05:00
|
|
|
get_callbacks()->on_pq_created(pq, playqueue_deck.size() - 1);
|
|
|
|
return pq;
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void deck :: remove(unsigned int id)
|
|
|
|
{
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it = playqueue_deck.begin();
|
2013-12-22 22:21:19 -05:00
|
|
|
for (unsigned int i = 0; i < id; i++)
|
|
|
|
it++;
|
2014-01-30 22:31:36 -05:00
|
|
|
get_callbacks()->on_pq_removed(&(*it));
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.erase(it);
|
2014-01-30 22:31:36 -05:00
|
|
|
write();
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
Queue *deck :: get(unsigned int id)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it = playqueue_deck.begin();
|
2013-12-22 22:21:19 -05:00
|
|
|
for (unsigned int i = 0; i < id; i++)
|
|
|
|
it++;
|
|
|
|
return &(*it);
|
|
|
|
}
|
|
|
|
|
2014-01-31 22:45:26 -05:00
|
|
|
unsigned int deck :: size()
|
|
|
|
{
|
|
|
|
return playqueue_deck.size();
|
|
|
|
}
|
|
|
|
|
2013-12-22 22:21:19 -05:00
|
|
|
void deck :: move(unsigned int old_pos, unsigned int new_pos)
|
|
|
|
{
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it_old = playqueue_deck.begin();
|
|
|
|
std::list<Queue>::iterator it_new = playqueue_deck.begin();
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
for (unsigned int i = 0; i < playqueue_deck.size(); i++) {
|
2013-12-22 22:21:19 -05:00
|
|
|
if (i < old_pos)
|
|
|
|
it_old++;
|
|
|
|
if (i < new_pos)
|
|
|
|
it_new++;
|
2014-01-31 22:45:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_pos > old_pos)
|
|
|
|
it_new++;
|
|
|
|
|
|
|
|
playqueue_deck.splice(it_new, playqueue_deck, it_old);
|
|
|
|
}
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
void deck :: move(Queue *pq, unsigned int new_pos)
|
2014-01-31 22:45:26 -05:00
|
|
|
{
|
|
|
|
unsigned int old_pos = 0;
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it_old = playqueue_deck.begin();
|
|
|
|
std::list<Queue>::iterator it_new = playqueue_deck.begin();
|
2014-01-31 22:45:26 -05:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < playqueue_deck.size(); i++) {
|
|
|
|
if (&(*it_old) != pq) {
|
|
|
|
it_old++;
|
|
|
|
old_pos++;
|
|
|
|
}
|
|
|
|
if (i < new_pos)
|
|
|
|
it_new++;
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (new_pos > old_pos)
|
|
|
|
it_new++;
|
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.splice(it_new, playqueue_deck, it_old);
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
Track *deck :: next()
|
2013-12-23 10:22:37 -05:00
|
|
|
{
|
2014-04-13 12:26:32 -04:00
|
|
|
Track *track;
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it;
|
2013-12-23 10:22:37 -05:00
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
2014-04-13 12:26:32 -04:00
|
|
|
if (it->has_flag(Q_ENABLED)) {
|
2014-01-30 22:31:36 -05:00
|
|
|
if (it->size() == 0) {
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.erase(it);
|
2014-01-30 22:31:36 -05:00
|
|
|
get_callbacks()->on_pq_removed(&(*it));
|
2014-02-07 20:34:03 -05:00
|
|
|
} else {
|
2014-04-13 12:26:32 -04:00
|
|
|
track = it->next();
|
2014-02-07 20:34:03 -05:00
|
|
|
if (it->size() == 0) {
|
|
|
|
playqueue_deck.erase(it);
|
|
|
|
get_callbacks()->on_pq_removed(&(*it));
|
|
|
|
}
|
2014-01-30 22:31:36 -05:00
|
|
|
}
|
|
|
|
write();
|
2014-04-13 12:26:32 -04:00
|
|
|
return track;
|
2013-12-23 10:22:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 22:25:04 -05:00
|
|
|
return library_playqueue.next();
|
|
|
|
}
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
Queue *deck :: get_library_pq()
|
2014-01-16 22:25:04 -05:00
|
|
|
{
|
|
|
|
return &library_playqueue;
|
2013-12-23 10:22:37 -05:00
|
|
|
}
|
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
#ifdef CONFIG_TEST
|
2014-01-29 21:06:24 -05:00
|
|
|
static void no_op() {}
|
|
|
|
|
2013-12-23 10:37:22 -05:00
|
|
|
void deck :: reset()
|
|
|
|
{
|
2014-01-29 21:06:24 -05:00
|
|
|
get_callbacks()->on_queue_changed = no_op;
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.clear();
|
2014-01-22 19:34:01 -05:00
|
|
|
library_playqueue.reset();
|
2013-12-23 10:37:22 -05:00
|
|
|
}
|
|
|
|
|
2013-12-22 22:21:19 -05:00
|
|
|
void deck :: print_info()
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
2014-04-01 20:28:19 -04:00
|
|
|
std::list<Queue>::iterator it;
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
2014-04-01 20:28:19 -04:00
|
|
|
print("deck[%u] = Queue { size = %u, flags = %u }\n",
|
2014-04-13 12:26:32 -04:00
|
|
|
i, it->size());
|
2013-12-22 22:21:19 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 11:21:05 -05:00
|
|
|
#endif /* CONFIG_TEST */
|