obs-scripting-python.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 *
  67. add_python_obs_callback_extra(struct obs_python_script *script, PyObject *func,
  68. size_t extra_size)
  69. {
  70. struct python_obs_callback *cb = add_script_callback(
  71. &script->first_callback, (obs_script_t *)script,
  72. sizeof(*cb) + extra_size);
  73. Py_XINCREF(func);
  74. cb->func = func;
  75. return cb;
  76. }
  77. static inline struct python_obs_callback *
  78. add_python_obs_callback(struct obs_python_script *script, PyObject *func)
  79. {
  80. return add_python_obs_callback_extra(script, func, 0);
  81. }
  82. static inline void *
  83. python_obs_callback_extra_data(struct python_obs_callback *cb)
  84. {
  85. return (void *)&cb[1];
  86. }
  87. static inline struct obs_python_script *
  88. python_obs_callback_script(struct python_obs_callback *cb)
  89. {
  90. return (struct obs_python_script *)cb->base.script;
  91. }
  92. static inline struct python_obs_callback *
  93. find_next_python_obs_callback(struct obs_python_script *script,
  94. struct python_obs_callback *cb, PyObject *func)
  95. {
  96. cb = cb ? (struct python_obs_callback *)cb->base.next
  97. : (struct python_obs_callback *)script->first_callback;
  98. while (cb) {
  99. if (cb->func == func)
  100. break;
  101. cb = (struct python_obs_callback *)cb->base.next;
  102. }
  103. return cb;
  104. }
  105. static inline struct python_obs_callback *
  106. find_python_obs_callback(struct obs_python_script *script, PyObject *func)
  107. {
  108. return find_next_python_obs_callback(script, NULL, func);
  109. }
  110. static inline void remove_python_obs_callback(struct python_obs_callback *cb)
  111. {
  112. remove_script_callback(&cb->base);
  113. Py_XDECREF(cb->func);
  114. cb->func = NULL;
  115. }
  116. static inline void just_free_python_obs_callback(struct python_obs_callback *cb)
  117. {
  118. just_free_script_callback(&cb->base);
  119. }
  120. static inline void free_python_obs_callback(struct python_obs_callback *cb)
  121. {
  122. free_script_callback(&cb->base);
  123. }
  124. /* ------------------------------------------------------------ */
  125. static int parse_args_(PyObject *args, const char *func, const char *format,
  126. ...)
  127. {
  128. char new_format[128];
  129. va_list va_args;
  130. int ret;
  131. snprintf(new_format, sizeof(new_format), "%s:%s", format, func);
  132. va_start(va_args, format);
  133. ret = PyArg_VaParse(args, new_format, va_args);
  134. va_end(va_args);
  135. return ret;
  136. }
  137. #define parse_args(args, format, ...) \
  138. parse_args_(args, __FUNCTION__, format, ##__VA_ARGS__)
  139. static inline bool py_error_(const char *func, int line)
  140. {
  141. if (PyErr_Occurred()) {
  142. warn("Python failure in %s:%d:", func, line);
  143. PyErr_Print();
  144. return true;
  145. }
  146. return false;
  147. }
  148. #define py_error() py_error_(__FUNCTION__, __LINE__)
  149. #define lock_python() PyGILState_STATE gstate = PyGILState_Ensure()
  150. #define relock_python() gstate = PyGILState_Ensure()
  151. #define unlock_python() PyGILState_Release(gstate)
  152. struct py_source;
  153. typedef struct py_source py_source_t;
  154. extern PyObject *py_libobs;
  155. extern struct python_obs_callback *cur_python_cb;
  156. extern struct obs_python_script *cur_python_script;
  157. extern void py_to_obs_source_info(py_source_t *py_info);
  158. extern PyObject *py_obs_register_source(PyObject *self, PyObject *args);
  159. extern PyObject *py_obs_get_script_config_path(PyObject *self, PyObject *args);
  160. extern void add_functions_to_py_module(PyObject *module,
  161. PyMethodDef *method_list);
  162. /* ------------------------------------------------------------ */
  163. /* Warning: the following functions expect python to be locked! */
  164. extern bool py_to_libobs_(const char *type, PyObject *py_in, void *libobs_out,
  165. const char *id, const char *func, int line);
  166. extern bool libobs_to_py_(const char *type, void *libobs_in, bool ownership,
  167. PyObject **py_out, const char *id, const char *func,
  168. int line);
  169. extern bool py_call(PyObject *call, PyObject **ret, const char *arg_def, ...);
  170. extern bool py_import_script(const char *name);
  171. static inline PyObject *python_none(void)
  172. {
  173. PyObject *none = Py_None;
  174. Py_INCREF(none);
  175. return none;
  176. }