ap

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

commit 800410a3d6773fe21dab5c539a6f973a3d2340df
parent 1927f056fabfa553dfb81630e017b8be6f02f477
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Fri, 16 Jul 2021 22:14:22 -0700

Add helper functions and fix the next and previous commands

Duplicate code from aps_seek, aps_next, and aps_previous put into
the new findset and findplay functions.

Fix the next command erroneously playing the next item when arguments
were given and make the next and previous commands turn on the
player regardless of it's previous state.

Diffstat:
Maps/command.c | 69++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 36 insertions(+), 33 deletions(-)

diff --git a/aps/command.c b/aps/command.c @@ -37,6 +37,30 @@ static char *enotfound = "Not found"; static char *eposition = "No position in queue"; char * +findset(struct aps *aps, char **patterns, unsigned int len, unsigned int flags) +{ + struct item *item; + + item = find(aps->queue, aps->queue, patterns, len, FIND_NEXT | flags); + if (item == NULL) + return enotfound; + if (queue_set(aps, item)) + return strerror(errno); + return NULL; +} + +char * +findplay(struct aps *aps, char **patterns, unsigned int len, unsigned int flags) +{ + char *e; + + if ((e = findset(aps, patterns, len, flags))) + return e; + aps->player->state = OFF; + return NULL; +} + +char * aps_add(struct aps *aps, int s, int argc, char **argv) { int i; @@ -90,29 +114,17 @@ aps_list(struct aps *aps, int s, int argc, char **argv) char * aps_seek(struct aps *aps, int s, int argc, char **argv) { - struct item *item; - - item = find(aps->queue, aps->queue, argv, argc, FIND_NEXT); - if (item == NULL) - return enotfound; - - if (queue_set(aps, item)) - return strerror(errno); - return NULL; + return findset(aps, argv, argc, 0); } char * aps_play(struct aps *aps, int s, int argc, char **argv) { - char *err; + char *e; - if (argc) { - err = aps_seek(aps, s, argc, argv); - if (err) - return err; - } - - if (aps->queue == NULL) + if (argc && (e = aps_seek(aps, s, argc, argv))) + return e; + else if (aps->queue == NULL) return eposition; if (pplay(aps->player, aps->queue->path)) return strerror(errno); @@ -160,27 +172,18 @@ aps_name(struct aps *aps, int s, int argc, char **argv) char * aps_next(struct aps *aps, int s, int argc, char **argv) { - struct item *n; - - if (argc && argsub(&aps->next, argv, argc) == NULL) - return strerror(errno); - - n = find(aps->queue, aps->queue, aps->next, arglen(aps->next), FIND_NEXT); - if (n == NULL || queue_set(aps, n)) - return strerror(errno); - return NULL; + if (argc) { + if (argsub(&aps->next, argv, argc) == NULL) + return strerror(errno); + return NULL; + } + return findplay(aps, argv, argc, 0); } char * aps_previous(struct aps *aps, int s, int argc, char **argv) { - struct item *n; - - n = find(aps->queue, aps->queue, aps->next, arglen(aps->next), - FIND_REVERSE | FIND_NEXT); - if (n == NULL || queue_set(aps, n)) - return strerror(errno); - return NULL; + return findplay(aps, argv, argc, FIND_REVERSE); } char *