2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/socket.h>
45 struct imsgev *iev_player;
62 msgbuf_clear(&iev_player->ibuf.w);
63 close(iev_player->ibuf.fd);
66 log_debug("waiting for children to terminate");
70 if (errno != EINTR && errno != ECHILD)
72 } else if (WIFSIGNALED(status))
73 log_warnx("player terminated; signal %d",
75 } while (pid != -1 || (pid == -1 && errno == EINTR));
77 log_info("terminating");
82 main_sig_handler(int sig, int event, void *arg)
85 * Normal signal handler rules don't apply because ev.c
95 fatalx("unexpected signal %d", sig);
100 main_dispatch_player(int sig, int event, void *d)
103 struct imsgev *iev = d;
104 struct imsgbuf *ibuf = &iev->ibuf;
110 if (event & POLLIN) {
111 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
112 fatal("imsg_read error");
113 if (n == 0) /* Connection closed */
116 if (event & POLLOUT) {
117 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
118 fatal("msgbuf_write");
119 if (n == 0) /* Connection closed */
124 if ((n = imsg_get(ibuf, &imsg)) == -1)
126 if (n == 0) /* No more messages. */
129 datalen = IMSG_DATA_SIZE(imsg);
130 switch (imsg.hdr.type) {
132 if (datalen != sizeof(current_position))
133 fatalx("IMSG_POS: got wrong size (%zu vs %zu)",
134 datalen, sizeof(current_position));
135 memcpy(¤t_position, imsg.data,
136 sizeof(current_position));
137 if (current_position < 0)
138 current_position = -1;
139 control_notify(IMSG_CTL_SEEK);
142 if (datalen != sizeof(current_duration))
143 fatalx("IMSG_LEN: got wrong size (%zu vs %zu)",
144 datalen, sizeof(current_duration));
145 memcpy(¤t_duration, imsg.data,
146 sizeof(current_duration));
147 if (current_duration < 0)
148 current_duration = -1;
152 errstr = "unknown error";
155 errstr[datalen-1] = '\0';
157 log_warnx("%s; skipping %s", errstr, current_song);
158 playlist_dropcurrent();
159 main_playlist_advance();
160 if (play_state == STATE_PLAYING)
161 control_notify(IMSG_CTL_NEXT);
163 control_notify(IMSG_CTL_STOP);
166 if (repeat_one && main_play_song(current_song))
168 else if (repeat_one || consume)
169 playlist_dropcurrent();
170 main_playlist_advance();
171 if (play_state == STATE_PLAYING)
172 control_notify(IMSG_CTL_NEXT);
174 control_notify(IMSG_CTL_STOP);
177 log_debug("%s: error handling imsg %d", __func__,
191 start_child(enum amused_process proc, int fd)
197 if (fd == -1 && debug)
200 switch (pid = fork()) {
202 fatal("cannot fork");
211 if (fd != -1 && dup2(fd, 3) == -1)
212 fatal("cannot setup imsg fd");
213 } else if (fcntl(F_SETFD, 0) == -1)
214 fatal("cannot setup imsg fd");
217 argv[argc++] = argv0;
222 argv[argc++] = csock;
223 argv[argc++] = "-Tm";
226 argv[argc++] = "-Tp";
236 /* obnoxious casts */
237 execvp(argv0, (char *const *)argv);
238 fatal("execvp %s", argv0);
241 /* daemon main routine */
245 int pipe_main2player[2];
246 int control_fd, flags;
248 log_init(debug, LOG_DAEMON);
249 log_setverbose(verbose);
250 log_procinit("main");
255 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_main2player) == -1)
258 if ((flags = fcntl(pipe_main2player[0], F_GETFL)) == -1 ||
259 fcntl(pipe_main2player[0], F_SETFL, flags | O_NONBLOCK) == -1 ||
260 (flags = fcntl(pipe_main2player[1], F_GETFL)) == -1 ||
261 fcntl(pipe_main2player[1], F_SETFL, flags | O_NONBLOCK) == -1)
262 fatal("fcntl(O_NONBLOCK)");
264 if (fcntl(pipe_main2player[0], F_SETFD, FD_CLOEXEC) == -1 ||
265 fcntl(pipe_main2player[1], F_SETFD, FD_CLOEXEC) == -1)
266 fatal("fcntl(CLOEXEC)");
268 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
273 signal(SIGHUP, SIG_IGN);
274 signal(SIGCHLD, SIG_IGN);
275 signal(SIGPIPE, SIG_IGN);
277 ev_signal(SIGINT, main_sig_handler, NULL);
278 ev_signal(SIGTERM, main_sig_handler, NULL);
280 iev_player = xmalloc(sizeof(*iev_player));
281 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
282 iev_player->handler = main_dispatch_player;
283 iev_player->events = POLLIN;
284 ev_add(iev_player->ibuf.fd, iev_player->events,
285 iev_player->handler, iev_player);
287 if ((control_fd = control_init(csock)) == -1)
288 fatal("control socket setup failed %s", csock);
289 control_listen(control_fd);
291 if (pledge("stdio rpath unix sendfd", NULL) == -1)
300 main(int argc, char **argv)
304 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
311 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
318 csock = xstrdup(optarg);
342 if (proc == PROC_MAIN)
344 if (proc == PROC_PLAYER)
345 exit(player(debug, verbose));
350 if ((tmpdir = getenv("TMPDIR")) == NULL)
353 xasprintf(&csock, "%s/amused-%d", tmpdir, getuid());
365 start_child(PROC_MAIN, -1);
369 imsg_event_add(struct imsgev *iev)
371 iev->events = POLLIN;
372 if (iev->ibuf.w.queued)
373 iev->events |= POLLOUT;
375 ev_add(iev->ibuf.fd, iev->events, iev->handler, iev);
379 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
380 pid_t pid, int fd, const void *data, uint16_t datalen)
384 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
392 main_send_player(uint16_t type, int fd, const void *data, size_t len)
394 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
398 main_play_song(const char *path)
403 if ((fd = open(path, O_RDONLY)) == -1) {
404 log_warn("open %s", path);
408 if (fstat(fd, &sb) == -1) {
409 log_warn("failed to stat %s", path);
414 if (!S_ISREG(sb.st_mode)) {
415 log_info("skipping non-regular file: %s", path);
420 play_state = STATE_PLAYING;
421 main_send_player(IMSG_PLAY, fd, NULL, 0);
426 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
432 datalen = IMSG_DATA_SIZE(*imsg);
433 if (datalen != sizeof(arg)) {
434 main_senderr(iev, "wrong size");
438 memcpy(arg, imsg->data, sizeof(arg));
439 if (arg[sizeof(arg)-1] != '\0') {
440 main_senderr(iev, "data corrupted");
444 song = playlist_jump(arg);
446 main_senderr(iev, "not found");
450 control_notify(IMSG_CTL_JUMP);
452 main_send_player(IMSG_STOP, -1, NULL, 0);
453 if (!main_play_song(song)) {
454 main_senderr(iev, "can't play");
455 playlist_dropcurrent();
456 main_playlist_advance();
460 main_send_status(iev);
464 main_playlist_resume(void)
468 if ((song = current_song) == NULL)
469 song = playlist_advance();
471 for (; song != NULL; song = playlist_advance()) {
472 if (main_play_song(song))
475 playlist_dropcurrent();
480 main_playlist_advance(void)
485 song = playlist_advance();
489 if (main_play_song(song))
492 playlist_dropcurrent();
497 main_playlist_previous(void)
502 song = playlist_previous();
506 if (main_play_song(song))
509 playlist_dropcurrent();
514 main_senderr(struct imsgev *iev, const char *msg)
516 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
521 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
526 const char *err = NULL;
528 datalen = IMSG_DATA_SIZE(*imsg);
529 if (datalen != sizeof(path)) {
530 err = "data size mismatch";
534 memcpy(path, imsg->data, sizeof(path));
535 if (path[datalen-1] != '\0') {
536 err = "malformed data";
541 playlist_push(px, path);
543 playlist_enqueue(path);
544 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
547 main_senderr(iev, err);
551 main_send_playlist(struct imsgev *iev)
553 struct player_status s;
556 for (i = 0; i < playlist.len; ++i) {
557 memset(&s, 0, sizeof(s));
558 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
559 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
560 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
564 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
568 main_send_status(struct imsgev *iev)
570 struct player_status s;
572 memset(&s, 0, sizeof(s));
574 if (current_song != NULL)
575 strlcpy(s.path, current_song, sizeof(s.path));
576 s.status = play_state;
577 s.position = current_position;
578 s.duration = current_duration;
579 s.mode.repeat_all = repeat_all;
580 s.mode.repeat_one = repeat_one;
581 s.mode.consume = consume;
583 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
587 main_seek(struct player_seek *s)
589 switch (play_state) {
591 main_playlist_resume();
596 play_state = STATE_PLAYING;
600 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));