1
0

obs-scene.c 27 KB

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