ap

An audio player suited to my tastes
Log | Files | Refs | README | LICENSE

commit aa3afe5769604feb4ea5b39c4e7d9aabd354b5eb
parent 4e4763cf7a2592753229976d926f8655775877fc
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 14 Jul 2021 03:34:22 -0700

Move implementation specific functions from aps.c to main.c

Diffstat:
Maps/aps.c | 144++++++++++++++++++-------------------------------------------------------------
Maps/aps.h | 7+++++++
Aaps/main.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Maps/mkfile | 2+-
4 files changed, 131 insertions(+), 113 deletions(-)

diff --git a/aps/aps.c b/aps/aps.c @@ -17,45 +17,54 @@ */ #include <sys/socket.h> -#include <sys/wait.h> +#include <assert.h> #include <errno.h> #include <limits.h> #include <poll.h> -#include <signal.h> -#include <stdio.h> #include <stdlib.h> -#ifdef __OpenBSD__ -#include <unistd.h> -#endif - -#include <aputil.h> - #include "aps.h" #include "command.h" #include "config.h" #include "util.h" -static struct aps *aps; - -void -sigclose(int sig) +struct aps * +aps_open(char *path) { - aps->close = 1; + struct aps *aps; + int i; + + aps = calloc(1, sizeof(*aps)); + if (aps == NULL) + return NULL; + + aps->con = apopen(path, bind, SOCK_NONBLOCK); + if (aps->con == NULL || listen(aps->con->sock, 10)) { + free(aps); + return NULL; + } + + for (i = 0; i < LEN(aps->pfds); ++i) { + aps->pfds[i].fd = -1; + aps->pfds[i].events = POLLIN | POLLOUT; + } + + return aps; } void -sigchld(int sig) +aps_close(struct aps *aps) { - int status; - pid_t pid; + int i; - while ((pid = - waitpid(-1, &status, WCONTINUED | WNOHANG | WUNTRACED)) > 0) { - if (pid == aps->player.pid) - pupdate(&aps->player, status); - } + for (i = 0; i < LEN(aps->next); ++i) + free(aps->next[i]); + for (i = 0; i < LEN(aps->pfds); ++i) + if (aps->pfds[i].fd != -1) + aps_drop(aps, i); + apclose(aps->con); + pstop(&aps->player); } int @@ -105,7 +114,7 @@ aps_handle(struct aps *aps, int fd) if (aps->clients[fd].resp->lock && respond(aps, fd)) return 1; } else { - die("unhandled poll revent"); + assert(!"unhandled poll event"); } return 0; @@ -129,92 +138,3 @@ aps_update(struct aps *aps) return 0; } - -int -run(struct aps *aps) -{ - while (!aps->close) { - if (!aps->player.state && aps->queue) - aps_next(aps, -1, 0, NULL); - if ((aps_accept(aps) || aps_update(aps)) && errno != EINTR) - return 1; - } - return 0; -} - -struct aps * -aps_open(char *path) -{ - struct aps *aps; - int i; - - aps = calloc(1, sizeof(*aps)); - if (aps == NULL) - return NULL; - - aps->con = apopen(path, bind, SOCK_NONBLOCK); - if (aps->con == NULL || listen(aps->con->sock, 10)) { - free(aps); - return NULL; - } - - for (i = 0; i < LEN(aps->pfds); ++i) { - aps->pfds[i].fd = -1; - aps->pfds[i].events = POLLIN | POLLOUT; - } - - return aps; -} - -void -aps_close(struct aps *aps) -{ - int i; - - for (i = 0; i < LEN(aps->next); ++i) - free(aps->next[i]); - for (i = 0; i < LEN(aps->pfds); ++i) - if (aps->pfds[i].fd != -1) - aps_drop(aps, i); - apclose(aps->con); - pstop(&aps->player); -} - -int -main(int argc, char *argv[]) -{ - int i; - -#ifdef __OpenBSD__ - if (pledge("stdio rpath cpath proc exec unix", NULL)) - die("pledge"); -#endif - - signal(SIGCHLD, sigchld); - signal(SIGINT, sigclose); - signal(SIGTERM, sigclose); - - aps = aps_open(NULL); - if (aps == NULL) - die("unable to open connection"); - - for (i = 1; player[i]; ++i) - if (!player[i][0]) - player[i] = aps->player.path; - - if (aps_next(aps, -1, LEN(next), next)) - die("aps_next"); - - aps->player.argv = player + 1; - aps->player.prog = player[0]; - aps->player.state = STOPPED; - - if (run(aps)) { - perror("error"); - aps_close(aps); - return 1; - } - - aps_close(aps); - return 0; -} diff --git a/aps/aps.h b/aps/aps.h @@ -21,3 +21,10 @@ struct aps { struct pollfd pfds[OPEN_MAX]; struct item *queue; }; + +struct aps *aps_open(char *); +void aps_close(struct aps *); +int aps_drop(struct aps *, int); +int aps_accept(struct aps *); +int aps_handle(struct aps *, int); +int aps_update(struct aps *); diff --git a/aps/main.c b/aps/main.c @@ -0,0 +1,91 @@ +#include <sys/socket.h> +#include <sys/wait.h> + +#include <errno.h> +#include <limits.h> +#include <poll.h> +#include <signal.h> +#include <stdio.h> + +#ifdef __OpenBSD__ +#include <unistd.h> +#endif + +#include <aputil.h> + +#include "aps.h" +#include "command.h" +#include "config.h" +#include "util.h" + +static struct aps *aps; + +void +sigclose(int sig) +{ + aps->close = 1; +} + +void +sigchld(int sig) +{ + int status; + pid_t pid; + + while ((pid = + waitpid(-1, &status, WCONTINUED | WNOHANG | WUNTRACED)) > 0) { + if (pid == aps->player.pid) + pupdate(&aps->player, status); + } +} + +int +run(struct aps *aps) +{ + while (!aps->close) { + if (!aps->player.state && aps->queue) + aps_next(aps, -1, 0, NULL); + if ((aps_accept(aps) || aps_update(aps)) && errno != EINTR) + return 1; + } + return 0; +} + +int +main(int argc, char *argv[]) +{ + int i; + +#ifdef __OpenBSD__ + if (pledge("stdio rpath cpath proc exec unix", NULL)) + die("pledge"); +#endif + + aps = aps_open(NULL); + if (aps == NULL) + die("unable to open connection"); + + signal(SIGCHLD, sigchld); + signal(SIGINT, sigclose); + signal(SIGTERM, sigclose); + + for (i = 1; player[i]; ++i) + if (!player[i][0]) + player[i] = aps->player.path; + + if (aps_next(aps, -1, LEN(next), next)) + die("aps_next"); + + aps->player.argv = player + 1; + aps->player.prog = player[0]; + aps->player.state = STOPPED; + + if (run(aps)) { + perror("error"); + aps_close(aps); + return 1; + } + + aps_close(aps); + return 0; +} diff --git a/aps/mkfile b/aps/mkfile @@ -1,7 +1,7 @@ # Copyright 2021 Jacob R. Edwards name = aps -src = aps.c buf.c command.c find.c item.c match.c player.c queue.c response.c split.c +src = aps.c buf.c command.c find.c item.c main.c match.c player.c queue.c response.c split.c obj = ${src:%.c=%.o} lib = ap aputil mk = ../mk