Browse Source

libobs: Add function to remove properties

Allows removing a property from a properties list in get_properties or
modified callback to enable dynamic property generation. The behavior
is undefined if the UI properties list is not refreshed by returning
true from the modified callback.
Michael Fabian 'Xaymar' Dirks 6 years ago
parent
commit
9bb276feab
2 changed files with 38 additions and 0 deletions
  1. 25 0
      libobs/obs-properties.c
  2. 13 0
      libobs/obs-properties.h

+ 25 - 0
libobs/obs-properties.c

@@ -271,6 +271,31 @@ obs_property_t *obs_properties_get(obs_properties_t *props, const char *name)
 	return NULL;
 }
 
+void obs_properties_remove_by_name(obs_properties_t *props, const char *name)
+{
+	if (!props)
+		return;
+
+	/* obs_properties_t is a forward-linked-list, so we need to keep both
+	 * previous and current pointers around. That way we can fix up the
+	 * pointers for the previous element if we find a match.
+	 */
+	struct obs_property *cur = props->first_property;
+	struct obs_property *prev = props->first_property;
+
+	while (cur) {
+		if (strcmp(cur->name, name) == 0) {
+			prev->next = cur->next;
+			cur->next = 0;
+			obs_property_destroy(cur);
+			break;
+		}
+
+		prev = cur;
+		cur = cur->next;
+	}
+}
+
 void obs_properties_apply_settings(obs_properties_t *props, obs_data_t *settings)
 {
 	struct obs_property *p;

+ 13 - 0
libobs/obs-properties.h

@@ -122,6 +122,19 @@ EXPORT obs_property_t *obs_properties_first(obs_properties_t *props);
 EXPORT obs_property_t *obs_properties_get(obs_properties_t *props,
 		const char *property);
 
+/** Remove a property from a properties list.
+ *
+ * Removes a property from a properties list. Only valid in either
+ * get_properties or modified_callback(2). modified_callback(2) must return
+ * true so that all UI properties are rebuilt and returning false is undefined
+ * behavior.
+ *
+ * @param props Properties to remove from.
+ * @param property Name of the property to remove.
+ */
+EXPORT void obs_properties_remove_by_name(obs_properties_t *props,
+		const char *property);
+
 /**
  * Applies settings to the properties by calling all the necessary
  * modification callbacks