obs-scripting-lua.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, ...) \
  38. blog(level, "[Lua] " format, ##__VA_ARGS__)
  39. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  40. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  41. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  42. /* ------------------------------------------------------------ */
  43. struct obs_lua_script;
  44. struct lua_obs_callback;
  45. extern THREAD_LOCAL struct lua_obs_callback *current_lua_cb;
  46. extern THREAD_LOCAL struct obs_lua_script *current_lua_script;
  47. /* ------------------------------------------------------------ */
  48. struct lua_obs_callback;
  49. struct obs_lua_script {
  50. obs_script_t base;
  51. struct dstr dir;
  52. struct dstr log_chunk;
  53. pthread_mutex_t mutex;
  54. lua_State *script;
  55. struct script_callback *first_callback;
  56. int update;
  57. int get_properties;
  58. int save;
  59. int tick;
  60. struct obs_lua_script *next_tick;
  61. struct obs_lua_script **p_prev_next_tick;
  62. bool defined_sources;
  63. };
  64. #define lock_callback() \
  65. struct obs_lua_script *__last_script = current_lua_script; \
  66. struct lua_obs_callback *__last_callback = current_lua_cb; \
  67. current_lua_cb = cb; \
  68. current_lua_script = (struct obs_lua_script *)cb->base.script; \
  69. pthread_mutex_lock(&current_lua_script->mutex);
  70. #define unlock_callback() \
  71. pthread_mutex_unlock(&current_lua_script->mutex); \
  72. current_lua_script = __last_script; \
  73. current_lua_cb = __last_callback;
  74. /* ------------------------------------------------ */
  75. struct lua_obs_callback {
  76. struct script_callback base;
  77. lua_State *script;
  78. int reg_idx;
  79. };
  80. static inline struct lua_obs_callback *add_lua_obs_callback_extra(
  81. lua_State *script,
  82. int stack_idx,
  83. size_t extra_size)
  84. {
  85. struct obs_lua_script *data = current_lua_script;
  86. struct lua_obs_callback *cb = add_script_callback(
  87. &data->first_callback,
  88. (obs_script_t *)data,
  89. sizeof(*cb) + extra_size);
  90. lua_pushvalue(script, stack_idx);
  91. cb->reg_idx = luaL_ref(script, LUA_REGISTRYINDEX);
  92. cb->script = script;
  93. return cb;
  94. }
  95. static inline struct lua_obs_callback *add_lua_obs_callback(
  96. lua_State *script, int stack_idx)
  97. {
  98. return add_lua_obs_callback_extra(script, stack_idx, 0);
  99. }
  100. static inline void *lua_obs_callback_extra_data(struct lua_obs_callback *cb)
  101. {
  102. return (void*)&cb[1];
  103. }
  104. static inline struct obs_lua_script *lua_obs_callback_script(
  105. struct lua_obs_callback *cb)
  106. {
  107. return (struct obs_lua_script *)cb->base.script;
  108. }
  109. static inline struct lua_obs_callback *find_next_lua_obs_callback(
  110. lua_State *script, struct lua_obs_callback *cb, int stack_idx)
  111. {
  112. struct obs_lua_script *data = current_lua_script;
  113. cb = cb ? (struct lua_obs_callback *)cb->base.next
  114. : (struct lua_obs_callback *)data->first_callback;
  115. while (cb) {
  116. lua_rawgeti(script, LUA_REGISTRYINDEX, cb->reg_idx);
  117. bool match = lua_rawequal(script, -1, stack_idx);
  118. lua_pop(script, 1);
  119. if (match)
  120. break;
  121. cb = (struct lua_obs_callback *)cb->base.next;
  122. }
  123. return cb;
  124. }
  125. static inline struct lua_obs_callback *find_lua_obs_callback(
  126. lua_State *script, int stack_idx)
  127. {
  128. return find_next_lua_obs_callback(script, NULL, stack_idx);
  129. }
  130. static inline void remove_lua_obs_callback(struct lua_obs_callback *cb)
  131. {
  132. remove_script_callback(&cb->base);
  133. luaL_unref(cb->script, LUA_REGISTRYINDEX, cb->reg_idx);
  134. }
  135. static inline void just_free_lua_obs_callback(struct lua_obs_callback *cb)
  136. {
  137. just_free_script_callback(&cb->base);
  138. }
  139. static inline void free_lua_obs_callback(struct lua_obs_callback *cb)
  140. {
  141. free_script_callback(&cb->base);
  142. }
  143. /* ------------------------------------------------ */
  144. static int is_ptr(lua_State *script, int idx)
  145. {
  146. return lua_isuserdata(script, idx) || lua_isnil(script, idx);
  147. }
  148. static int is_table(lua_State *script, int idx)
  149. {
  150. return lua_istable(script, idx);
  151. }
  152. static int is_function(lua_State *script, int idx)
  153. {
  154. return lua_isfunction(script, idx);
  155. }
  156. typedef int (*param_cb)(lua_State *script, int idx);
  157. static inline bool verify_args1_(lua_State *script,
  158. param_cb param1_check,
  159. const char *func)
  160. {
  161. if (lua_gettop(script) != 1) {
  162. warn("Wrong number of parameters for %s", func);
  163. return false;
  164. }
  165. if (!param1_check(script, 1)) {
  166. warn("Wrong parameter type for parameter %d of %s", 1, func);
  167. return false;
  168. }
  169. return true;
  170. }
  171. #define verify_args1(script, param1_check) \
  172. verify_args1_(script, param1_check, __FUNCTION__)
  173. static inline bool call_func_(lua_State *script,
  174. int reg_idx, int args, int rets,
  175. const char *func, const char *display_name)
  176. {
  177. if (reg_idx == LUA_REFNIL)
  178. return false;
  179. struct obs_lua_script *data = current_lua_script;
  180. lua_rawgeti(script, LUA_REGISTRYINDEX, reg_idx);
  181. lua_insert(script, -1 - args);
  182. if (lua_pcall(script, args, rets, 0) != 0) {
  183. script_warn(&data->base, "Failed to call %s for %s: %s", func,
  184. display_name,
  185. lua_tostring(script, -1));
  186. lua_pop(script, 1);
  187. return false;
  188. }
  189. return true;
  190. }
  191. bool ls_get_libobs_obj_(lua_State * script,
  192. const char *type,
  193. int lua_idx,
  194. void * libobs_out,
  195. const char *id,
  196. const char *func,
  197. int line);
  198. bool ls_push_libobs_obj_(lua_State * script,
  199. const char *type,
  200. void * libobs_in,
  201. bool ownership,
  202. const char *id,
  203. const char *func,
  204. int line);
  205. extern void add_lua_source_functions(lua_State *script);