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