obs-scripting-python.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /******************************************************************************
  2. Copyright (C) 2015 by Andrew Skinner <[email protected]>
  3. Copyright (C) 2017 by Hugh Bailey <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #pragma once
  16. /* ---------------------------- */
  17. #define SWIG_TYPE_TABLE obspython
  18. #ifdef _MSC_VER
  19. #pragma warning(push)
  20. #pragma warning(disable : 4100)
  21. #pragma warning(disable : 4115)
  22. #pragma warning(disable : 4204)
  23. #endif
  24. #include "obs-scripting-python-import.h"
  25. #include <structmember.h>
  26. #include "swig/swigpyrun.h"
  27. #ifdef _MSC_VER
  28. #pragma warning(pop)
  29. #endif
  30. /* ---------------------------- */
  31. #include "obs-scripting-internal.h"
  32. #include "obs-scripting-callback.h"
  33. #ifdef _WIN32
  34. #define __func__ __FUNCTION__
  35. #else
  36. #include <dlfcn.h>
  37. #endif
  38. #include <callback/calldata.h>
  39. #include <util/threading.h>
  40. #include <util/base.h>
  41. #define do_log(level, format, ...) \
  42. blog(level, "[Python] " format, ##__VA_ARGS__)
  43. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  44. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  45. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  46. /* ------------------------------------------------------------ */
  47. struct python_obs_callback;
  48. struct obs_python_script {
  49. obs_script_t base;
  50. struct dstr dir;
  51. struct dstr name;
  52. PyObject *module;
  53. PyObject *save;
  54. PyObject *update;
  55. PyObject *get_properties;
  56. struct script_callback *first_callback;
  57. PyObject *tick;
  58. struct obs_python_script *next_tick;
  59. struct obs_python_script **p_prev_next_tick;
  60. };
  61. /* ------------------------------------------------------------ */
  62. struct python_obs_callback {
  63. struct script_callback base;
  64. PyObject *func;
  65. };
  66. static inline struct python_obs_callback *add_python_obs_callback_extra(
  67. struct obs_python_script *script,
  68. PyObject *func,
  69. size_t extra_size)
  70. {
  71. struct python_obs_callback *cb = add_script_callback(
  72. &script->first_callback,
  73. (obs_script_t *)script,
  74. sizeof(*cb) + extra_size);
  75. Py_XINCREF(func);
  76. cb->func = func;
  77. return cb;
  78. }
  79. static inline struct python_obs_callback *add_python_obs_callback(
  80. struct obs_python_script *script,
  81. PyObject *func)
  82. {
  83. return add_python_obs_callback_extra(script, func, 0);
  84. }
  85. static inline void *python_obs_callback_extra_data(
  86. struct python_obs_callback *cb)
  87. {
  88. return (void*)&cb[1];
  89. }
  90. static inline struct obs_python_script *python_obs_callback_script(
  91. struct python_obs_callback *cb)
  92. {
  93. return (struct obs_python_script *)cb->base.script;
  94. }
  95. static inline struct python_obs_callback *find_next_python_obs_callback(
  96. struct obs_python_script *script,
  97. struct python_obs_callback *cb, PyObject *func)
  98. {
  99. cb = cb ? (struct python_obs_callback *)cb->base.next
  100. : (struct python_obs_callback *)script->first_callback;
  101. while (cb) {
  102. if (cb->func == func)
  103. break;
  104. cb = (struct python_obs_callback *)cb->base.next;
  105. }
  106. return cb;
  107. }
  108. static inline struct python_obs_callback *find_python_obs_callback(
  109. struct obs_python_script *script,
  110. PyObject *func)
  111. {
  112. return find_next_python_obs_callback(script, NULL, func);
  113. }
  114. static inline void remove_python_obs_callback(struct python_obs_callback *cb)
  115. {
  116. remove_script_callback(&cb->base);
  117. Py_XDECREF(cb->func);
  118. cb->func = NULL;
  119. }
  120. static inline void just_free_python_obs_callback(struct python_obs_callback *cb)
  121. {
  122. just_free_script_callback(&cb->base);
  123. }
  124. static inline void free_python_obs_callback(struct python_obs_callback *cb)
  125. {
  126. free_script_callback(&cb->base);
  127. }
  128. /* ------------------------------------------------------------ */
  129. static int parse_args_(PyObject *args, const char *func, const char *format, ...)
  130. {
  131. char new_format[128];
  132. va_list va_args;
  133. int ret;
  134. snprintf(new_format, sizeof(new_format), "%s:%s", format, func);
  135. va_start(va_args, format);
  136. ret = PyArg_VaParse(args, new_format, va_args);
  137. va_end(va_args);
  138. return ret;
  139. }
  140. #define parse_args(args, format, ...) \
  141. parse_args_(args, __FUNCTION__, format, ##__VA_ARGS__)
  142. static inline bool py_error_(const char *func, int line)
  143. {
  144. if (PyErr_Occurred()) {
  145. warn("Python failure in %s:%d:", func, line);
  146. PyErr_Print();
  147. return true;
  148. }
  149. return false;
  150. }
  151. #define py_error() py_error_(__FUNCTION__, __LINE__)
  152. #define lock_python() \
  153. PyGILState_STATE gstate = PyGILState_Ensure()
  154. #define unlock_python() \
  155. PyGILState_Release(gstate)
  156. struct py_source;
  157. typedef struct py_source py_source_t;
  158. extern PyObject* py_libobs;
  159. extern struct python_obs_callback *cur_python_cb;
  160. extern struct obs_python_script *cur_python_script;
  161. extern void py_to_obs_source_info(py_source_t *py_info);
  162. extern PyObject *py_obs_register_source(PyObject *self, PyObject *args);
  163. extern PyObject *py_obs_get_script_config_path(PyObject *self, PyObject *args);
  164. extern void add_functions_to_py_module(PyObject *module,
  165. PyMethodDef *method_list);
  166. /* ------------------------------------------------------------ */
  167. /* Warning: the following functions expect python to be locked! */
  168. extern bool py_to_libobs_(const char *type,
  169. PyObject * py_in,
  170. void * libobs_out,
  171. const char *id,
  172. const char *func,
  173. int line);
  174. extern bool libobs_to_py_(const char *type,
  175. void * libobs_in,
  176. bool ownership,
  177. PyObject ** py_out,
  178. const char *id,
  179. const char *func,
  180. int line);
  181. extern bool py_call(PyObject *call, PyObject **ret, const char *arg_def, ...);
  182. extern bool py_import_script(const char *name);
  183. static inline PyObject *python_none(void)
  184. {
  185. PyObject *none = Py_None;
  186. Py_INCREF(none);
  187. return none;
  188. }