obs-scripting-lua.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /******************************************************************************
  2. Copyright (C) 2017 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. /* ---------------------------- */
  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 *
  80. add_lua_obs_callback_extra(lua_State *script, int stack_idx, size_t extra_size)
  81. {
  82. struct obs_lua_script *data = current_lua_script;
  83. struct lua_obs_callback *cb =
  84. add_script_callback(&data->first_callback, (obs_script_t *)data,
  85. sizeof(*cb) + extra_size);
  86. lua_pushvalue(script, stack_idx);
  87. cb->reg_idx = luaL_ref(script, LUA_REGISTRYINDEX);
  88. cb->script = script;
  89. return cb;
  90. }
  91. static inline struct lua_obs_callback *add_lua_obs_callback(lua_State *script,
  92. int stack_idx)
  93. {
  94. return add_lua_obs_callback_extra(script, stack_idx, 0);
  95. }
  96. static inline void *lua_obs_callback_extra_data(struct lua_obs_callback *cb)
  97. {
  98. return (void *)&cb[1];
  99. }
  100. static inline struct obs_lua_script *
  101. lua_obs_callback_script(struct lua_obs_callback *cb)
  102. {
  103. return (struct obs_lua_script *)cb->base.script;
  104. }
  105. static inline struct lua_obs_callback *
  106. find_next_lua_obs_callback(lua_State *script, struct lua_obs_callback *cb,
  107. int stack_idx)
  108. {
  109. struct obs_lua_script *data = current_lua_script;
  110. cb = cb ? (struct lua_obs_callback *)cb->base.next
  111. : (struct lua_obs_callback *)data->first_callback;
  112. while (cb) {
  113. lua_rawgeti(script, LUA_REGISTRYINDEX, cb->reg_idx);
  114. bool match = lua_rawequal(script, -1, stack_idx);
  115. lua_pop(script, 1);
  116. if (match)
  117. break;
  118. cb = (struct lua_obs_callback *)cb->base.next;
  119. }
  120. return cb;
  121. }
  122. static inline struct lua_obs_callback *find_lua_obs_callback(lua_State *script,
  123. int stack_idx)
  124. {
  125. return find_next_lua_obs_callback(script, NULL, stack_idx);
  126. }
  127. static inline void remove_lua_obs_callback(struct lua_obs_callback *cb)
  128. {
  129. remove_script_callback(&cb->base);
  130. luaL_unref(cb->script, LUA_REGISTRYINDEX, cb->reg_idx);
  131. }
  132. static inline void just_free_lua_obs_callback(struct lua_obs_callback *cb)
  133. {
  134. just_free_script_callback(&cb->base);
  135. }
  136. static inline void free_lua_obs_callback(struct lua_obs_callback *cb)
  137. {
  138. free_script_callback(&cb->base);
  139. }
  140. /* ------------------------------------------------ */
  141. static int is_ptr(lua_State *script, int idx)
  142. {
  143. return lua_isuserdata(script, idx) || lua_isnil(script, idx);
  144. }
  145. static int is_table(lua_State *script, int idx)
  146. {
  147. return lua_istable(script, idx);
  148. }
  149. static int is_function(lua_State *script, int idx)
  150. {
  151. return lua_isfunction(script, idx);
  152. }
  153. typedef int (*param_cb)(lua_State *script, int idx);
  154. static inline bool verify_args1_(lua_State *script, param_cb param1_check,
  155. const char *func)
  156. {
  157. if (lua_gettop(script) != 1) {
  158. warn("Wrong number of parameters for %s", func);
  159. return false;
  160. }
  161. if (!param1_check(script, 1)) {
  162. warn("Wrong parameter type for parameter %d of %s", 1, func);
  163. return false;
  164. }
  165. return true;
  166. }
  167. #define verify_args1(script, param1_check) \
  168. verify_args1_(script, param1_check, __FUNCTION__)
  169. static inline bool call_func_(lua_State *script, int reg_idx, int args,
  170. int rets, const char *func,
  171. const char *display_name)
  172. {
  173. if (reg_idx == LUA_REFNIL)
  174. return false;
  175. struct obs_lua_script *data = current_lua_script;
  176. lua_rawgeti(script, LUA_REGISTRYINDEX, reg_idx);
  177. lua_insert(script, -1 - args);
  178. if (lua_pcall(script, args, rets, 0) != 0) {
  179. script_warn(&data->base, "Failed to call %s for %s: %s", func,
  180. display_name, lua_tostring(script, -1));
  181. lua_pop(script, 1);
  182. return false;
  183. }
  184. return true;
  185. }
  186. bool ls_get_libobs_obj_(lua_State *script, const char *type, int lua_idx,
  187. void *libobs_out, const char *id, const char *func,
  188. int line);
  189. bool ls_push_libobs_obj_(lua_State *script, const char *type, void *libobs_in,
  190. bool ownership, const char *id, const char *func,
  191. int line);
  192. extern void add_lua_source_functions(lua_State *script);