File: obs-scripting-lua-frontend.c

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 (315 lines) | stat: -rw-r--r-- 7,434 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/******************************************************************************
    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/>.
******************************************************************************/

#include <obs-module.h>
#include <obs-frontend-api.h>

#include "obs-scripting-lua.h"

#define ls_get_libobs_obj(type, lua_index, obs_obj) \
	ls_get_libobs_obj_(script, #type " *", lua_index, obs_obj, \
			NULL, __FUNCTION__, __LINE__)
#define ls_push_libobs_obj(type, obs_obj, ownership) \
	ls_push_libobs_obj_(script, #type " *", obs_obj, ownership, \
			NULL, __FUNCTION__, __LINE__)
#define call_func(func, args, rets) \
	call_func_(script, cb->reg_idx, args, rets, #func, "frontend API")

/* ----------------------------------- */

static int get_scene_names(lua_State *script)
{
	char **names = obs_frontend_get_scene_names();
	char **name = names;
	int i = 0;

	lua_newtable(script);

	while (name && *name) {
		lua_pushstring(script, *name);
		lua_rawseti(script, -2, ++i);
		name++;
	}

	bfree(names);
	return 1;
}

static int get_scenes(lua_State *script)
{
	struct obs_frontend_source_list list = {0};
	obs_frontend_get_scenes(&list);

	lua_newtable(script);

	for (size_t i = 0; i < list.sources.num; i++) {
		obs_source_t *source = list.sources.array[i];
		ls_push_libobs_obj(obs_source_t, source, false);
		lua_rawseti(script, -2, (int)(i + 1));
	}

	da_free(list.sources);
	return 1;
}

static int get_current_scene(lua_State *script)
{
	obs_source_t *source = obs_frontend_get_current_scene();
	ls_push_libobs_obj(obs_source_t, source, false);
	return 1;
}

static int set_current_scene(lua_State *script)
{
	obs_source_t *source = NULL;
	ls_get_libobs_obj(obs_source_t, 1, &source);
	obs_frontend_set_current_scene(source);
	return 0;
}

static int get_transitions(lua_State *script)
{
	struct obs_frontend_source_list list = {0};
	obs_frontend_get_transitions(&list);

	lua_newtable(script);

	for (size_t i = 0; i < list.sources.num; i++) {
		obs_source_t *source = list.sources.array[i];
		ls_push_libobs_obj(obs_source_t, source, false);
		lua_rawseti(script, -2, (int)(i + 1));
	}

	da_free(list.sources);
	return 1;
}

static int get_current_transition(lua_State *script)
{
	obs_source_t *source = obs_frontend_get_current_transition();
	ls_push_libobs_obj(obs_source_t, source, false);
	return 1;
}

static int set_current_transition(lua_State *script)
{
	obs_source_t *source = NULL;
	ls_get_libobs_obj(obs_source_t, 1, &source);
	obs_frontend_set_current_transition(source);
	return 0;
}

static int get_scene_collections(lua_State *script)
{
	char **names = obs_frontend_get_scene_collections();
	char **name = names;
	int i = 0;

	lua_newtable(script);

	while (name && *name) {
		lua_pushstring(script, *name);
		lua_rawseti(script, -2, ++i);
		name++;
	}

	bfree(names);
	return 1;
}

static int get_current_scene_collection(lua_State *script)
{
	char *name = obs_frontend_get_current_scene_collection();
	lua_pushstring(script, name);
	bfree(name);
	return 1;
}

static int set_current_scene_collection(lua_State *script)
{
	if (lua_isstring(script, 1)) {
		const char *name = lua_tostring(script, 1);
		obs_frontend_set_current_scene_collection(name);
	}
	return 0;
}

static int get_profiles(lua_State *script)
{
	char **names = obs_frontend_get_profiles();
	char **name = names;
	int i = 0;

	lua_newtable(script);

	while (name && *name) {
		lua_pushstring(script, *name);
		lua_rawseti(script, -2, ++i);
		name++;
	}

	bfree(names);
	return 1;
}

static int get_current_profile(lua_State *script)
{
	char *name = obs_frontend_get_current_profile();
	lua_pushstring(script, name);
	bfree(name);
	return 1;
}

static int set_current_profile(lua_State *script)
{
	if (lua_isstring(script, 1)) {
		const char *name = lua_tostring(script, 1);
		obs_frontend_set_current_profile(name);
	}
	return 0;
}

/* ----------------------------------- */

static void frontend_event_callback(enum obs_frontend_event event, void *priv)
{
	struct lua_obs_callback *cb = priv;
	lua_State *script = cb->script;

	if (cb->base.removed) {
		obs_frontend_remove_event_callback(frontend_event_callback, cb);		
		return;
	}

	lock_callback();

	lua_pushinteger(script, (int)event);
	call_func(frontend_event_callback, 1, 0);

	unlock_callback();
}

static int remove_event_callback(lua_State *script)
{
	if (!verify_args1(script, is_function))
		return 0;

	struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
	if (cb) {
		remove_lua_obs_callback(cb);
	}
	return 0;
}

static void add_event_callback_defer(void *cb)
{
	obs_frontend_add_event_callback(frontend_event_callback, cb);
}

static int add_event_callback(lua_State *script)
{
	if (!verify_args1(script, is_function))
		return 0;

	struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
	defer_call_post(add_event_callback_defer, cb);
	return 0;
}

/* ----------------------------------- */

static void frontend_save_callback(obs_data_t *save_data, bool saving,
		void *priv)
{
	struct lua_obs_callback *cb = priv;
	lua_State *script = cb->script;

	if (cb->base.removed) {
		obs_frontend_remove_save_callback(frontend_save_callback, cb);
		return;
	}

	lock_callback();

	ls_push_libobs_obj(obs_data_t, save_data, false);
	lua_pushboolean(script, saving);
	call_func(frontend_save_callback, 2, 0);

	unlock_callback();
}

static int remove_save_callback(lua_State *script)
{
	if (!verify_args1(script, is_function))
		return 0;

	struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
	if (cb) {
		remove_lua_obs_callback(cb);
	}
	return 0;
}

static void add_save_callback_defer(void *cb)
{
	obs_frontend_add_save_callback(frontend_save_callback, cb);
}

static int add_save_callback(lua_State *script)
{
	if (!verify_args1(script, is_function))
		return 0;

	struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
	defer_call_post(add_save_callback_defer, cb);
	return 0;
}

/* ----------------------------------- */

void add_lua_frontend_funcs(lua_State *script)
{
	lua_getglobal(script, "obslua");

#define add_func(name) \
	do { \
		lua_pushstring(script, "obs_frontend_" #name); \
		lua_pushcfunction(script, name); \
		lua_rawset(script, -3); \
	} while (false)

	add_func(get_scene_names);
	add_func(get_scenes);
	add_func(get_current_scene);
	add_func(set_current_scene);
	add_func(get_transitions);
	add_func(get_current_transition);
	add_func(set_current_transition);
	add_func(get_scene_collections);
	add_func(get_current_scene_collection);
	add_func(set_current_scene_collection);
	add_func(get_profiles);
	add_func(get_current_profile);
	add_func(set_current_profile);
	add_func(remove_event_callback);
	add_func(add_event_callback);
	add_func(remove_save_callback);
	add_func(add_save_callback);
#undef add_func

	lua_pop(script, 1);
}