obs-scene.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Philippe Groarke <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "util/threading.h"
  16. #include "graphics/math-defs.h"
  17. #include "obs-scene.h"
  18. static const char *obs_scene_signals[] = {
  19. "void item_add(ptr scene, ptr item)",
  20. "void item_remove(ptr scene, ptr item)",
  21. "void reorder(ptr scene)",
  22. "void item_visible(ptr scene, ptr item, bool visible)",
  23. "void item_select(ptr scene, ptr item)",
  24. "void item_deselect(ptr scene, ptr item)",
  25. "void item_transform(ptr scene, ptr item)",
  26. NULL
  27. };
  28. static inline void signal_item_remove(struct obs_scene_item *item)
  29. {
  30. struct calldata params = {0};
  31. calldata_set_ptr(&params, "scene", item->parent);
  32. calldata_set_ptr(&params, "item", item);
  33. signal_handler_signal(item->parent->source->context.signals,
  34. "item_remove", &params);
  35. calldata_free(&params);
  36. }
  37. static const char *scene_getname(void *unused)
  38. {
  39. UNUSED_PARAMETER(unused);
  40. return "Scene";
  41. }
  42. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  43. {
  44. pthread_mutexattr_t attr;
  45. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  46. scene->source = source;
  47. scene->first_item = NULL;
  48. signal_handler_add_array(obs_source_get_signal_handler(source),
  49. obs_scene_signals);
  50. if (pthread_mutexattr_init(&attr) != 0)
  51. goto fail;
  52. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  53. goto fail;
  54. if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
  55. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  56. "mutex");
  57. goto fail;
  58. }
  59. if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
  60. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  61. "mutex");
  62. goto fail;
  63. }
  64. UNUSED_PARAMETER(settings);
  65. return scene;
  66. fail:
  67. pthread_mutexattr_destroy(&attr);
  68. bfree(scene);
  69. return NULL;
  70. }
  71. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  72. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  73. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  74. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  75. static inline void full_lock(struct obs_scene *scene)
  76. {
  77. video_lock(scene);
  78. audio_lock(scene);
  79. }
  80. static inline void full_unlock(struct obs_scene *scene)
  81. {
  82. audio_unlock(scene);
  83. video_unlock(scene);
  84. }
  85. static void remove_all_items(struct obs_scene *scene)
  86. {
  87. struct obs_scene_item *item;
  88. full_lock(scene);
  89. item = scene->first_item;
  90. while (item) {
  91. struct obs_scene_item *del_item = item;
  92. item = item->next;
  93. obs_sceneitem_remove(del_item);
  94. }
  95. full_unlock(scene);
  96. }
  97. static void scene_destroy(void *data)
  98. {
  99. struct obs_scene *scene = data;
  100. remove_all_items(scene);
  101. pthread_mutex_destroy(&scene->video_mutex);
  102. pthread_mutex_destroy(&scene->audio_mutex);
  103. bfree(scene);
  104. }
  105. static void scene_enum_sources(void *data,
  106. obs_source_enum_proc_t enum_callback,
  107. void *param)
  108. {
  109. struct obs_scene *scene = data;
  110. struct obs_scene_item *item;
  111. struct obs_scene_item *next;
  112. full_lock(scene);
  113. item = scene->first_item;
  114. while (item) {
  115. next = item->next;
  116. obs_sceneitem_addref(item);
  117. if (item->visible)
  118. enum_callback(scene->source, item->source, param);
  119. obs_sceneitem_release(item);
  120. item = next;
  121. }
  122. full_unlock(scene);
  123. }
  124. static inline void detach_sceneitem(struct obs_scene_item *item)
  125. {
  126. if (item->prev)
  127. item->prev->next = item->next;
  128. else
  129. item->parent->first_item = item->next;
  130. if (item->next)
  131. item->next->prev = item->prev;
  132. item->parent = NULL;
  133. }
  134. static inline void attach_sceneitem(struct obs_scene *parent,
  135. struct obs_scene_item *item, struct obs_scene_item *prev)
  136. {
  137. item->prev = prev;
  138. item->parent = parent;
  139. if (prev) {
  140. item->next = prev->next;
  141. if (prev->next)
  142. prev->next->prev = item;
  143. prev->next = item;
  144. } else {
  145. item->next = parent->first_item;
  146. if (parent->first_item)
  147. parent->first_item->prev = item;
  148. parent->first_item = item;
  149. }
  150. }
  151. static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  152. {
  153. if (align & OBS_ALIGN_RIGHT)
  154. v->x += (float)cx;
  155. else if ((align & OBS_ALIGN_LEFT) == 0)
  156. v->x += (float)(cx / 2);
  157. if (align & OBS_ALIGN_BOTTOM)
  158. v->y += (float)cy;
  159. else if ((align & OBS_ALIGN_TOP) == 0)
  160. v->y += (float)(cy / 2);
  161. }
  162. static void calculate_bounds_data(struct obs_scene_item *item,
  163. struct vec2 *origin, struct vec2 *scale,
  164. uint32_t *cx, uint32_t *cy)
  165. {
  166. float width = (float)(*cx) * fabsf(scale->x);
  167. float height = (float)(*cy) * fabsf(scale->y);
  168. float item_aspect = width / height;
  169. float bounds_aspect = item->bounds.x / item->bounds.y;
  170. uint32_t bounds_type = item->bounds_type;
  171. float width_diff, height_diff;
  172. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  173. if (width > item->bounds.x || height > item->bounds.y)
  174. bounds_type = OBS_BOUNDS_SCALE_INNER;
  175. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  176. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  177. bool use_width = (bounds_aspect < item_aspect);
  178. float mul;
  179. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  180. use_width = !use_width;
  181. mul = use_width ?
  182. item->bounds.x / width :
  183. item->bounds.y / height;
  184. vec2_mulf(scale, scale, mul);
  185. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  186. vec2_mulf(scale, scale, item->bounds.x / width);
  187. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  188. vec2_mulf(scale, scale, item->bounds.y / height);
  189. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  190. scale->x = item->bounds.x / (float)(*cx);
  191. scale->y = item->bounds.y / (float)(*cy);
  192. }
  193. width = (float)(*cx) * scale->x;
  194. height = (float)(*cy) * scale->y;
  195. width_diff = item->bounds.x - width;
  196. height_diff = item->bounds.y - height;
  197. *cx = (uint32_t)item->bounds.x;
  198. *cy = (uint32_t)item->bounds.y;
  199. add_alignment(origin, item->bounds_align,
  200. (int)-width_diff, (int)-height_diff);
  201. }
  202. static void update_item_transform(struct obs_scene_item *item)
  203. {
  204. uint32_t width = obs_source_get_width(item->source);
  205. uint32_t height = obs_source_get_height(item->source);
  206. uint32_t cx = width;
  207. uint32_t cy = height;
  208. struct vec2 base_origin;
  209. struct vec2 origin;
  210. struct vec2 scale = item->scale;
  211. struct calldata params = {0};
  212. vec2_zero(&base_origin);
  213. vec2_zero(&origin);
  214. /* ----------------------- */
  215. if (item->bounds_type != OBS_BOUNDS_NONE) {
  216. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  217. } else {
  218. cx = (uint32_t)((float)cx * scale.x);
  219. cy = (uint32_t)((float)cy * scale.y);
  220. }
  221. add_alignment(&origin, item->align, (int)cx, (int)cy);
  222. matrix4_identity(&item->draw_transform);
  223. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  224. scale.x, scale.y, 1.0f);
  225. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  226. -origin.x, -origin.y, 0.0f);
  227. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  228. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  229. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  230. item->pos.x, item->pos.y, 0.0f);
  231. /* ----------------------- */
  232. if (item->bounds_type != OBS_BOUNDS_NONE) {
  233. vec2_copy(&scale, &item->bounds);
  234. } else {
  235. scale.x = (float)width * item->scale.x;
  236. scale.y = (float)height * item->scale.y;
  237. }
  238. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  239. matrix4_identity(&item->box_transform);
  240. matrix4_scale3f(&item->box_transform, &item->box_transform,
  241. scale.x, scale.y, 1.0f);
  242. matrix4_translate3f(&item->box_transform, &item->box_transform,
  243. -base_origin.x, -base_origin.y, 0.0f);
  244. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  245. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  246. matrix4_translate3f(&item->box_transform, &item->box_transform,
  247. item->pos.x, item->pos.y, 0.0f);
  248. /* ----------------------- */
  249. item->last_width = width;
  250. item->last_height = height;
  251. calldata_set_ptr(&params, "scene", item->parent);
  252. calldata_set_ptr(&params, "item", item);
  253. signal_handler_signal(item->parent->source->context.signals,
  254. "item_transform", &params);
  255. calldata_free(&params);
  256. }
  257. static inline bool source_size_changed(struct obs_scene_item *item)
  258. {
  259. uint32_t width = obs_source_get_width(item->source);
  260. uint32_t height = obs_source_get_height(item->source);
  261. return item->last_width != width || item->last_height != height;
  262. }
  263. static void scene_video_render(void *data, gs_effect_t *effect)
  264. {
  265. struct obs_scene *scene = data;
  266. struct obs_scene_item *item;
  267. video_lock(scene);
  268. item = scene->first_item;
  269. gs_blend_state_push();
  270. gs_reset_blend_state();
  271. while (item) {
  272. if (obs_source_removed(item->source)) {
  273. struct obs_scene_item *del_item = item;
  274. item = item->next;
  275. obs_sceneitem_remove(del_item);
  276. continue;
  277. }
  278. if (source_size_changed(item))
  279. update_item_transform(item);
  280. if (item->visible) {
  281. gs_matrix_push();
  282. gs_matrix_mul(&item->draw_transform);
  283. obs_source_video_render(item->source);
  284. gs_matrix_pop();
  285. }
  286. item = item->next;
  287. }
  288. gs_blend_state_pop();
  289. video_unlock(scene);
  290. UNUSED_PARAMETER(effect);
  291. }
  292. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  293. {
  294. const char *name = obs_data_get_string(item_data, "name");
  295. obs_source_t *source = obs_get_source_by_name(name);
  296. struct obs_scene_item *item;
  297. if (!source) {
  298. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  299. name);
  300. return;
  301. }
  302. item = obs_scene_add(scene, source);
  303. if (!item) {
  304. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  305. "to scene '%s'!",
  306. name, obs_source_get_name(scene->source));
  307. obs_source_release(source);
  308. return;
  309. }
  310. obs_data_set_default_int(item_data, "align",
  311. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  312. item->rot = (float)obs_data_get_double(item_data, "rot");
  313. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  314. item->visible = obs_data_get_bool(item_data, "visible");
  315. obs_data_get_vec2(item_data, "pos", &item->pos);
  316. obs_data_get_vec2(item_data, "scale", &item->scale);
  317. if (!item->visible)
  318. obs_source_remove_active_child(scene->source, source);
  319. item->bounds_type =
  320. (enum obs_bounds_type)obs_data_get_int(item_data,
  321. "bounds_type");
  322. item->bounds_align =
  323. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  324. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  325. obs_source_release(source);
  326. update_item_transform(item);
  327. }
  328. static void scene_load(void *scene, obs_data_t *settings)
  329. {
  330. obs_data_array_t *items = obs_data_get_array(settings, "items");
  331. size_t count, i;
  332. remove_all_items(scene);
  333. if (!items) return;
  334. count = obs_data_array_count(items);
  335. for (i = 0; i < count; i++) {
  336. obs_data_t *item_data = obs_data_array_item(items, i);
  337. scene_load_item(scene, item_data);
  338. obs_data_release(item_data);
  339. }
  340. obs_data_array_release(items);
  341. }
  342. static void scene_save_item(obs_data_array_t *array,
  343. struct obs_scene_item *item)
  344. {
  345. obs_data_t *item_data = obs_data_create();
  346. const char *name = obs_source_get_name(item->source);
  347. obs_data_set_string(item_data, "name", name);
  348. obs_data_set_bool (item_data, "visible", item->visible);
  349. obs_data_set_double(item_data, "rot", item->rot);
  350. obs_data_set_vec2 (item_data, "pos", &item->pos);
  351. obs_data_set_vec2 (item_data, "scale", &item->scale);
  352. obs_data_set_int (item_data, "align", (int)item->align);
  353. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  354. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  355. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  356. obs_data_array_push_back(array, item_data);
  357. obs_data_release(item_data);
  358. }
  359. static void scene_save(void *data, obs_data_t *settings)
  360. {
  361. struct obs_scene *scene = data;
  362. obs_data_array_t *array = obs_data_array_create();
  363. struct obs_scene_item *item;
  364. full_lock(scene);
  365. item = scene->first_item;
  366. while (item) {
  367. scene_save_item(array, item);
  368. item = item->next;
  369. }
  370. full_unlock(scene);
  371. obs_data_set_array(settings, "items", array);
  372. obs_data_array_release(array);
  373. }
  374. static uint32_t scene_getwidth(void *data)
  375. {
  376. UNUSED_PARAMETER(data);
  377. return obs->video.base_width;
  378. }
  379. static uint32_t scene_getheight(void *data)
  380. {
  381. UNUSED_PARAMETER(data);
  382. return obs->video.base_height;
  383. }
  384. const struct obs_source_info scene_info =
  385. {
  386. .id = "scene",
  387. .type = OBS_SOURCE_TYPE_INPUT,
  388. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  389. .get_name = scene_getname,
  390. .create = scene_create,
  391. .destroy = scene_destroy,
  392. .video_render = scene_video_render,
  393. .get_width = scene_getwidth,
  394. .get_height = scene_getheight,
  395. .load = scene_load,
  396. .save = scene_save,
  397. .enum_active_sources = scene_enum_sources
  398. };
  399. obs_scene_t *obs_scene_create(const char *name)
  400. {
  401. struct obs_source *source =
  402. obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL,
  403. NULL);
  404. return source->context.data;
  405. }
  406. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name)
  407. {
  408. struct obs_scene *new_scene = obs_scene_create(name);
  409. struct obs_scene_item *item = scene->first_item;
  410. full_lock(scene);
  411. while (item) {
  412. struct obs_source *source = item->source;
  413. if (source) {
  414. struct obs_scene_item *new_item =
  415. obs_scene_add(new_scene, source);
  416. new_item->visible = item->visible;
  417. if (!new_item->visible)
  418. obs_source_remove_active_child(
  419. new_scene->source, source);
  420. new_item->selected = item->selected;
  421. new_item->pos = item->pos;
  422. new_item->scale = item->scale;
  423. new_item->align = item->align;
  424. new_item->last_width = item->last_width;
  425. new_item->last_height = item->last_height;
  426. new_item->box_transform = item->box_transform;
  427. new_item->draw_transform = item->draw_transform;
  428. new_item->bounds_type = item->bounds_type;
  429. new_item->bounds_align = item->bounds_align;
  430. new_item->bounds = item->bounds;
  431. }
  432. item = item->next;
  433. }
  434. full_unlock(scene);
  435. return new_scene;
  436. }
  437. void obs_scene_addref(obs_scene_t *scene)
  438. {
  439. if (scene)
  440. obs_source_addref(scene->source);
  441. }
  442. void obs_scene_release(obs_scene_t *scene)
  443. {
  444. if (scene)
  445. obs_source_release(scene->source);
  446. }
  447. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  448. {
  449. return scene ? scene->source : NULL;
  450. }
  451. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  452. {
  453. if (!source || source->info.id != scene_info.id)
  454. return NULL;
  455. return source->context.data;
  456. }
  457. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  458. {
  459. struct obs_scene_item *item;
  460. if (!scene)
  461. return NULL;
  462. full_lock(scene);
  463. item = scene->first_item;
  464. while (item) {
  465. if (strcmp(item->source->context.name, name) == 0)
  466. break;
  467. item = item->next;
  468. }
  469. full_unlock(scene);
  470. return item;
  471. }
  472. void obs_scene_enum_items(obs_scene_t *scene,
  473. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  474. void *param)
  475. {
  476. struct obs_scene_item *item;
  477. if (!scene || !callback)
  478. return;
  479. full_lock(scene);
  480. item = scene->first_item;
  481. while (item) {
  482. struct obs_scene_item *next = item->next;
  483. obs_sceneitem_addref(item);
  484. if (!callback(scene, item, param)) {
  485. obs_sceneitem_release(item);
  486. break;
  487. }
  488. obs_sceneitem_release(item);
  489. item = next;
  490. }
  491. full_unlock(scene);
  492. }
  493. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  494. {
  495. long owners = si->ref;
  496. while (owners > 0) {
  497. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  498. return si;
  499. owners = si->ref;
  500. }
  501. return NULL;
  502. }
  503. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  504. obs_hotkey_t *hotkey, bool pressed)
  505. {
  506. UNUSED_PARAMETER(id);
  507. UNUSED_PARAMETER(hotkey);
  508. obs_sceneitem_t *si = sceneitem_get_ref(data);
  509. if (pressed && si && !si->visible) {
  510. obs_sceneitem_set_visible(si, true);
  511. obs_sceneitem_release(si);
  512. return true;
  513. }
  514. obs_sceneitem_release(si);
  515. return false;
  516. }
  517. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  518. obs_hotkey_t *hotkey, bool pressed)
  519. {
  520. UNUSED_PARAMETER(id);
  521. UNUSED_PARAMETER(hotkey);
  522. obs_sceneitem_t *si = sceneitem_get_ref(data);
  523. if (pressed && si && si->visible) {
  524. obs_sceneitem_set_visible(si, false);
  525. obs_sceneitem_release(si);
  526. return true;
  527. }
  528. obs_sceneitem_release(si);
  529. return false;
  530. }
  531. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  532. const char *name)
  533. {
  534. struct dstr show = {0};
  535. struct dstr hide = {0};
  536. struct dstr show_desc = {0};
  537. struct dstr hide_desc = {0};
  538. dstr_copy(&show, "libobs.show_scene_item.%1");
  539. dstr_replace(&show, "%1", name);
  540. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  541. dstr_replace(&hide, "%1", name);
  542. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  543. dstr_replace(&show_desc, "%1", name);
  544. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  545. dstr_replace(&hide_desc, "%1", name);
  546. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  547. show.array, show_desc.array,
  548. hide.array, hide_desc.array,
  549. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  550. item, item);
  551. dstr_free(&show);
  552. dstr_free(&hide);
  553. dstr_free(&show_desc);
  554. dstr_free(&hide_desc);
  555. }
  556. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  557. {
  558. struct obs_scene_item *last;
  559. struct obs_scene_item *item;
  560. struct calldata params = {0};
  561. if (!scene)
  562. return NULL;
  563. if (!source) {
  564. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  565. return NULL;
  566. }
  567. if (!obs_source_add_active_child(scene->source, source)) {
  568. blog(LOG_WARNING, "Failed to add source to scene due to "
  569. "infinite source recursion");
  570. return NULL;
  571. }
  572. item = bzalloc(sizeof(struct obs_scene_item));
  573. item->source = source;
  574. item->visible = true;
  575. item->parent = scene;
  576. item->ref = 1;
  577. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  578. vec2_set(&item->scale, 1.0f, 1.0f);
  579. matrix4_identity(&item->draw_transform);
  580. matrix4_identity(&item->box_transform);
  581. obs_source_addref(source);
  582. full_lock(scene);
  583. last = scene->first_item;
  584. if (!last) {
  585. scene->first_item = item;
  586. } else {
  587. while (last->next)
  588. last = last->next;
  589. last->next = item;
  590. item->prev = last;
  591. }
  592. full_unlock(scene);
  593. init_hotkeys(scene, item, obs_source_get_name(source));
  594. calldata_set_ptr(&params, "scene", scene);
  595. calldata_set_ptr(&params, "item", item);
  596. signal_handler_signal(scene->source->context.signals, "item_add",
  597. &params);
  598. calldata_free(&params);
  599. return item;
  600. }
  601. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  602. {
  603. if (item) {
  604. obs_hotkey_pair_unregister(item->toggle_visibility);
  605. if (item->source)
  606. obs_source_release(item->source);
  607. bfree(item);
  608. }
  609. }
  610. void obs_sceneitem_addref(obs_sceneitem_t *item)
  611. {
  612. if (item)
  613. os_atomic_inc_long(&item->ref);
  614. }
  615. void obs_sceneitem_release(obs_sceneitem_t *item)
  616. {
  617. if (!item)
  618. return;
  619. if (os_atomic_dec_long(&item->ref) == 0)
  620. obs_sceneitem_destroy(item);
  621. }
  622. void obs_sceneitem_remove(obs_sceneitem_t *item)
  623. {
  624. obs_scene_t *scene;
  625. if (!item)
  626. return;
  627. scene = item->parent;
  628. if (scene)
  629. full_lock(scene);
  630. if (item->removed) {
  631. if (scene)
  632. full_unlock(scene);
  633. return;
  634. }
  635. item->removed = true;
  636. assert(scene != NULL);
  637. assert(scene->source != NULL);
  638. if (item->visible)
  639. obs_source_remove_active_child(scene->source, item->source);
  640. signal_item_remove(item);
  641. detach_sceneitem(item);
  642. full_unlock(scene);
  643. obs_sceneitem_release(item);
  644. }
  645. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  646. {
  647. return item ? item->parent : NULL;
  648. }
  649. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  650. {
  651. return item ? item->source : NULL;
  652. }
  653. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  654. {
  655. struct calldata params = {0};
  656. const char *command = select ? "item_select" : "item_deselect";
  657. if (!item || item->selected == select)
  658. return;
  659. item->selected = select;
  660. calldata_set_ptr(&params, "scene", item->parent);
  661. calldata_set_ptr(&params, "item", item);
  662. signal_handler_signal(item->parent->source->context.signals,
  663. command, &params);
  664. calldata_free(&params);
  665. }
  666. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  667. {
  668. return item ? item->selected : false;
  669. }
  670. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  671. {
  672. if (item) {
  673. vec2_copy(&item->pos, pos);
  674. update_item_transform(item);
  675. }
  676. }
  677. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  678. {
  679. if (item) {
  680. item->rot = rot;
  681. update_item_transform(item);
  682. }
  683. }
  684. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  685. {
  686. if (item) {
  687. vec2_copy(&item->scale, scale);
  688. update_item_transform(item);
  689. }
  690. }
  691. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  692. {
  693. if (item) {
  694. item->align = alignment;
  695. update_item_transform(item);
  696. }
  697. }
  698. static inline void signal_reorder(struct obs_scene_item *item)
  699. {
  700. const char *command = NULL;
  701. struct calldata params = {0};
  702. command = "reorder";
  703. calldata_set_ptr(&params, "scene", item->parent);
  704. signal_handler_signal(item->parent->source->context.signals,
  705. command, &params);
  706. calldata_free(&params);
  707. }
  708. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  709. enum obs_order_movement movement)
  710. {
  711. if (!item) return;
  712. struct obs_scene_item *next, *prev;
  713. struct obs_scene *scene = item->parent;
  714. obs_scene_addref(scene);
  715. full_lock(scene);
  716. next = item->next;
  717. prev = item->prev;
  718. detach_sceneitem(item);
  719. if (movement == OBS_ORDER_MOVE_DOWN) {
  720. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  721. } else if (movement == OBS_ORDER_MOVE_UP) {
  722. attach_sceneitem(scene, item, next ? next : prev);
  723. } else if (movement == OBS_ORDER_MOVE_TOP) {
  724. struct obs_scene_item *last = next;
  725. if (!last) {
  726. last = prev;
  727. } else {
  728. while (last->next)
  729. last = last->next;
  730. }
  731. attach_sceneitem(scene, item, last);
  732. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  733. attach_sceneitem(scene, item, NULL);
  734. }
  735. signal_reorder(item);
  736. full_unlock(scene);
  737. obs_scene_release(scene);
  738. }
  739. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  740. int position)
  741. {
  742. if (!item) return;
  743. struct obs_scene *scene = item->parent;
  744. struct obs_scene_item *next;
  745. obs_scene_addref(scene);
  746. full_lock(scene);
  747. detach_sceneitem(item);
  748. next = scene->first_item;
  749. if (position == 0) {
  750. attach_sceneitem(scene, item, NULL);
  751. } else {
  752. for (int i = position; i > 1; --i) {
  753. if (next->next == NULL)
  754. break;
  755. next = next->next;
  756. }
  757. attach_sceneitem(scene, item, next);
  758. }
  759. signal_reorder(item);
  760. full_unlock(scene);
  761. obs_scene_release(scene);
  762. }
  763. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  764. enum obs_bounds_type type)
  765. {
  766. if (item) {
  767. item->bounds_type = type;
  768. update_item_transform(item);
  769. }
  770. }
  771. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  772. uint32_t alignment)
  773. {
  774. if (item) {
  775. item->bounds_align = alignment;
  776. update_item_transform(item);
  777. }
  778. }
  779. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  780. {
  781. if (item) {
  782. item->bounds = *bounds;
  783. update_item_transform(item);
  784. }
  785. }
  786. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  787. {
  788. if (item)
  789. vec2_copy(pos, &item->pos);
  790. }
  791. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  792. {
  793. return item ? item->rot : 0.0f;
  794. }
  795. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  796. {
  797. if (item)
  798. vec2_copy(scale, &item->scale);
  799. }
  800. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  801. {
  802. return item ? item->align : 0;
  803. }
  804. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  805. {
  806. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  807. }
  808. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  809. {
  810. return item ? item->bounds_align : 0;
  811. }
  812. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  813. {
  814. if (item)
  815. *bounds = item->bounds;
  816. }
  817. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  818. struct obs_transform_info *info)
  819. {
  820. if (item && info) {
  821. info->pos = item->pos;
  822. info->rot = item->rot;
  823. info->scale = item->scale;
  824. info->alignment = item->align;
  825. info->bounds_type = item->bounds_type;
  826. info->bounds_alignment = item->bounds_align;
  827. info->bounds = item->bounds;
  828. }
  829. }
  830. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  831. const struct obs_transform_info *info)
  832. {
  833. if (item && info) {
  834. item->pos = info->pos;
  835. item->rot = info->rot;
  836. item->scale = info->scale;
  837. item->align = info->alignment;
  838. item->bounds_type = info->bounds_type;
  839. item->bounds_align = info->bounds_alignment;
  840. item->bounds = info->bounds;
  841. update_item_transform(item);
  842. }
  843. }
  844. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  845. struct matrix4 *transform)
  846. {
  847. if (item)
  848. matrix4_copy(transform, &item->draw_transform);
  849. }
  850. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  851. struct matrix4 *transform)
  852. {
  853. if (item)
  854. matrix4_copy(transform, &item->box_transform);
  855. }
  856. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  857. {
  858. return item ? item->visible : false;
  859. }
  860. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  861. {
  862. struct calldata cd = {0};
  863. if (!item)
  864. return false;
  865. if (item->visible == visible)
  866. return false;
  867. if (!item->parent)
  868. return false;
  869. if (visible) {
  870. if (!obs_source_add_active_child(item->parent->source,
  871. item->source))
  872. return false;
  873. } else {
  874. obs_source_remove_active_child(item->parent->source,
  875. item->source);
  876. }
  877. item->visible = visible;
  878. calldata_set_ptr(&cd, "scene", item->parent);
  879. calldata_set_ptr(&cd, "item", item);
  880. calldata_set_bool(&cd, "visible", visible);
  881. signal_handler_signal(item->parent->source->context.signals,
  882. "item_visible", &cd);
  883. calldata_free(&cd);
  884. return true;
  885. }
  886. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  887. size_t size, bool *order_matches)
  888. {
  889. obs_sceneitem_t *item = scene->first_item;
  890. size_t count = 0;
  891. while (item) {
  892. bool found = false;
  893. for (size_t i = 0; i < size; i++) {
  894. if (items[i] != item)
  895. continue;
  896. if (count != i)
  897. *order_matches = false;
  898. found = true;
  899. break;
  900. }
  901. if (!found)
  902. return false;
  903. item = item->next;
  904. count += 1;
  905. }
  906. return count == size;
  907. }
  908. bool obs_scene_reorder_items(obs_scene_t *scene,
  909. obs_sceneitem_t * const *item_order, size_t item_order_size)
  910. {
  911. if (!scene || !item_order_size)
  912. return false;
  913. obs_scene_addref(scene);
  914. full_lock(scene);
  915. bool order_matches = true;
  916. if (!sceneitems_match(scene, item_order, item_order_size,
  917. &order_matches) || order_matches) {
  918. full_unlock(scene);
  919. obs_scene_release(scene);
  920. return false;
  921. }
  922. scene->first_item = item_order[0];
  923. obs_sceneitem_t *prev = NULL;
  924. for (size_t i = 0; i < item_order_size; i++) {
  925. item_order[i]->prev = prev;
  926. item_order[i]->next = NULL;
  927. if (prev)
  928. prev->next = item_order[i];
  929. prev = item_order[i];
  930. }
  931. signal_reorder(scene->first_item);
  932. full_unlock(scene);
  933. obs_scene_release(scene);
  934. return true;
  935. }
  936. void obs_scene_atomic_update(obs_scene_t *scene,
  937. obs_scene_atomic_update_func func, void *data)
  938. {
  939. if (!scene)
  940. return;
  941. obs_scene_addref(scene);
  942. full_lock(scene);
  943. func(data, scene);
  944. full_unlock(scene);
  945. obs_scene_release(scene);
  946. }