commit 8ecae9d6b0b92314eec7e9c7d017a5e40e8a28ae
parent 8f329af6a91db849190497dd607ada73b45efb4b
Author: Jacob R. Edwards <n/a>
Date: Tue, 22 Feb 2022 13:05:46 -0800
Improve queue and item functions
- Items are now always circularly linked, this shortens all item
handling as you don't have to treat some differently than others
- item_insert() can no longer fail, but
- Add item_insert_string() to replace item_insert() error handling
Also, in queue_set() pstop() will never fail; so queue_set() won't
either, and now returns void.
Diffstat:
6 files changed, 37 insertions(+), 39 deletions(-)
diff --git a/aps/aps.c b/aps/aps.c
@@ -264,15 +264,13 @@ aps_seek(struct aps *aps, struct item *(*incr)(struct item *), char *pattern)
item = find(aps->queue, aps->queue, incr, pattern);
if (item == NULL)
return 1;
- if (queue_set(aps, item))
- return 1;
+ queue_set(aps, item);
return 0;
}
int
aps_play(struct aps *aps, struct item *item)
{
- if (queue_set(aps, item))
- return 1;
+ queue_set(aps, item);
return pplay(aps->player, item->path);
}
diff --git a/aps/queue.c b/aps/queue.c
@@ -26,18 +26,16 @@
#include "aps.h"
#include "log.h"
-int
+void
queue_set(struct aps *aps, struct item *item)
{
aps->queue = item;
aps_logfmt(aps, QUEUE, 0, "set '%s'", item ? item->path : "");
if (aps->player->state != OFF && aps->player->state != STOPPED) {
- if (pstop(aps->player))
- return 1;
+ pstop(aps->player);
aps->player->state = OFF;
}
- return 0;
}
int
@@ -45,19 +43,19 @@ queue_add(struct aps *aps, char *s)
{
struct item *item;
- item = item_new(s);
- if (item == NULL)
- return 1;
+ aps_logfmt(aps, QUEUE, 0, "add '%s'", s);
- if (aps->queue == NULL) {
- item->prev = item->next = item;
- queue_set(aps, item); /* ignore player errors */
- } else if (item_insert(aps->queue, item)) {
- item_free(item);
+ item = item_new(s);
+ if (item == NULL) {
+ aps_log(aps, ERROR, 0, "unable to create queue item");
return 1;
}
- aps_logfmt(aps, QUEUE, 0, "add '%s'", item->path);
+ if (aps->queue == NULL)
+ queue_set(aps, item);
+ else
+ item_insert(aps->queue, item);
+
return 0;
}
diff --git a/aps/queue.h b/aps/queue.h
@@ -1,3 +1,3 @@
-int queue_set(struct aps *, struct item *);
+void queue_set(struct aps *, struct item *);
int queue_add(struct aps *, char *);
struct item *queue_remove(struct aps *, struct item *);
diff --git a/include/ap/item.h b/include/ap/item.h
@@ -8,5 +8,6 @@ struct item {
struct item *item_new(char *);
void item_free(struct item *);
-int item_insert(struct item *, struct item *);
+void item_insert(struct item *, struct item *);
+int item_insert_string(struct item *, char *);
void item_unlink(struct item *);
diff --git a/lib/ap/client.c b/lib/ap/client.c
@@ -57,7 +57,7 @@ apc_add(struct apc *apc, char *arg)
apc->args = item_new(arg);
return apc->args == NULL;
}
- return item_insert(apc->args, item_new(arg));
+ return item_insert_string(apc->args, arg);
}
char *
diff --git a/lib/ap/item.c b/lib/ap/item.c
@@ -39,6 +39,8 @@ item_new(char *path)
return NULL;
}
+ item->prev = item->next = item;
+
return item;
}
@@ -51,33 +53,32 @@ item_free(struct item *item)
}
}
-int
+void
item_insert(struct item *list, struct item *item)
{
- if (list == NULL || item == NULL || item->next || item->prev) {
- errno = EINVAL;
- return 1;
- }
+ item_unlink(item);
+ list->prev->next = item;
+ item->prev = list->prev;
+ item->next = list;
+ list->prev = item;
+}
- if (list->prev == NULL) {
- assert(list->next == NULL);
- list->next = list->prev = item;
- item->next = item->prev = list;
- } else {
- list->prev->next = item;
- item->prev = list->prev;
- item->next = list;
- list->prev = item;
- }
+int
+item_insert_string(struct item *list, char *s)
+{
+ struct item *item;
+ item = item_new(s);
+ if (item == NULL)
+ return 1;
+ item_insert(list, item);
return 0;
}
void
item_unlink(struct item *item)
{
- if (item->prev)
- item->prev->next = item->next;
- if (item->next)
- item->next->prev = item->prev;
+ item->prev->next = item->next;
+ item->next->prev = item->prev;
+ item->next = item->prev = item;
}