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