Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
7 3baa2617 2022-02-16 op *
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/queue.h>
19 3baa2617 2022-02-16 op #include <sys/socket.h>
20 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <sys/wait.h>
22 3baa2617 2022-02-16 op
23 3baa2617 2022-02-16 op #include <event.h>
24 3baa2617 2022-02-16 op #include <errno.h>
25 3baa2617 2022-02-16 op #include <fcntl.h>
26 3baa2617 2022-02-16 op #include <limits.h>
27 3baa2617 2022-02-16 op #include <sndio.h>
28 3baa2617 2022-02-16 op #include <signal.h>
29 3baa2617 2022-02-16 op #include <stdio.h>
30 3baa2617 2022-02-16 op #include <stdlib.h>
31 3baa2617 2022-02-16 op #include <stdint.h>
32 3baa2617 2022-02-16 op #include <string.h>
33 3baa2617 2022-02-16 op #include <syslog.h>
34 3baa2617 2022-02-16 op #include <unistd.h>
35 3baa2617 2022-02-16 op #include <imsg.h>
36 3baa2617 2022-02-16 op
37 3baa2617 2022-02-16 op #include "amused.h"
38 3baa2617 2022-02-16 op #include "control.h"
39 3baa2617 2022-02-16 op #include "log.h"
40 3baa2617 2022-02-16 op #include "playlist.h"
41 3baa2617 2022-02-16 op #include "xmalloc.h"
42 3baa2617 2022-02-16 op
43 3baa2617 2022-02-16 op struct sio_hdl *hdl;
44 3baa2617 2022-02-16 op char *csock = NULL;
45 3baa2617 2022-02-16 op int debug;
46 3baa2617 2022-02-16 op int verbose;
47 3baa2617 2022-02-16 op struct imsgev *iev_player;
48 3baa2617 2022-02-16 op
49 3baa2617 2022-02-16 op const char *argv0;
50 3baa2617 2022-02-16 op pid_t player_pid;
51 3baa2617 2022-02-16 op struct event ev_sigint;
52 3baa2617 2022-02-16 op struct event ev_sigterm;
53 3baa2617 2022-02-16 op
54 3baa2617 2022-02-16 op enum amused_process {
55 3baa2617 2022-02-16 op PROC_MAIN,
56 3baa2617 2022-02-16 op PROC_PLAYER,
57 3baa2617 2022-02-16 op };
58 3baa2617 2022-02-16 op
59 3baa2617 2022-02-16 op __dead void
60 3baa2617 2022-02-16 op main_shutdown(void)
61 3baa2617 2022-02-16 op {
62 3baa2617 2022-02-16 op pid_t pid;
63 3baa2617 2022-02-16 op int status;
64 3baa2617 2022-02-16 op
65 3baa2617 2022-02-16 op /* close pipes. */
66 3baa2617 2022-02-16 op msgbuf_clear(&iev_player->ibuf.w);
67 3baa2617 2022-02-16 op close(iev_player->ibuf.fd);
68 3baa2617 2022-02-16 op free(iev_player);
69 3baa2617 2022-02-16 op
70 3baa2617 2022-02-16 op log_debug("waiting for children to terminate");
71 3baa2617 2022-02-16 op do {
72 3baa2617 2022-02-16 op pid = wait(&status);
73 3baa2617 2022-02-16 op if (pid == -1) {
74 3baa2617 2022-02-16 op if (errno != EINTR && errno != ECHILD)
75 3baa2617 2022-02-16 op fatal("wait");
76 3baa2617 2022-02-16 op } else if (WIFSIGNALED(status))
77 3baa2617 2022-02-16 op log_warnx("player terminated; signal %d",
78 3baa2617 2022-02-16 op WTERMSIG(status));
79 3baa2617 2022-02-16 op } while (pid != -1 || (pid == -1 && errno == EINTR));
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op log_info("terminating");
82 3baa2617 2022-02-16 op exit(0);
83 3baa2617 2022-02-16 op }
84 3baa2617 2022-02-16 op
85 3baa2617 2022-02-16 op static void
86 3baa2617 2022-02-16 op main_sig_handler(int sig, short event, void *arg)
87 3baa2617 2022-02-16 op {
88 3baa2617 2022-02-16 op /*
89 3baa2617 2022-02-16 op * Normal signal handler rules don't apply because libevent
90 3baa2617 2022-02-16 op * decouples for us.
91 3baa2617 2022-02-16 op */
92 3baa2617 2022-02-16 op
93 3baa2617 2022-02-16 op switch (sig) {
94 3baa2617 2022-02-16 op case SIGTERM:
95 3baa2617 2022-02-16 op case SIGINT:
96 3baa2617 2022-02-16 op main_shutdown();
97 3baa2617 2022-02-16 op break;
98 3baa2617 2022-02-16 op default:
99 3baa2617 2022-02-16 op fatalx("unexpected signal %d", sig);
100 3baa2617 2022-02-16 op }
101 3baa2617 2022-02-16 op }
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op static void
104 3baa2617 2022-02-16 op main_dispatch_player(int sig, short event, void *d)
105 3baa2617 2022-02-16 op {
106 3baa2617 2022-02-16 op struct imsgev *iev = d;
107 3baa2617 2022-02-16 op struct imsgbuf *ibuf = &iev->ibuf;
108 3baa2617 2022-02-16 op struct imsg imsg;
109 3baa2617 2022-02-16 op ssize_t n;
110 3baa2617 2022-02-16 op int shut = 0;
111 3baa2617 2022-02-16 op
112 3baa2617 2022-02-16 op if (event & EV_READ) {
113 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
114 3baa2617 2022-02-16 op fatal("imsg_read error");
115 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
116 3baa2617 2022-02-16 op shut = 1;
117 3baa2617 2022-02-16 op }
118 3baa2617 2022-02-16 op if (event & EV_WRITE) {
119 3baa2617 2022-02-16 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
120 3baa2617 2022-02-16 op fatal("msgbuf_write");
121 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
122 3baa2617 2022-02-16 op shut = 1;
123 3baa2617 2022-02-16 op }
124 3baa2617 2022-02-16 op
125 3baa2617 2022-02-16 op for (;;) {
126 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
127 3baa2617 2022-02-16 op fatal("imsg_get");
128 3baa2617 2022-02-16 op if (n == 0) /* No more messages. */
129 3baa2617 2022-02-16 op break;
130 3baa2617 2022-02-16 op
131 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
132 3baa2617 2022-02-16 op case IMSG_ERR:
133 13b83883 2022-02-16 op playlist_dropcurrent();
134 601093db 2022-02-22 op /* fallthrough */
135 aaccc123 2022-02-16 op case IMSG_EOF:
136 0be6c0b1 2022-02-22 op if (repeat_one && current_song != NULL) {
137 f3bd773b 2022-02-19 op if (main_play_song(current_song))
138 f3bd773b 2022-02-19 op break;
139 0be6c0b1 2022-02-22 op playlist_dropcurrent();
140 0be6c0b1 2022-02-22 op }
141 aaccc123 2022-02-16 op main_playlist_advance();
142 87f575c3 2022-02-21 op if (play_state == STATE_PLAYING)
143 87f575c3 2022-02-21 op control_notify(NULL, IMSG_CTL_NEXT);
144 87f575c3 2022-02-21 op else
145 87f575c3 2022-02-21 op control_notify(NULL, IMSG_CTL_STOP);
146 3baa2617 2022-02-16 op break;
147 3baa2617 2022-02-16 op default:
148 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
149 3baa2617 2022-02-16 op imsg.hdr.type);
150 3baa2617 2022-02-16 op break;
151 3baa2617 2022-02-16 op }
152 3baa2617 2022-02-16 op imsg_free(&imsg);
153 3baa2617 2022-02-16 op }
154 3baa2617 2022-02-16 op
155 3baa2617 2022-02-16 op if (!shut)
156 3baa2617 2022-02-16 op imsg_event_add(iev);
157 3baa2617 2022-02-16 op else {
158 3baa2617 2022-02-16 op /* This pipe is dead. Remove its event handler. */
159 3baa2617 2022-02-16 op event_del(&iev->ev);
160 3baa2617 2022-02-16 op event_loopexit(NULL);
161 3baa2617 2022-02-16 op }
162 3baa2617 2022-02-16 op }
163 3baa2617 2022-02-16 op
164 3baa2617 2022-02-16 op static pid_t
165 3baa2617 2022-02-16 op start_child(enum amused_process proc, int fd)
166 3baa2617 2022-02-16 op {
167 4c86871a 2022-02-17 op const char *argv[6];
168 3baa2617 2022-02-16 op int argc = 0;
169 3baa2617 2022-02-16 op pid_t pid;
170 3baa2617 2022-02-16 op
171 3baa2617 2022-02-16 op switch (pid = fork()) {
172 3baa2617 2022-02-16 op case -1:
173 3baa2617 2022-02-16 op fatal("cannot fork");
174 3baa2617 2022-02-16 op case 0:
175 3baa2617 2022-02-16 op break;
176 3baa2617 2022-02-16 op default:
177 3baa2617 2022-02-16 op close(fd);
178 3baa2617 2022-02-16 op return pid;
179 3baa2617 2022-02-16 op }
180 3baa2617 2022-02-16 op
181 3baa2617 2022-02-16 op if (fd != 3) {
182 fea541a8 2022-02-16 op if (fd != -1 && dup2(fd, 3) == -1)
183 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
184 3baa2617 2022-02-16 op } else if (fcntl(F_SETFD, 0) == -1)
185 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
186 3baa2617 2022-02-16 op
187 3baa2617 2022-02-16 op argv[argc++] = argv0;
188 4c86871a 2022-02-17 op
189 3baa2617 2022-02-16 op switch (proc) {
190 3baa2617 2022-02-16 op case PROC_MAIN:
191 4c86871a 2022-02-17 op argv[argc++] = "-s";
192 4c86871a 2022-02-17 op argv[argc++] = csock;
193 fea541a8 2022-02-16 op break;
194 3baa2617 2022-02-16 op case PROC_PLAYER:
195 3baa2617 2022-02-16 op argv[argc++] = "-Tp";
196 3baa2617 2022-02-16 op break;
197 3baa2617 2022-02-16 op }
198 3baa2617 2022-02-16 op
199 3baa2617 2022-02-16 op if (debug)
200 3baa2617 2022-02-16 op argv[argc++] = "-d";
201 3baa2617 2022-02-16 op if (verbose)
202 3baa2617 2022-02-16 op argv[argc++] = "-v";
203 3baa2617 2022-02-16 op argv[argc++] = NULL;
204 3baa2617 2022-02-16 op
205 3baa2617 2022-02-16 op /* obnoxious casts */
206 3baa2617 2022-02-16 op execvp(argv0, (char *const *)argv);
207 3baa2617 2022-02-16 op fatal("execvp %s", argv0);
208 3baa2617 2022-02-16 op }
209 3baa2617 2022-02-16 op
210 3baa2617 2022-02-16 op /* daemon main routine */
211 3baa2617 2022-02-16 op static __dead int
212 3baa2617 2022-02-16 op amused_main(void)
213 3baa2617 2022-02-16 op {
214 3baa2617 2022-02-16 op int pipe_main2player[2];
215 3baa2617 2022-02-16 op int control_fd;
216 3baa2617 2022-02-16 op
217 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
218 3baa2617 2022-02-16 op log_setverbose(verbose);
219 3baa2617 2022-02-16 op log_procinit("main");
220 3baa2617 2022-02-16 op
221 3baa2617 2022-02-16 op if (!debug)
222 3baa2617 2022-02-16 op daemon(1, 0);
223 3baa2617 2022-02-16 op
224 3baa2617 2022-02-16 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
225 3baa2617 2022-02-16 op PF_UNSPEC, pipe_main2player) == -1)
226 3baa2617 2022-02-16 op fatal("socketpair");
227 3baa2617 2022-02-16 op
228 3baa2617 2022-02-16 op player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
229 3baa2617 2022-02-16 op
230 3baa2617 2022-02-16 op event_init();
231 3baa2617 2022-02-16 op
232 3baa2617 2022-02-16 op signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
233 3baa2617 2022-02-16 op signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
234 3baa2617 2022-02-16 op
235 3baa2617 2022-02-16 op signal_add(&ev_sigint, NULL);
236 3baa2617 2022-02-16 op signal_add(&ev_sigterm, NULL);
237 3baa2617 2022-02-16 op
238 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
239 3baa2617 2022-02-16 op signal(SIGCHLD, SIG_IGN);
240 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
241 3baa2617 2022-02-16 op
242 3baa2617 2022-02-16 op iev_player = xmalloc(sizeof(*iev_player));
243 3baa2617 2022-02-16 op imsg_init(&iev_player->ibuf, pipe_main2player[0]);
244 3baa2617 2022-02-16 op iev_player->handler = main_dispatch_player;
245 3baa2617 2022-02-16 op iev_player->events = EV_READ;
246 3baa2617 2022-02-16 op event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
247 3baa2617 2022-02-16 op iev_player->handler, iev_player);
248 3baa2617 2022-02-16 op event_add(&iev_player->ev, NULL);
249 3baa2617 2022-02-16 op
250 3baa2617 2022-02-16 op if ((control_fd = control_init(csock)) == -1)
251 3baa2617 2022-02-16 op fatal("control socket setup failed %s", csock);
252 3baa2617 2022-02-16 op control_listen(control_fd);
253 3baa2617 2022-02-16 op
254 3baa2617 2022-02-16 op if (pledge("stdio rpath unix sendfd", NULL) == -1)
255 3baa2617 2022-02-16 op fatal("pledge");
256 3baa2617 2022-02-16 op
257 3baa2617 2022-02-16 op log_info("startup");
258 3baa2617 2022-02-16 op event_dispatch();
259 3baa2617 2022-02-16 op main_shutdown();
260 3baa2617 2022-02-16 op }
261 3baa2617 2022-02-16 op
262 3baa2617 2022-02-16 op int
263 3baa2617 2022-02-16 op main(int argc, char **argv)
264 3baa2617 2022-02-16 op {
265 3baa2617 2022-02-16 op int ch, proc = PROC_MAIN;
266 3baa2617 2022-02-16 op
267 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
268 3baa2617 2022-02-16 op log_setverbose(1);
269 3baa2617 2022-02-16 op
270 3baa2617 2022-02-16 op argv0 = argv[0];
271 3baa2617 2022-02-16 op if (argv0 == NULL)
272 3baa2617 2022-02-16 op argv0 = "amused";
273 3baa2617 2022-02-16 op
274 d307e5a2 2022-02-19 op while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
275 3baa2617 2022-02-16 op switch (ch) {
276 3baa2617 2022-02-16 op case 'd':
277 3baa2617 2022-02-16 op debug = 1;
278 3baa2617 2022-02-16 op break;
279 3baa2617 2022-02-16 op case 's':
280 3baa2617 2022-02-16 op free(csock);
281 3baa2617 2022-02-16 op csock = xstrdup(optarg);
282 3baa2617 2022-02-16 op break;
283 3baa2617 2022-02-16 op case 'T':
284 3baa2617 2022-02-16 op switch (*optarg) {
285 3baa2617 2022-02-16 op case 'p':
286 3baa2617 2022-02-16 op proc = PROC_PLAYER;
287 3baa2617 2022-02-16 op break;
288 3baa2617 2022-02-16 op default:
289 3baa2617 2022-02-16 op usage();
290 3baa2617 2022-02-16 op }
291 3baa2617 2022-02-16 op break;
292 3baa2617 2022-02-16 op case 'v':
293 3baa2617 2022-02-16 op verbose++;
294 3baa2617 2022-02-16 op break;
295 3baa2617 2022-02-16 op default:
296 3baa2617 2022-02-16 op usage();
297 3baa2617 2022-02-16 op }
298 3baa2617 2022-02-16 op }
299 3baa2617 2022-02-16 op argv += optind;
300 3baa2617 2022-02-16 op argc -= optind;
301 3baa2617 2022-02-16 op
302 3baa2617 2022-02-16 op if (proc == PROC_PLAYER)
303 3baa2617 2022-02-16 op exit(player(debug, verbose));
304 3baa2617 2022-02-16 op
305 3baa2617 2022-02-16 op if (csock == NULL)
306 3baa2617 2022-02-16 op xasprintf(&csock, "/tmp/amused-%d", getuid());
307 3baa2617 2022-02-16 op
308 3baa2617 2022-02-16 op if (argc == 0)
309 3baa2617 2022-02-16 op amused_main();
310 3baa2617 2022-02-16 op else
311 3baa2617 2022-02-16 op ctl(argc, argv);
312 3baa2617 2022-02-16 op return 0;
313 3baa2617 2022-02-16 op }
314 3baa2617 2022-02-16 op
315 3baa2617 2022-02-16 op void
316 fea541a8 2022-02-16 op spawn_daemon(void)
317 fea541a8 2022-02-16 op {
318 fea541a8 2022-02-16 op debug = 0;
319 fea541a8 2022-02-16 op start_child(PROC_MAIN, -1);
320 fea541a8 2022-02-16 op }
321 fea541a8 2022-02-16 op
322 fea541a8 2022-02-16 op void
323 3baa2617 2022-02-16 op imsg_event_add(struct imsgev *iev)
324 3baa2617 2022-02-16 op {
325 3baa2617 2022-02-16 op iev->events = EV_READ;
326 3baa2617 2022-02-16 op if (iev->ibuf.w.queued)
327 3baa2617 2022-02-16 op iev->events |= EV_WRITE;
328 3baa2617 2022-02-16 op
329 3baa2617 2022-02-16 op event_del(&iev->ev);
330 3baa2617 2022-02-16 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
331 3baa2617 2022-02-16 op event_add(&iev->ev, NULL);
332 3baa2617 2022-02-16 op }
333 3baa2617 2022-02-16 op
334 3baa2617 2022-02-16 op int
335 3baa2617 2022-02-16 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
336 3baa2617 2022-02-16 op pid_t pid, int fd, const void *data, uint16_t datalen)
337 3baa2617 2022-02-16 op {
338 3baa2617 2022-02-16 op int ret;
339 3baa2617 2022-02-16 op
340 3baa2617 2022-02-16 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
341 3baa2617 2022-02-16 op datalen)) != -1)
342 3baa2617 2022-02-16 op imsg_event_add(iev);
343 3baa2617 2022-02-16 op
344 3baa2617 2022-02-16 op return ret;
345 06961b72 2022-02-16 op }
346 06961b72 2022-02-16 op
347 06961b72 2022-02-16 op int
348 06961b72 2022-02-16 op main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
349 06961b72 2022-02-16 op {
350 06961b72 2022-02-16 op return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
351 3baa2617 2022-02-16 op }
352 3baa2617 2022-02-16 op
353 3baa2617 2022-02-16 op int
354 3baa2617 2022-02-16 op main_play_song(const char *song)
355 3baa2617 2022-02-16 op {
356 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
357 3baa2617 2022-02-16 op int fd;
358 3baa2617 2022-02-16 op
359 3baa2617 2022-02-16 op strlcpy(path, song, sizeof(path));
360 3baa2617 2022-02-16 op if ((fd = open(path, O_RDONLY)) == -1) {
361 3baa2617 2022-02-16 op log_warn("open %s", path);
362 62396e0b 2022-02-16 op return 0;
363 3baa2617 2022-02-16 op }
364 3baa2617 2022-02-16 op
365 aaccc123 2022-02-16 op play_state = STATE_PLAYING;
366 3baa2617 2022-02-16 op imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
367 3baa2617 2022-02-16 op path, sizeof(path));
368 62396e0b 2022-02-16 op return 1;
369 a913de21 2022-02-17 op }
370 a913de21 2022-02-17 op
371 a913de21 2022-02-17 op void
372 a913de21 2022-02-17 op main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
373 a913de21 2022-02-17 op {
374 a913de21 2022-02-17 op size_t datalen;
375 a913de21 2022-02-17 op char arg[PATH_MAX];
376 a913de21 2022-02-17 op const char *song;
377 a913de21 2022-02-17 op
378 a913de21 2022-02-17 op datalen = IMSG_DATA_SIZE(*imsg);
379 a913de21 2022-02-17 op if (datalen != sizeof(arg)) {
380 a913de21 2022-02-17 op main_senderr(iev, "wrong size");
381 a913de21 2022-02-17 op return;
382 a913de21 2022-02-17 op }
383 a913de21 2022-02-17 op
384 a913de21 2022-02-17 op memcpy(arg, imsg->data, sizeof(arg));
385 a913de21 2022-02-17 op if (arg[sizeof(arg)-1] != '\0') {
386 a913de21 2022-02-17 op main_senderr(iev, "data corrupted");
387 a913de21 2022-02-17 op return;
388 a913de21 2022-02-17 op }
389 a913de21 2022-02-17 op
390 a913de21 2022-02-17 op song = playlist_jump(arg);
391 a913de21 2022-02-17 op if (song == NULL) {
392 a913de21 2022-02-17 op main_senderr(iev, "not found");
393 a913de21 2022-02-17 op return;
394 a913de21 2022-02-17 op }
395 a913de21 2022-02-17 op
396 a913de21 2022-02-17 op main_send_player(IMSG_STOP, -1, NULL, 0);
397 a913de21 2022-02-17 op if (!main_play_song(song)) {
398 a913de21 2022-02-17 op main_senderr(iev, "can't play");
399 a913de21 2022-02-17 op playlist_dropcurrent();
400 a913de21 2022-02-17 op main_playlist_advance();
401 a913de21 2022-02-17 op return;
402 a913de21 2022-02-17 op }
403 a913de21 2022-02-17 op
404 a913de21 2022-02-17 op main_send_status(iev);
405 2f589330 2022-02-17 op }
406 2f589330 2022-02-17 op
407 2f589330 2022-02-17 op void
408 2f589330 2022-02-17 op main_playlist_resume(void)
409 2f589330 2022-02-17 op {
410 2f589330 2022-02-17 op const char *song;
411 2f589330 2022-02-17 op
412 74c987d5 2022-02-19 op if ((song = current_song) == NULL)
413 2f589330 2022-02-17 op song = playlist_advance();
414 2f589330 2022-02-17 op
415 c8777fe4 2022-02-17 op for (; song != NULL; song = playlist_advance()) {
416 2f589330 2022-02-17 op if (main_play_song(song))
417 2f589330 2022-02-17 op return;
418 2f589330 2022-02-17 op
419 2f589330 2022-02-17 op playlist_dropcurrent();
420 2f589330 2022-02-17 op }
421 aaccc123 2022-02-16 op }
422 aaccc123 2022-02-16 op
423 aaccc123 2022-02-16 op void
424 aaccc123 2022-02-16 op main_playlist_advance(void)
425 aaccc123 2022-02-16 op {
426 aaccc123 2022-02-16 op const char *song;
427 aaccc123 2022-02-16 op
428 aaccc123 2022-02-16 op for (;;) {
429 aaccc123 2022-02-16 op song = playlist_advance();
430 aaccc123 2022-02-16 op if (song == NULL)
431 aaccc123 2022-02-16 op return;
432 aaccc123 2022-02-16 op
433 aaccc123 2022-02-16 op if (main_play_song(song))
434 aaccc123 2022-02-16 op break;
435 aaccc123 2022-02-16 op
436 13b83883 2022-02-16 op playlist_dropcurrent();
437 aaccc123 2022-02-16 op }
438 3baa2617 2022-02-16 op }
439 3baa2617 2022-02-16 op
440 3baa2617 2022-02-16 op void
441 af27e631 2022-02-17 op main_playlist_previous(void)
442 af27e631 2022-02-17 op {
443 af27e631 2022-02-17 op const char *song;
444 af27e631 2022-02-17 op
445 af27e631 2022-02-17 op for (;;) {
446 af27e631 2022-02-17 op song = playlist_previous();
447 af27e631 2022-02-17 op if (song == NULL)
448 af27e631 2022-02-17 op return;
449 af27e631 2022-02-17 op
450 af27e631 2022-02-17 op if (main_play_song(song))
451 af27e631 2022-02-17 op break;
452 af27e631 2022-02-17 op
453 af27e631 2022-02-17 op playlist_dropcurrent();
454 af27e631 2022-02-17 op }
455 af27e631 2022-02-17 op }
456 af27e631 2022-02-17 op
457 af27e631 2022-02-17 op void
458 aaccc123 2022-02-16 op main_restart_track(void)
459 aaccc123 2022-02-16 op {
460 aaccc123 2022-02-16 op const char *song;
461 aaccc123 2022-02-16 op
462 74c987d5 2022-02-19 op song = current_song;
463 aaccc123 2022-02-16 op if (song == NULL)
464 aaccc123 2022-02-16 op return;
465 aaccc123 2022-02-16 op
466 aaccc123 2022-02-16 op if (main_play_song(song))
467 aaccc123 2022-02-16 op return;
468 aaccc123 2022-02-16 op
469 13b83883 2022-02-16 op playlist_dropcurrent();
470 aaccc123 2022-02-16 op main_playlist_advance();
471 aaccc123 2022-02-16 op }
472 aaccc123 2022-02-16 op
473 aaccc123 2022-02-16 op void
474 19d6b480 2022-02-17 op main_senderr(struct imsgev *iev, const char *msg)
475 3baa2617 2022-02-16 op {
476 19d6b480 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
477 19d6b480 2022-02-17 op msg, strlen(msg)+1);
478 19d6b480 2022-02-17 op }
479 19d6b480 2022-02-17 op
480 19d6b480 2022-02-17 op void
481 19d6b480 2022-02-17 op main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
482 19d6b480 2022-02-17 op struct imsg *imsg)
483 19d6b480 2022-02-17 op {
484 3baa2617 2022-02-16 op size_t datalen;
485 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
486 3baa2617 2022-02-16 op const char *err = NULL;
487 3baa2617 2022-02-16 op
488 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
489 3baa2617 2022-02-16 op if (datalen != sizeof(path)) {
490 3baa2617 2022-02-16 op err = "data size mismatch";
491 3baa2617 2022-02-16 op goto err;
492 3baa2617 2022-02-16 op }
493 3baa2617 2022-02-16 op
494 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
495 3baa2617 2022-02-16 op if (path[datalen-1] != '\0') {
496 3baa2617 2022-02-16 op err = "malformed data";
497 3baa2617 2022-02-16 op goto err;
498 3baa2617 2022-02-16 op }
499 3baa2617 2022-02-16 op
500 170d7716 2022-02-18 op if (tx)
501 19d6b480 2022-02-17 op playlist_push(px, path);
502 19d6b480 2022-02-17 op else
503 19d6b480 2022-02-17 op playlist_enqueue(path);
504 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
505 3baa2617 2022-02-16 op return;
506 3baa2617 2022-02-16 op err:
507 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
508 3baa2617 2022-02-16 op }
509 d980494c 2022-02-16 op
510 d980494c 2022-02-16 op void
511 d980494c 2022-02-16 op main_send_playlist(struct imsgev *iev)
512 d980494c 2022-02-16 op {
513 63dd8e70 2022-02-17 op struct player_status s;
514 d980494c 2022-02-16 op size_t i;
515 d980494c 2022-02-16 op
516 d980494c 2022-02-16 op for (i = 0; i < playlist.len; ++i) {
517 63dd8e70 2022-02-17 op memset(&s, 0, sizeof(s));
518 63dd8e70 2022-02-17 op strlcpy(s.path, playlist.songs[i], sizeof(s.path));
519 63dd8e70 2022-02-17 op s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
520 63dd8e70 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
521 63dd8e70 2022-02-17 op sizeof(s));
522 d980494c 2022-02-16 op }
523 d980494c 2022-02-16 op
524 d980494c 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
525 bb3f279f 2022-02-16 op }
526 bb3f279f 2022-02-16 op
527 bb3f279f 2022-02-16 op void
528 bb3f279f 2022-02-16 op main_send_status(struct imsgev *iev)
529 bb3f279f 2022-02-16 op {
530 bb3f279f 2022-02-16 op struct player_status s;
531 bb3f279f 2022-02-16 op
532 bb3f279f 2022-02-16 op memset(&s, 0, sizeof(s));
533 bb3f279f 2022-02-16 op
534 74c987d5 2022-02-19 op if (current_song != NULL)
535 74c987d5 2022-02-19 op strlcpy(s.path, current_song, sizeof(s.path));
536 bb3f279f 2022-02-16 op s.status = play_state;
537 44398779 2022-02-19 op s.rp.repeat_all = repeat_all;
538 44398779 2022-02-19 op s.rp.repeat_one = repeat_one;
539 bb3f279f 2022-02-16 op
540 bb3f279f 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
541 d980494c 2022-02-16 op }