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];
248 log_init(debug, LOG_DAEMON);
249 log_setverbose(verbose);
250 log_procinit("main");
255 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
256 PF_UNSPEC, pipe_main2player) == -1)
259 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
264 signal(SIGHUP, SIG_IGN);
265 signal(SIGCHLD, SIG_IGN);
266 signal(SIGPIPE, SIG_IGN);
268 ev_signal(SIGINT, main_sig_handler, NULL);
269 ev_signal(SIGTERM, main_sig_handler, NULL);
271 iev_player = xmalloc(sizeof(*iev_player));
272 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
273 iev_player->handler = main_dispatch_player;
274 iev_player->events = POLLIN;
275 ev_add(iev_player->ibuf.fd, iev_player->events,
276 iev_player->handler, iev_player);
278 if ((control_fd = control_init(csock)) == -1)
279 fatal("control socket setup failed %s", csock);
280 control_listen(control_fd);
282 if (pledge("stdio rpath unix sendfd", NULL) == -1)
291 main(int argc, char **argv)
295 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
302 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
309 csock = xstrdup(optarg);
333 if (proc == PROC_MAIN)
335 if (proc == PROC_PLAYER)
336 exit(player(debug, verbose));
339 xasprintf(&csock, "/tmp/amused-%d", getuid());
350 start_child(PROC_MAIN, -1);
354 imsg_event_add(struct imsgev *iev)
356 iev->events = POLLIN;
357 if (iev->ibuf.w.queued)
358 iev->events |= POLLOUT;
360 ev_add(iev->ibuf.fd, iev->events, iev->handler, iev);
364 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
365 pid_t pid, int fd, const void *data, uint16_t datalen)
369 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
377 main_send_player(uint16_t type, int fd, const void *data, size_t len)
379 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
383 main_play_song(const char *path)
388 if ((fd = open(path, O_RDONLY)) == -1) {
389 log_warn("open %s", path);
393 if (fstat(fd, &sb) == -1) {
394 log_warn("failed to stat %s", path);
399 if (!S_ISREG(sb.st_mode)) {
400 log_info("skipping non-regular file: %s", path);
405 play_state = STATE_PLAYING;
406 main_send_player(IMSG_PLAY, fd, NULL, 0);
411 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
417 datalen = IMSG_DATA_SIZE(*imsg);
418 if (datalen != sizeof(arg)) {
419 main_senderr(iev, "wrong size");
423 memcpy(arg, imsg->data, sizeof(arg));
424 if (arg[sizeof(arg)-1] != '\0') {
425 main_senderr(iev, "data corrupted");
429 song = playlist_jump(arg);
431 main_senderr(iev, "not found");
435 control_notify(IMSG_CTL_JUMP);
437 main_send_player(IMSG_STOP, -1, NULL, 0);
438 if (!main_play_song(song)) {
439 main_senderr(iev, "can't play");
440 playlist_dropcurrent();
441 main_playlist_advance();
445 main_send_status(iev);
449 main_playlist_resume(void)
453 if ((song = current_song) == NULL)
454 song = playlist_advance();
456 for (; song != NULL; song = playlist_advance()) {
457 if (main_play_song(song))
460 playlist_dropcurrent();
465 main_playlist_advance(void)
470 song = playlist_advance();
474 if (main_play_song(song))
477 playlist_dropcurrent();
482 main_playlist_previous(void)
487 song = playlist_previous();
491 if (main_play_song(song))
494 playlist_dropcurrent();
499 main_senderr(struct imsgev *iev, const char *msg)
501 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
506 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
511 const char *err = NULL;
513 datalen = IMSG_DATA_SIZE(*imsg);
514 if (datalen != sizeof(path)) {
515 err = "data size mismatch";
519 memcpy(path, imsg->data, sizeof(path));
520 if (path[datalen-1] != '\0') {
521 err = "malformed data";
526 playlist_push(px, path);
528 playlist_enqueue(path);
529 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
532 main_senderr(iev, err);
536 main_send_playlist(struct imsgev *iev)
538 struct player_status s;
541 for (i = 0; i < playlist.len; ++i) {
542 memset(&s, 0, sizeof(s));
543 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
544 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
545 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
549 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
553 main_send_status(struct imsgev *iev)
555 struct player_status s;
557 memset(&s, 0, sizeof(s));
559 if (current_song != NULL)
560 strlcpy(s.path, current_song, sizeof(s.path));
561 s.status = play_state;
562 s.position = current_position;
563 s.duration = current_duration;
564 s.mode.repeat_all = repeat_all;
565 s.mode.repeat_one = repeat_one;
566 s.mode.consume = consume;
568 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
572 main_seek(struct player_seek *s)
574 switch (play_state) {
576 main_playlist_resume();
581 play_state = STATE_PLAYING;
585 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));