From 796496013368d216b404e390f965381a7cffc0d1 Mon Sep 17 00:00:00 2001 From: Vladimir Sumarov Date: Thu, 26 May 2022 15:56:58 -0700 Subject: [PATCH] add a function to put filter into a exact position --- libobs/obs-source.c | 50 +++++++++++++++++++++++++++++++++++++-------- libobs/obs.h | 9 ++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/libobs/obs-source.c b/libobs/obs-source.c index 571c7e8629f13b..090225994ef152 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -2705,6 +2705,19 @@ static size_t find_prev_filter(obs_source_t *source, obs_source_t *filter, return find_prev_filter(source, filter, cur_idx - 1); } +/* reorder filter targets, not the nicest way of dealing with things */ +static void reorder_filters(obs_source_t *source) +{ + 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) @@ -2738,15 +2751,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_filters(source); return true; } @@ -2769,6 +2774,33 @@ void obs_source_filter_set_order(obs_source_t *source, obs_source_t *filter, obs_source_dosignal(source, NULL, "reorder_filters"); } +void obs_source_filter_set_position(obs_source_t *source, obs_source_t *filter, + size_t position) +{ + bool success = false; + + if (!obs_source_valid(source, "obs_source_filter_set_position")) + return; + if (!obs_ptr_valid(filter, "obs_source_filter_set_position")) + return; + + pthread_mutex_lock(&source->filter_mutex); + if (position < source->filters.num) { + size_t new_idx = source->filters.num - 1 - position; + size_t idx = da_find(source->filters, &filter, 0); + if (idx != DARRAY_INVALID && idx != new_idx) { + da_move_item(source->filters, idx, new_idx); + success = true; + + reorder_filters(source); + } + } + 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 4defdc280774d4..3c26fbd12e273f 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -1104,6 +1104,15 @@ EXPORT void obs_source_filter_set_order(obs_source_t *source, obs_source_t *filter, enum obs_order_movement movement); +/** + * Set the position of a specific filter, + * positions refered to obs_source_enum_filters + * so it goes backward to filters array indexes + */ +EXPORT void obs_source_filter_set_position(obs_source_t *source, + obs_source_t *filter, + size_t position); + /** Gets the settings string for a source */ EXPORT obs_data_t *obs_source_get_settings(const obs_source_t *source);