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