From 243d4d42e9fbe214d4faaa99b1995a8975bfd143 Mon Sep 17 00:00:00 2001 From: cg2121 Date: Wed, 21 Jun 2023 02:46:54 -0700 Subject: [PATCH] libobs: Add functions to get/set the index of filters --- libobs/obs-source.c | 70 +++++++++++++++++++++++++++++++++++++++------ libobs/obs.h | 8 ++++++ 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/libobs/obs-source.c b/libobs/obs-source.c index 68a6dbf5b..7a4c4a4d4 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -3225,6 +3225,19 @@ static size_t find_prev_filter(obs_source_t *source, obs_source_t *filter, return find_prev_filter(source, filter, cur_idx - 1); } +static void reorder_filter_targets(obs_source_t *source) +{ + /* reorder filter targets, not the nicest way of dealing with things */ + for (size_t i = 0; i < source->filters.num; i++) { + obs_source_t *next_filter = + (i == source->filters.num - 1) + ? source + : source->filters.array[i + 1]; + + source->filters.array[i]->filter_target = next_filter; + } +} + /* moves filters above/below matching filter types */ static bool move_filter_dir(obs_source_t *source, obs_source_t *filter, enum obs_order_movement movement) @@ -3258,15 +3271,7 @@ static bool move_filter_dir(obs_source_t *source, obs_source_t *filter, da_move_item(source->filters, idx, 0); } - /* reorder filter targets, not the nicest way of dealing with things */ - for (size_t i = 0; i < source->filters.num; i++) { - obs_source_t *next_filter = - (i == source->filters.num - 1) - ? source - : source->filters.array[i + 1]; - - source->filters.array[i]->filter_target = next_filter; - } + reorder_filter_targets(source); return true; } @@ -3289,6 +3294,53 @@ void obs_source_filter_set_order(obs_source_t *source, obs_source_t *filter, obs_source_dosignal(source, NULL, "reorder_filters"); } +size_t obs_source_filter_get_index(obs_source_t *source, obs_source_t *filter) +{ + if (!obs_source_valid(source, "obs_source_filter_get_index")) + return DARRAY_INVALID; + if (!obs_ptr_valid(filter, "obs_source_filter_get_index")) + return DARRAY_INVALID; + + size_t idx; + + pthread_mutex_lock(&source->filter_mutex); + idx = da_find(source->filters, &filter, 0); + pthread_mutex_unlock(&source->filter_mutex); + + return idx; +} + +static bool set_filter_index(obs_source_t *source, obs_source_t *filter, + size_t index) +{ + size_t idx = da_find(source->filters, &filter, 0); + if (idx == DARRAY_INVALID) + return false; + + da_move_item(source->filters, idx, index); + reorder_filter_targets(source); + + return true; +} + +void obs_source_filter_set_index(obs_source_t *source, obs_source_t *filter, + size_t index) +{ + bool success; + + if (!obs_source_valid(source, "obs_source_filter_set_index")) + return; + if (!obs_ptr_valid(filter, "obs_source_filter_set_index")) + return; + + pthread_mutex_lock(&source->filter_mutex); + success = set_filter_index(source, filter, index); + pthread_mutex_unlock(&source->filter_mutex); + + if (success) + obs_source_dosignal(source, NULL, "reorder_filters"); +} + obs_data_t *obs_source_get_settings(const obs_source_t *source) { if (!obs_source_valid(source, "obs_source_get_settings")) diff --git a/libobs/obs.h b/libobs/obs.h index 69df107ca..6bf1ec72d 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -1130,6 +1130,14 @@ EXPORT void obs_source_filter_set_order(obs_source_t *source, obs_source_t *filter, enum obs_order_movement movement); +/** Gets filter index */ +EXPORT size_t obs_source_filter_get_index(obs_source_t *source, + obs_source_t *filter); + +/** Sets filter index */ +EXPORT void obs_source_filter_set_index(obs_source_t *source, + obs_source_t *filter, size_t index); + /** Gets the settings string for a source */ EXPORT obs_data_t *obs_source_get_settings(const obs_source_t *source);