77 lines
2.2 KiB
C
77 lines
2.2 KiB
C
/*
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
*/
|
|
#ifndef OCARINA_CORE_PLAYLISTS_ITERATOR_H
|
|
#define OCARINA_CORE_PLAYLISTS_ITERATOR_H
|
|
#include <core/playlists/playlist.h>
|
|
#include <core/queue.h>
|
|
|
|
/* Called to set the playlist iterator to a specific position. */
|
|
static inline bool playlist_iter_set(struct playlist *playlist,
|
|
struct queue_iter *iter,
|
|
unsigned int n)
|
|
{
|
|
if (!playlist || !iter)
|
|
return false;
|
|
iter->it_iter = g_queue_peek_nth_link(&playlist->pl_queue.q_tracks, n);
|
|
iter->it_pos = n;
|
|
return iter->it_iter != NULL;
|
|
}
|
|
|
|
/* Called to advance the requested playlist iterator. */
|
|
static inline bool playlist_iter_next(struct queue_iter *iter)
|
|
{
|
|
if (!iter)
|
|
return false;
|
|
iter->it_iter = g_list_next(iter->it_iter);
|
|
iter->it_pos++;
|
|
return iter->it_iter != NULL;
|
|
}
|
|
|
|
/* Called to get a pointer to the track at the requested iterator. */
|
|
static inline struct track *playlist_iter_track(struct queue_iter *iter)
|
|
{
|
|
return (iter && iter->it_iter) ? iter->it_iter->data : NULL;
|
|
}
|
|
|
|
/* Called to iterate over the entire playlist. */
|
|
#define playlist_for_each(playlist, it) \
|
|
for (playlist_iter_set(playlist, it, 0); (it)->it_iter; \
|
|
playlist_iter_next(it))
|
|
|
|
|
|
/* Called to set the index of the current track. */
|
|
static inline bool playlist_current_set(struct playlist *playlist,
|
|
unsigned int n)
|
|
{
|
|
if (!playlist)
|
|
return false;
|
|
return playlist_iter_set(playlist, &playlist->pl_current, n);
|
|
}
|
|
|
|
/* Called to advance the current track. */
|
|
static inline bool playlist_current_next(struct playlist *playlist)
|
|
{
|
|
if (!playlist)
|
|
return false;
|
|
return playlist_iter_next(&playlist->pl_current);
|
|
}
|
|
|
|
/* Called to rewind the current track. */
|
|
static inline bool playlist_current_previous(struct playlist *playlist)
|
|
{
|
|
if (!playlist)
|
|
return false;
|
|
playlist->pl_current.it_iter = g_list_previous(playlist->pl_current.it_iter);
|
|
playlist->pl_current.it_pos--;
|
|
return playlist->pl_current.it_iter != NULL;
|
|
}
|
|
|
|
/* Called to get a pointer to the current track. */
|
|
static inline struct track *playlist_current_track(struct playlist *playlist)
|
|
{
|
|
return playlist ? playlist_iter_track(&playlist->pl_current) : NULL;
|
|
}
|
|
|
|
#endif /* OCARINA_CORE_PLAYLISTS_ITERATOR_H */
|