yolobs-studio/deps/obs-scripting/obs-scripting-callback.h

94 lines
2.5 KiB
C
Raw Normal View History

2018-02-19 19:54:37 +00:00
/******************************************************************************
Copyright (C) 2017 by Hugh Bailey <jim@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#pragma once
#include <callback/calldata.h>
#include <util/threading.h>
#include <util/bmem.h>
#include "obs-scripting-internal.h"
extern pthread_mutex_t detach_mutex;
extern struct script_callback *detached_callbacks;
struct script_callback {
struct script_callback *next;
struct script_callback **p_prev_next;
void (*on_remove)(void *p_cb);
obs_script_t *script;
calldata_t extra;
bool removed;
};
2019-09-22 21:19:10 +00:00
static inline void *add_script_callback(struct script_callback **first,
obs_script_t *script, size_t extra_size)
2018-02-19 19:54:37 +00:00
{
struct script_callback *cb = bzalloc(sizeof(*cb) + extra_size);
cb->script = script;
struct script_callback *next = *first;
cb->next = next;
cb->p_prev_next = first;
2019-09-22 21:19:10 +00:00
if (next)
next->p_prev_next = &cb->next;
2018-02-19 19:54:37 +00:00
*first = cb;
return cb;
}
static inline void remove_script_callback(struct script_callback *cb)
{
cb->removed = true;
struct script_callback *next = cb->next;
2019-09-22 21:19:10 +00:00
if (next)
next->p_prev_next = cb->p_prev_next;
2018-02-19 19:54:37 +00:00
*cb->p_prev_next = cb->next;
pthread_mutex_lock(&detach_mutex);
next = detached_callbacks;
cb->next = next;
2019-09-22 21:19:10 +00:00
if (next)
next->p_prev_next = &cb->next;
2018-02-19 19:54:37 +00:00
cb->p_prev_next = &detached_callbacks;
detached_callbacks = cb;
pthread_mutex_unlock(&detach_mutex);
if (cb->on_remove)
cb->on_remove(cb);
}
static inline void just_free_script_callback(struct script_callback *cb)
{
calldata_free(&cb->extra);
bfree(cb);
}
static inline void free_script_callback(struct script_callback *cb)
{
pthread_mutex_lock(&detach_mutex);
struct script_callback *next = cb->next;
2019-09-22 21:19:10 +00:00
if (next)
next->p_prev_next = cb->p_prev_next;
2018-02-19 19:54:37 +00:00
*cb->p_prev_next = cb->next;
pthread_mutex_unlock(&detach_mutex);
just_free_script_callback(cb);
}