obs-module.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "obs.h"
  16. #ifdef __cplusplus
  17. #define MODULE_EXPORT extern "C" EXPORT
  18. #define MODULE_EXTERN extern "C"
  19. #else
  20. #define MODULE_EXPORT EXPORT
  21. #define MODULE_EXTERN extern
  22. #endif
  23. /**
  24. * @file
  25. * @brief This file is used by modules for module declaration and module
  26. * exports.
  27. *
  28. * @page modules_page Modules
  29. * @brief Modules or plugins are libraries that can be loaded by libobs and
  30. * subsequently interact with it.
  31. *
  32. * @section modules_overview_sec Overview
  33. *
  34. * Modules can provide a wide range of functionality to libobs, they for example
  35. * can feed captured audio or video to libobs, or interface with an encoder to
  36. * provide some codec to libobs.
  37. *
  38. * @section modules_basic_sec Creating a basic module
  39. *
  40. * In order to create a module for libobs you will need to build a shared
  41. * library that implements a basic interface for libobs to interact with.
  42. * The following code would create a simple source plugin without localization:
  43. *
  44. @code
  45. #include <obs-module.h>
  46. OBS_DECLARE_MODULE()
  47. extern struct obs_source_info my_source;
  48. bool obs_module_load(void)
  49. {
  50. obs_register_source(&my_source);
  51. return true;
  52. }
  53. @endcode
  54. *
  55. * If you want to enable localization, you will need to also use the
  56. * @ref OBS_MODULE_USE_DEFAULT_LOCALE() macro.
  57. *
  58. * Other module types:
  59. * - @ref obs_register_encoder()
  60. * - @ref obs_register_service()
  61. * - @ref obs_register_output()
  62. *
  63. */
  64. /** Required: Declares a libobs module. */
  65. #define OBS_DECLARE_MODULE() \
  66. static obs_module_t *obs_module_pointer; \
  67. MODULE_EXPORT void obs_module_set_pointer(obs_module_t *module); \
  68. void obs_module_set_pointer(obs_module_t *module) \
  69. { \
  70. obs_module_pointer = module; \
  71. } \
  72. obs_module_t *obs_current_module(void) \
  73. { \
  74. return obs_module_pointer; \
  75. } \
  76. MODULE_EXPORT uint32_t obs_module_ver(void); \
  77. uint32_t obs_module_ver(void) \
  78. { \
  79. return LIBOBS_API_VER; \
  80. }
  81. /**
  82. * Required: Called when the module is loaded. Use this function to load all
  83. * the sources/encoders/outputs/services for your module, or anything else that
  84. * may need loading.
  85. *
  86. * @return Return true to continue loading the module, otherwise
  87. * false to indicate failure and unload the module
  88. */
  89. MODULE_EXPORT bool obs_module_load(void);
  90. /** Optional: Called when the module is unloaded. */
  91. MODULE_EXPORT void obs_module_unload(void);
  92. /** Optional: Called when all modules have finished loading */
  93. MODULE_EXPORT void obs_module_post_load(void);
  94. /** Called to set the current locale data for the module. */
  95. MODULE_EXPORT void obs_module_set_locale(const char *locale);
  96. /** Called to free the current locale data for the module. */
  97. MODULE_EXPORT void obs_module_free_locale(void);
  98. /** Optional: Use this macro in a module to use default locale handling. */
  99. #define OBS_MODULE_USE_DEFAULT_LOCALE(module_name, default_locale) \
  100. lookup_t *obs_module_lookup = NULL; \
  101. const char *obs_module_text(const char *val) \
  102. { \
  103. const char *out = val; \
  104. text_lookup_getstr(obs_module_lookup, val, &out); \
  105. return out; \
  106. } \
  107. bool obs_module_get_string(const char *val, const char **out) \
  108. { \
  109. return text_lookup_getstr(obs_module_lookup, val, out); \
  110. } \
  111. void obs_module_set_locale(const char *locale) \
  112. { \
  113. if (obs_module_lookup) \
  114. text_lookup_destroy(obs_module_lookup); \
  115. obs_module_lookup = obs_module_load_locale(obs_current_module(), default_locale, locale); \
  116. } \
  117. void obs_module_free_locale(void) \
  118. { \
  119. text_lookup_destroy(obs_module_lookup); \
  120. obs_module_lookup = NULL; \
  121. }
  122. /** Helper function for looking up locale if default locale handler was used */
  123. MODULE_EXTERN const char *obs_module_text(const char *lookup_string);
  124. /** Helper function for looking up locale if default locale handler was used,
  125. * returns true if text found, otherwise false */
  126. MODULE_EXPORT bool obs_module_get_string(const char *lookup_string, const char **translated_string);
  127. /** Helper function that returns the current module */
  128. MODULE_EXTERN obs_module_t *obs_current_module(void);
  129. /**
  130. * Returns the location to a module data file associated with the current
  131. * module. Free with bfree when complete. Equivalent to:
  132. * obs_find_module_file(obs_current_module(), file);
  133. */
  134. #define obs_module_file(file) obs_find_module_file(obs_current_module(), file)
  135. /**
  136. * Returns the location to a module config file associated with the current
  137. * module. Free with bfree when complete. Will return NULL if configuration
  138. * directory is not set. Equivalent to:
  139. * obs_module_get_config_path(obs_current_module(), file);
  140. */
  141. #define obs_module_config_path(file) obs_module_get_config_path(obs_current_module(), file)
  142. /**
  143. * Optional: Declares the author(s) of the module
  144. *
  145. * @param name Author name(s)
  146. */
  147. #define OBS_MODULE_AUTHOR(name) \
  148. MODULE_EXPORT const char *obs_module_author(void); \
  149. const char *obs_module_author(void) \
  150. { \
  151. return name; \
  152. }
  153. /** Optional: Returns the full name of the module */
  154. MODULE_EXPORT const char *obs_module_name(void);
  155. /** Optional: Returns a description of the module */
  156. MODULE_EXPORT const char *obs_module_description(void);
  157. /** Returns the module's unique ID, or NULL if it doesn't have one */
  158. MODULE_EXPORT const char *obs_get_module_id(obs_module_t *module);
  159. /** Returns the module's semver version number or NULL if it doesn't have one */
  160. MODULE_EXPORT const char *obs_get_module_version(obs_module_t *module);