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->imsgbuf.w);
63 close(iev_player->imsgbuf.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 *imsgbuf = &iev->imsgbuf;
111 if (event & POLLIN) {
112 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
113 fatal("imsg_read error");
114 if (n == 0) /* Connection closed */
117 if (event & POLLOUT) {
118 if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
119 fatal("msgbuf_write");
120 if (n == 0) /* Connection closed */
125 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
127 if (n == 0) /* No more messages. */
130 switch (imsg.hdr.type) {
132 if (imsg_get_data(&imsg, ¤t_position,
133 sizeof(current_position)) == -1)
134 fatalx("IMSG_POS: got wrong size");
135 if (current_position < 0)
136 current_position = -1;
137 control_notify(IMSG_CTL_SEEK);
140 if (imsg_get_data(&imsg, ¤t_duration,
141 sizeof(current_duration)) == -1)
142 fatalx("IMSG_LEN: got wrong size");
143 if (current_duration < 0)
144 current_duration = -1;
147 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
148 (datalen = ibuf_size(&ibuf)) == 0)
149 errstr = "unknown error";
151 errstr = ibuf_data(&ibuf);
152 errstr[datalen-1] = '\0';
154 log_warnx("%s; skipping %s", errstr, current_song);
155 playlist_dropcurrent();
156 main_playlist_advance();
157 if (play_state == STATE_PLAYING)
158 control_notify(IMSG_CTL_NEXT);
160 control_notify(IMSG_CTL_STOP);
163 if (repeat_one && main_play_song(current_song))
165 else if (repeat_one || consume)
166 playlist_dropcurrent();
167 main_playlist_advance();
168 if (play_state == STATE_PLAYING)
169 control_notify(IMSG_CTL_NEXT);
171 control_notify(IMSG_CTL_STOP);
174 log_debug("%s: error handling imsg %d", __func__,
188 start_child(enum amused_process proc, int fd)
194 if (fd == -1 && debug)
197 switch (pid = fork()) {
199 fatal("cannot fork");
208 if (fd != -1 && dup2(fd, 3) == -1)
209 fatal("cannot setup imsg fd");
210 } else if (fcntl(F_SETFD, 0) == -1)
211 fatal("cannot setup imsg fd");
214 argv[argc++] = argv0;
219 argv[argc++] = csock;
220 argv[argc++] = "-Tm";
223 argv[argc++] = "-Tp";
233 /* obnoxious casts */
234 execvp(argv0, (char *const *)argv);
235 fatal("execvp %s", argv0);
238 /* daemon main routine */
242 int pipe_main2player[2];
243 int control_fd, flags;
245 log_init(debug, LOG_DAEMON);
246 log_setverbose(verbose);
247 log_procinit("main");
252 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_main2player) == -1)
255 if ((flags = fcntl(pipe_main2player[0], F_GETFL)) == -1 ||
256 fcntl(pipe_main2player[0], F_SETFL, flags | O_NONBLOCK) == -1 ||
257 (flags = fcntl(pipe_main2player[1], F_GETFL)) == -1 ||
258 fcntl(pipe_main2player[1], F_SETFL, flags | O_NONBLOCK) == -1)
259 fatal("fcntl(O_NONBLOCK)");
261 if (fcntl(pipe_main2player[0], F_SETFD, FD_CLOEXEC) == -1 ||
262 fcntl(pipe_main2player[1], F_SETFD, FD_CLOEXEC) == -1)
263 fatal("fcntl(CLOEXEC)");
265 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
270 signal(SIGHUP, SIG_IGN);
271 signal(SIGCHLD, SIG_IGN);
272 signal(SIGPIPE, SIG_IGN);
274 ev_signal(SIGINT, main_sig_handler, NULL);
275 ev_signal(SIGTERM, main_sig_handler, NULL);
277 iev_player = xmalloc(sizeof(*iev_player));
278 imsg_init(&iev_player->imsgbuf, pipe_main2player[0]);
279 iev_player->handler = main_dispatch_player;
280 iev_player->events = POLLIN;
281 ev_add(iev_player->imsgbuf.fd, iev_player->events,
282 iev_player->handler, iev_player);
284 if ((control_fd = control_init(csock)) == -1)
285 fatal("control socket setup failed %s", csock);
286 control_listen(control_fd);
288 if (pledge("stdio rpath unix sendfd", NULL) == -1)
297 main(int argc, char **argv)
301 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
308 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
315 csock = xstrdup(optarg);
339 if (proc == PROC_MAIN)
341 if (proc == PROC_PLAYER)
342 exit(player(debug, verbose));
347 if ((tmpdir = getenv("TMPDIR")) == NULL)
350 xasprintf(&csock, "%s/amused-%d", tmpdir, getuid());
362 start_child(PROC_MAIN, -1);
366 imsg_event_add(struct imsgev *iev)
368 iev->events = POLLIN;
369 if (iev->imsgbuf.w.queued)
370 iev->events |= POLLOUT;
372 ev_add(iev->imsgbuf.fd, iev->events, iev->handler, iev);
376 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
377 pid_t pid, int fd, const void *data, uint16_t datalen)
381 if ((ret = imsg_compose(&iev->imsgbuf, type, peerid, pid, fd, data,
389 main_send_player(uint16_t type, int fd, const void *data, size_t len)
391 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
395 main_play_song(const char *path)
400 if ((fd = open(path, O_RDONLY)) == -1) {
401 log_warn("open %s", path);
405 if (fstat(fd, &sb) == -1) {
406 log_warn("failed to stat %s", path);
411 if (!S_ISREG(sb.st_mode)) {
412 log_info("skipping non-regular file: %s", path);
417 play_state = STATE_PLAYING;
418 main_send_player(IMSG_PLAY, fd, NULL, 0);
423 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
428 if (imsg_get_data(imsg, arg, sizeof(arg)) == -1) {
429 main_senderr(iev, "wrong size");
433 if (arg[sizeof(arg)-1] != '\0') {
434 main_senderr(iev, "data corrupted");
438 song = playlist_jump(arg);
440 main_senderr(iev, "not found");
444 control_notify(IMSG_CTL_JUMP);
446 main_send_player(IMSG_STOP, -1, NULL, 0);
447 if (!main_play_song(song)) {
448 main_senderr(iev, "can't play");
449 playlist_dropcurrent();
450 main_playlist_advance();
454 main_send_status(iev);
458 main_playlist_resume(void)
462 if ((song = current_song) == NULL)
463 song = playlist_advance();
465 for (; song != NULL; song = playlist_advance()) {
466 if (main_play_song(song))
469 playlist_dropcurrent();
474 main_playlist_advance(void)
479 song = playlist_advance();
483 if (main_play_song(song))
486 playlist_dropcurrent();
491 main_playlist_previous(void)
496 song = playlist_previous();
500 if (main_play_song(song))
503 playlist_dropcurrent();
508 main_senderr(struct imsgev *iev, const char *msg)
510 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
515 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
519 const char *err = NULL;
521 if (imsg_get_data(imsg, path, sizeof(path)) == -1) {
522 err = "data size mismatch";
526 if (path[sizeof(path)-1] != '\0') {
527 err = "malformed data";
532 playlist_push(px, path);
534 playlist_enqueue(path);
535 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
538 main_senderr(iev, err);
542 main_send_playlist(struct imsgev *iev)
544 struct player_status s;
547 for (i = 0; i < playlist.len; ++i) {
548 memset(&s, 0, sizeof(s));
549 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
550 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
551 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
555 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
559 main_send_status(struct imsgev *iev)
561 struct player_status s;
563 memset(&s, 0, sizeof(s));
565 if (current_song != NULL)
566 strlcpy(s.path, current_song, sizeof(s.path));
567 s.status = play_state;
568 s.position = current_position;
569 s.duration = current_duration;
570 s.mode.repeat_all = repeat_all;
571 s.mode.repeat_one = repeat_one;
572 s.mode.consume = consume;
574 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
578 main_seek(struct player_seek *s)
580 switch (play_state) {
582 main_playlist_resume();
587 play_state = STATE_PLAYING;
591 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));