obs-scripting-callback.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #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. bool removed;
  28. };
  29. static inline void *add_script_callback(
  30. struct script_callback **first,
  31. obs_script_t *script,
  32. size_t extra_size)
  33. {
  34. struct script_callback *cb = bzalloc(sizeof(*cb) + extra_size);
  35. cb->script = script;
  36. struct script_callback *next = *first;
  37. cb->next = next;
  38. cb->p_prev_next = first;
  39. if (next) next->p_prev_next = &cb->next;
  40. *first = cb;
  41. return cb;
  42. }
  43. static inline void remove_script_callback(struct script_callback *cb)
  44. {
  45. cb->removed = true;
  46. struct script_callback *next = cb->next;
  47. if (next) next->p_prev_next = cb->p_prev_next;
  48. *cb->p_prev_next = cb->next;
  49. pthread_mutex_lock(&detach_mutex);
  50. next = detached_callbacks;
  51. cb->next = next;
  52. if (next) next->p_prev_next = &cb->next;
  53. cb->p_prev_next = &detached_callbacks;
  54. detached_callbacks = cb;
  55. pthread_mutex_unlock(&detach_mutex);
  56. if (cb->on_remove)
  57. cb->on_remove(cb);
  58. }
  59. static inline void just_free_script_callback(struct script_callback *cb)
  60. {
  61. calldata_free(&cb->extra);
  62. bfree(cb);
  63. }
  64. static inline void free_script_callback(struct script_callback *cb)
  65. {
  66. pthread_mutex_lock(&detach_mutex);
  67. struct script_callback *next = cb->next;
  68. if (next) next->p_prev_next = cb->p_prev_next;
  69. *cb->p_prev_next = cb->next;
  70. pthread_mutex_unlock(&detach_mutex);
  71. just_free_script_callback(cb);
  72. }