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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
/******************************************************************************
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.h>
#include <util/dstr.h>
#include <util/platform.h>
#include <util/threading.h>
#include <util/circlebuf.h>
#include "obs-scripting-internal.h"
#include "obs-scripting-callback.h"
#include "obs-scripting-config.h"
#if COMPILE_LUA
extern obs_script_t *obs_lua_script_create(const char *path,
obs_data_t *settings);
extern bool obs_lua_script_load(obs_script_t *s);
extern void obs_lua_script_unload(obs_script_t *s);
extern void obs_lua_script_destroy(obs_script_t *s);
extern void obs_lua_load(void);
extern void obs_lua_unload(void);
extern obs_properties_t *obs_lua_script_get_properties(obs_script_t *script);
extern void obs_lua_script_update(obs_script_t *script, obs_data_t *settings);
extern void obs_lua_script_save(obs_script_t *script);
#endif
#if COMPILE_PYTHON
extern obs_script_t *obs_python_script_create(const char *path,
obs_data_t *settings);
extern bool obs_python_script_load(obs_script_t *s);
extern void obs_python_script_unload(obs_script_t *s);
extern void obs_python_script_destroy(obs_script_t *s);
extern void obs_python_load(void);
extern void obs_python_unload(void);
extern obs_properties_t *obs_python_script_get_properties(obs_script_t *script);
extern void obs_python_script_update(obs_script_t *script, obs_data_t *settings);
extern void obs_python_script_save(obs_script_t *script);
#endif
pthread_mutex_t detach_mutex;
struct script_callback *detached_callbacks;
static struct dstr file_filter = {0};
static bool scripting_loaded = false;
static const char *supported_formats[] = {
#if COMPILE_LUA
"lua",
#endif
#if COMPILE_PYTHON
"py",
#endif
NULL
};
/* -------------------------------------------- */
static pthread_mutex_t defer_call_mutex;
static struct circlebuf defer_call_queue;
static bool defer_call_exit = false;
static os_sem_t *defer_call_semaphore;
static pthread_t defer_call_thread;
struct defer_call {
defer_call_cb call;
void *cb;
};
static void *defer_thread(void *unused)
{
UNUSED_PARAMETER(unused);
while (os_sem_wait(defer_call_semaphore) == 0) {
struct defer_call info;
pthread_mutex_lock(&defer_call_mutex);
if (defer_call_exit) {
pthread_mutex_unlock(&defer_call_mutex);
return NULL;
}
circlebuf_pop_front(&defer_call_queue, &info, sizeof(info));
pthread_mutex_unlock(&defer_call_mutex);
info.call(info.cb);
}
return NULL;
}
void defer_call_post(defer_call_cb call, void *cb)
{
struct defer_call info;
info.call = call;
info.cb = cb;
pthread_mutex_lock(&defer_call_mutex);
if (!defer_call_exit)
circlebuf_push_back(&defer_call_queue, &info, sizeof(info));
pthread_mutex_unlock(&defer_call_mutex);
os_sem_post(defer_call_semaphore);
}
/* -------------------------------------------- */
bool obs_scripting_load(void)
{
circlebuf_init(&defer_call_queue);
if (pthread_mutex_init(&detach_mutex, NULL) != 0) {
return false;
}
if (pthread_mutex_init(&defer_call_mutex, NULL) != 0) {
pthread_mutex_destroy(&detach_mutex);
return false;
}
if (os_sem_init(&defer_call_semaphore, 0) != 0) {
pthread_mutex_destroy(&defer_call_mutex);
pthread_mutex_destroy(&detach_mutex);
return false;
}
if (pthread_create(&defer_call_thread, NULL, defer_thread, NULL) != 0) {
os_sem_destroy(defer_call_semaphore);
pthread_mutex_destroy(&defer_call_mutex);
pthread_mutex_destroy(&detach_mutex);
return false;
}
#if COMPILE_LUA
obs_lua_load();
#endif
#if COMPILE_PYTHON
obs_python_load();
#ifndef _WIN32 /* don't risk python startup load issues on windows */
obs_scripting_load_python(NULL);
#endif
#endif
scripting_loaded = true;
return true;
}
void obs_scripting_unload(void)
{
if (!scripting_loaded)
return;
/* ---------------------- */
#if COMPILE_LUA
obs_lua_unload();
#endif
#if COMPILE_PYTHON
obs_python_unload();
#endif
dstr_free(&file_filter);
/* ---------------------- */
int total_detached = 0;
pthread_mutex_lock(&detach_mutex);
struct script_callback *cur = detached_callbacks;
while (cur) {
struct script_callback *next = cur->next;
just_free_script_callback(cur);
cur = next;
++total_detached;
}
pthread_mutex_unlock(&detach_mutex);
pthread_mutex_destroy(&detach_mutex);
blog(LOG_INFO, "[Scripting] Total detached callbacks: %d",
total_detached);
/* ---------------------- */
pthread_mutex_lock(&defer_call_mutex);
/* TODO */
defer_call_exit = true;
circlebuf_free(&defer_call_queue);
pthread_mutex_unlock(&defer_call_mutex);
os_sem_post(defer_call_semaphore);
pthread_join(defer_call_thread, NULL);
pthread_mutex_destroy(&defer_call_mutex);
os_sem_destroy(defer_call_semaphore);
scripting_loaded = false;
}
const char **obs_scripting_supported_formats(void)
{
return supported_formats;
}
static inline bool pointer_valid(const void *x, const char *name,
const char *func)
{
if (!x) {
blog(LOG_WARNING, "obs-scripting: [%s] %s is null",
func, name);
return false;
}
return true;
}
#define ptr_valid(x) pointer_valid(x, #x, __FUNCTION__)
obs_script_t *obs_script_create(const char *path, obs_data_t *settings)
{
obs_script_t *script = NULL;
const char *ext;
if (!scripting_loaded)
return NULL;
if (!ptr_valid(path))
return NULL;
ext = strrchr(path, '.');
if (!ext)
return NULL;
#if COMPILE_LUA
if (strcmp(ext, ".lua") == 0) {
script = obs_lua_script_create(path, settings);
} else
#endif
#if COMPILE_PYTHON
if (strcmp(ext, ".py") == 0) {
script = obs_python_script_create(path, settings);
} else
#endif
{
blog(LOG_WARNING, "Unsupported/unknown script type: %s", path);
}
return script;
}
const char *obs_script_get_description(const obs_script_t *script)
{
return ptr_valid(script) ? script->desc.array : NULL;
}
const char *obs_script_get_path(const obs_script_t *script)
{
const char *path = ptr_valid(script) ? script->path.array : "";
return path ? path : "";
}
const char *obs_script_get_file(const obs_script_t *script)
{
const char *file = ptr_valid(script) ? script->file.array : "";
return file ? file : "";
}
enum obs_script_lang obs_script_get_lang(const obs_script_t *script)
{
return ptr_valid(script) ? script->type : OBS_SCRIPT_LANG_UNKNOWN;
}
obs_data_t *obs_script_get_settings(obs_script_t *script)
{
obs_data_t *settings;
if (!ptr_valid(script))
return NULL;
settings = script->settings;
obs_data_addref(settings);
return settings;
}
obs_properties_t *obs_script_get_properties(obs_script_t *script)
{
obs_properties_t *props = NULL;
if (!ptr_valid(script))
return NULL;
#if COMPILE_LUA
if (script->type == OBS_SCRIPT_LANG_LUA) {
props = obs_lua_script_get_properties(script);
goto out;
}
#endif
#if COMPILE_PYTHON
if (script->type == OBS_SCRIPT_LANG_PYTHON) {
props = obs_python_script_get_properties(script);
goto out;
}
#endif
out:
if (!props)
props = obs_properties_create();
return props;
}
obs_data_t *obs_script_save(obs_script_t *script)
{
obs_data_t *settings;
if (!ptr_valid(script))
return NULL;
#if COMPILE_LUA
if (script->type == OBS_SCRIPT_LANG_LUA) {
obs_lua_script_save(script);
goto out;
}
#endif
#if COMPILE_PYTHON
if (script->type == OBS_SCRIPT_LANG_PYTHON) {
obs_python_script_save(script);
goto out;
}
#endif
out:
settings = script->settings;
obs_data_addref(settings);
return settings;
}
static void clear_queue_signal(void *p_event)
{
os_event_t *event = p_event;
os_event_signal(event);
}
static void clear_call_queue(void)
{
os_event_t *event;
if (os_event_init(&event, OS_EVENT_TYPE_AUTO) != 0)
return;
defer_call_post(clear_queue_signal, event);
os_event_wait(event);
os_event_destroy(event);
}
void obs_script_update(obs_script_t *script, obs_data_t *settings)
{
if (!ptr_valid(script))
return;
#if COMPILE_LUA
if (script->type == OBS_SCRIPT_LANG_LUA) {
obs_lua_script_update(script, settings);
}
#endif
#if COMPILE_PYTHON
if (script->type == OBS_SCRIPT_LANG_PYTHON) {
obs_python_script_update(script, settings);
}
#endif
}
bool obs_script_reload(obs_script_t *script)
{
if (!scripting_loaded)
return false;
if (!ptr_valid(script))
return false;
#if COMPILE_LUA
if (script->type == OBS_SCRIPT_LANG_LUA) {
obs_lua_script_unload(script);
clear_call_queue();
obs_lua_script_load(script);
goto out;
}
#endif
#if COMPILE_PYTHON
if (script->type == OBS_SCRIPT_LANG_PYTHON) {
obs_python_script_unload(script);
clear_call_queue();
obs_python_script_load(script);
goto out;
}
#endif
out:
return script->loaded;
}
bool obs_script_loaded(const obs_script_t *script)
{
return ptr_valid(script) ? script->loaded : false;
}
void obs_script_destroy(obs_script_t *script)
{
if (!script)
return;
#if COMPILE_LUA
if (script->type == OBS_SCRIPT_LANG_LUA) {
obs_lua_script_unload(script);
obs_lua_script_destroy(script);
return;
}
#endif
#if COMPILE_PYTHON
if (script->type == OBS_SCRIPT_LANG_PYTHON) {
obs_python_script_unload(script);
obs_python_script_destroy(script);
return;
}
#endif
}
#if !COMPILE_PYTHON
bool obs_scripting_load_python(const char *python_path)
{
UNUSED_PARAMETER(python_path);
return false;
}
bool obs_scripting_python_loaded(void)
{
return false;
}
bool obs_scripting_python_runtime_linked(void)
{
return (bool)true;
}
#endif
|