obs-scene.c 27 KB

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