obs-scripting-python.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /******************************************************************************
  2. Copyright (C) 2015 by Andrew Skinner <[email protected]>
  3. Copyright (C) 2023 by Lain 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, ...) blog(level, "[Python] " format, ##__VA_ARGS__)
  42. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  43. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  44. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  45. /* ------------------------------------------------------------ */
  46. struct python_obs_callback;
  47. struct obs_python_script {
  48. obs_script_t base;
  49. struct dstr dir;
  50. struct dstr name;
  51. PyObject *module;
  52. PyObject *save;
  53. PyObject *update;
  54. PyObject *get_properties;
  55. struct script_callback *first_callback;
  56. PyObject *tick;
  57. struct obs_python_script *next_tick;
  58. struct obs_python_script **p_prev_next_tick;
  59. };
  60. /* ------------------------------------------------------------ */
  61. struct python_obs_callback {
  62. struct script_callback base;
  63. PyObject *func;
  64. };
  65. static inline struct python_obs_callback *add_python_obs_callback_extra(struct obs_python_script *script,
  66. PyObject *func, size_t extra_size)
  67. {
  68. struct python_obs_callback *cb =
  69. add_script_callback(&script->first_callback, (obs_script_t *)script, sizeof(*cb) + extra_size);
  70. Py_XINCREF(func);
  71. cb->func = func;
  72. return cb;
  73. }
  74. static inline struct python_obs_callback *add_python_obs_callback(struct obs_python_script *script, PyObject *func)
  75. {
  76. return add_python_obs_callback_extra(script, func, 0);
  77. }
  78. static inline void *python_obs_callback_extra_data(struct python_obs_callback *cb)
  79. {
  80. return (void *)&cb[1];
  81. }
  82. static inline struct obs_python_script *python_obs_callback_script(struct python_obs_callback *cb)
  83. {
  84. return (struct obs_python_script *)cb->base.script;
  85. }
  86. static inline struct python_obs_callback *find_next_python_obs_callback(struct obs_python_script *script,
  87. struct python_obs_callback *cb, PyObject *func)
  88. {
  89. cb = cb ? (struct python_obs_callback *)cb->base.next : (struct python_obs_callback *)script->first_callback;
  90. while (cb) {
  91. if (cb->func == func)
  92. break;
  93. cb = (struct python_obs_callback *)cb->base.next;
  94. }
  95. return cb;
  96. }
  97. static inline struct python_obs_callback *find_python_obs_callback(struct obs_python_script *script, PyObject *func)
  98. {
  99. return find_next_python_obs_callback(script, NULL, func);
  100. }
  101. static inline void remove_python_obs_callback(struct python_obs_callback *cb)
  102. {
  103. remove_script_callback(&cb->base);
  104. Py_XDECREF(cb->func);
  105. cb->func = NULL;
  106. }
  107. static inline void just_free_python_obs_callback(struct python_obs_callback *cb)
  108. {
  109. just_free_script_callback(&cb->base);
  110. }
  111. static inline void free_python_obs_callback(struct python_obs_callback *cb)
  112. {
  113. free_script_callback(&cb->base);
  114. }
  115. /* ------------------------------------------------------------ */
  116. static int parse_args_(PyObject *args, const char *func, const char *format, ...)
  117. {
  118. char new_format[128];
  119. va_list va_args;
  120. int ret;
  121. snprintf(new_format, sizeof(new_format), "%s:%s", format, func);
  122. va_start(va_args, format);
  123. ret = PyArg_VaParse(args, new_format, va_args);
  124. va_end(va_args);
  125. return ret;
  126. }
  127. #define parse_args(args, format, ...) parse_args_(args, __FUNCTION__, format, ##__VA_ARGS__)
  128. static inline bool py_error_(const char *func, int line)
  129. {
  130. if (PyErr_Occurred()) {
  131. warn("Python failure in %s:%d:", func, line);
  132. PyErr_Print();
  133. return true;
  134. }
  135. return false;
  136. }
  137. #define py_error() py_error_(__FUNCTION__, __LINE__)
  138. #define lock_python() PyGILState_STATE gstate = PyGILState_Ensure()
  139. #define relock_python() gstate = PyGILState_Ensure()
  140. #define unlock_python() PyGILState_Release(gstate)
  141. struct py_source;
  142. typedef struct py_source py_source_t;
  143. extern PyObject *py_libobs;
  144. extern struct python_obs_callback *cur_python_cb;
  145. extern struct obs_python_script *cur_python_script;
  146. extern void py_to_obs_source_info(py_source_t *py_info);
  147. extern PyObject *py_obs_register_source(PyObject *self, PyObject *args);
  148. extern PyObject *py_obs_get_script_config_path(PyObject *self, PyObject *args);
  149. extern void add_functions_to_py_module(PyObject *module, PyMethodDef *method_list);
  150. /* ------------------------------------------------------------ */
  151. /* Warning: the following functions expect python to be locked! */
  152. extern bool py_to_libobs_(const char *type, PyObject *py_in, void *libobs_out, const char *id, const char *func,
  153. int line);
  154. extern bool libobs_to_py_(const char *type, void *libobs_in, bool ownership, PyObject **py_out, const char *id,
  155. const char *func, int line);
  156. extern bool py_call(PyObject *call, PyObject **ret, const char *arg_def, ...);
  157. extern bool py_import_script(const char *name);
  158. static inline PyObject *python_none(void)
  159. {
  160. PyObject *none = Py_None;
  161. Py_INCREF(none);
  162. return none;
  163. }