2013-12-22 22:21:19 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <deck.h>
|
2014-01-04 13:09:44 -05:00
|
|
|
#include <error.h>
|
2013-12-22 22:21:19 -05:00
|
|
|
#include <print.h>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
static std::list<Playqueue> playqueue_deck;
|
2013-12-22 22:21:19 -05:00
|
|
|
|
2013-12-23 10:37:22 -05:00
|
|
|
void deck :: read(File &f)
|
|
|
|
{
|
|
|
|
unsigned int num;
|
2014-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::iterator it;
|
2013-12-23 10:37:22 -05:00
|
|
|
|
|
|
|
f >> num;
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.resize(num);
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++)
|
2013-12-23 10:37:22 -05:00
|
|
|
it->read(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deck :: write(File &f)
|
|
|
|
{
|
2014-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::iterator it;
|
2013-12-23 10:37:22 -05:00
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
f << playqueue_deck.size() << std :: endl;
|
|
|
|
for (it = playqueue_deck.begin(); it != playqueue_deck.end(); it++) {
|
2013-12-23 10:37:22 -05:00
|
|
|
it->write(f);
|
|
|
|
f << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
Playqueue *deck :: create()
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.push_back(Playqueue(PQ_ENABLED));
|
|
|
|
return &playqueue_deck.back();
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void deck :: remove(unsigned int id)
|
|
|
|
{
|
2014-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::iterator it = playqueue_deck.begin();
|
2013-12-22 22:21:19 -05:00
|
|
|
for (unsigned int i = 0; i < id; i++)
|
|
|
|
it++;
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.erase(it);
|
2013-12-22 22:21:19 -05:00
|
|
|
}
|
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
Playqueue *deck :: get(unsigned int id)
|
2013-12-22 22:21:19 -05:00
|
|
|
{
|
2014-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::iterator it = playqueue_deck.begin();
|
2013-12-22 22:21:19 -05:00
|
|
|
for (unsigned int i = 0; i < id; i++)
|
|
|
|
it++;
|
|
|
|
return &(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deck :: move(unsigned int old_pos, unsigned int new_pos)
|
|
|
|
{
|
2014-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::iterator it_old = playqueue_deck.begin();
|
|
|
|
std::list<Playqueue>::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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-23 10:22:37 -05:00
|
|
|
unsigned int deck :: next()
|
|
|
|
{
|
|
|
|
unsigned int id = 0;
|
2014-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::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++) {
|
|
|
|
if (it->get_flags() & PQ_ENABLED) {
|
2013-12-23 10:22:37 -05:00
|
|
|
id = it->next();
|
|
|
|
if (it->size() == 0)
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.erase(it);
|
2013-12-23 10:22:37 -05:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 13:09:44 -05:00
|
|
|
throw -E_EXIST;
|
2013-12-23 10:22:37 -05:00
|
|
|
}
|
|
|
|
|
2014-01-04 11:21:05 -05:00
|
|
|
#ifdef CONFIG_TEST
|
2013-12-23 10:37:22 -05:00
|
|
|
void deck :: reset()
|
|
|
|
{
|
2014-01-04 11:21:05 -05:00
|
|
|
playqueue_deck.clear();
|
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-01-04 11:21:05 -05:00
|
|
|
std::list<Playqueue>::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++) {
|
|
|
|
print("deck[%u] = Playqueue { size = %u, flags = %u }\n",
|
2013-12-22 22:21:19 -05:00
|
|
|
i, it->size(), it->get_flags());
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 11:21:05 -05:00
|
|
|
#endif /* CONFIG_TEST */
|