summaryrefslogtreecommitdiffstats
path: root/src/player.c
blob: 6e1a69b7b31cdc6df8207b03828872f108a1625f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "player.h"

#include "list.h"
#include "data.h"

static bool player_history_contains(struct track *cmp, int depth);
static struct track *playlist_track_next_unused(struct link *start);
static struct track *player_next_from_playlist(void);

bool
player_history_contains(struct track *cmp, int depth)
{
	struct link *link;
	struct track *track;

	link = list_back(&player.history);
	while (LIST_INNER(link) && depth-- > 0) {
		track = UPCAST(link, struct track, link_hs);
		if (track == cmp)
			return true;
		link = link->prev;
	}

	return false;
}

struct track *
playlist_track_next_unused(struct link *start)
{
	struct track *track;
	struct link *link;
	bool in_history;
	int len;

	track = NULL;

	len = list_len(&player.playlist);
	if (!len) return NULL;

	link = start;
	while (LIST_INNER(link)) {
		track = UPCAST(link, struct track, link_pl);

		in_history = player_history_contains(track, len - 2);
		if (track != player.track && !in_history)
			break;

		link = link->next;
		if (!LIST_INNER(link))
			link = list_front(&player.playlist);

		if (link == start)
			return NULL;
	}

	return track;
}

struct track *
player_next_from_playlist(void)
{
	struct link *link;
	int index;

	if (list_empty(&player.playlist))
		return NULL;

	if (player.shuffle) {
		index = rand() % list_len(&player.playlist);
		link = list_at(&player.playlist, index);
		return playlist_track_next_unused(link);
	} else {
		if (player.track && link_inuse(&player.track->link_pl)) {
			index = list_index(&player.playlist,
				&player.track->link_pl);
			if (index < 0) PANIC();
			if (++index == list_len(&player.playlist))
				return NULL;
		} else {
			index = 0;
		}
		link = list_at(&player.playlist, index);
		return UPCAST(link, struct track, link_pl);
	}

	return NULL;
}

int
player_prev(void)
{
	struct link *next;
	struct track *track;

	if (list_empty(&player.history))
		return PLAYER_STATUS_ERR;

	if (!player.track || !link_inuse(&player.track->link_hs)) {
		next = list_back(&player.history);
	} else if (LIST_INNER(player.track->link_hs.prev)) {
		next = player.track->link_hs.prev;
	} else {
		return PLAYER_STATUS_ERR;
	}

	track = UPCAST(next, struct track, link_hs);
	player_play_track(track, false);

	return PLAYER_STATUS_OK;
}

int
player_next(void)
{
	struct track *next_track;
	struct link *link;
	bool new_entry;

	if (player.track && link_inuse(&player.track->link_hs)
			&& LIST_INNER(player.track->link_hs.next)) {
		next_track = UPCAST(player.track->link_hs.next,
			struct track, link_hs);
		new_entry = false;
	} else if (!list_empty(&player.queue)) {
		link = list_pop_front(&player.queue);
		next_track = UPCAST(link, struct track, link_pq);
		new_entry = true;
	} else {
		next_track = player_next_from_playlist();
		if (!next_track) goto clear;
		new_entry = true;
	}

	player_play_track(next_track, new_entry);
	return PLAYER_STATUS_OK;

clear:
	player_clear_track();
	return PLAYER_STATUS_ERR;
}