1
0

obs-scripting-callback.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  15. #include <callback/calldata.h>
  16. #include <util/threading.h>
  17. #include <util/bmem.h>
  18. #include "obs-scripting-internal.h"
  19. extern pthread_mutex_t detach_mutex;
  20. extern struct script_callback *detached_callbacks;
  21. struct script_callback {
  22. struct script_callback *next;
  23. struct script_callback **p_prev_next;
  24. void (*on_remove)(void *p_cb);
  25. obs_script_t *script;
  26. calldata_t extra;
  27. volatile bool removed;
  28. };
  29. static inline bool script_callback_removed(struct script_callback *cb)
  30. {
  31. return os_atomic_load_bool(&cb->removed);
  32. }
  33. static inline void *add_script_callback(struct script_callback **first, obs_script_t *script, size_t extra_size)
  34. {
  35. struct script_callback *cb = bzalloc(sizeof(*cb) + extra_size);
  36. cb->script = script;
  37. struct script_callback *next = *first;
  38. cb->next = next;
  39. cb->p_prev_next = first;
  40. if (next)
  41. next->p_prev_next = &cb->next;
  42. *first = cb;
  43. return cb;
  44. }
  45. static inline void remove_script_callback(struct script_callback *cb)
  46. {
  47. os_atomic_set_bool(&cb->removed, true);
  48. struct script_callback *next = cb->next;
  49. if (next)
  50. next->p_prev_next = cb->p_prev_next;
  51. *cb->p_prev_next = cb->next;
  52. pthread_mutex_lock(&detach_mutex);
  53. next = detached_callbacks;
  54. cb->next = next;
  55. if (next)
  56. next->p_prev_next = &cb->next;
  57. cb->p_prev_next = &detached_callbacks;
  58. detached_callbacks = cb;
  59. pthread_mutex_unlock(&detach_mutex);
  60. if (cb->on_remove)
  61. cb->on_remove(cb);
  62. }
  63. static inline void just_free_script_callback(struct script_callback *cb)
  64. {
  65. calldata_free(&cb->extra);
  66. bfree(cb);
  67. }
  68. static inline void free_script_callback(struct script_callback *cb)
  69. {
  70. pthread_mutex_lock(&detach_mutex);
  71. struct script_callback *next = cb->next;
  72. if (next)
  73. next->p_prev_next = cb->p_prev_next;
  74. *cb->p_prev_next = cb->next;
  75. pthread_mutex_unlock(&detach_mutex);
  76. just_free_script_callback(cb);
  77. }