From 60b4ebfbf3be78dba9dc7c65ab2bdaa0b218c0c2 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 21 Oct 2024 19:22:50 -0400 Subject: Everything before initial synced list Signed-off-by: Andrew Opalach --- src/list/list.c | 257 --------------------------------------------------- src/list/list.h | 57 ------------ src/list/meson.build | 5 - 3 files changed, 319 deletions(-) delete mode 100644 src/list/list.c delete mode 100644 src/list/list.h delete mode 100644 src/list/meson.build (limited to 'src/list') diff --git a/src/list/list.c b/src/list/list.c deleted file mode 100644 index d5be3dc..0000000 --- a/src/list/list.c +++ /dev/null @@ -1,257 +0,0 @@ -#include -#include - -#include "../libsink/common.h" -#include "../shrub/handler.h" - -#include "list.h" - -void camu_list_init(struct camu_list *list, str *name) -{ - al_str_clone(&list->name, name); - list->current = -1; - list->prev = -1; - list->idle = true; - list->sequence = 0; - al_array_init(list->entries); - al_array_init(list->sinks); -} - -static void camu_list_pump(struct camu_list *list) -{ - s32 size = (s32)list->entries.size; - if (list->current == -1 || size <= list->current) return; - struct camu_list_entry *entry = al_array_at(list->entries, list->current); - struct camu_list_entry *upcoming = NULL; - if (list->current + 1 < size) { - upcoming = al_array_at(list->entries, list->current + 1); - } - struct camu_list_entry *prev = NULL; - if (list->prev >= 0) { - prev = al_array_at(list->entries, list->prev); - list->prev = -1; - } - struct camu_list_sink *sink; - al_array_foreach(list->sinks, i, sink) { - if (sink->set != list->current) { - void *prev_opaque = NULL; - if (prev) prev_opaque = prev->opaque; - sink->callback(sink->userdata, CAMU_SINK_SET, entry->opaque, prev_opaque, entry->sequence); - sink->set = list->current; - } - if (upcoming) { - sink->callback(sink->userdata, CAMU_SINK_QUEUE, upcoming->opaque, entry->opaque, upcoming->sequence); - } - } -} - -void camu_list_add_sink(struct camu_list *list, void (*callback)(void *, u8, void *, void *, s32), - void *userdata) -{ - struct camu_list_sink *sink = al_alloc_object(struct camu_list_sink); - sink->set = -1; - sink->callback = callback; - sink->userdata = userdata; - al_array_push(list->sinks, sink); - camu_list_pump(list); -} - -void camu_list_remove_sink(struct camu_list *list, void *userdata) -{ - struct camu_list_sink *sink; - al_array_foreach(list->sinks, i, sink) { - if (sink->userdata == userdata) { - al_array_remove_at_iter(list->sinks, i); - break; - } - } -} - -void camu_list_add(struct camu_list *list, void *opaque, void (*callback)(void *, u8, void *), - u64 duration, bool set) -{ - s32 size = (s32)list->entries.size; - struct camu_list_entry *entry = al_alloc_object(struct camu_list_entry); - entry->sequence = list->sequence++; - entry->opaque = opaque; - entry->callback = callback; - entry->duration = duration; - entry->start = aki_get_timestamp(); - entry->paused = false; - if (!list->idle) { - struct camu_list_entry *prev = al_array_at(list->entries, size - 1); - u64 end = prev->start + prev->duration; - if (end > entry->start) { - entry->start += end - entry->start; - } - } - al_array_push(list->entries, entry); - if (set) list->current = size; - else if (list->idle) { - list->current++; - list->idle = false; - } - camu_list_pump(list); - entry->callback(entry->opaque, CAMU_LIST_IMPULSE, &entry->start); -} - -static s32 index_of_entry_from_sequence(struct camu_list *list, s32 sequence) -{ - if (sequence != CAMU_SEQUENCE_INVALID) { - struct camu_list_entry *entry; - al_array_foreach(list->entries, i, entry) { - if (entry->sequence == sequence) return i; - } - } - return CAMU_SEQUENCE_INVALID; -} - -void camu_list_skip(struct camu_list *list, s32 sequence, s32 n) -{ - s32 size = (s32)list->entries.size; - s32 index = index_of_entry_from_sequence(list, sequence); - if (index == CAMU_SEQUENCE_INVALID) index = list->current; - if (index + n < 0 || index + n >= size || index + n == list->current) return; - list->prev = list->current; - struct camu_list_entry *prev = al_array_at(list->entries, list->prev); - prev->callback(prev->opaque, CAMU_LIST_PAUSE, NULL); - list->current = index + n; - struct camu_list_entry *entry = al_array_at(list->entries, list->current); - entry->start = aki_get_timestamp(); - entry->callback(entry->opaque, CAMU_LIST_IMPULSE, &entry->start); - camu_list_pump(list); -} - -void camu_list_skipto(struct camu_list *list, s32 i) -{ - s32 size = (s32)list->entries.size; - if (i < 0 || i >= size) return; - list->current = i; - camu_list_pump(list); -} - -void camu_list_toggle_pause(struct camu_list *list, s32 sequence, u64 pos) -{ - s32 index = index_of_entry_from_sequence(list, sequence); - if (index == CAMU_SEQUENCE_INVALID) return; - struct camu_list_entry *entry = al_array_at(list->entries, index); - if (entry->paused) { - u64 ts = aki_get_timestamp(); - struct shrb_resume_req req = { - .start = ts, - .offset = ts - entry->start - }; - entry->start = ts; - entry->callback(entry->opaque, CAMU_LIST_USER_RESUME, &req); - entry->paused = false; - } else { - entry->start = aki_get_timestamp(); - entry->callback(entry->opaque, CAMU_LIST_USER_PAUSE, &pos); - entry->paused = true; - } -} - -void camu_list_seek(struct camu_list *list, s32 sequence, f64 percent) -{ - s32 index = index_of_entry_from_sequence(list, sequence); - if (index == CAMU_SEQUENCE_INVALID) return; - list->idle = false; - struct camu_list_entry *entry = al_array_at(list->entries, index); - entry->start = aki_get_timestamp(); - entry->callback(entry->opaque, CAMU_LIST_IMPULSE, &entry->start); - u64 pos = percent * entry->duration; - entry->callback(entry->opaque, CAMU_LIST_SEEK, &pos); -} - -void camu_list_finished(struct camu_list *list, s32 sequence) -{ - s32 size = (s32)list->entries.size; - s32 index = index_of_entry_from_sequence(list, sequence); - if (index == CAMU_SEQUENCE_INVALID) return; - if (index + 1 >= size) { - list->idle = true; - return; - } - list->current = index + 1; - camu_list_pump(list); -} - -void camu_list_shuffle(struct camu_list *list) -{ - u32 size = list->entries.size; - if (size == 0) return; - for (u32 i = 0; i < size - 1; i++) { - u32 j = i + al_rand() / (AL_RAND_MAX / (size - i) + 1); - struct camu_list_entry *tmp = al_array_at(list->entries, j); - al_array_at(list->entries, j) = al_array_at(list->entries, i); - al_array_at(list->entries, i) = tmp; - } - struct camu_list_sink *sink; - al_array_foreach(list->sinks, i, sink) { - sink->set = -1; - } - camu_list_pump(list); -} - -static s64 camu_db_num_from_path(str *path) -{ - s32 slash = al_str_rfind(path, '/'); - if (slash < 0) return -1; - str s = *al_str_substr(path, slash + 1, path->len); - s32 underscore = al_str_find(&s, '_'); - if (underscore < 0) return -1; - s = *al_str_substr(&s, underscore + 1, s.len); - underscore = al_str_find(&s, '_'); - if (underscore < 0) return -1; - s = *al_str_substr(&s, underscore + 1, s.len); - underscore = al_str_find(&s, '_'); - if (underscore < 0) return -1; - s = *al_str_substr(&s, 0, underscore); - s64 num = al_str_to_long(&s, 10); - if (num == INT64_MIN || num == INT64_MAX) { - return -1; - } - return num; -} - -static s32 camu_db_compare(void *a, void *b) -{ - struct camu_list_entry *aa = *((struct camu_list_entry **)a); - struct camu_list_entry *bb = *((struct camu_list_entry **)b); - str *astr = NULL, *bstr = NULL; - aa->callback(aa->opaque, CAMU_LIST_ENTRY_ID, &astr); - bb->callback(bb->opaque, CAMU_LIST_ENTRY_ID, &bstr); - s64 anum = camu_db_num_from_path(astr); - s64 bnum = camu_db_num_from_path(bstr); - if (anum > bnum) { - return -1; - } else if (anum < bnum) { - return 1; - } - return 0; -} - -void camu_list_sort(struct camu_list *list) -{ - al_array_sort(list->entries, struct camu_list_entry *, camu_db_compare); - struct camu_list_sink *sink; - al_array_foreach(list->sinks, i, sink) { - sink->set = -1; - } - camu_list_pump(list); -} - -void camu_list_reverse(struct camu_list *list) -{ - u32 size = list->entries.size; - for (u32 i = 0; i < size; i++) { - u32 tail = size - (i + 1); - if (tail <= i) break; - AL_SWAP(al_array_at(list->entries, i), al_array_at(list->entries, tail), struct camu_list_entry *); - } - struct camu_list_sink *sink; - al_array_foreach(list->sinks, i, sink) { - sink->set = -1; - } - camu_list_pump(list); -} diff --git a/src/list/list.h b/src/list/list.h deleted file mode 100644 index 4873265..0000000 --- a/src/list/list.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include -#include - -#define CAMU_SEQUENCE_INVALID INT32_MAX - -// TODO: Improve naming between this and server LIST_ACTION's -enum { - CAMU_LIST_ENTRY_ID = 0, - CAMU_LIST_IMPULSE, - CAMU_LIST_USER_PAUSE, - CAMU_LIST_USER_RESUME, - CAMU_LIST_PAUSE, // Pause at current position. - CAMU_LIST_SEEK -}; - -struct camu_list_entry { - s32 sequence; - void *opaque; - void (*callback)(void *, u8, void *); - u64 duration; - u64 start; - bool paused; -}; - -struct camu_list_sink { - s32 set; - bool (*action)(void *, u8, void *); - void (*callback)(void *, u8, void *, void *, s32); - void *userdata; -}; - -struct camu_list { - str name; - s32 current; - s32 prev; - bool idle; - s32 sequence; // Holds the next entry sequence. - array(struct camu_list_entry *) entries; - array(struct camu_list_sink *) sinks; -}; - -void camu_list_init(struct camu_list *list, str *name); -void camu_list_add_sink(struct camu_list *list, void (*callback)(void *, u8, void *, void *, s32), - void *userdata); -void camu_list_remove_sink(struct camu_list *list, void *userdata); -void camu_list_add(struct camu_list *list, void *opaque, void (*callback)(void *, u8, void *), - u64 duration, bool set); -void camu_list_skip(struct camu_list *list, s32 sequence, s32 n); -void camu_list_skipto(struct camu_list *list, s32 i); -void camu_list_toggle_pause(struct camu_list *list, s32 sequence, u64 pos); -void camu_list_seek(struct camu_list *list, s32 sequence, f64 percent); -void camu_list_finished(struct camu_list *list, s32 sequence); -void camu_list_shuffle(struct camu_list *list); -void camu_list_sort(struct camu_list *list); -void camu_list_reverse(struct camu_list *list); diff --git a/src/list/meson.build b/src/list/meson.build deleted file mode 100644 index c61ac39..0000000 --- a/src/list/meson.build +++ /dev/null @@ -1,5 +0,0 @@ -list_src = [ - 'list.c' -] -list_deps = [common_deps] -list = declare_dependency(sources: list_src, dependencies: list_deps) -- cgit v1.2.3-101-g0448