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