Browse Source

libobs: Add API func to get module config path

Allows the ability to get a module-specific configuration file/path
jp9000 10 years ago
parent
commit
0d30d1229f
4 changed files with 54 additions and 0 deletions
  1. 1 0
      libobs/obs-internal.h
  2. 31 0
      libobs/obs-module.c
  3. 9 0
      libobs/obs-module.h
  4. 13 0
      libobs/obs.h

+ 1 - 0
libobs/obs-internal.h

@@ -52,6 +52,7 @@ struct draw_callback {
 /* modules */
 
 struct obs_module {
+	char *mod_name;
 	const char *file;
 	char *bin_path;
 	char *data_path;

+ 31 - 0
libobs/obs-module.c

@@ -56,6 +56,21 @@ static int load_module_exports(struct obs_module *mod, const char *path)
 	return MODULE_SUCCESS;
 }
 
+static inline char *get_module_name(const char *file)
+{
+	static int ext_len = 0;
+	struct dstr name = {0};
+
+	if (ext_len == 0) {
+		const char *ext = get_module_extension();
+		ext_len = strlen(ext);
+	}
+
+	dstr_copy(&name, file);
+	dstr_resize(&name, name.len - ext_len);
+	return name.array;
+}
+
 int obs_open_module(obs_module_t **module, const char *path,
 		const char *data_path)
 {
@@ -78,6 +93,7 @@ int obs_open_module(obs_module_t **module, const char *path,
 	mod.bin_path  = bstrdup(path);
 	mod.file      = strrchr(mod.bin_path, '/');
 	mod.file      = (!mod.file) ? mod.bin_path : (mod.file + 1);
+	mod.mod_name  = get_module_name(mod.file);
 	mod.data_path = bstrdup(data_path);
 	mod.next      = obs->first_module;
 
@@ -167,6 +183,20 @@ char *obs_find_module_file(obs_module_t *module, const char *file)
 	return output.array;
 }
 
+char *obs_module_get_config_path(obs_module_t *module, const char *file)
+{
+	struct dstr output = {0};
+
+	dstr_copy(&output, obs->module_config_path);
+	if (!dstr_is_empty(&output) && dstr_end(&output) != '/')
+		dstr_cat_ch(&output, '/');
+	dstr_cat(&output, module->mod_name);
+	dstr_cat_ch(&output, '/');
+	dstr_cat(&output, file);
+
+	return output.array;
+}
+
 void obs_add_module_path(const char *bin, const char *data)
 {
 	struct obs_module_path omp;
@@ -380,6 +410,7 @@ void free_module(struct obs_module *mod)
 		/* os_dlclose(mod->module); */
 	}
 
+	bfree(mod->mod_name);
 	bfree(mod->bin_path);
 	bfree(mod->data_path);
 	bfree(mod);

+ 9 - 0
libobs/obs-module.h

@@ -137,6 +137,15 @@ MODULE_EXTERN obs_module_t *obs_current_module(void);
  */
 #define obs_module_file(file) obs_find_module_file(obs_current_module(), file)
 
+/**
+ * Returns the location to a module config file associated with the current
+ * module.  Free with bfree when complete.  Will return NULL if configuration
+ * directory is not set.  Equivalent to:
+ *    obs_module_get_config_path(obs_current_modile(), file);
+ */
+#define obs_module_config_path(file) \
+	obs_module_get_config_path(obs_current_module(), file)
+
 /**
  * Optional: Declares the author(s) of the module
  *

+ 13 - 0
libobs/obs.h

@@ -402,6 +402,19 @@ EXPORT lookup_t *obs_module_load_locale(obs_module_t *module,
  */
 EXPORT char *obs_find_module_file(obs_module_t *module, const char *file);
 
+/**
+ * Returns the path of a plugin module config file (whether it exists or not)
+ *
+ * @note   Modules should use obs_module_config_path function defined in
+ *         obs-module.h as a more elegant means of getting their files without
+ *         having to specify the module parameter.
+ *
+ * @param  module  The module associated with the path
+ * @param  file    The file to get a path to
+ * @return         Path string, or NULL if not found.  Use bfree to free string.
+ */
+EXPORT char *obs_module_get_config_path(obs_module_t *module, const char *file);
+
 /**
  * Enumerates all available inputs source types.
  *