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>
44 struct imsgev *iev_player;
61 msgbuf_clear(&iev_player->imsgbuf.w);
62 close(iev_player->imsgbuf.fd);
65 log_debug("waiting for children to terminate");
69 if (errno != EINTR && errno != ECHILD)
71 } else if (WIFSIGNALED(status))
72 log_warnx("player terminated; signal %d",
74 } while (pid != -1 || (pid == -1 && errno == EINTR));
76 log_info("terminating");
81 main_sig_handler(int sig, int event, void *arg)
84 * Normal signal handler rules don't apply because ev.c
94 fatalx("unexpected signal %d", sig);
99 main_dispatch_player(int sig, int event, void *d)
102 struct imsgev *iev = d;
103 struct imsgbuf *imsgbuf = &iev->imsgbuf;
110 if (event & EV_READ) {
111 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
112 fatal("imsg_read error");
113 if (n == 0) /* Connection closed */
116 if (event & EV_WRITE) {
117 if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
118 fatal("msgbuf_write");
119 if (n == 0) /* Connection closed */
124 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
126 if (n == 0) /* No more messages. */
129 switch (imsg_get_type(&imsg)) {
131 if (imsg_get_data(&imsg, ¤t_position,
132 sizeof(current_position)) == -1)
133 fatalx("IMSG_POS: got wrong size");
134 if (current_position < 0)
135 current_position = -1;
136 control_notify(IMSG_CTL_SEEK);
139 if (imsg_get_data(&imsg, ¤t_duration,
140 sizeof(current_duration)) == -1)
141 fatalx("IMSG_LEN: got wrong size");
142 if (current_duration < 0)
143 current_duration = -1;
146 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
147 (datalen = ibuf_size(&ibuf)) == 0)
148 errstr = "unknown error";
150 errstr = ibuf_data(&ibuf);
151 errstr[datalen-1] = '\0';
153 log_warnx("%s; skipping %s", errstr, current_song);
154 playlist_dropcurrent();
155 main_playlist_advance();
156 if (play_state == STATE_PLAYING)
157 control_notify(IMSG_CTL_NEXT);
159 control_notify(IMSG_CTL_STOP);
162 if (repeat_one && main_play_song(current_song))
164 else if (repeat_one || consume)
165 playlist_dropcurrent();
166 main_playlist_advance();
167 if (play_state == STATE_PLAYING)
168 control_notify(IMSG_CTL_NEXT);
170 control_notify(IMSG_CTL_STOP);
173 log_debug("%s: error handling imsg %d", __func__,
174 imsg_get_type(&imsg));
187 start_child(enum amused_process proc, int fd)
193 if (fd == -1 && debug)
196 switch (pid = fork()) {
198 fatal("cannot fork");
207 if (fd != -1 && dup2(fd, 3) == -1)
208 fatal("cannot setup imsg fd");
209 } else if (fcntl(F_SETFD, 0) == -1)
210 fatal("cannot setup imsg fd");
213 argv[argc++] = argv0;
218 argv[argc++] = csock;
219 argv[argc++] = "-Tm";
222 argv[argc++] = "-Tp";
232 /* obnoxious casts */
233 execvp(argv0, (char *const *)argv);
234 fatal("execvp %s", argv0);
237 /* daemon main routine */
241 int pipe_main2player[2];
242 int control_fd, flags;
244 log_init(debug, LOG_DAEMON);
245 log_setverbose(verbose);
246 log_procinit("main");
248 if (!debug && daemon(1, 0) == -1)
251 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_main2player) == -1)
254 if ((flags = fcntl(pipe_main2player[0], F_GETFL)) == -1 ||
255 fcntl(pipe_main2player[0], F_SETFL, flags | O_NONBLOCK) == -1 ||
256 (flags = fcntl(pipe_main2player[1], F_GETFL)) == -1 ||
257 fcntl(pipe_main2player[1], F_SETFL, flags | O_NONBLOCK) == -1)
258 fatal("fcntl(O_NONBLOCK)");
260 if (fcntl(pipe_main2player[0], F_SETFD, FD_CLOEXEC) == -1 ||
261 fcntl(pipe_main2player[1], F_SETFD, FD_CLOEXEC) == -1)
262 fatal("fcntl(CLOEXEC)");
264 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
269 signal(SIGHUP, SIG_IGN);
270 signal(SIGCHLD, SIG_IGN);
271 signal(SIGPIPE, SIG_IGN);
273 ev_signal(SIGINT, main_sig_handler, NULL);
274 ev_signal(SIGTERM, main_sig_handler, NULL);
276 iev_player = xmalloc(sizeof(*iev_player));
277 imsg_init(&iev_player->imsgbuf, pipe_main2player[0]);
278 iev_player->handler = main_dispatch_player;
279 iev_player->events = EV_READ;
280 ev_add(iev_player->imsgbuf.fd, iev_player->events,
281 iev_player->handler, iev_player);
283 if ((control_fd = control_init(csock)) == -1)
284 fatal("control socket setup failed %s", csock);
285 control_listen(control_fd);
287 if (pledge("stdio rpath unix sendfd", NULL) == -1)
296 main(int argc, char **argv)
300 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
307 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
314 csock = xstrdup(optarg);
338 if (proc == PROC_MAIN)
340 if (proc == PROC_PLAYER)
341 exit(player(debug, verbose));
346 if ((tmpdir = getenv("TMPDIR")) == NULL)
349 xasprintf(&csock, "%s/amused-%d", tmpdir, getuid());
361 start_child(PROC_MAIN, -1);
365 imsg_event_add(struct imsgev *iev)
367 iev->events = EV_READ;
368 if (iev->imsgbuf.w.queued)
369 iev->events |= EV_WRITE;
371 ev_add(iev->imsgbuf.fd, iev->events, iev->handler, iev);
375 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
376 pid_t pid, int fd, const void *data, uint16_t datalen)
380 if ((ret = imsg_compose(&iev->imsgbuf, type, peerid, pid, fd, data,
388 main_send_player(uint16_t type, int fd, const void *data, size_t len)
390 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
394 main_play_song(const char *path)
399 if ((fd = open(path, O_RDONLY)) == -1) {
400 log_warn("open %s", path);
404 if (fstat(fd, &sb) == -1) {
405 log_warn("failed to stat %s", path);
410 if (!S_ISREG(sb.st_mode)) {
411 log_info("skipping non-regular file: %s", path);
416 play_state = STATE_PLAYING;
417 main_send_player(IMSG_PLAY, fd, NULL, 0);
422 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
427 if (imsg_get_data(imsg, arg, sizeof(arg)) == -1) {
428 main_senderr(iev, "wrong size");
432 if (arg[sizeof(arg)-1] != '\0') {
433 main_senderr(iev, "data corrupted");
437 song = playlist_jump(arg);
439 main_senderr(iev, "not found");
443 control_notify(IMSG_CTL_JUMP);
445 main_send_player(IMSG_STOP, -1, NULL, 0);
446 if (!main_play_song(song)) {
447 main_senderr(iev, "can't play");
448 playlist_dropcurrent();
449 main_playlist_advance();
453 main_send_status(iev);
457 main_playlist_resume(void)
461 if ((song = current_song) == NULL)
462 song = playlist_advance();
464 for (; song != NULL; song = playlist_advance()) {
465 if (main_play_song(song))
468 playlist_dropcurrent();
473 main_playlist_advance(void)
478 song = playlist_advance();
482 if (main_play_song(song))
485 playlist_dropcurrent();
490 main_playlist_previous(void)
495 song = playlist_previous();
499 if (main_play_song(song))
502 playlist_dropcurrent();
507 main_senderr(struct imsgev *iev, const char *msg)
509 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
514 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
518 const char *err = NULL;
520 if (imsg_get_data(imsg, path, sizeof(path)) == -1) {
521 err = "data size mismatch";
525 if (path[sizeof(path)-1] != '\0') {
526 err = "malformed data";
531 playlist_push(px, path);
533 playlist_enqueue(path);
534 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
537 main_senderr(iev, err);
541 main_send_playlist(struct imsgev *iev)
543 struct player_status s;
546 for (i = 0; i < playlist.len; ++i) {
547 memset(&s, 0, sizeof(s));
548 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
549 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
550 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
554 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
558 main_send_status(struct imsgev *iev)
560 struct player_status s;
562 memset(&s, 0, sizeof(s));
564 if (current_song != NULL)
565 strlcpy(s.path, current_song, sizeof(s.path));
566 s.status = play_state;
567 s.position = current_position;
568 s.duration = current_duration;
569 s.mode.repeat_all = repeat_all;
570 s.mode.repeat_one = repeat_one;
571 s.mode.consume = consume;
573 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
577 main_seek(struct player_seek *s)
579 switch (play_state) {
581 main_playlist_resume();
586 play_state = STATE_PLAYING;
590 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));