obs-scripting-callback.h 2.5 KB

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