obs-module.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh 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) { return obs_module_pointer; } \
  73. MODULE_EXPORT uint32_t obs_module_ver(void); \
  74. uint32_t obs_module_ver(void) { return LIBOBS_API_VER; }
  75. /**
  76. * Required: Called when the module is loaded. Use this function to load all
  77. * the sources/encoders/outputs/services for your module, or anything else that
  78. * may need loading.
  79. *
  80. * @return Return true to continue loading the module, otherwise
  81. * false to indicate failure and unload the module
  82. */
  83. MODULE_EXPORT bool obs_module_load(void);
  84. /** Optional: Called when the module is unloaded. */
  85. MODULE_EXPORT void obs_module_unload(void);
  86. /** Optional: Called when all modules have finished loading */
  87. MODULE_EXPORT void obs_module_post_load(void);
  88. /** Called to set the current locale data for the module. */
  89. MODULE_EXPORT void obs_module_set_locale(const char *locale);
  90. /** Called to free the current locale data for the module. */
  91. MODULE_EXPORT void obs_module_free_locale(void);
  92. /** Optional: Use this macro in a module to use default locale handling. */
  93. #define OBS_MODULE_USE_DEFAULT_LOCALE(module_name, default_locale) \
  94. lookup_t *obs_module_lookup = NULL; \
  95. const char *obs_module_text(const char *val) \
  96. { \
  97. const char *out = val; \
  98. text_lookup_getstr(obs_module_lookup, val, &out); \
  99. return out; \
  100. } \
  101. bool obs_module_get_string(const char *val, const char **out) \
  102. { \
  103. return text_lookup_getstr(obs_module_lookup, val, out); \
  104. } \
  105. void obs_module_set_locale(const char *locale) \
  106. { \
  107. if (obs_module_lookup) \
  108. text_lookup_destroy(obs_module_lookup); \
  109. obs_module_lookup = obs_module_load_locale( \
  110. obs_current_module(), default_locale, locale); \
  111. } \
  112. void obs_module_free_locale(void) \
  113. { \
  114. text_lookup_destroy(obs_module_lookup); \
  115. obs_module_lookup = NULL; \
  116. }
  117. /** Helper function for looking up locale if default locale handler was used */
  118. MODULE_EXTERN const char *obs_module_text(const char *lookup_string);
  119. /** Helper function for looking up locale if default locale handler was used,
  120. * returns true if text found, otherwise false */
  121. MODULE_EXPORT bool obs_module_get_string(const char *lookup_string,
  122. const char **translated_string);
  123. /** Helper function that returns the current module */
  124. MODULE_EXTERN obs_module_t *obs_current_module(void);
  125. /**
  126. * Returns the location to a module data file associated with the current
  127. * module. Free with bfree when complete. Equivalent to:
  128. * obs_find_module_file(obs_current_module(), file);
  129. */
  130. #define obs_module_file(file) obs_find_module_file(obs_current_module(), file)
  131. /**
  132. * Returns the location to a module config file associated with the current
  133. * module. Free with bfree when complete. Will return NULL if configuration
  134. * directory is not set. Equivalent to:
  135. * obs_module_get_config_path(obs_current_module(), file);
  136. */
  137. #define obs_module_config_path(file) \
  138. obs_module_get_config_path(obs_current_module(), file)
  139. /**
  140. * Optional: Declares the author(s) of the module
  141. *
  142. * @param name Author name(s)
  143. */
  144. #define OBS_MODULE_AUTHOR(name) \
  145. MODULE_EXPORT const char *obs_module_author(void); \
  146. const char *obs_module_author(void) { return name; }
  147. /** Optional: Returns the full name of the module */
  148. MODULE_EXPORT const char *obs_module_name(void);
  149. /** Optional: Returns a description of the module */
  150. MODULE_EXPORT const char *obs_module_description(void);