Blame


1 3baa2617 2022-02-16 op /*
2 e3317c86 2023-05-02 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 0a53bd62 2023-08-16 op #include <poll.h>
27 3baa2617 2022-02-16 op #include <signal.h>
28 3baa2617 2022-02-16 op #include <stdio.h>
29 3baa2617 2022-02-16 op #include <stdlib.h>
30 3baa2617 2022-02-16 op #include <stdint.h>
31 3baa2617 2022-02-16 op #include <string.h>
32 3baa2617 2022-02-16 op #include <syslog.h>
33 3baa2617 2022-02-16 op #include <unistd.h>
34 3baa2617 2022-02-16 op
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "control.h"
37 0a53bd62 2023-08-16 op #include "ev.h"
38 3baa2617 2022-02-16 op #include "log.h"
39 3baa2617 2022-02-16 op #include "playlist.h"
40 3baa2617 2022-02-16 op #include "xmalloc.h"
41 3baa2617 2022-02-16 op
42 3baa2617 2022-02-16 op char *csock = NULL;
43 3baa2617 2022-02-16 op int debug;
44 3baa2617 2022-02-16 op int verbose;
45 3baa2617 2022-02-16 op struct imsgev *iev_player;
46 3baa2617 2022-02-16 op
47 3baa2617 2022-02-16 op const char *argv0;
48 3baa2617 2022-02-16 op pid_t player_pid;
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 50e0da0e 2024-01-21 op msgbuf_clear(&iev_player->imsgbuf.w);
63 50e0da0e 2024-01-21 op close(iev_player->imsgbuf.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 0a53bd62 2023-08-16 op main_sig_handler(int sig, int event, void *arg)
83 3baa2617 2022-02-16 op {
84 3baa2617 2022-02-16 op /*
85 0a53bd62 2023-08-16 op * Normal signal handler rules don't apply because ev.c
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 0a53bd62 2023-08-16 op main_dispatch_player(int sig, int 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 50e0da0e 2024-01-21 op struct imsgbuf *imsgbuf = &iev->imsgbuf;
105 3baa2617 2022-02-16 op struct imsg imsg;
106 9307af9e 2024-01-21 op struct ibuf ibuf;
107 17ef54d6 2022-06-22 op size_t datalen;
108 3baa2617 2022-02-16 op ssize_t n;
109 3baa2617 2022-02-16 op int shut = 0;
110 3baa2617 2022-02-16 op
111 0a53bd62 2023-08-16 op if (event & POLLIN) {
112 50e0da0e 2024-01-21 op if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
113 3baa2617 2022-02-16 op fatal("imsg_read error");
114 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
115 3baa2617 2022-02-16 op shut = 1;
116 3baa2617 2022-02-16 op }
117 0a53bd62 2023-08-16 op if (event & POLLOUT) {
118 50e0da0e 2024-01-21 op if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
119 3baa2617 2022-02-16 op fatal("msgbuf_write");
120 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
121 3baa2617 2022-02-16 op shut = 1;
122 3baa2617 2022-02-16 op }
123 3baa2617 2022-02-16 op
124 3baa2617 2022-02-16 op for (;;) {
125 50e0da0e 2024-01-21 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
126 3baa2617 2022-02-16 op fatal("imsg_get");
127 3baa2617 2022-02-16 op if (n == 0) /* No more messages. */
128 3baa2617 2022-02-16 op break;
129 3baa2617 2022-02-16 op
130 ddaffed6 2024-01-21 op switch (imsg_get_type(&imsg)) {
131 ff06024f 2022-07-08 op case IMSG_POS:
132 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &current_position,
133 9307af9e 2024-01-21 op sizeof(current_position)) == -1)
134 9307af9e 2024-01-21 op fatalx("IMSG_POS: got wrong size");
135 ff06024f 2022-07-08 op if (current_position < 0)
136 ff06024f 2022-07-08 op current_position = -1;
137 93cde5d1 2023-01-09 op control_notify(IMSG_CTL_SEEK);
138 ff06024f 2022-07-08 op break;
139 ff06024f 2022-07-08 op case IMSG_LEN:
140 9307af9e 2024-01-21 op if (imsg_get_data(&imsg, &current_duration,
141 9307af9e 2024-01-21 op sizeof(current_duration)) == -1)
142 9307af9e 2024-01-21 op fatalx("IMSG_LEN: got wrong size");
143 ff06024f 2022-07-08 op if (current_duration < 0)
144 ff06024f 2022-07-08 op current_duration = -1;
145 ff06024f 2022-07-08 op break;
146 3baa2617 2022-02-16 op case IMSG_ERR:
147 9307af9e 2024-01-21 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
148 9307af9e 2024-01-21 op (datalen = ibuf_size(&ibuf)) == 0)
149 17ef54d6 2022-06-22 op errstr = "unknown error";
150 17ef54d6 2022-06-22 op else {
151 9307af9e 2024-01-21 op errstr = ibuf_data(&ibuf);
152 17ef54d6 2022-06-22 op errstr[datalen-1] = '\0';
153 17ef54d6 2022-06-22 op }
154 17ef54d6 2022-06-22 op log_warnx("%s; skipping %s", errstr, current_song);
155 13b83883 2022-02-16 op playlist_dropcurrent();
156 9fb94242 2022-07-13 op main_playlist_advance();
157 9fb94242 2022-07-13 op if (play_state == STATE_PLAYING)
158 9fb94242 2022-07-13 op control_notify(IMSG_CTL_NEXT);
159 9fb94242 2022-07-13 op else
160 9fb94242 2022-07-13 op control_notify(IMSG_CTL_STOP);
161 9fb94242 2022-07-13 op break;
162 aaccc123 2022-02-16 op case IMSG_EOF:
163 9fb94242 2022-07-13 op if (repeat_one && main_play_song(current_song))
164 9fb94242 2022-07-13 op break;
165 9fb94242 2022-07-13 op else if (repeat_one || consume)
166 0be6c0b1 2022-02-22 op playlist_dropcurrent();
167 aaccc123 2022-02-16 op main_playlist_advance();
168 87f575c3 2022-02-21 op if (play_state == STATE_PLAYING)
169 949b5c2e 2022-07-12 op control_notify(IMSG_CTL_NEXT);
170 87f575c3 2022-02-21 op else
171 949b5c2e 2022-07-12 op control_notify(IMSG_CTL_STOP);
172 3baa2617 2022-02-16 op break;
173 3baa2617 2022-02-16 op default:
174 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
175 ddaffed6 2024-01-21 op imsg_get_type(&imsg));
176 3baa2617 2022-02-16 op break;
177 3baa2617 2022-02-16 op }
178 3baa2617 2022-02-16 op imsg_free(&imsg);
179 3baa2617 2022-02-16 op }
180 3baa2617 2022-02-16 op
181 0a53bd62 2023-08-16 op if (shut)
182 0a53bd62 2023-08-16 op ev_break();
183 0a53bd62 2023-08-16 op else
184 3baa2617 2022-02-16 op imsg_event_add(iev);
185 3baa2617 2022-02-16 op }
186 3baa2617 2022-02-16 op
187 3baa2617 2022-02-16 op static pid_t
188 3baa2617 2022-02-16 op start_child(enum amused_process proc, int fd)
189 3baa2617 2022-02-16 op {
190 1d673950 2022-03-03 op const char *argv[7];
191 3baa2617 2022-02-16 op int argc = 0;
192 3baa2617 2022-02-16 op pid_t pid;
193 3baa2617 2022-02-16 op
194 1d673950 2022-03-03 op if (fd == -1 && debug)
195 1d673950 2022-03-03 op goto exec;
196 1d673950 2022-03-03 op
197 3baa2617 2022-02-16 op switch (pid = fork()) {
198 3baa2617 2022-02-16 op case -1:
199 3baa2617 2022-02-16 op fatal("cannot fork");
200 3baa2617 2022-02-16 op case 0:
201 3baa2617 2022-02-16 op break;
202 3baa2617 2022-02-16 op default:
203 3baa2617 2022-02-16 op close(fd);
204 3baa2617 2022-02-16 op return pid;
205 3baa2617 2022-02-16 op }
206 3baa2617 2022-02-16 op
207 3baa2617 2022-02-16 op if (fd != 3) {
208 fea541a8 2022-02-16 op if (fd != -1 && dup2(fd, 3) == -1)
209 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
210 3baa2617 2022-02-16 op } else if (fcntl(F_SETFD, 0) == -1)
211 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
212 3baa2617 2022-02-16 op
213 1d673950 2022-03-03 op exec:
214 3baa2617 2022-02-16 op argv[argc++] = argv0;
215 4c86871a 2022-02-17 op
216 3baa2617 2022-02-16 op switch (proc) {
217 3baa2617 2022-02-16 op case PROC_MAIN:
218 4c86871a 2022-02-17 op argv[argc++] = "-s";
219 4c86871a 2022-02-17 op argv[argc++] = csock;
220 1d673950 2022-03-03 op argv[argc++] = "-Tm";
221 fea541a8 2022-02-16 op break;
222 3baa2617 2022-02-16 op case PROC_PLAYER:
223 3baa2617 2022-02-16 op argv[argc++] = "-Tp";
224 3baa2617 2022-02-16 op break;
225 3baa2617 2022-02-16 op }
226 3baa2617 2022-02-16 op
227 3baa2617 2022-02-16 op if (debug)
228 3baa2617 2022-02-16 op argv[argc++] = "-d";
229 3baa2617 2022-02-16 op if (verbose)
230 3baa2617 2022-02-16 op argv[argc++] = "-v";
231 3baa2617 2022-02-16 op argv[argc++] = NULL;
232 3baa2617 2022-02-16 op
233 3baa2617 2022-02-16 op /* obnoxious casts */
234 3baa2617 2022-02-16 op execvp(argv0, (char *const *)argv);
235 3baa2617 2022-02-16 op fatal("execvp %s", argv0);
236 3baa2617 2022-02-16 op }
237 3baa2617 2022-02-16 op
238 3baa2617 2022-02-16 op /* daemon main routine */
239 6a1b899f 2022-03-10 op static __dead void
240 3baa2617 2022-02-16 op amused_main(void)
241 3baa2617 2022-02-16 op {
242 3baa2617 2022-02-16 op int pipe_main2player[2];
243 1738062c 2023-10-07 op int control_fd, flags;
244 3baa2617 2022-02-16 op
245 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
246 3baa2617 2022-02-16 op log_setverbose(verbose);
247 3baa2617 2022-02-16 op log_procinit("main");
248 3baa2617 2022-02-16 op
249 3baa2617 2022-02-16 op if (!debug)
250 3baa2617 2022-02-16 op daemon(1, 0);
251 3baa2617 2022-02-16 op
252 1738062c 2023-10-07 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_main2player) == -1)
253 3baa2617 2022-02-16 op fatal("socketpair");
254 3baa2617 2022-02-16 op
255 1738062c 2023-10-07 op if ((flags = fcntl(pipe_main2player[0], F_GETFL)) == -1 ||
256 1738062c 2023-10-07 op fcntl(pipe_main2player[0], F_SETFL, flags | O_NONBLOCK) == -1 ||
257 1738062c 2023-10-07 op (flags = fcntl(pipe_main2player[1], F_GETFL)) == -1 ||
258 1738062c 2023-10-07 op fcntl(pipe_main2player[1], F_SETFL, flags | O_NONBLOCK) == -1)
259 1738062c 2023-10-07 op fatal("fcntl(O_NONBLOCK)");
260 1738062c 2023-10-07 op
261 1738062c 2023-10-07 op if (fcntl(pipe_main2player[0], F_SETFD, FD_CLOEXEC) == -1 ||
262 1738062c 2023-10-07 op fcntl(pipe_main2player[1], F_SETFD, FD_CLOEXEC) == -1)
263 1738062c 2023-10-07 op fatal("fcntl(CLOEXEC)");
264 1738062c 2023-10-07 op
265 3baa2617 2022-02-16 op player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
266 3baa2617 2022-02-16 op
267 0a53bd62 2023-08-16 op if (ev_init() == -1)
268 0a53bd62 2023-08-16 op fatal("ev_init");
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 0a53bd62 2023-08-16 op ev_signal(SIGINT, main_sig_handler, NULL);
275 0a53bd62 2023-08-16 op ev_signal(SIGTERM, main_sig_handler, NULL);
276 0a53bd62 2023-08-16 op
277 3baa2617 2022-02-16 op iev_player = xmalloc(sizeof(*iev_player));
278 50e0da0e 2024-01-21 op imsg_init(&iev_player->imsgbuf, pipe_main2player[0]);
279 3baa2617 2022-02-16 op iev_player->handler = main_dispatch_player;
280 0a53bd62 2023-08-16 op iev_player->events = POLLIN;
281 50e0da0e 2024-01-21 op ev_add(iev_player->imsgbuf.fd, iev_player->events,
282 3baa2617 2022-02-16 op iev_player->handler, iev_player);
283 3baa2617 2022-02-16 op
284 3baa2617 2022-02-16 op if ((control_fd = control_init(csock)) == -1)
285 3baa2617 2022-02-16 op fatal("control socket setup failed %s", csock);
286 3baa2617 2022-02-16 op control_listen(control_fd);
287 3baa2617 2022-02-16 op
288 3baa2617 2022-02-16 op if (pledge("stdio rpath unix sendfd", NULL) == -1)
289 3baa2617 2022-02-16 op fatal("pledge");
290 3baa2617 2022-02-16 op
291 3baa2617 2022-02-16 op log_info("startup");
292 0a53bd62 2023-08-16 op ev_loop();
293 3baa2617 2022-02-16 op main_shutdown();
294 3baa2617 2022-02-16 op }
295 3baa2617 2022-02-16 op
296 3baa2617 2022-02-16 op int
297 3baa2617 2022-02-16 op main(int argc, char **argv)
298 3baa2617 2022-02-16 op {
299 1d673950 2022-03-03 op int ch, proc = -1;
300 3baa2617 2022-02-16 op
301 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
302 3baa2617 2022-02-16 op log_setverbose(1);
303 3baa2617 2022-02-16 op
304 3baa2617 2022-02-16 op argv0 = argv[0];
305 3baa2617 2022-02-16 op if (argv0 == NULL)
306 3baa2617 2022-02-16 op argv0 = "amused";
307 3baa2617 2022-02-16 op
308 d307e5a2 2022-02-19 op while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
309 3baa2617 2022-02-16 op switch (ch) {
310 3baa2617 2022-02-16 op case 'd':
311 3baa2617 2022-02-16 op debug = 1;
312 3baa2617 2022-02-16 op break;
313 3baa2617 2022-02-16 op case 's':
314 3baa2617 2022-02-16 op free(csock);
315 3baa2617 2022-02-16 op csock = xstrdup(optarg);
316 3baa2617 2022-02-16 op break;
317 3baa2617 2022-02-16 op case 'T':
318 3baa2617 2022-02-16 op switch (*optarg) {
319 1d673950 2022-03-03 op case 'm':
320 1d673950 2022-03-03 op proc = PROC_MAIN;
321 1d673950 2022-03-03 op break;
322 3baa2617 2022-02-16 op case 'p':
323 3baa2617 2022-02-16 op proc = PROC_PLAYER;
324 3baa2617 2022-02-16 op break;
325 3baa2617 2022-02-16 op default:
326 3baa2617 2022-02-16 op usage();
327 3baa2617 2022-02-16 op }
328 3baa2617 2022-02-16 op break;
329 3baa2617 2022-02-16 op case 'v':
330 3baa2617 2022-02-16 op verbose++;
331 3baa2617 2022-02-16 op break;
332 3baa2617 2022-02-16 op default:
333 3baa2617 2022-02-16 op usage();
334 3baa2617 2022-02-16 op }
335 3baa2617 2022-02-16 op }
336 3baa2617 2022-02-16 op argv += optind;
337 3baa2617 2022-02-16 op argc -= optind;
338 3baa2617 2022-02-16 op
339 1d673950 2022-03-03 op if (proc == PROC_MAIN)
340 1d673950 2022-03-03 op amused_main();
341 3baa2617 2022-02-16 op if (proc == PROC_PLAYER)
342 3baa2617 2022-02-16 op exit(player(debug, verbose));
343 3baa2617 2022-02-16 op
344 83f1c895 2023-12-10 op if (csock == NULL) {
345 83f1c895 2023-12-10 op const char *tmpdir;
346 83f1c895 2023-12-10 op
347 83f1c895 2023-12-10 op if ((tmpdir = getenv("TMPDIR")) == NULL)
348 83f1c895 2023-12-10 op tmpdir = "/tmp";
349 3baa2617 2022-02-16 op
350 83f1c895 2023-12-10 op xasprintf(&csock, "%s/amused-%d", tmpdir, getuid());
351 83f1c895 2023-12-10 op }
352 83f1c895 2023-12-10 op
353 1d673950 2022-03-03 op if (argc > 0)
354 1d673950 2022-03-03 op debug = 0;
355 1d673950 2022-03-03 op
356 1d673950 2022-03-03 op ctl(argc, argv);
357 3baa2617 2022-02-16 op }
358 3baa2617 2022-02-16 op
359 3baa2617 2022-02-16 op void
360 fea541a8 2022-02-16 op spawn_daemon(void)
361 fea541a8 2022-02-16 op {
362 fea541a8 2022-02-16 op start_child(PROC_MAIN, -1);
363 fea541a8 2022-02-16 op }
364 fea541a8 2022-02-16 op
365 fea541a8 2022-02-16 op void
366 3baa2617 2022-02-16 op imsg_event_add(struct imsgev *iev)
367 3baa2617 2022-02-16 op {
368 0a53bd62 2023-08-16 op iev->events = POLLIN;
369 50e0da0e 2024-01-21 op if (iev->imsgbuf.w.queued)
370 0a53bd62 2023-08-16 op iev->events |= POLLOUT;
371 3baa2617 2022-02-16 op
372 50e0da0e 2024-01-21 op ev_add(iev->imsgbuf.fd, iev->events, iev->handler, iev);
373 3baa2617 2022-02-16 op }
374 3baa2617 2022-02-16 op
375 3baa2617 2022-02-16 op int
376 3baa2617 2022-02-16 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
377 3baa2617 2022-02-16 op pid_t pid, int fd, const void *data, uint16_t datalen)
378 3baa2617 2022-02-16 op {
379 3baa2617 2022-02-16 op int ret;
380 3baa2617 2022-02-16 op
381 50e0da0e 2024-01-21 op if ((ret = imsg_compose(&iev->imsgbuf, type, peerid, pid, fd, data,
382 3baa2617 2022-02-16 op datalen)) != -1)
383 3baa2617 2022-02-16 op imsg_event_add(iev);
384 3baa2617 2022-02-16 op
385 3baa2617 2022-02-16 op return ret;
386 06961b72 2022-02-16 op }
387 06961b72 2022-02-16 op
388 06961b72 2022-02-16 op int
389 791d3db3 2022-07-09 op main_send_player(uint16_t type, int fd, const void *data, size_t len)
390 06961b72 2022-02-16 op {
391 791d3db3 2022-07-09 op return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
392 3baa2617 2022-02-16 op }
393 3baa2617 2022-02-16 op
394 3baa2617 2022-02-16 op int
395 a975dca9 2022-05-10 op main_play_song(const char *path)
396 3baa2617 2022-02-16 op {
397 dfe2ad96 2022-02-24 op struct stat sb;
398 3baa2617 2022-02-16 op int fd;
399 3baa2617 2022-02-16 op
400 3baa2617 2022-02-16 op if ((fd = open(path, O_RDONLY)) == -1) {
401 3baa2617 2022-02-16 op log_warn("open %s", path);
402 dfe2ad96 2022-02-24 op return 0;
403 dfe2ad96 2022-02-24 op }
404 dfe2ad96 2022-02-24 op
405 dfe2ad96 2022-02-24 op if (fstat(fd, &sb) == -1) {
406 dfe2ad96 2022-02-24 op log_warn("failed to stat %s", path);
407 dfe2ad96 2022-02-24 op close(fd);
408 62396e0b 2022-02-16 op return 0;
409 3baa2617 2022-02-16 op }
410 3baa2617 2022-02-16 op
411 58b2f322 2022-06-11 op if (!S_ISREG(sb.st_mode)) {
412 58b2f322 2022-06-11 op log_info("skipping non-regular file: %s", path);
413 dfe2ad96 2022-02-24 op close(fd);
414 dfe2ad96 2022-02-24 op return 0;
415 dfe2ad96 2022-02-24 op }
416 dfe2ad96 2022-02-24 op
417 aaccc123 2022-02-16 op play_state = STATE_PLAYING;
418 791d3db3 2022-07-09 op main_send_player(IMSG_PLAY, fd, NULL, 0);
419 62396e0b 2022-02-16 op return 1;
420 a913de21 2022-02-17 op }
421 a913de21 2022-02-17 op
422 a913de21 2022-02-17 op void
423 a913de21 2022-02-17 op main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
424 a913de21 2022-02-17 op {
425 a913de21 2022-02-17 op char arg[PATH_MAX];
426 a913de21 2022-02-17 op const char *song;
427 a913de21 2022-02-17 op
428 9307af9e 2024-01-21 op if (imsg_get_data(imsg, arg, sizeof(arg)) == -1) {
429 a913de21 2022-02-17 op main_senderr(iev, "wrong size");
430 a913de21 2022-02-17 op return;
431 a913de21 2022-02-17 op }
432 a913de21 2022-02-17 op
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 e06ad444 2022-06-09 op char path[PATH_MAX];
519 3baa2617 2022-02-16 op const char *err = NULL;
520 3baa2617 2022-02-16 op
521 9307af9e 2024-01-21 op if (imsg_get_data(imsg, path, sizeof(path)) == -1) {
522 3baa2617 2022-02-16 op err = "data size mismatch";
523 3baa2617 2022-02-16 op goto err;
524 3baa2617 2022-02-16 op }
525 3baa2617 2022-02-16 op
526 9307af9e 2024-01-21 op if (path[sizeof(path)-1] != '\0') {
527 3baa2617 2022-02-16 op err = "malformed data";
528 3baa2617 2022-02-16 op goto err;
529 3baa2617 2022-02-16 op }
530 3baa2617 2022-02-16 op
531 170d7716 2022-02-18 op if (tx)
532 19d6b480 2022-02-17 op playlist_push(px, path);
533 19d6b480 2022-02-17 op else
534 19d6b480 2022-02-17 op playlist_enqueue(path);
535 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
536 3baa2617 2022-02-16 op return;
537 3baa2617 2022-02-16 op err:
538 0f5568cb 2022-03-02 op main_senderr(iev, err);
539 3baa2617 2022-02-16 op }
540 d980494c 2022-02-16 op
541 d980494c 2022-02-16 op void
542 d980494c 2022-02-16 op main_send_playlist(struct imsgev *iev)
543 d980494c 2022-02-16 op {
544 63dd8e70 2022-02-17 op struct player_status s;
545 d980494c 2022-02-16 op size_t i;
546 d980494c 2022-02-16 op
547 d980494c 2022-02-16 op for (i = 0; i < playlist.len; ++i) {
548 63dd8e70 2022-02-17 op memset(&s, 0, sizeof(s));
549 63dd8e70 2022-02-17 op strlcpy(s.path, playlist.songs[i], sizeof(s.path));
550 63dd8e70 2022-02-17 op s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
551 63dd8e70 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
552 63dd8e70 2022-02-17 op sizeof(s));
553 d980494c 2022-02-16 op }
554 d980494c 2022-02-16 op
555 d980494c 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
556 bb3f279f 2022-02-16 op }
557 bb3f279f 2022-02-16 op
558 bb3f279f 2022-02-16 op void
559 bb3f279f 2022-02-16 op main_send_status(struct imsgev *iev)
560 bb3f279f 2022-02-16 op {
561 bb3f279f 2022-02-16 op struct player_status s;
562 bb3f279f 2022-02-16 op
563 bb3f279f 2022-02-16 op memset(&s, 0, sizeof(s));
564 bb3f279f 2022-02-16 op
565 74c987d5 2022-02-19 op if (current_song != NULL)
566 74c987d5 2022-02-19 op strlcpy(s.path, current_song, sizeof(s.path));
567 bb3f279f 2022-02-16 op s.status = play_state;
568 ff06024f 2022-07-08 op s.position = current_position;
569 ff06024f 2022-07-08 op s.duration = current_duration;
570 9fb94242 2022-07-13 op s.mode.repeat_all = repeat_all;
571 9fb94242 2022-07-13 op s.mode.repeat_one = repeat_one;
572 9fb94242 2022-07-13 op s.mode.consume = consume;
573 bb3f279f 2022-02-16 op
574 bb3f279f 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
575 15aecb89 2022-07-12 op }
576 15aecb89 2022-07-12 op
577 15aecb89 2022-07-12 op void
578 15aecb89 2022-07-12 op main_seek(struct player_seek *s)
579 15aecb89 2022-07-12 op {
580 15aecb89 2022-07-12 op switch (play_state) {
581 15aecb89 2022-07-12 op case STATE_STOPPED:
582 15aecb89 2022-07-12 op main_playlist_resume();
583 15aecb89 2022-07-12 op break;
584 15aecb89 2022-07-12 op case STATE_PLAYING:
585 15aecb89 2022-07-12 op break;
586 15aecb89 2022-07-12 op case STATE_PAUSED:
587 15aecb89 2022-07-12 op play_state = STATE_PLAYING;
588 15aecb89 2022-07-12 op break;
589 15aecb89 2022-07-12 op }
590 15aecb89 2022-07-12 op
591 15aecb89 2022-07-12 op main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));
592 d980494c 2022-02-16 op }