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
|
#include "player.h"
#include "tui.h"
#include "data.h"
#include "list.h"
#include "util.h"
#include "log.h"
#include <sys/wait.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MPLAY_STATUS(line) do { \
free(user_status); \
user_status = aprintf("Player: %s", \
line ? line : "no response"); \
user_status_uptime = 20; \
} while (0)
struct mplay_player {
FILE *stdin;
FILE *stdout;
pid_t pid;
uint64_t update_ms;
};
struct player player;
struct mplay_player mplay;
static void sigpipe_handler(int sig);
static void mplay_kill(void);
static bool mplay_run(struct track *track);
static char *mplay_readline(void);
void
sigpipe_handler(int sig)
{
mplay_kill();
}
bool
mplay_run(struct track *track)
{
int output[2];
int input[2];
char *path;
char *line;
ASSERT(!player.loaded);
if (pipe(input) == -1)
ERROR(SYSTEM, "pipe");
if (pipe(output) == -1)
ERROR(SYSTEM, "pipe");
mplay.pid = fork();
if (mplay.pid < 0) ERROR(SYSTEM, "fork");
if (mplay.pid != 0) {
close(output[1]);
mplay.stdout = fdopen(output[0], "r");
if (!mplay.stdout) ERROR(SYSTEM, "fdopen");
setvbuf(mplay.stdout, NULL, _IONBF, 0);
close(input[0]);
mplay.stdin = fdopen(input[1], "w");
if (!mplay.stdin) ERROR(SYSTEM, "fdopen");
setvbuf(mplay.stdin, NULL, _IONBF, 0);
} else {
close(0);
close(1);
close(2);
dup2(input[0], 0);
dup2(output[1], 1);
dup2(output[1], 2);
close(output[0]);
close(output[1]);
path = aprintf("%s/%s/%s", datadir,
track->tag->name, track->name);
execl("/usr/bin/mplay", "mplay", "-i", path, NULL);
abort();
}
player.loaded = true;
line = mplay_readline();
if (!line || strcmp(line, "+READY")) {
mplay_kill();
MPLAY_STATUS(line);
return false;
}
return true;
}
void
mplay_kill(void)
{
if (!player.loaded)
return;
kill(mplay.pid, SIGKILL);
waitpid(mplay.pid, NULL, 0);
player.loaded = false;
}
char *
mplay_readline(void)
{
static char linebuf[256];
char *tok;
/* TODO: add timeout */
if (!mplay.stdout || !fgets(linebuf, sizeof(linebuf), mplay.stdout)) {
mplay_kill(); /* dont clear track yet */
return NULL;
}
tok = strchr(linebuf, '\n');
if (tok) *tok = '\0';
return linebuf;
}
void
player_init(void)
{
list_init(&player.playlist);
list_init(&player.history);
list_init(&player.queue);
player.track = NULL;
player.track_name = NULL;
player.loaded = false;
player.autoplay = true;
player.shuffle = true;
player.state = PLAYER_STATE_PAUSED;
player.volume = 50;
player.time_pos = 0;
player.time_end = 0;
signal(SIGPIPE, sigpipe_handler);
}
void
player_deinit(void)
{
list_clear(&player.playlist);
list_clear(&player.queue);
list_clear(&player.history);
free(player.track_name);
mplay_kill();
}
void
player_update(void)
{
bool queue_empty;
char *tok, *line;
if (!player.loaded) {
queue_empty = list_empty(&player.queue);
if (player.track && player.autoplay || !queue_empty) {
if (player_next() != PLAYER_OK)
player_clear_track();
} else if (player.track) {
player_clear_track();
}
}
if (!player.loaded) return;
if (current_ms() >= mplay.update_ms + 330) {
mplay.update_ms = current_ms();
fprintf(mplay.stdin, "status\n");
line = mplay_readline();
if (!line || strncmp(line, "+STATUS:", 8)) {
mplay_kill();
if (!line || strncmp(line, "+EXIT:", 6))
MPLAY_STATUS(line);
return;
}
tok = line;
while ((tok = strchr(tok, ' '))) {
if (!strncmp(tok + 1, "vol:", 4)) {
player.volume = atoi(tok + 5);
} else if (!strncmp(tok + 1, "pause:", 6)) {
player.state = atoi(tok + 7)
? PLAYER_STATE_PAUSED : PLAYER_STATE_PLAYING;
} else if (!strncmp(tok + 1, "pos:", 4)) {
player.time_pos = atoi(tok + 5);
player.time_end = MAX(player.time_pos, player.time_end);
}
tok += 1;
}
}
}
int
player_play_track(struct track *track, bool new)
{
ASSERT(track != NULL);
player_clear_track();
player.track = track;
if (!mplay_run(track))
return PLAYER_ERR;
/* new invocations are removed from history */
if (new) link_pop(&track->link_hs);
player.time_pos = 0;
player.time_end = 0;
return PLAYER_OK;
}
int
player_clear_track(void)
{
if (player.track)
player_add_history(player.track);
player.track = NULL;
mplay_kill();
return PLAYER_OK;
}
int
player_toggle_pause(void)
{
char *line;
fprintf(mplay.stdin, "pause\n");
line = mplay_readline();
if (!line || strncmp(line, "+PAUSE:", 7)) {
mplay_kill();
MPLAY_STATUS(line);
return PLAYER_ERR;
}
return PLAYER_OK;
}
int
player_pause(void)
{
if (player.state != PLAYER_STATE_PAUSED)
player_toggle_pause();
return PLAYER_OK;
}
int
player_resume(void)
{
if (player.state != PLAYER_STATE_PLAYING)
player_toggle_pause();
return PLAYER_OK;
}
int
player_play(void)
{
return PLAYER_OK;
}
int
player_stop(void)
{
player_clear_track();
return PLAYER_OK;
}
int
player_seek(int sec)
{
char *line;
fprintf(mplay.stdin, "seek %i\n", sec);
line = mplay_readline();
if (!line || strncmp(line, "+SEEK:", 6)) {
mplay_kill();
if (line && !strncmp(line, "+EXIT:", 6))
return PLAYER_OK;
MPLAY_STATUS(line);
return PLAYER_ERR;
}
player.time_pos = atoi(line + 7);
player.time_end = MAX(player.time_pos, player.time_end);
return PLAYER_OK;
}
int
player_set_volume(unsigned int vol)
{
char *line;
if (player.volume == -1) {
PLAYER_STATUS("volume control not supported");
return PLAYER_ERR;
}
fprintf(mplay.stdin, "vol %i\n", vol);
line = mplay_readline();
if (!line || strncmp(line, "+VOLUME:", 8)) {
mplay_kill();
MPLAY_STATUS(line);
return PLAYER_ERR;
}
player.volume = atoi(line + 9);
return PLAYER_OK;
}
|