obs-scene.c 24.2 KB
Newer Older
J
jp9000 已提交
1
/******************************************************************************
S
Socapex 已提交
2 3
    Copyright (C) 2013-2015 by Hugh Bailey <obs.jim@gmail.com>
                               Philippe Groarke <philippe.groarke@gmail.com>
J
jp9000 已提交
4 5 6

    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
7
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
8 9 10 11 12 13 14 15 16 17 18
    (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/>.
******************************************************************************/

J
jp9000 已提交
19
#include "util/threading.h"
J
jp9000 已提交
20 21 22
#include "graphics/math-defs.h"
#include "obs-scene.h"

23 24 25
static const char *obs_scene_signals[] = {
	"void item_add(ptr scene, ptr item)",
	"void item_remove(ptr scene, ptr item)",
S
Socapex 已提交
26
	"void reorder(ptr scene)",
27
	"void item_visible(ptr scene, ptr item, bool visible)",
28 29 30
	"void item_select(ptr scene, ptr item)",
	"void item_deselect(ptr scene, ptr item)",
	"void item_transform(ptr scene, ptr item)",
31 32 33
	NULL
};

J
jp9000 已提交
34
static inline void signal_item_remove(struct obs_scene_item *item)
J
jp9000 已提交
35
{
J
jp9000 已提交
36
	struct calldata params = {0};
37 38
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item", item);
J
jp9000 已提交
39

40 41
	signal_handler_signal(item->parent->source->context.signals,
			"item_remove", &params);
J
jp9000 已提交
42
	calldata_free(&params);
J
jp9000 已提交
43 44
}

45
static const char *scene_getname(void)
46
{
47 48
	/* TODO: locale */
	return "Scene";
49 50
}

51
static void *scene_create(obs_data_t *settings, struct obs_source *source)
J
jp9000 已提交
52
{
P
Palana 已提交
53
	pthread_mutexattr_t attr;
J
jp9000 已提交
54
	struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
55 56 57
	scene->source     = source;
	scene->first_item = NULL;

58
	signal_handler_add_array(obs_source_get_signal_handler(source),
59 60
			obs_scene_signals);

P
Palana 已提交
61 62 63 64 65
	if (pthread_mutexattr_init(&attr) != 0)
		goto fail;
	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
		goto fail;
	if (pthread_mutex_init(&scene->mutex, &attr) != 0) {
66
		blog(LOG_ERROR, "scene_create: Couldn't initialize mutex");
P
Palana 已提交
67
		goto fail;
68
	}
J
jp9000 已提交
69

J
jp9000 已提交
70
	UNUSED_PARAMETER(settings);
J
jp9000 已提交
71
	return scene;
P
Palana 已提交
72 73 74 75 76

fail:
	pthread_mutexattr_destroy(&attr);
	bfree(scene);
	return NULL;
J
jp9000 已提交
77 78
}

79
static void remove_all_items(struct obs_scene *scene)
J
jp9000 已提交
80
{
J
jp9000 已提交
81
	struct obs_scene_item *item;
J
jp9000 已提交
82 83

	pthread_mutex_lock(&scene->mutex);
J
jp9000 已提交
84

J
jp9000 已提交
85 86
	item = scene->first_item;

87 88 89 90
	while (item) {
		struct obs_scene_item *del_item = item;
		item = item->next;

J
jp9000 已提交
91
		obs_sceneitem_remove(del_item);
92
	}
J
jp9000 已提交
93

J
jp9000 已提交
94
	pthread_mutex_unlock(&scene->mutex);
95 96 97 98 99
}

static void scene_destroy(void *data)
{
	struct obs_scene *scene = data;
J
jp9000 已提交
100

101
	remove_all_items(scene);
102
	pthread_mutex_destroy(&scene->mutex);
J
jp9000 已提交
103 104 105
	bfree(scene);
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static void scene_enum_sources(void *data,
		obs_source_enum_proc_t enum_callback,
		void *param)
{
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
		struct obs_scene_item *next = item->next;

		obs_sceneitem_addref(item);
		enum_callback(scene->source, item->source, param);
		obs_sceneitem_release(item);

		item = next;
	}

	pthread_mutex_unlock(&scene->mutex);
}

129 130 131 132 133 134 135 136 137
static inline void detach_sceneitem(struct obs_scene_item *item)
{
	if (item->prev)
		item->prev->next = item->next;
	else
		item->parent->first_item = item->next;

	if (item->next)
		item->next->prev = item->prev;
J
jp9000 已提交
138 139

	item->parent = NULL;
140 141
}

J
jp9000 已提交
142 143
static inline void attach_sceneitem(struct obs_scene *parent,
		struct obs_scene_item *item, struct obs_scene_item *prev)
144
{
J
jp9000 已提交
145 146
	item->prev   = prev;
	item->parent = parent;
147 148 149 150 151 152 153

	if (prev) {
		item->next = prev->next;
		if (prev->next)
			prev->next->prev = item;
		prev->next = item;
	} else {
154 155 156 157
		item->next = parent->first_item;
		if (parent->first_item)
			parent->first_item->prev = item;
		parent->first_item = item;
158 159 160
	}
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
{
	if (align & OBS_ALIGN_RIGHT)
		v->x += (float)cx;
	else if ((align & OBS_ALIGN_LEFT) == 0)
		v->x += (float)(cx / 2);

	if (align & OBS_ALIGN_BOTTOM)
		v->y += (float)cy;
	else if ((align & OBS_ALIGN_TOP) == 0)
		v->y += (float)(cy / 2);
}

static void calculate_bounds_data(struct obs_scene_item *item,
		struct vec2 *origin, struct vec2 *scale,
		uint32_t *cx, uint32_t *cy)
{
J
jp9000 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190
	float    width         = (float)(*cx) * fabsf(scale->x);
	float    height        = (float)(*cy) * fabsf(scale->y);
	float    item_aspect   = width / height;
	float    bounds_aspect = item->bounds.x / item->bounds.y;
	uint32_t bounds_type   = item->bounds_type;
	float    width_diff, height_diff;

	if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
		if (width > item->bounds.x || height > item->bounds.y)
			bounds_type = OBS_BOUNDS_SCALE_INNER;

	if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
	    bounds_type == OBS_BOUNDS_SCALE_OUTER) {
191 192 193 194 195 196 197 198 199 200 201 202
		bool  use_width = (bounds_aspect < item_aspect);
		float mul;

		if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
			use_width = !use_width;

		mul = use_width ?
			item->bounds.x / width :
			item->bounds.y / height;

		vec2_mulf(scale, scale, mul);

J
jp9000 已提交
203
	} else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
204 205
		vec2_mulf(scale, scale, item->bounds.x / width);

J
jp9000 已提交
206
	} else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
207 208
		vec2_mulf(scale, scale, item->bounds.y / height);

J
jp9000 已提交
209
	} else if (bounds_type == OBS_BOUNDS_STRETCH) {
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		scale->x = item->bounds.x / (float)(*cx);
		scale->y = item->bounds.y / (float)(*cy);
	}

	width       = (float)(*cx) * scale->x;
	height      = (float)(*cy) * scale->y;
	width_diff  = item->bounds.x - width;
	height_diff = item->bounds.y - height;
	*cx         = (uint32_t)item->bounds.x;
	*cy         = (uint32_t)item->bounds.y;

	add_alignment(origin, item->bounds_align,
			(int)-width_diff, (int)-height_diff);
}

static void update_item_transform(struct obs_scene_item *item)
{
227 228
	uint32_t        width         = obs_source_get_width(item->source);
	uint32_t        height        = obs_source_get_height(item->source);
229 230
	uint32_t        cx            = width;
	uint32_t        cy            = height;
J
jp9000 已提交
231 232
	struct vec2     base_origin;
	struct vec2     origin;
233 234 235
	struct vec2     scale         = item->scale;
	struct calldata params        = {0};

J
jp9000 已提交
236 237 238
	vec2_zero(&base_origin);
	vec2_zero(&origin);

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
	/* ----------------------- */

	if (item->bounds_type != OBS_BOUNDS_NONE) {
		calculate_bounds_data(item, &origin, &scale, &cx, &cy);
	} else {
		cx = (uint32_t)((float)cx * scale.x);
		cy = (uint32_t)((float)cy * scale.y);
	}

	add_alignment(&origin, item->align, (int)cx, (int)cy);

	matrix4_identity(&item->draw_transform);
	matrix4_scale3f(&item->draw_transform, &item->draw_transform,
			scale.x, scale.y, 1.0f);
	matrix4_translate3f(&item->draw_transform, &item->draw_transform,
			-origin.x, -origin.y, 0.0f);
	matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
			0.0f, 0.0f, 1.0f, RAD(item->rot));
	matrix4_translate3f(&item->draw_transform, &item->draw_transform,
			item->pos.x, item->pos.y, 0.0f);

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

	if (item->bounds_type != OBS_BOUNDS_NONE) {
		vec2_copy(&scale, &item->bounds);
	} else {
		scale.x = (float)width  * item->scale.x;
		scale.y = (float)height * item->scale.y;
	}

	add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);

	matrix4_identity(&item->box_transform);
	matrix4_scale3f(&item->box_transform, &item->box_transform,
			scale.x, scale.y, 1.0f);
	matrix4_translate3f(&item->box_transform, &item->box_transform,
			-base_origin.x, -base_origin.y, 0.0f);
	matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
			0.0f, 0.0f, 1.0f, RAD(item->rot));
	matrix4_translate3f(&item->box_transform, &item->box_transform,
			item->pos.x, item->pos.y, 0.0f);

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

	item->last_width  = width;
	item->last_height = height;

286 287
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item", item);
288 289 290 291 292 293 294
	signal_handler_signal(item->parent->source->context.signals,
			"item_transform", &params);
	calldata_free(&params);
}

static inline bool source_size_changed(struct obs_scene_item *item)
{
295 296
	uint32_t width  = obs_source_get_width(item->source);
	uint32_t height = obs_source_get_height(item->source);
297 298 299 300

	return item->last_width != width || item->last_height != height;
}

301
static void scene_video_render(void *data, gs_effect_t *effect)
J
jp9000 已提交
302
{
303 304 305
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

306 307
	pthread_mutex_lock(&scene->mutex);

308
	item = scene->first_item;
J
jp9000 已提交
309

J
jp9000 已提交
310
	gs_blend_state_push();
311
	gs_reset_blend_state();
J
jp9000 已提交
312

313
	while (item) {
314
		if (obs_source_removed(item->source)) {
315 316 317
			struct obs_scene_item *del_item = item;
			item = item->next;

J
jp9000 已提交
318
			obs_sceneitem_remove(del_item);
319 320 321
			continue;
		}

322 323
		if (source_size_changed(item))
			update_item_transform(item);
J
jp9000 已提交
324

325 326 327 328 329 330
		if (item->visible) {
			gs_matrix_push();
			gs_matrix_mul(&item->draw_transform);
			obs_source_video_render(item->source);
			gs_matrix_pop();
		}
331 332

		item = item->next;
J
jp9000 已提交
333
	}
334

J
jp9000 已提交
335 336
	gs_blend_state_pop();

337
	pthread_mutex_unlock(&scene->mutex);
J
jp9000 已提交
338 339 340 341

	UNUSED_PARAMETER(effect);
}

342
static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
343
{
J
jp9000 已提交
344
	const char            *name = obs_data_get_string(item_data, "name");
345
	obs_source_t          *source = obs_get_source_by_name(name);
346 347 348 349 350 351 352 353 354
	struct obs_scene_item *item;

	if (!source) {
		blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
				name);
		return;
	}

	item = obs_scene_add(scene, source);
P
Palana 已提交
355 356 357 358 359 360 361 362
	if (!item) {
		blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
		                  "to scene '%s'!",
		                  name, obs_source_get_name(scene->source));
		
		obs_source_release(source);
		return;
	}
363

364 365 366
	obs_data_set_default_int(item_data, "align",
			OBS_ALIGN_TOP | OBS_ALIGN_LEFT);

J
jp9000 已提交
367 368 369
	item->rot     = (float)obs_data_get_double(item_data, "rot");
	item->align   = (uint32_t)obs_data_get_int(item_data, "align");
	item->visible = obs_data_get_bool(item_data, "visible");
370 371
	obs_data_get_vec2(item_data, "pos",    &item->pos);
	obs_data_get_vec2(item_data, "scale",  &item->scale);
372 373

	item->bounds_type =
J
jp9000 已提交
374 375
		(enum obs_bounds_type)obs_data_get_int(item_data,
				"bounds_type");
376
	item->bounds_align =
J
jp9000 已提交
377
		(uint32_t)obs_data_get_int(item_data, "bounds_align");
378 379
	obs_data_get_vec2(item_data, "bounds", &item->bounds);

380
	obs_source_release(source);
381 382

	update_item_transform(item);
383 384
}

385
static void scene_load(void *scene, obs_data_t *settings)
386
{
387
	obs_data_array_t *items = obs_data_get_array(settings, "items");
388 389 390 391 392 393 394 395 396
	size_t           count, i;

	remove_all_items(scene);

	if (!items) return;

	count = obs_data_array_count(items);

	for (i = 0; i < count; i++) {
397
		obs_data_t *item_data = obs_data_array_item(items, i);
398 399 400 401 402 403 404
		scene_load_item(scene, item_data);
		obs_data_release(item_data);
	}

	obs_data_array_release(items);
}

405 406
static void scene_save_item(obs_data_array_t *array,
		struct obs_scene_item *item)
407
{
408
	obs_data_t *item_data = obs_data_create();
409
	const char *name     = obs_source_get_name(item->source);
410

J
jp9000 已提交
411 412 413
	obs_data_set_string(item_data, "name",         name);
	obs_data_set_bool  (item_data, "visible",      item->visible);
	obs_data_set_double(item_data, "rot",          item->rot);
414 415
	obs_data_set_vec2 (item_data, "pos",          &item->pos);
	obs_data_set_vec2 (item_data, "scale",        &item->scale);
J
jp9000 已提交
416 417 418
	obs_data_set_int   (item_data, "align",        (int)item->align);
	obs_data_set_int   (item_data, "bounds_type",  (int)item->bounds_type);
	obs_data_set_int   (item_data, "bounds_align", (int)item->bounds_align);
419
	obs_data_set_vec2 (item_data, "bounds",       &item->bounds);
420 421 422 423 424

	obs_data_array_push_back(array, item_data);
	obs_data_release(item_data);
}

425
static void scene_save(void *data, obs_data_t *settings)
426 427
{
	struct obs_scene      *scene = data;
428
	obs_data_array_t      *array  = obs_data_array_create();
429 430 431 432 433 434
	struct obs_scene_item *item;

	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
435
		scene_save_item(array, item);
436 437 438 439 440
		item = item->next;
	}

	pthread_mutex_unlock(&scene->mutex);

J
jp9000 已提交
441
	obs_data_set_array(settings, "items", array);
442 443 444
	obs_data_array_release(array);
}

J
jp9000 已提交
445 446 447 448
static uint32_t scene_getwidth(void *data)
{
	UNUSED_PARAMETER(data);
	return obs->video.base_width;
J
jp9000 已提交
449 450
}

J
jp9000 已提交
451
static uint32_t scene_getheight(void *data)
J
jp9000 已提交
452
{
J
jp9000 已提交
453 454
	UNUSED_PARAMETER(data);
	return obs->video.base_height;
J
jp9000 已提交
455 456
}

457
const struct obs_source_info scene_info =
J
jp9000 已提交
458
{
459 460 461 462 463 464 465 466 467 468 469 470
	.id            = "scene",
	.type          = OBS_SOURCE_TYPE_INPUT,
	.output_flags  = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
	.get_name      = scene_getname,
	.create        = scene_create,
	.destroy       = scene_destroy,
	.video_render  = scene_video_render,
	.get_width     = scene_getwidth,
	.get_height    = scene_getheight,
	.load          = scene_load,
	.save          = scene_save,
	.enum_sources  = scene_enum_sources
J
jp9000 已提交
471 472
};

473
obs_scene_t *obs_scene_create(const char *name)
J
jp9000 已提交
474
{
475
	struct obs_source *source =
476 477
		obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL,
				NULL);
478
	return source->context.data;
J
jp9000 已提交
479 480
}

481
void obs_scene_addref(obs_scene_t *scene)
482
{
P
Palana 已提交
483 484
	if (scene)
		obs_source_addref(scene->source);
485 486
}

487
void obs_scene_release(obs_scene_t *scene)
J
jp9000 已提交
488
{
P
Palana 已提交
489 490
	if (scene)
		obs_source_release(scene->source);
J
jp9000 已提交
491 492
}

493
obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
J
jp9000 已提交
494
{
J
jp9000 已提交
495
	return scene ? scene->source : NULL;
J
jp9000 已提交
496 497
}

498
obs_scene_t *obs_scene_from_source(const obs_source_t *source)
499
{
500
	if (!source || source->info.id != scene_info.id)
501 502
		return NULL;

503
	return source->context.data;
504 505
}

506
obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
507 508 509
{
	struct obs_scene_item *item;

J
jp9000 已提交
510 511 512
	if (!scene)
		return NULL;

513 514 515 516
	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
517
		if (strcmp(item->source->context.name, name) == 0)
518 519 520 521 522 523 524 525 526 527
			break;

		item = item->next;
	}

	pthread_mutex_unlock(&scene->mutex);

	return item;
}

528 529
void obs_scene_enum_items(obs_scene_t *scene,
		bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
530 531 532 533
		void *param)
{
	struct obs_scene_item *item;

J
jp9000 已提交
534 535 536
	if (!scene || !callback)
		return;

537 538 539 540
	pthread_mutex_lock(&scene->mutex);

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
541 542 543 544
		struct obs_scene_item *next = item->next;

		obs_sceneitem_addref(item);

545 546
		if (!callback(scene, item, param)) {
			obs_sceneitem_release(item);
547
			break;
548
		}
549

J
jp9000 已提交
550 551 552
		obs_sceneitem_release(item);

		item = next;
553 554 555 556 557
	}

	pthread_mutex_unlock(&scene->mutex);
}

P
Palana 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
{
	long owners = si->ref;
	while (owners > 0) {
		if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
			return si;

		owners = si->ref;
	}
	return NULL;
}

static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
		obs_hotkey_t *hotkey, bool pressed)
{
	UNUSED_PARAMETER(id);
	UNUSED_PARAMETER(hotkey);

	obs_sceneitem_t *si = sceneitem_get_ref(data);
	if (pressed && si && !si->visible) {
		obs_sceneitem_set_visible(si, true);
		obs_sceneitem_release(si);
		return true;
	}

	obs_sceneitem_release(si);
	return false;
}

static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
		obs_hotkey_t *hotkey, bool pressed)
{
	UNUSED_PARAMETER(id);
	UNUSED_PARAMETER(hotkey);

	obs_sceneitem_t *si = sceneitem_get_ref(data);
	if (pressed && si && si->visible) {
		obs_sceneitem_set_visible(si, false);
		obs_sceneitem_release(si);
		return true;
	}

	obs_sceneitem_release(si);
	return false;
}

static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
		const char *name)
{
	struct dstr show = {0};
	struct dstr hide = {0};
	struct dstr show_desc = {0};
	struct dstr hide_desc = {0};

	dstr_copy(&show, "libobs.show_scene_item.%1");
	dstr_replace(&show, "%1", name);
	dstr_copy(&hide, "libobs.hide_scene_item.%1");
	dstr_replace(&hide, "%1", name);

	dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
	dstr_replace(&show_desc, "%1", name);
	dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
	dstr_replace(&hide_desc, "%1", name);

	item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
			show.array, show_desc.array,
			hide.array, hide_desc.array,
			hotkey_show_sceneitem, hotkey_hide_sceneitem,
			item, item);

	dstr_free(&show);
	dstr_free(&hide);
	dstr_free(&show_desc);
	dstr_free(&hide_desc);
}

634
obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
J
jp9000 已提交
635
{
636
	struct obs_scene_item *last;
637
	struct obs_scene_item *item;
J
jp9000 已提交
638
	struct calldata params = {0};
639

640 641 642 643
	if (!scene)
		return NULL;

	if (!source) {
J
jp9000 已提交
644
		blog(LOG_ERROR, "Tried to add a NULL source to a scene");
645 646 647
		return NULL;
	}

J
jp9000 已提交
648 649 650 651 652 653
	if (!obs_source_add_child(scene->source, source)) {
		blog(LOG_WARNING, "Failed to add source to scene due to "
		                  "infinite source recursion");
		return NULL;
	}

654
	item = bzalloc(sizeof(struct obs_scene_item));
J
jp9000 已提交
655 656 657
	item->source  = source;
	item->visible = true;
	item->parent  = scene;
J
jp9000 已提交
658
	item->ref     = 1;
659
	item->align   = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
J
jp9000 已提交
660
	vec2_set(&item->scale, 1.0f, 1.0f);
661 662
	matrix4_identity(&item->draw_transform);
	matrix4_identity(&item->box_transform);
J
jp9000 已提交
663

664
	obs_source_addref(source);
665

666 667 668 669 670 671 672 673 674 675 676 677
	pthread_mutex_lock(&scene->mutex);

	last = scene->first_item;
	if (!last) {
		scene->first_item = item;
	} else {
		while (last->next)
			last = last->next;

		last->next = item;
		item->prev = last;
	}
J
jp9000 已提交
678

679 680
	pthread_mutex_unlock(&scene->mutex);

P
Palana 已提交
681 682
	init_hotkeys(scene, item, obs_source_get_name(source));

683 684
	calldata_set_ptr(&params, "scene", scene);
	calldata_set_ptr(&params, "item", item);
685 686
	signal_handler_signal(scene->source->context.signals, "item_add",
			&params);
J
jp9000 已提交
687 688
	calldata_free(&params);

J
jp9000 已提交
689 690 691
	return item;
}

692
static void obs_sceneitem_destroy(obs_sceneitem_t *item)
J
jp9000 已提交
693
{
694
	if (item) {
P
Palana 已提交
695
		obs_hotkey_pair_unregister(item->toggle_visibility);
696
		if (item->source)
J
jp9000 已提交
697
			obs_source_release(item->source);
J
jp9000 已提交
698
		bfree(item);
J
jp9000 已提交
699 700 701
	}
}

702
void obs_sceneitem_addref(obs_sceneitem_t *item)
J
jp9000 已提交
703
{
P
Palana 已提交
704
	if (item)
J
jp9000 已提交
705
		os_atomic_inc_long(&item->ref);
J
jp9000 已提交
706
}
707

708
void obs_sceneitem_release(obs_sceneitem_t *item)
J
jp9000 已提交
709
{
P
Palana 已提交
710 711
	if (!item)
		return;
P
Palana 已提交
712

J
jp9000 已提交
713
	if (os_atomic_dec_long(&item->ref) == 0)
P
Palana 已提交
714
		obs_sceneitem_destroy(item);
715 716
}

717
void obs_sceneitem_remove(obs_sceneitem_t *item)
J
jp9000 已提交
718
{
719
	obs_scene_t *scene;
J
jp9000 已提交
720

721
	if (!item)
J
jp9000 已提交
722 723
		return;

724
	scene = item->parent;
J
jp9000 已提交
725

726 727
	if (scene)
		pthread_mutex_lock(&scene->mutex);
J
jp9000 已提交
728

J
jp9000 已提交
729
	if (item->removed) {
730 731 732 733 734
		if (scene)
			pthread_mutex_unlock(&scene->mutex);
		return;
	}

J
jp9000 已提交
735 736
	item->removed = true;

737 738
	assert(scene != NULL);
	assert(scene->source != NULL);
739 740
	obs_source_remove_child(scene->source, item->source);

741
	signal_item_remove(item);
J
jp9000 已提交
742
	detach_sceneitem(item);
743

744
	pthread_mutex_unlock(&scene->mutex);
J
jp9000 已提交
745 746 747 748

	obs_sceneitem_release(item);
}

749
obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
J
jp9000 已提交
750
{
J
jp9000 已提交
751
	return item ? item->parent : NULL;
J
jp9000 已提交
752 753
}

754
obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
755
{
J
jp9000 已提交
756
	return item ? item->source : NULL;
J
jp9000 已提交
757 758
}

759
void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
760 761 762 763 764 765 766 767 768
{
	struct calldata params = {0};
	const char *command = select ? "item_select" : "item_deselect";

	if (!item || item->selected == select)
		return;

	item->selected = select;

769 770
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item",  item);
771 772 773 774 775 776
	signal_handler_signal(item->parent->source->context.signals,
			command, &params);

	calldata_free(&params);
}

777
bool obs_sceneitem_selected(const obs_sceneitem_t *item)
778 779 780 781
{
	return item ? item->selected : false;
}

782
void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
J
jp9000 已提交
783
{
784
	if (item) {
J
jp9000 已提交
785
		vec2_copy(&item->pos, pos);
786 787
		update_item_transform(item);
	}
J
jp9000 已提交
788 789
}

790
void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
J
jp9000 已提交
791
{
792
	if (item) {
J
jp9000 已提交
793
		item->rot = rot;
794 795
		update_item_transform(item);
	}
J
jp9000 已提交
796 797
}

798
void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
J
jp9000 已提交
799
{
800 801 802 803
	if (item) {
		vec2_copy(&item->scale, scale);
		update_item_transform(item);
	}
J
jp9000 已提交
804 805
}

806
void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
J
jp9000 已提交
807
{
808 809 810 811
	if (item) {
		item->align = alignment;
		update_item_transform(item);
	}
J
jp9000 已提交
812 813
}

S
Socapex 已提交
814
static inline void signal_reorder(struct obs_scene_item *item)
J
jp9000 已提交
815
{
816
	const char *command = NULL;
J
jp9000 已提交
817 818
	struct calldata params = {0};

S
Socapex 已提交
819
	command = "reorder";
J
jp9000 已提交
820

821
	calldata_set_ptr(&params, "scene", item->parent);
J
jp9000 已提交
822 823 824 825 826 827 828

	signal_handler_signal(item->parent->source->context.signals,
			command, &params);

	calldata_free(&params);
}

829
void obs_sceneitem_set_order(obs_sceneitem_t *item,
J
jp9000 已提交
830
		enum obs_order_movement movement)
J
jp9000 已提交
831
{
J
jp9000 已提交
832 833
	if (!item) return;

J
jp9000 已提交
834
	struct obs_scene_item *next, *prev;
J
jp9000 已提交
835 836
	struct obs_scene *scene = item->parent;

J
jp9000 已提交
837
	obs_scene_addref(scene);
838
	pthread_mutex_lock(&scene->mutex);
839

J
jp9000 已提交
840 841 842
	next = item->next;
	prev = item->prev;

843 844
	detach_sceneitem(item);

J
jp9000 已提交
845
	if (movement == OBS_ORDER_MOVE_DOWN) {
J
jp9000 已提交
846
		attach_sceneitem(scene, item, prev ? prev->prev : NULL);
J
jp9000 已提交
847

J
jp9000 已提交
848
	} else if (movement == OBS_ORDER_MOVE_UP) {
J
jp9000 已提交
849
		attach_sceneitem(scene, item, next ? next : prev);
J
jp9000 已提交
850

J
jp9000 已提交
851
	} else if (movement == OBS_ORDER_MOVE_TOP) {
J
jp9000 已提交
852
		struct obs_scene_item *last = next;
853
		if (!last) {
J
jp9000 已提交
854
			last = prev;
855 856 857 858
		} else {
			while (last->next)
				last = last->next;
		}
J
jp9000 已提交
859

J
jp9000 已提交
860
		attach_sceneitem(scene, item, last);
861

J
jp9000 已提交
862
	} else if (movement == OBS_ORDER_MOVE_BOTTOM) {
J
jp9000 已提交
863
		attach_sceneitem(scene, item, NULL);
J
jp9000 已提交
864
	}
865

S
Socapex 已提交
866
	signal_reorder(item);
J
jp9000 已提交
867

868
	pthread_mutex_unlock(&scene->mutex);
869
	obs_scene_release(scene);
J
jp9000 已提交
870 871
}

872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
		int position)
{
	if (!item) return;

	struct obs_scene *scene = item->parent;
	struct obs_scene_item *next;

	obs_scene_addref(scene);
	pthread_mutex_lock(&scene->mutex);

	detach_sceneitem(item);
	next = scene->first_item;

	if (position == 0) {
		attach_sceneitem(scene, item, NULL);
	} else {
		for (int i = position; i > 1; --i) {
			if (next->next == NULL)
				break;
			next = next->next;
		}

		attach_sceneitem(scene, item, next);
	}

	signal_reorder(item);

	pthread_mutex_unlock(&scene->mutex);
	obs_scene_release(scene);
}

904
void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
905 906 907 908 909 910 911 912
		enum obs_bounds_type type)
{
	if (item) {
		item->bounds_type = type;
		update_item_transform(item);
	}
}

913
void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
914 915 916 917 918 919 920 921
		uint32_t alignment)
{
	if (item) {
		item->bounds_align = alignment;
		update_item_transform(item);
	}
}

922
void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
923 924 925 926 927 928 929
{
	if (item) {
		item->bounds = *bounds;
		update_item_transform(item);
	}
}

930
void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
J
jp9000 已提交
931
{
J
jp9000 已提交
932 933
	if (item)
		vec2_copy(pos, &item->pos);
J
jp9000 已提交
934 935
}

936
float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
J
jp9000 已提交
937
{
J
jp9000 已提交
938
	return item ? item->rot : 0.0f;
J
jp9000 已提交
939 940
}

941
void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
J
jp9000 已提交
942
{
J
jp9000 已提交
943
	if (item)
944
		vec2_copy(scale, &item->scale);
J
jp9000 已提交
945 946
}

947
uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
948 949 950 951
{
	return item ? item->align : 0;
}

952
enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
953 954 955 956
{
	return item ? item->bounds_type : OBS_BOUNDS_NONE;
}

957
uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
958 959 960 961
{
	return item ? item->bounds_align : 0;
}

962
void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
J
jp9000 已提交
963
{
J
jp9000 已提交
964
	if (item)
965 966 967
		*bounds = item->bounds;
}

968
void obs_sceneitem_get_info(const obs_sceneitem_t *item,
969
		struct obs_transform_info *info)
970 971 972 973 974 975 976 977 978 979 980 981
{
	if (item && info) {
		info->pos              = item->pos;
		info->rot              = item->rot;
		info->scale            = item->scale;
		info->alignment        = item->align;
		info->bounds_type      = item->bounds_type;
		info->bounds_alignment = item->bounds_align;
		info->bounds           = item->bounds;
	}
}

982
void obs_sceneitem_set_info(obs_sceneitem_t *item,
983
		const struct obs_transform_info *info)
984 985 986 987 988 989 990 991 992 993 994 995 996
{
	if (item && info) {
		item->pos          = info->pos;
		item->rot          = info->rot;
		item->scale        = info->scale;
		item->align        = info->alignment;
		item->bounds_type  = info->bounds_type;
		item->bounds_align = info->bounds_alignment;
		item->bounds       = info->bounds;
		update_item_transform(item);
	}
}

997
void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
998 999 1000 1001 1002 1003
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->draw_transform);
}

1004
void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
1005 1006 1007 1008
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->box_transform);
J
jp9000 已提交
1009
}
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

bool obs_sceneitem_visible(const obs_sceneitem_t *item)
{
	return item ? item->visible : false;
}

void obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
{
	struct calldata cd = {0};

	if (!item)
		return;

	item->visible = visible;

	if (!item->parent)
		return;

	calldata_set_ptr(&cd, "scene", item->parent);
	calldata_set_ptr(&cd, "item", item);
	calldata_set_bool(&cd, "visible", visible);

	signal_handler_signal(item->parent->source->context.signals,
			"item_visible", &cd);

	calldata_free(&cd);
}