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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
#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;
static void player_clear_msg(void);
static int handle_mpd_status(int status);
static void
player_clear_msg(void)
{
free(player->msg);
player->msg = NULL;
player->msglvl = PLAYER_MSG_NONE;
}
static int
handle_mpd_status(int status)
{
player_clear_msg();
switch (status) {
case MPD_ERROR_SERVER:
case MPD_ERROR_ARGUMENT:
if (!mpd_connection_clear_error(player->conn))
PANIC("Player failed to recover from error");
case MPD_ERROR_SYSTEM:
PLAYER_STATUS(PLAYER_MSG_ERR, "%s",
mpd_connection_get_error_message(player->conn));
return 1;
case MPD_ERROR_CLOSED:
PANIC("Player encountered non-recoverable error");
}
return 0;
}
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->history = LIST_HEAD;
player->playlist = LIST_HEAD;
player->track = NULL;
player->state = PLAYER_STATE_PAUSED;
player->autoplay = 0;
player->shuffle = 0;
player->action = PLAYER_ACTION_NONE;
player->seek_delay = 0;
player->volume = 0;
player->time_pos = 0;
player->time_end = 0;
player->msg = NULL;
player->msglvl = PLAYER_MSG_INFO;
}
void
player_free(void)
{
struct link *iter;
if (!player->conn) return;
refs_free(&player->queue);
refs_free(&player->history);
refs_free(&player->playlist);
player_clear_msg();
//mpd_run_clear(player->conn);
mpd_connection_free(player->conn);
}
void
player_play_next(struct track *prev)
{
struct link *iter;
struct track *track;
int index;
if (list_empty(&player->playlist))
return;
iter = NULL;
if (player->shuffle) {
/* TODO better algorithm for random sequence */
index = rand() % list_len(&player->playlist);
iter = link_iter(player->playlist.next, index);
ASSERT(iter != NULL);
} else if (player->loaded) {
iter = player->playlist.next;
for (; iter; iter = iter->next) {
track = UPCAST(iter, struct ref)->data;
if (track == prev)
break;
}
if (iter) iter = iter->next;
}
if (!iter) iter = player->playlist.next;
track = UPCAST(iter, struct ref)->data;
player_play_track(track);
}
void
player_update(void)
{
struct mpd_status *status;
struct mpd_song *song;
struct ref *ref;
const char *tmp;
status = mpd_run_status(player->conn);
ASSERT(status != NULL);
song = mpd_run_current_song(player->conn);
if (!song) {
/* if autoplay and another track just finished,
* or there are tracks in queue to be played */
if (player->track && player->autoplay
|| !list_empty(&player->queue)) {
player->action = PLAYER_ACTION_PLAY_NEXT;
}
}
mpd_song_free(song);
mpd_status_free(status);
if (player->action != PLAYER_ACTION_NONE) {
handle_mpd_status(mpd_run_clear(player->conn));
ref = NULL;
switch (player->action) {
case PLAYER_ACTION_PLAY_PREV:
if (list_empty(&player->history))
break;
ref = UPCAST(list_pop_front(&player->history),
struct ref);
/* TODO keep index instead until new track is played */
/* TODO create slimmer player_backend interface */
/* dont add current song to history */
player->track = NULL;
player_play_track(ref->data);
ref_free(ref);
break;
case PLAYER_ACTION_PLAY_NEXT:
if (!list_empty(&player->queue)) {
ref = UPCAST(list_pop_front(&player->queue),
struct ref);
player_play_track(ref->data);
ref_free(ref);
} else {
player_play_next(player->track);
}
break;
default:
PANIC();
}
player->action = PLAYER_ACTION_NONE;
}
/* TODO move prev / next handling to own functions */
status = mpd_run_status(player->conn);
ASSERT(status != NULL);
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);
} else {
player->loaded = false;
player->track = NULL;
player->time_pos = 0;
player->time_end = 0;
}
mpd_song_free(song);
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:
PANIC();
}
player->volume = mpd_status_get_volume(status);
if (player->seek_delay) {
player->seek_delay--;
if (!player->seek_delay)
player_play();
}
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)
{
struct ref *ref;
struct link *link;
ref = ref_init(track);
link = link_back(&player->queue);
link_append(link, LINK(ref));
}
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.next, pos);
link_prepend(link, LINK(ref));
}
int
player_play_track(struct track *track)
{
struct link *link;
int status;
if (player->track && player->track != track) {
list_push_front(&player->history,
LINK(ref_init(player->track)));
}
handle_mpd_status(mpd_run_clear(player->conn));
status = mpd_run_add(player->conn, track->fpath);
if (handle_mpd_status(status))
return PLAYER_ERR;
status = mpd_run_play(player->conn);
if (handle_mpd_status(status))
return PLAYER_ERR;
player->track = track;
return PLAYER_OK;
}
int
player_toggle_pause(void)
{
int status;
status = mpd_run_toggle_pause(player->conn);
if (handle_mpd_status(status))
return PLAYER_ERR;
return PLAYER_OK;
}
int
player_pause(void)
{
int status;
status = mpd_run_pause(player->conn, true);
if (handle_mpd_status(status))
return PLAYER_ERR;
return PLAYER_OK;
}
int
player_resume(void)
{
int status;
status = mpd_run_pause(player->conn, false);
if (handle_mpd_status(status))
return PLAYER_ERR;
return PLAYER_OK;
}
int
player_next(void)
{
player->action = PLAYER_ACTION_PLAY_NEXT;
return PLAYER_OK;
}
int
player_prev(void)
{
player->action = PLAYER_ACTION_PLAY_PREV;
return PLAYER_OK;
}
int
player_play(void)
{
int status;
status = mpd_run_play(player->conn);
if (handle_mpd_status(status))
return PLAYER_ERR;
return PLAYER_OK;
}
int
player_stop(void)
{
int status;
status = mpd_run_stop(player->conn);
if (handle_mpd_status(status))
return PLAYER_ERR;
return PLAYER_OK;
}
int
player_seek(int sec)
{
int status;
player_clear_msg();
if (!player->loaded || player->state == PLAYER_STATE_STOPPED) {
PLAYER_STATUS(PLAYER_MSG_INFO, "No track loaded");
return PLAYER_ERR;
}
status = mpd_run_seek_current(player->conn, sec, false);
if (handle_mpd_status(status))
return PLAYER_ERR;
player->seek_delay = 7;
player_pause();
return PLAYER_OK;
}
int
player_set_volume(unsigned int vol)
{
int status;
player_clear_msg();
if (player->volume == -1) {
PLAYER_STATUS(PLAYER_MSG_INFO, "Volume control not supported");
return PLAYER_ERR;
}
status = mpd_run_set_volume(player->conn, vol);
if (handle_mpd_status(status))
return PLAYER_ERR;
return PLAYER_OK;
}
|