obs-scripting-python-frontend.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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-python.h"
  17. #define libobs_to_py(type, obs_obj, ownership, py_obj) \
  18. libobs_to_py_(#type " *", obs_obj, ownership, py_obj, NULL, __func__, \
  19. __LINE__)
  20. #define py_to_libobs(type, py_obj, libobs_out) \
  21. py_to_libobs_(#type " *", py_obj, libobs_out, NULL, __func__, __LINE__)
  22. /* ----------------------------------- */
  23. static PyObject *get_scene_names(PyObject *self, PyObject *args)
  24. {
  25. char **names = obs_frontend_get_scene_names();
  26. char **name = names;
  27. PyObject *list = PyList_New(0);
  28. while (name && *name) {
  29. PyObject *py_name = PyUnicode_FromString(*name);
  30. if (py_name) {
  31. PyList_Append(list, py_name);
  32. Py_DECREF(py_name);
  33. }
  34. name++;
  35. }
  36. UNUSED_PARAMETER(self);
  37. UNUSED_PARAMETER(args);
  38. bfree(names);
  39. return list;
  40. }
  41. static PyObject *get_scenes(PyObject *self, PyObject *args)
  42. {
  43. struct obs_frontend_source_list list = {0};
  44. obs_frontend_get_scenes(&list);
  45. PyObject *ret = PyList_New(0);
  46. for (size_t i = 0; i < list.sources.num; i++) {
  47. obs_source_t *source = list.sources.array[i];
  48. PyObject *py_source;
  49. if (libobs_to_py(obs_source_t, source, false, &py_source)) {
  50. PyList_Append(ret, py_source);
  51. Py_DECREF(py_source);
  52. }
  53. }
  54. UNUSED_PARAMETER(self);
  55. UNUSED_PARAMETER(args);
  56. da_free(list.sources);
  57. return ret;
  58. }
  59. static PyObject *get_current_scene(PyObject *self, PyObject *args)
  60. {
  61. obs_source_t *source = obs_frontend_get_current_scene();
  62. PyObject *py_source;
  63. if (!libobs_to_py(obs_source_t, source, false, &py_source)) {
  64. obs_source_release(source);
  65. return python_none();
  66. }
  67. UNUSED_PARAMETER(self);
  68. UNUSED_PARAMETER(args);
  69. return py_source;
  70. }
  71. static PyObject *set_current_scene(PyObject *self, PyObject *args)
  72. {
  73. PyObject *py_source;
  74. obs_source_t *source = NULL;
  75. if (!parse_args(args, "O", &py_source))
  76. return python_none();
  77. if (!py_to_libobs(obs_source_t, py_source, &source))
  78. return python_none();
  79. UNUSED_PARAMETER(self);
  80. obs_frontend_set_current_scene(source);
  81. return python_none();
  82. }
  83. static PyObject *get_transitions(PyObject *self, PyObject *args)
  84. {
  85. struct obs_frontend_source_list list = {0};
  86. obs_frontend_get_transitions(&list);
  87. PyObject *ret = PyList_New(0);
  88. for (size_t i = 0; i < list.sources.num; i++) {
  89. obs_source_t *source = list.sources.array[i];
  90. PyObject *py_source;
  91. if (libobs_to_py(obs_source_t, source, false, &py_source)) {
  92. PyList_Append(ret, py_source);
  93. Py_DECREF(py_source);
  94. }
  95. }
  96. UNUSED_PARAMETER(self);
  97. UNUSED_PARAMETER(args);
  98. da_free(list.sources);
  99. return ret;
  100. }
  101. static PyObject *get_current_transition(PyObject *self, PyObject *args)
  102. {
  103. obs_source_t *source = obs_frontend_get_current_transition();
  104. PyObject *py_source;
  105. if (!libobs_to_py(obs_source_t, source, false, &py_source)) {
  106. obs_source_release(source);
  107. return python_none();
  108. }
  109. UNUSED_PARAMETER(self);
  110. UNUSED_PARAMETER(args);
  111. return py_source;
  112. }
  113. static PyObject *set_current_transition(PyObject *self, PyObject *args)
  114. {
  115. PyObject *py_source;
  116. obs_source_t *source = NULL;
  117. if (!parse_args(args, "O", &py_source))
  118. return python_none();
  119. if (!py_to_libobs(obs_source_t, py_source, &source))
  120. return python_none();
  121. UNUSED_PARAMETER(self);
  122. obs_frontend_set_current_transition(source);
  123. return python_none();
  124. }
  125. static PyObject *get_scene_collections(PyObject *self, PyObject *args)
  126. {
  127. char **names = obs_frontend_get_scene_collections();
  128. char **name = names;
  129. PyObject *list = PyList_New(0);
  130. while (name && *name) {
  131. PyObject *py_name = PyUnicode_FromString(*name);
  132. if (py_name) {
  133. PyList_Append(list, py_name);
  134. Py_DECREF(py_name);
  135. }
  136. name++;
  137. }
  138. UNUSED_PARAMETER(self);
  139. UNUSED_PARAMETER(args);
  140. bfree(names);
  141. return list;
  142. }
  143. static PyObject *get_current_scene_collection(PyObject *self, PyObject *args)
  144. {
  145. char *name = obs_frontend_get_current_scene_collection();
  146. PyObject *ret = PyUnicode_FromString(name);
  147. bfree(name);
  148. UNUSED_PARAMETER(self);
  149. UNUSED_PARAMETER(args);
  150. return ret;
  151. }
  152. static PyObject *set_current_scene_collection(PyObject *self, PyObject *args)
  153. {
  154. const char *name;
  155. if (!parse_args(args, "s", &name))
  156. return python_none();
  157. UNUSED_PARAMETER(self);
  158. obs_frontend_set_current_scene_collection(name);
  159. return python_none();
  160. }
  161. static PyObject *get_profiles(PyObject *self, PyObject *args)
  162. {
  163. char **names = obs_frontend_get_profiles();
  164. char **name = names;
  165. PyObject *list = PyList_New(0);
  166. while (name && *name) {
  167. PyObject *py_name = PyUnicode_FromString(*name);
  168. if (py_name) {
  169. PyList_Append(list, py_name);
  170. Py_DECREF(py_name);
  171. }
  172. name++;
  173. }
  174. UNUSED_PARAMETER(self);
  175. UNUSED_PARAMETER(args);
  176. bfree(names);
  177. return list;
  178. }
  179. static PyObject *get_current_profile(PyObject *self, PyObject *args)
  180. {
  181. char *name = obs_frontend_get_current_profile();
  182. PyObject *ret = PyUnicode_FromString(name);
  183. bfree(name);
  184. UNUSED_PARAMETER(self);
  185. UNUSED_PARAMETER(args);
  186. return ret;
  187. }
  188. static PyObject *set_current_profile(PyObject *self, PyObject *args)
  189. {
  190. const char *name;
  191. if (!parse_args(args, "s", &name))
  192. return python_none();
  193. UNUSED_PARAMETER(self);
  194. obs_frontend_set_current_profile(name);
  195. return python_none();
  196. }
  197. /* ----------------------------------- */
  198. static void frontend_save_callback(obs_data_t *save_data, bool saving,
  199. void *priv)
  200. {
  201. struct python_obs_callback *cb = priv;
  202. if (cb->base.removed) {
  203. obs_frontend_remove_save_callback(frontend_save_callback, cb);
  204. return;
  205. }
  206. lock_python();
  207. PyObject *py_save_data;
  208. if (libobs_to_py(obs_data_t, save_data, false, &py_save_data)) {
  209. PyObject *args = Py_BuildValue("(Op)", py_save_data, saving);
  210. struct python_obs_callback *last_cb = cur_python_cb;
  211. cur_python_cb = cb;
  212. cur_python_script = (struct obs_python_script *)cb->base.script;
  213. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  214. Py_XDECREF(py_ret);
  215. py_error();
  216. cur_python_script = NULL;
  217. cur_python_cb = last_cb;
  218. Py_XDECREF(args);
  219. Py_XDECREF(py_save_data);
  220. }
  221. unlock_python();
  222. }
  223. static PyObject *remove_save_callback(PyObject *self, PyObject *args)
  224. {
  225. struct obs_python_script *script = cur_python_script;
  226. PyObject *py_cb = NULL;
  227. UNUSED_PARAMETER(self);
  228. if (!parse_args(args, "O", &py_cb))
  229. return python_none();
  230. if (!py_cb || !PyFunction_Check(py_cb))
  231. return python_none();
  232. struct python_obs_callback *cb =
  233. find_python_obs_callback(script, py_cb);
  234. if (cb)
  235. remove_python_obs_callback(cb);
  236. return python_none();
  237. }
  238. static void add_save_callback_defer(void *cb)
  239. {
  240. obs_frontend_add_save_callback(frontend_save_callback, cb);
  241. }
  242. static PyObject *add_save_callback(PyObject *self, PyObject *args)
  243. {
  244. struct obs_python_script *script = cur_python_script;
  245. PyObject *py_cb = NULL;
  246. UNUSED_PARAMETER(self);
  247. if (!parse_args(args, "O", &py_cb))
  248. return python_none();
  249. if (!py_cb || !PyFunction_Check(py_cb))
  250. return python_none();
  251. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  252. defer_call_post(add_save_callback_defer, cb);
  253. return python_none();
  254. }
  255. static void frontend_event_callback(enum obs_frontend_event event, void *priv)
  256. {
  257. struct python_obs_callback *cb = priv;
  258. if (cb->base.removed) {
  259. obs_frontend_remove_event_callback(frontend_event_callback, cb);
  260. return;
  261. }
  262. lock_python();
  263. PyObject *args = Py_BuildValue("(i)", event);
  264. struct python_obs_callback *last_cb = cur_python_cb;
  265. cur_python_cb = cb;
  266. cur_python_script = (struct obs_python_script *)cb->base.script;
  267. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  268. Py_XDECREF(py_ret);
  269. py_error();
  270. cur_python_script = NULL;
  271. cur_python_cb = last_cb;
  272. Py_XDECREF(args);
  273. unlock_python();
  274. }
  275. static PyObject *remove_event_callback(PyObject *self, PyObject *args)
  276. {
  277. struct obs_python_script *script = cur_python_script;
  278. PyObject *py_cb = NULL;
  279. UNUSED_PARAMETER(self);
  280. if (!parse_args(args, "O", &py_cb))
  281. return python_none();
  282. if (!py_cb || !PyFunction_Check(py_cb))
  283. return python_none();
  284. struct python_obs_callback *cb =
  285. find_python_obs_callback(script, py_cb);
  286. if (cb)
  287. remove_python_obs_callback(cb);
  288. return python_none();
  289. }
  290. static void add_event_callback_defer(void *cb)
  291. {
  292. obs_frontend_add_event_callback(frontend_event_callback, cb);
  293. }
  294. static PyObject *add_event_callback(PyObject *self, PyObject *args)
  295. {
  296. struct obs_python_script *script = cur_python_script;
  297. PyObject *py_cb = NULL;
  298. UNUSED_PARAMETER(self);
  299. if (!parse_args(args, "O", &py_cb))
  300. return python_none();
  301. if (!py_cb || !PyFunction_Check(py_cb))
  302. return python_none();
  303. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  304. defer_call_post(add_event_callback_defer, cb);
  305. return python_none();
  306. }
  307. /* ----------------------------------- */
  308. void add_python_frontend_funcs(PyObject *module)
  309. {
  310. static PyMethodDef funcs[] = {
  311. #define DEF_FUNC(c) {"obs_frontend_" #c, c, METH_VARARGS, NULL}
  312. DEF_FUNC(get_scene_names),
  313. DEF_FUNC(get_scenes),
  314. DEF_FUNC(get_current_scene),
  315. DEF_FUNC(set_current_scene),
  316. DEF_FUNC(get_transitions),
  317. DEF_FUNC(get_current_transition),
  318. DEF_FUNC(set_current_transition),
  319. DEF_FUNC(get_scene_collections),
  320. DEF_FUNC(get_current_scene_collection),
  321. DEF_FUNC(set_current_scene_collection),
  322. DEF_FUNC(get_profiles),
  323. DEF_FUNC(get_current_profile),
  324. DEF_FUNC(set_current_profile),
  325. DEF_FUNC(remove_save_callback),
  326. DEF_FUNC(add_save_callback),
  327. DEF_FUNC(remove_event_callback),
  328. DEF_FUNC(add_event_callback),
  329. #undef DEF_FUNC
  330. {0}};
  331. add_functions_to_py_module(module, funcs);
  332. }