2014-10-17 09:42:45 -04:00
|
|
|
/**
|
2013-12-31 15:44:44 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/queue.h>
|
2015-09-09 09:00:17 -04:00
|
|
|
extern "C" {
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/random.h>
|
2015-09-09 09:00:17 -04:00
|
|
|
}
|
2015-01-29 08:27:25 -05:00
|
|
|
#include <core/string.h>
|
2014-01-22 21:17:51 -05:00
|
|
|
|
2014-01-26 13:44:25 -05:00
|
|
|
#include <algorithm>
|
2014-01-22 21:17:51 -05:00
|
|
|
#include <sstream>
|
|
|
|
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2015-04-03 10:30:46 -04:00
|
|
|
static class DefaultNotifier : public QNotifier {
|
|
|
|
public:
|
|
|
|
DefaultNotifier() {};
|
2015-04-03 12:24:26 -04:00
|
|
|
void on_track_added(unsigned int pos) {};
|
|
|
|
void on_track_removed(unsigned int pos) {};
|
2015-04-03 20:54:45 -04:00
|
|
|
void on_track_updated(unsigned int pos) {};
|
2015-04-03 10:30:46 -04:00
|
|
|
} def_notify;
|
|
|
|
|
|
|
|
|
2014-12-09 08:39:04 -05:00
|
|
|
Queue :: Queue(unsigned int flags)
|
2015-04-03 10:30:46 -04:00
|
|
|
: _cur(-1), _flags(flags), _length(0), _notify(&def_notify)
|
2014-12-09 08:39:04 -05:00
|
|
|
{}
|
|
|
|
|
|
|
|
Queue :: Queue()
|
2015-04-03 10:30:46 -04:00
|
|
|
: _cur(-1), _flags(0), _length(0), _notify(&def_notify)
|
2014-12-09 08:39:04 -05:00
|
|
|
{}
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
Queue :: ~Queue()
|
2015-04-09 09:15:06 -04:00
|
|
|
{}
|
2013-12-31 15:44:44 -05:00
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
void Queue :: write(file &file)
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2015-10-08 09:41:51 -04:00
|
|
|
file_writef(&file, "%u %zu", _flags, _tracks.size());
|
2014-04-13 12:26:32 -04:00
|
|
|
for (unsigned int i = 0; i < _tracks.size(); i++)
|
2015-11-12 08:41:46 -05:00
|
|
|
file_writef(&file, " %u", _tracks[i]->tr_dbe.dbe_index);
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2015-09-10 08:00:42 -04:00
|
|
|
void Queue :: read(file &file)
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2014-04-13 12:26:32 -04:00
|
|
|
unsigned int n, id;
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&file, "%u %u", &_flags, &n);
|
2014-04-13 12:26:32 -04:00
|
|
|
_tracks.resize(n);
|
|
|
|
for (unsigned int i = 0; i < n; i++) {
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&file, "%u", &id);
|
2015-09-28 15:54:21 -04:00
|
|
|
_tracks[i] = track_get(id);
|
2015-10-27 17:34:00 -04:00
|
|
|
_length += _tracks[i]->tr_length;
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 08:39:04 -05:00
|
|
|
void Queue :: set_flag(queue_flags flag)
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2014-12-09 08:39:04 -05:00
|
|
|
_flags |= flag;
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 08:39:04 -05:00
|
|
|
void Queue :: unset_flag(queue_flags flag)
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2014-12-09 08:39:04 -05:00
|
|
|
_flags &= ~flag;
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 08:39:04 -05:00
|
|
|
bool Queue :: has_flag(queue_flags flag)
|
2014-01-02 21:58:18 -05:00
|
|
|
{
|
2014-12-09 08:39:04 -05:00
|
|
|
return (_flags & flag) == (unsigned int)flag;
|
2014-01-02 21:58:18 -05:00
|
|
|
}
|
|
|
|
|
2015-04-03 10:30:46 -04:00
|
|
|
void Queue :: set_notifier(QNotifier *notify)
|
|
|
|
{
|
|
|
|
_notify = notify;
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
static bool track_less_than(struct track *lhs, struct track *rhs,
|
2014-05-18 14:11:21 -04:00
|
|
|
std::vector<struct sort_info> &order)
|
2014-01-26 12:12:01 -05:00
|
|
|
{
|
|
|
|
int res;
|
2014-01-25 14:43:51 -05:00
|
|
|
|
2014-05-18 14:11:21 -04:00
|
|
|
for (unsigned int i = 0; i < order.size(); i++) {
|
|
|
|
if (order[i].ascending == true)
|
2014-11-28 10:28:15 -05:00
|
|
|
res = track_compare(lhs, rhs, order[i].field);
|
2014-01-26 14:44:56 -05:00
|
|
|
else
|
2014-11-28 10:28:15 -05:00
|
|
|
res = track_compare(rhs, lhs, order[i].field);
|
2014-05-18 14:11:21 -04:00
|
|
|
if (res == 0)
|
|
|
|
continue;
|
|
|
|
break;
|
2014-01-26 12:12:01 -05:00
|
|
|
}
|
2014-05-18 14:11:21 -04:00
|
|
|
return res < 0;
|
2014-01-25 14:43:51 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
unsigned int Queue :: find_sorted_id(struct track *rhs)
|
2014-01-25 14:43:51 -05:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *lhs;
|
2014-04-13 12:26:32 -04:00
|
|
|
unsigned int begin = 0, end = (_tracks.size() - 1), mid;
|
2014-01-25 14:43:51 -05:00
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
if (_tracks.size() == 0)
|
2014-01-27 11:22:35 -05:00
|
|
|
return 0;
|
2014-01-26 10:41:13 -05:00
|
|
|
|
2014-01-28 09:42:38 -05:00
|
|
|
while (end > begin) {
|
|
|
|
mid = begin + ((end - begin) / 2);
|
2014-04-13 12:26:32 -04:00
|
|
|
lhs = _tracks[mid];
|
|
|
|
if (track_less_than(lhs, rhs, _sort_order))
|
2014-01-28 09:42:38 -05:00
|
|
|
begin = mid + 1;
|
|
|
|
else {
|
|
|
|
if (mid == begin)
|
|
|
|
return begin;
|
|
|
|
else
|
|
|
|
end = mid - 1;
|
2014-01-27 11:22:35 -05:00
|
|
|
}
|
2014-01-27 10:23:22 -05:00
|
|
|
}
|
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
lhs = _tracks[begin];
|
|
|
|
if (track_less_than(lhs, rhs, _sort_order))
|
2014-01-28 09:42:38 -05:00
|
|
|
return begin + 1;
|
|
|
|
return begin;
|
2014-01-25 14:43:51 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
unsigned int Queue :: _add_at(struct track *track, unsigned int pos)
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2014-05-11 10:53:48 -04:00
|
|
|
_tracks.insert(_tracks.begin() + pos, track);
|
2015-10-27 17:34:00 -04:00
|
|
|
_length += track->tr_length;
|
2015-04-03 11:35:23 -04:00
|
|
|
_notify->on_track_added(pos);
|
2014-05-11 10:53:48 -04:00
|
|
|
return pos;
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
unsigned int Queue :: add(struct track *track)
|
2014-01-02 21:58:18 -05:00
|
|
|
{
|
2014-05-11 10:53:48 -04:00
|
|
|
unsigned int id = _tracks.size();
|
|
|
|
if (_sort_order.size() > 0)
|
|
|
|
id = find_sorted_id(track);
|
|
|
|
return _add_at(track, id);
|
2014-01-02 21:58:18 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 08:48:27 -05:00
|
|
|
void Queue :: del(unsigned int index)
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2015-10-27 17:34:00 -04:00
|
|
|
_length -= _tracks[index]->tr_length;
|
2014-12-09 08:48:27 -05:00
|
|
|
_tracks.erase(_tracks.begin() + index);
|
2015-04-03 12:24:26 -04:00
|
|
|
_notify->on_track_removed(index);
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void Queue :: del(struct track *track)
|
2014-01-16 22:05:36 -05:00
|
|
|
{
|
2014-12-09 08:48:27 -05:00
|
|
|
for (unsigned int i = 0; i < _tracks.size(); i++) {
|
|
|
|
while ((i < _tracks.size()) && (_tracks[i] == track))
|
|
|
|
del(i);
|
|
|
|
}
|
2014-01-16 22:05:36 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void Queue :: updated(struct track *track)
|
2014-01-29 22:45:01 -05:00
|
|
|
{
|
2014-04-13 12:26:32 -04:00
|
|
|
for (unsigned int i = 0; i < _tracks.size(); i++) {
|
2015-04-03 21:02:23 -04:00
|
|
|
if (_tracks[i] == track)
|
2015-04-03 20:54:45 -04:00
|
|
|
_notify->on_track_updated(i);
|
2014-01-29 22:45:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *Queue :: next()
|
2014-05-18 11:08:58 -04:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *res;
|
2014-05-18 11:08:58 -04:00
|
|
|
|
|
|
|
if (_tracks.size() == 0)
|
|
|
|
return NULL;
|
|
|
|
else if (_tracks.size() == 1)
|
|
|
|
_cur = 0;
|
|
|
|
else if (_flags & Q_RANDOM)
|
2015-09-09 08:55:57 -04:00
|
|
|
_cur += random_range(1, _tracks.size() / 2);
|
2014-05-18 11:08:58 -04:00
|
|
|
else
|
|
|
|
_cur++;
|
|
|
|
|
|
|
|
_cur %= _tracks.size();
|
|
|
|
res = _tracks[_cur];
|
|
|
|
if (!(_flags & Q_REPEAT)) {
|
|
|
|
del(_cur);
|
|
|
|
_cur--;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-01 20:28:19 -04:00
|
|
|
unsigned int Queue :: size()
|
2013-12-31 15:44:44 -05:00
|
|
|
{
|
2014-04-13 12:26:32 -04:00
|
|
|
return _tracks.size();
|
2013-12-31 15:44:44 -05:00
|
|
|
}
|
|
|
|
|
2015-01-29 08:27:25 -05:00
|
|
|
unsigned int Queue :: length()
|
2014-05-11 19:52:08 -04:00
|
|
|
{
|
2015-01-29 08:27:25 -05:00
|
|
|
return _length;
|
2014-05-11 19:52:08 -04:00
|
|
|
}
|
|
|
|
|
2014-01-26 13:44:25 -05:00
|
|
|
class SortTracks {
|
|
|
|
public:
|
2014-05-18 14:11:21 -04:00
|
|
|
std::vector<struct sort_info> fields;
|
|
|
|
SortTracks(std::vector<sort_info> f) : fields(f) {}
|
2015-10-24 19:34:45 -04:00
|
|
|
bool operator()(struct track *lhs, struct track *rhs)
|
2014-01-26 13:44:25 -05:00
|
|
|
{
|
|
|
|
return track_less_than(lhs, rhs, fields);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-16 09:54:47 -05:00
|
|
|
void Queue :: sort(compare_t field, bool reset)
|
2014-01-26 12:12:01 -05:00
|
|
|
{
|
2014-05-18 14:11:21 -04:00
|
|
|
bool found = false;
|
|
|
|
struct sort_info info = { field, true };
|
2014-01-26 14:44:56 -05:00
|
|
|
|
2014-04-13 18:38:49 -04:00
|
|
|
if (_flags & Q_NO_SORT)
|
2014-01-26 14:49:07 -05:00
|
|
|
return;
|
2014-05-18 14:11:21 -04:00
|
|
|
if (reset)
|
|
|
|
_sort_order.clear();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < _sort_order.size(); i++) {
|
|
|
|
if (_sort_order[i].field == info.field) {
|
|
|
|
_sort_order[i].ascending = !_sort_order[i].ascending;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
_sort_order.push_back(info);
|
2014-01-26 14:49:07 -05:00
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
std::stable_sort(_tracks.begin(), _tracks.end(), SortTracks(_sort_order));
|
2014-01-26 14:44:56 -05:00
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
for (unsigned int i = 0; i < _tracks.size(); i++)
|
2015-04-03 21:02:23 -04:00
|
|
|
_notify->on_track_updated(i);
|
2014-01-26 12:12:01 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *Queue :: operator[](unsigned int index)
|
2014-01-20 19:06:52 -05:00
|
|
|
{
|
2014-12-09 08:39:04 -05:00
|
|
|
return _tracks[index];
|
2014-01-20 19:06:52 -05:00
|
|
|
}
|
|
|
|
|
2014-12-09 08:39:04 -05:00
|
|
|
void Queue :: track_selected(unsigned int index)
|
2014-02-06 20:06:58 -05:00
|
|
|
{
|
2014-12-09 08:39:04 -05:00
|
|
|
_cur = index;
|
2014-05-18 14:24:46 -04:00
|
|
|
if (has_flag(Q_REPEAT) == false) {
|
2014-04-13 12:26:32 -04:00
|
|
|
del(_cur);
|
|
|
|
_cur--;
|
2014-02-06 20:06:58 -05:00
|
|
|
}
|
|
|
|
}
|