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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
#include "player.h"
#include "ref.h"
#include "portaudio.h"
#include "sndfile.h"
#include "util.h"
#include <mpd/song.h>
#include <mpd/status.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define PLAYER_STATUS(lvl, ...) do { \
player->msglvl = lvl; \
if (player->msg) free(player->msg); \
player->msg = aprintf(__VA_ARGS__); \
} while (0)
static struct player player_static;
struct player *player;
void
player_init(void)
{
player = malloc(sizeof(struct player));
ASSERT(player != NULL);
player->conn = mpd_connection_new(NULL, 0, 0);
ASSERT(player->conn != NULL);
player->queue = LIST_HEAD;
player->track = NULL;
player->state = PLAYER_STATE_PAUSED;
player->volume = 0;
player->time_pos = 0;
player->time_end = 0;
player->msg = NULL;
player->msglvl = PLAYER_MSG_INFO;
mpd_run_stop(player->conn);
mpd_run_clear(player->conn);
}
void
player_free(void)
{
if (!player->conn) return;
mpd_run_stop(player->conn);
mpd_run_clear(player->conn);
mpd_connection_free(player->conn);
}
void
player_update(void)
{
struct mpd_status *status;
struct mpd_song *song;
const char *tmp;
status = mpd_run_status(player->conn);
ASSERT(status != NULL);
switch (mpd_status_get_state(status)) {
case MPD_STATE_PAUSE:
player->state = PLAYER_STATE_PAUSED;
break;
case MPD_STATE_PLAY:
player->state = PLAYER_STATE_PLAYING;
break;
case MPD_STATE_STOP:
player->state = PLAYER_STATE_STOPPED;
break;
default:
ASSERT(0);
}
player->volume = mpd_status_get_volume(status);
song = mpd_run_current_song(player->conn);
if (song) {
player->loaded = true;
player->time_pos = mpd_status_get_elapsed_time(status);
player->time_end = mpd_song_get_duration(song);
mpd_song_free(song);
} else {
player->track = NULL;
player->loaded = false;
player->time_pos = 0;
player->time_end = 0;
}
mpd_status_free(status);
}
void
player_queue_clear(void)
{
struct ref *ref;
while (player->queue.next) {
ref = UPCAST(link_pop(player->queue.next), struct ref);
ref_free(ref);
}
}
void
player_queue_append(struct track *track)
{
player_queue_insert(track, list_len(&player->queue));
}
void
player_queue_insert(struct track *track, size_t pos)
{
struct ref *ref;
struct link *link;
ref = ref_init(track);
link = link_iter(&player->queue, pos);
link_append(link, &ref->link);
}
int
player_play_track(struct track *track)
{
player_clear_msg();
player->track = track;
mpd_run_stop(player->conn);
mpd_run_clear(player->conn);
if (!mpd_run_add(player->conn, player->track->fpath)
|| !mpd_run_play(player->conn)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Playback failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_toggle_pause(void)
{
if (!mpd_run_toggle_pause(player->conn)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Pause toggle failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_pause(void)
{
if (!mpd_run_pause(player->conn, true)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Pausing track failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_resume(void)
{
if (!mpd_run_pause(player->conn, false)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Resuming track failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_next(void)
{
if (!mpd_run_next(player->conn)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Playing next track failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_prev(void)
{
if (!mpd_run_previous(player->conn)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Playing prev track failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_stop(void)
{
if (!mpd_run_stop(player->conn)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Stopping track failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_seek(int sec)
{
if (player->state == PLAYER_STATE_STOPPED) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Cannot seek stopped track");
return PLAYER_ERR;
}
if (!mpd_run_seek_current(player->conn, sec, false)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Track seek failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_set_volume(unsigned int vol)
{
if (player->volume == -1) {
PLAYER_STATUS(PLAYER_MSG_INFO, "Setting volume not supported");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
if (!mpd_run_set_volume(player->conn, vol)) {
PLAYER_STATUS(PLAYER_MSG_ERR, "Setting volume failed");
mpd_run_clearerror(player->conn);
return PLAYER_ERR;
}
return PLAYER_OK;
}
void
player_clear_msg(void)
{
free(player->msg);
player->msg = NULL;
player->msglvl = PLAYER_MSG_NONE;
}
|