File: obs-scripting-callback.h

package info (click to toggle)
obs-studio 22.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 23,052 kB
  • sloc: ansic: 134,708; cpp: 49,169; objc: 1,036; makefile: 829; sh: 410; python: 360
file content (91 lines) | stat: -rw-r--r-- 2,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/******************************************************************************
    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;
};

static inline void *add_script_callback(
		struct script_callback **first,
		obs_script_t *script,
		size_t extra_size)
{
	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;
	if (next) next->p_prev_next = &cb->next;
	*first = cb;

	return cb;
}

static inline void remove_script_callback(struct script_callback *cb)
{
	cb->removed = true;

	struct script_callback *next = cb->next;
	if (next) next->p_prev_next = cb->p_prev_next;
	*cb->p_prev_next = cb->next;

	pthread_mutex_lock(&detach_mutex);
	next = detached_callbacks;
	cb->next = next;
	if (next) next->p_prev_next = &cb->next;
	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;
	if (next) next->p_prev_next = cb->p_prev_next;
	*cb->p_prev_next = cb->next;
	pthread_mutex_unlock(&detach_mutex);

	just_free_script_callback(cb);
}