obs-scripting-lua-frontend.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #include <obs-module.h>
  15. #include <obs-frontend-api.h>
  16. #include "obs-scripting-lua.h"
  17. #define ls_get_libobs_obj(type, lua_index, obs_obj) \
  18. ls_get_libobs_obj_(script, #type " *", lua_index, obs_obj, \
  19. NULL, __FUNCTION__, __LINE__)
  20. #define ls_push_libobs_obj(type, obs_obj, ownership) \
  21. ls_push_libobs_obj_(script, #type " *", obs_obj, ownership, \
  22. NULL, __FUNCTION__, __LINE__)
  23. #define call_func(func, args, rets) \
  24. call_func_(script, cb->reg_idx, args, rets, #func, "frontend API")
  25. /* ----------------------------------- */
  26. static int get_scene_names(lua_State *script)
  27. {
  28. char **names = obs_frontend_get_scene_names();
  29. char **name = names;
  30. int i = 0;
  31. lua_newtable(script);
  32. while (name && *name) {
  33. lua_pushstring(script, *name);
  34. lua_rawseti(script, -2, ++i);
  35. name++;
  36. }
  37. bfree(names);
  38. return 1;
  39. }
  40. static int get_scenes(lua_State *script)
  41. {
  42. struct obs_frontend_source_list list = {0};
  43. obs_frontend_get_scenes(&list);
  44. lua_newtable(script);
  45. for (size_t i = 0; i < list.sources.num; i++) {
  46. obs_source_t *source = list.sources.array[i];
  47. ls_push_libobs_obj(obs_source_t, source, false);
  48. lua_rawseti(script, -2, (int)(i + 1));
  49. }
  50. da_free(list.sources);
  51. return 1;
  52. }
  53. static int get_current_scene(lua_State *script)
  54. {
  55. obs_source_t *source = obs_frontend_get_current_scene();
  56. ls_push_libobs_obj(obs_source_t, source, false);
  57. return 1;
  58. }
  59. static int set_current_scene(lua_State *script)
  60. {
  61. obs_source_t *source = NULL;
  62. ls_get_libobs_obj(obs_source_t, 1, &source);
  63. obs_frontend_set_current_scene(source);
  64. return 0;
  65. }
  66. static int get_transitions(lua_State *script)
  67. {
  68. struct obs_frontend_source_list list = {0};
  69. obs_frontend_get_transitions(&list);
  70. lua_newtable(script);
  71. for (size_t i = 0; i < list.sources.num; i++) {
  72. obs_source_t *source = list.sources.array[i];
  73. ls_push_libobs_obj(obs_source_t, source, false);
  74. lua_rawseti(script, -2, (int)(i + 1));
  75. }
  76. da_free(list.sources);
  77. return 1;
  78. }
  79. static int get_current_transition(lua_State *script)
  80. {
  81. obs_source_t *source = obs_frontend_get_current_transition();
  82. ls_push_libobs_obj(obs_source_t, source, false);
  83. return 1;
  84. }
  85. static int set_current_transition(lua_State *script)
  86. {
  87. obs_source_t *source = NULL;
  88. ls_get_libobs_obj(obs_source_t, 1, &source);
  89. obs_frontend_set_current_transition(source);
  90. return 0;
  91. }
  92. static int get_scene_collections(lua_State *script)
  93. {
  94. char **names = obs_frontend_get_scene_collections();
  95. char **name = names;
  96. int i = 0;
  97. lua_newtable(script);
  98. while (name && *name) {
  99. lua_pushstring(script, *name);
  100. lua_rawseti(script, -2, ++i);
  101. name++;
  102. }
  103. bfree(names);
  104. return 1;
  105. }
  106. static int get_current_scene_collection(lua_State *script)
  107. {
  108. char *name = obs_frontend_get_current_scene_collection();
  109. lua_pushstring(script, name);
  110. bfree(name);
  111. return 1;
  112. }
  113. static int set_current_scene_collection(lua_State *script)
  114. {
  115. if (lua_isstring(script, 1)) {
  116. const char *name = lua_tostring(script, 1);
  117. obs_frontend_set_current_scene_collection(name);
  118. }
  119. return 0;
  120. }
  121. static int get_profiles(lua_State *script)
  122. {
  123. char **names = obs_frontend_get_profiles();
  124. char **name = names;
  125. int i = 0;
  126. lua_newtable(script);
  127. while (name && *name) {
  128. lua_pushstring(script, *name);
  129. lua_rawseti(script, -2, ++i);
  130. name++;
  131. }
  132. bfree(names);
  133. return 1;
  134. }
  135. static int get_current_profile(lua_State *script)
  136. {
  137. char *name = obs_frontend_get_current_profile();
  138. lua_pushstring(script, name);
  139. bfree(name);
  140. return 1;
  141. }
  142. static int set_current_profile(lua_State *script)
  143. {
  144. if (lua_isstring(script, 1)) {
  145. const char *name = lua_tostring(script, 1);
  146. obs_frontend_set_current_profile(name);
  147. }
  148. return 0;
  149. }
  150. /* ----------------------------------- */
  151. static void frontend_event_callback(enum obs_frontend_event event, void *priv)
  152. {
  153. struct lua_obs_callback *cb = priv;
  154. lua_State *script = cb->script;
  155. if (cb->base.removed) {
  156. obs_frontend_remove_event_callback(frontend_event_callback, cb);
  157. return;
  158. }
  159. lock_callback();
  160. lua_pushinteger(script, (int)event);
  161. call_func(frontend_event_callback, 1, 0);
  162. unlock_callback();
  163. }
  164. static int remove_event_callback(lua_State *script)
  165. {
  166. if (!verify_args1(script, is_function))
  167. return 0;
  168. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  169. if (cb) {
  170. remove_lua_obs_callback(cb);
  171. }
  172. return 0;
  173. }
  174. static void add_event_callback_defer(void *cb)
  175. {
  176. obs_frontend_add_event_callback(frontend_event_callback, cb);
  177. }
  178. static int add_event_callback(lua_State *script)
  179. {
  180. if (!verify_args1(script, is_function))
  181. return 0;
  182. struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
  183. defer_call_post(add_event_callback_defer, cb);
  184. return 0;
  185. }
  186. /* ----------------------------------- */
  187. static void frontend_save_callback(obs_data_t *save_data, bool saving,
  188. void *priv)
  189. {
  190. struct lua_obs_callback *cb = priv;
  191. lua_State *script = cb->script;
  192. if (cb->base.removed) {
  193. obs_frontend_remove_save_callback(frontend_save_callback, cb);
  194. return;
  195. }
  196. lock_callback();
  197. ls_push_libobs_obj(obs_data_t, save_data, false);
  198. lua_pushboolean(script, saving);
  199. call_func(frontend_save_callback, 2, 0);
  200. unlock_callback();
  201. }
  202. static int remove_save_callback(lua_State *script)
  203. {
  204. if (!verify_args1(script, is_function))
  205. return 0;
  206. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  207. if (cb) {
  208. remove_lua_obs_callback(cb);
  209. }
  210. return 0;
  211. }
  212. static void add_save_callback_defer(void *cb)
  213. {
  214. obs_frontend_add_save_callback(frontend_save_callback, cb);
  215. }
  216. static int add_save_callback(lua_State *script)
  217. {
  218. if (!verify_args1(script, is_function))
  219. return 0;
  220. struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
  221. defer_call_post(add_save_callback_defer, cb);
  222. return 0;
  223. }
  224. /* ----------------------------------- */
  225. void add_lua_frontend_funcs(lua_State *script)
  226. {
  227. lua_getglobal(script, "obslua");
  228. #define add_func(name) \
  229. do { \
  230. lua_pushstring(script, "obs_frontend_" #name); \
  231. lua_pushcfunction(script, name); \
  232. lua_rawset(script, -3); \
  233. } while (false)
  234. add_func(get_scene_names);
  235. add_func(get_scenes);
  236. add_func(get_current_scene);
  237. add_func(set_current_scene);
  238. add_func(get_transitions);
  239. add_func(get_current_transition);
  240. add_func(set_current_transition);
  241. add_func(get_scene_collections);
  242. add_func(get_current_scene_collection);
  243. add_func(set_current_scene_collection);
  244. add_func(get_profiles);
  245. add_func(get_current_profile);
  246. add_func(set_current_profile);
  247. add_func(remove_event_callback);
  248. add_func(add_event_callback);
  249. add_func(remove_save_callback);
  250. add_func(add_save_callback);
  251. #undef add_func
  252. lua_pop(script, 1);
  253. }