obs-scripting-lua.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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. /* ---------------------------- */
  16. #include <lua.h>
  17. #include <lualib.h>
  18. #include <lauxlib.h>
  19. #ifdef _MSC_VER
  20. #pragma warning(push)
  21. #pragma warning(disable : 4100)
  22. #pragma warning(disable : 4189)
  23. #pragma warning(disable : 4244)
  24. #pragma warning(disable : 4267)
  25. #endif
  26. #define SWIG_TYPE_TABLE obslua
  27. #include "swig/swigluarun.h"
  28. #ifdef _MSC_VER
  29. #pragma warning(pop)
  30. #endif
  31. /* ---------------------------- */
  32. #include <util/threading.h>
  33. #include <util/base.h>
  34. #include <util/bmem.h>
  35. #include "obs-scripting-internal.h"
  36. #include "obs-scripting-callback.h"
  37. #define do_log(level, format, ...) blog(level, "[Lua] " format, ##__VA_ARGS__)
  38. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  39. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  40. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  41. /* ------------------------------------------------------------ */
  42. struct obs_lua_script;
  43. struct lua_obs_callback;
  44. extern THREAD_LOCAL struct lua_obs_callback *current_lua_cb;
  45. extern THREAD_LOCAL struct obs_lua_script *current_lua_script;
  46. /* ------------------------------------------------------------ */
  47. struct lua_obs_callback;
  48. struct obs_lua_script {
  49. obs_script_t base;
  50. struct dstr dir;
  51. struct dstr log_chunk;
  52. pthread_mutex_t mutex;
  53. lua_State *script;
  54. struct script_callback *first_callback;
  55. int update;
  56. int get_properties;
  57. int save;
  58. int tick;
  59. struct obs_lua_script *next_tick;
  60. struct obs_lua_script **p_prev_next_tick;
  61. bool defined_sources;
  62. };
  63. #define lock_callback() \
  64. struct obs_lua_script *__last_script = current_lua_script; \
  65. struct lua_obs_callback *__last_callback = current_lua_cb; \
  66. current_lua_cb = cb; \
  67. current_lua_script = (struct obs_lua_script *)cb->base.script; \
  68. pthread_mutex_lock(&current_lua_script->mutex);
  69. #define unlock_callback() \
  70. pthread_mutex_unlock(&current_lua_script->mutex); \
  71. current_lua_script = __last_script; \
  72. current_lua_cb = __last_callback;
  73. /* ------------------------------------------------ */
  74. struct lua_obs_callback {
  75. struct script_callback base;
  76. lua_State *script;
  77. int reg_idx;
  78. };
  79. static inline struct lua_obs_callback *add_lua_obs_callback_extra(lua_State *script, int stack_idx, size_t extra_size)
  80. {
  81. struct obs_lua_script *data = current_lua_script;
  82. struct lua_obs_callback *cb =
  83. add_script_callback(&data->first_callback, (obs_script_t *)data, sizeof(*cb) + extra_size);
  84. lua_pushvalue(script, stack_idx);
  85. cb->reg_idx = luaL_ref(script, LUA_REGISTRYINDEX);
  86. cb->script = script;
  87. return cb;
  88. }
  89. static inline struct lua_obs_callback *add_lua_obs_callback(lua_State *script, int stack_idx)
  90. {
  91. return add_lua_obs_callback_extra(script, stack_idx, 0);
  92. }
  93. static inline void *lua_obs_callback_extra_data(struct lua_obs_callback *cb)
  94. {
  95. return (void *)&cb[1];
  96. }
  97. static inline struct obs_lua_script *lua_obs_callback_script(struct lua_obs_callback *cb)
  98. {
  99. return (struct obs_lua_script *)cb->base.script;
  100. }
  101. static inline struct lua_obs_callback *find_next_lua_obs_callback(lua_State *script, struct lua_obs_callback *cb,
  102. int stack_idx)
  103. {
  104. struct obs_lua_script *data = current_lua_script;
  105. cb = cb ? (struct lua_obs_callback *)cb->base.next : (struct lua_obs_callback *)data->first_callback;
  106. while (cb) {
  107. lua_rawgeti(script, LUA_REGISTRYINDEX, cb->reg_idx);
  108. bool match = lua_rawequal(script, -1, stack_idx);
  109. lua_pop(script, 1);
  110. if (match)
  111. break;
  112. cb = (struct lua_obs_callback *)cb->base.next;
  113. }
  114. return cb;
  115. }
  116. static inline struct lua_obs_callback *find_lua_obs_callback(lua_State *script, int stack_idx)
  117. {
  118. return find_next_lua_obs_callback(script, NULL, stack_idx);
  119. }
  120. static inline void remove_lua_obs_callback(struct lua_obs_callback *cb)
  121. {
  122. remove_script_callback(&cb->base);
  123. luaL_unref(cb->script, LUA_REGISTRYINDEX, cb->reg_idx);
  124. }
  125. static inline void just_free_lua_obs_callback(struct lua_obs_callback *cb)
  126. {
  127. just_free_script_callback(&cb->base);
  128. }
  129. static inline void free_lua_obs_callback(struct lua_obs_callback *cb)
  130. {
  131. free_script_callback(&cb->base);
  132. }
  133. /* ------------------------------------------------ */
  134. static inline int is_ptr(lua_State *script, int idx)
  135. {
  136. return lua_isuserdata(script, idx) || lua_isnil(script, idx);
  137. }
  138. static inline int is_table(lua_State *script, int idx)
  139. {
  140. return lua_istable(script, idx);
  141. }
  142. static inline int is_function(lua_State *script, int idx)
  143. {
  144. return lua_isfunction(script, idx);
  145. }
  146. typedef int (*param_cb)(lua_State *script, int idx);
  147. static inline bool verify_args1_(lua_State *script, param_cb param1_check, const char *func)
  148. {
  149. if (lua_gettop(script) != 1) {
  150. warn("Wrong number of parameters for %s", func);
  151. return false;
  152. }
  153. if (!param1_check(script, 1)) {
  154. warn("Wrong parameter type for parameter %d of %s", 1, func);
  155. return false;
  156. }
  157. return true;
  158. }
  159. #define verify_args1(script, param1_check) verify_args1_(script, param1_check, __FUNCTION__)
  160. static inline bool call_func_(lua_State *script, int reg_idx, int args, int rets, const char *func,
  161. const char *display_name)
  162. {
  163. if (reg_idx == LUA_REFNIL)
  164. return false;
  165. struct obs_lua_script *data = current_lua_script;
  166. lua_rawgeti(script, LUA_REGISTRYINDEX, reg_idx);
  167. lua_insert(script, -1 - args);
  168. if (lua_pcall(script, args, rets, 0) != 0) {
  169. script_warn(&data->base, "Failed to call %s for %s: %s", func, display_name, lua_tostring(script, -1));
  170. lua_pop(script, 1);
  171. return false;
  172. }
  173. return true;
  174. }
  175. bool ls_get_libobs_obj_(lua_State *script, const char *type, int lua_idx, void *libobs_out, const char *id,
  176. const char *func, int line);
  177. bool ls_push_libobs_obj_(lua_State *script, const char *type, void *libobs_in, bool ownership, const char *id,
  178. const char *func, int line);
  179. extern void add_lua_source_functions(lua_State *script);