1
0

obs-scripting-lua-frontend.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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, \
  19. __FUNCTION__, __LINE__)
  20. #define ls_push_libobs_obj(type, obs_obj, ownership) \
  21. ls_push_libobs_obj_(script, #type " *", obs_obj, ownership, NULL, \
  22. __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_transition_duration(lua_State *script)
  93. {
  94. int duration = obs_frontend_get_transition_duration();
  95. lua_pushinteger(script, duration);
  96. return 1;
  97. }
  98. static int set_transition_duration(lua_State *script)
  99. {
  100. if (lua_isnumber(script, 1)) {
  101. int duration = (int)lua_tointeger(script, 1);
  102. obs_frontend_set_transition_duration(duration);
  103. }
  104. return 0;
  105. }
  106. static int get_scene_collections(lua_State *script)
  107. {
  108. char **names = obs_frontend_get_scene_collections();
  109. char **name = names;
  110. int i = 0;
  111. lua_newtable(script);
  112. while (name && *name) {
  113. lua_pushstring(script, *name);
  114. lua_rawseti(script, -2, ++i);
  115. name++;
  116. }
  117. bfree(names);
  118. return 1;
  119. }
  120. static int get_current_scene_collection(lua_State *script)
  121. {
  122. char *name = obs_frontend_get_current_scene_collection();
  123. lua_pushstring(script, name);
  124. bfree(name);
  125. return 1;
  126. }
  127. static int set_current_scene_collection(lua_State *script)
  128. {
  129. if (lua_isstring(script, 1)) {
  130. const char *name = lua_tostring(script, 1);
  131. obs_frontend_set_current_scene_collection(name);
  132. }
  133. return 0;
  134. }
  135. static int get_profiles(lua_State *script)
  136. {
  137. char **names = obs_frontend_get_profiles();
  138. char **name = names;
  139. int i = 0;
  140. lua_newtable(script);
  141. while (name && *name) {
  142. lua_pushstring(script, *name);
  143. lua_rawseti(script, -2, ++i);
  144. name++;
  145. }
  146. bfree(names);
  147. return 1;
  148. }
  149. static int get_current_profile(lua_State *script)
  150. {
  151. char *name = obs_frontend_get_current_profile();
  152. lua_pushstring(script, name);
  153. bfree(name);
  154. return 1;
  155. }
  156. static int set_current_profile(lua_State *script)
  157. {
  158. if (lua_isstring(script, 1)) {
  159. const char *name = lua_tostring(script, 1);
  160. obs_frontend_set_current_profile(name);
  161. }
  162. return 0;
  163. }
  164. /* ----------------------------------- */
  165. static void frontend_event_callback(enum obs_frontend_event event, void *priv)
  166. {
  167. struct lua_obs_callback *cb = priv;
  168. lua_State *script = cb->script;
  169. if (script_callback_removed(&cb->base)) {
  170. obs_frontend_remove_event_callback(frontend_event_callback, cb);
  171. return;
  172. }
  173. lock_callback();
  174. lua_pushinteger(script, (int)event);
  175. call_func(frontend_event_callback, 1, 0);
  176. unlock_callback();
  177. }
  178. static int remove_event_callback(lua_State *script)
  179. {
  180. if (!verify_args1(script, is_function))
  181. return 0;
  182. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  183. if (cb) {
  184. remove_lua_obs_callback(cb);
  185. }
  186. return 0;
  187. }
  188. static void add_event_callback_defer(void *cb)
  189. {
  190. obs_frontend_add_event_callback(frontend_event_callback, cb);
  191. }
  192. static int add_event_callback(lua_State *script)
  193. {
  194. if (!verify_args1(script, is_function))
  195. return 0;
  196. struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
  197. defer_call_post(add_event_callback_defer, cb);
  198. return 0;
  199. }
  200. /* ----------------------------------- */
  201. static void frontend_save_callback(obs_data_t *save_data, bool saving,
  202. void *priv)
  203. {
  204. struct lua_obs_callback *cb = priv;
  205. lua_State *script = cb->script;
  206. if (script_callback_removed(&cb->base)) {
  207. obs_frontend_remove_save_callback(frontend_save_callback, cb);
  208. return;
  209. }
  210. lock_callback();
  211. ls_push_libobs_obj(obs_data_t, save_data, false);
  212. lua_pushboolean(script, saving);
  213. call_func(frontend_save_callback, 2, 0);
  214. unlock_callback();
  215. }
  216. static int remove_save_callback(lua_State *script)
  217. {
  218. if (!verify_args1(script, is_function))
  219. return 0;
  220. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  221. if (cb) {
  222. remove_lua_obs_callback(cb);
  223. }
  224. return 0;
  225. }
  226. static void add_save_callback_defer(void *cb)
  227. {
  228. obs_frontend_add_save_callback(frontend_save_callback, cb);
  229. }
  230. static int add_save_callback(lua_State *script)
  231. {
  232. if (!verify_args1(script, is_function))
  233. return 0;
  234. struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
  235. defer_call_post(add_save_callback_defer, cb);
  236. return 0;
  237. }
  238. /* ----------------------------------- */
  239. typedef struct lua_function_tuple {
  240. const char *name;
  241. lua_CFunction function;
  242. } obs_frontend_func;
  243. static const obs_frontend_func functions[] = {
  244. {.name = "obs_frontend_get_scene_names", .function = get_scene_names},
  245. {.name = "obs_frontend_get_scenes", .function = get_scenes},
  246. {.name = "obs_frontend_get_current_scene",
  247. .function = get_current_scene},
  248. {.name = "obs_frontend_set_current_scene",
  249. .function = set_current_scene},
  250. {.name = "obs_frontend_get_transitions", .function = get_transitions},
  251. {.name = "obs_frontend_get_current_transition",
  252. .function = get_current_transition},
  253. {.name = "obs_frontend_set_current_transition",
  254. .function = set_current_transition},
  255. {.name = "obs_frontend_get_transition_duration",
  256. .function = get_transition_duration},
  257. {.name = "obs_frontend_set_transition_duration",
  258. .function = set_transition_duration},
  259. {.name = "obs_frontend_get_scene_collections",
  260. .function = get_scene_collections},
  261. {.name = "obs_frontend_get_current_scene_collection",
  262. .function = get_current_scene_collection},
  263. {.name = "obs_frontend_set_current_scene_collection",
  264. .function = set_current_scene_collection},
  265. {.name = "obs_frontend_get_profiles", .function = get_profiles},
  266. {.name = "obs_frontend_get_current_profile",
  267. .function = get_current_profile},
  268. {.name = "obs_frontend_set_current_profile",
  269. .function = set_current_profile},
  270. {.name = "obs_frontend_remove_event_callback",
  271. .function = remove_event_callback},
  272. {.name = "obs_frontend_add_event_callback",
  273. .function = add_event_callback},
  274. {.name = "obs_frontend_remove_save_callback",
  275. .function = remove_save_callback},
  276. {.name = "obs_frontend_add_save_callback",
  277. .function = add_save_callback},
  278. };
  279. void add_lua_frontend_funcs(lua_State *script)
  280. {
  281. lua_getglobal(script, "obslua");
  282. size_t num_items = OBS_COUNTOF(functions);
  283. for (size_t i = 0; i < num_items; i++) {
  284. lua_pushstring(script, functions[i].name);
  285. lua_pushcfunction(script, functions[i].function);
  286. lua_rawset(script, -3);
  287. }
  288. lua_pop(script, 1);
  289. }