1
0

obs-scripting-lua-frontend.c 8.8 KB

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