1mod transform_info;
19pub use transform_info::*;
20
21mod group;
22mod scene_drop_guards;
23mod scene_item;
24
25mod filter_traits;
26pub use filter_traits::*;
27
28pub use group::*;
29pub use scene_item::*;
30
31use std::collections::HashMap;
32use std::fmt::Debug;
33use std::sync::{Arc, RwLock};
34
35use libobs::{obs_scene_t, obs_source_t};
36
37use crate::macros::impl_eq_of_ptr;
38use crate::scenes::scene_drop_guards::_SceneDropGuard;
39use crate::sources::{ObsFilterGuardPair, ObsSourceTrait};
40use crate::unsafe_send::{NativeObjectId, SmartPointerSendable};
41use crate::utils::ObsDropGuard;
42use crate::{
43 impl_signal_manager, run_with_obs,
44 runtime::ObsRuntime,
45 unsafe_send::Sendable,
46 utils::{ObsError, ObsString},
47};
48
49#[derive(Debug)]
50struct _NoOpDropGuard;
51impl ObsDropGuard for _NoOpDropGuard {}
52
53pub(super) type SceneItemsBySource = Arc<
54 RwLock<
55 HashMap<
56 NativeObjectId,
57 (
58 Arc<dyn ObsSourceTrait>,
59 Vec<Arc<dyn SceneItemTrait + 'static>>,
60 ),
61 >,
62 >,
63>;
64
65#[derive(Debug, Clone)]
66pub struct ObsSceneRef {
68 name: ObsString,
69 attached_scene_items: SceneItemsBySource,
70 attached_groups: group::SceneGroups,
71 attached_filters: Arc<RwLock<Vec<ObsFilterGuardPair>>>,
72 runtime: ObsRuntime,
73 signals: Arc<ObsSceneSignals>,
74 scene: SmartPointerSendable<*mut obs_scene_t>,
75}
76
77impl_eq_of_ptr!(ObsSceneRef);
78
79impl ObsSceneRef {
80 pub(crate) fn new(name: ObsString, runtime: ObsRuntime) -> Result<Self, ObsError> {
81 let scene = run_with_obs!(runtime, (name), move || {
82 let name_ptr = name.as_ptr();
83
84 let scene_ptr = unsafe {
85 libobs::obs_scene_create(name_ptr.0)
87 };
88 if scene_ptr.is_null() {
89 return Err(ObsError::NullPointer(None));
90 }
91
92 let source_ptr = unsafe {
93 libobs::obs_scene_get_source(scene_ptr)
95 };
96
97 if source_ptr.is_null() {
98 unsafe {
99 libobs::obs_scene_release(scene_ptr);
101 }
102 return Err(ObsError::NullPointer(None));
103 }
104
105 Ok(Sendable(scene_ptr))
106 })??;
107
108 let drop_guard = Arc::new(_SceneDropGuard::new(scene.clone(), runtime.clone()));
109 let scene = SmartPointerSendable::new(scene.0, drop_guard, runtime.native_registry());
110
111 let signals = Arc::new(ObsSceneSignals::new(&scene, runtime.clone())?);
112 Ok(Self {
113 name,
114 scene,
115 attached_scene_items: Arc::new(RwLock::new(HashMap::new())),
116 attached_groups: Arc::new(RwLock::new(Vec::new())),
117 attached_filters: Arc::new(RwLock::new(Vec::new())),
118 runtime,
119 signals,
120 })
121 }
122
123 #[deprecated = "Use ObsSceneRef::set_to_channel instead"]
124 pub fn add_and_set(&self, channel: u32) -> Result<(), ObsError> {
125 self.set_to_channel(channel)
126 }
127
128 pub fn set_to_channel(&self, channel: u32) -> Result<(), ObsError> {
133 if channel >= libobs::MAX_CHANNELS {
134 return Err(ObsError::InvalidOperation(format!(
135 "Channel {} is out of bounds (max {})",
136 channel,
137 libobs::MAX_CHANNELS - 1
138 )));
139 }
140
141 let scene_source_ptr = self.get_scene_source_ptr()?;
142 run_with_obs!(self.runtime, (scene_source_ptr), move || unsafe {
143 libobs::obs_set_output_source(channel, scene_source_ptr.0);
146 })
147 }
148
149 pub fn remove_from_channel(&self, channel: u32) -> Result<(), ObsError> {
151 if channel >= libobs::MAX_CHANNELS {
152 return Err(ObsError::InvalidOperation(format!(
153 "Channel {} is out of bounds (max {})",
154 channel,
155 libobs::MAX_CHANNELS - 1
156 )));
157 }
158
159 run_with_obs!(self.runtime, (), move || unsafe {
160 libobs::obs_set_output_source(channel, std::ptr::null_mut());
162 })
163 }
164
165 pub(crate) fn get_scene_source_ptr(&self) -> Result<Sendable<*mut obs_source_t>, ObsError> {
167 let scene_ptr = self.scene.clone();
168 run_with_obs!(self.runtime, (scene_ptr), move || {
169 unsafe {
170 Sendable(libobs::obs_scene_get_source(scene_ptr.get_ptr()))
172 }
173 })
174 }
175
176 pub(crate) fn as_ptr(&self) -> SmartPointerSendable<*mut obs_scene_t> {
177 self.scene.clone()
178 }
179
180 pub fn object_id(&self) -> NativeObjectId {
181 self.scene.native_id()
182 }
183
184 pub fn name(&self) -> ObsString {
185 self.name.clone()
186 }
187
188 pub fn runtime(&self) -> &ObsRuntime {
189 &self.runtime
190 }
191
192 pub fn persistent_uuid(&self) -> Result<String, ObsError> {
194 let scene = self.as_ptr();
195 run_with_obs!(self.runtime, (scene), move || unsafe {
196 let source = libobs::obs_scene_get_source(scene.get_ptr());
199 if source.is_null() {
200 return Err(ObsError::NullPointer(Some("scene has no source".into())));
201 }
202 let uuid = libobs::obs_source_get_uuid(source);
203 if uuid.is_null() {
204 return Err(ObsError::NullPointer(Some(
205 "scene source has no persistent UUID".into(),
206 )));
207 }
208 Ok(std::ffi::CStr::from_ptr(uuid)
209 .to_string_lossy()
210 .into_owned())
211 })?
212 }
213
214 pub fn managed_sources(&self) -> Result<Vec<Arc<dyn ObsSourceTrait>>, ObsError> {
217 Ok(self
218 .attached_scene_items
219 .read()
220 .map_err(|error| ObsError::LockError(error.to_string()))?
221 .values()
222 .map(|(source, _)| source.clone())
223 .collect())
224 }
225
226 pub fn signals(&self) -> Arc<ObsSceneSignals> {
227 self.signals.clone()
228 }
229
230 pub fn add<T>(&mut self, source: T) -> Result<ObsSceneItemRef<T>, ObsError>
232 where
233 T: ObsSourceTrait + Clone + 'static,
234 {
235 SceneItemExtSceneTrait::add_source(self, source)
236 }
237
238 pub fn add_new_source(
240 &mut self,
241 info: crate::utils::SourceInfo,
242 ) -> Result<ObsSceneItemRef<crate::sources::ObsSourceRef>, ObsError> {
243 SceneItemExtSceneTrait::add_and_create_source(self, info)
244 }
245
246 pub fn remove_item<T: SceneItemTrait + ?Sized>(&mut self, item: &T) -> Result<(), ObsError> {
248 SceneItemExtSceneTrait::remove_item(self, item)
249 }
250
251 pub fn items_for_source<T>(&self, source: &T) -> Result<Vec<Arc<dyn SceneItemTrait>>, ObsError>
253 where
254 T: ObsSourceTrait + Clone,
255 {
256 SceneItemExtSceneTrait::items_for_source(self, source)
257 }
258
259 pub fn clear(&mut self) -> Result<(), ObsError> {
261 SceneItemExtSceneTrait::remove_all_sources(self)?;
262 self.clear_groups()
263 }
264}
265
266impl_signal_manager!(|scene_ptr: SmartPointerSendable<*mut obs_scene_t>| unsafe {
267 let source_ptr = libobs::obs_scene_get_source(scene_ptr.get_ptr());
269
270 libobs::obs_source_get_signal_handler(source_ptr)
271}, ObsSceneSignals for *mut obs_scene_t, [
272 "item_add": {
273 struct ItemAddSignal {
274 POINTERS {
275 item: *mut libobs::obs_sceneitem_t,
276 }
277 }
278 },
279 "item_remove": {
280 struct ItemRemoveSignal {
281 POINTERS {
282 item: *mut libobs::obs_sceneitem_t,
283 }
284 }
285 },
286 "reorder": {},
287 "refresh": {},
288 "item_visible": {
289 struct ItemVisibleSignal {
290 visible: bool;
291 POINTERS {
292 item: *mut libobs::obs_sceneitem_t,
293 }
294 }
295 },
296 "item_locked": {
297 struct ItemLockedSignal {
298 locked: bool;
299 POINTERS {
300 item: *mut libobs::obs_sceneitem_t,
301 }
302 }
303 },
304 "item_select": {
305 struct ItemSelectSignal {
306 POINTERS {
307 item: *mut libobs::obs_sceneitem_t,
308 }
309 }
310 },
311 "item_deselect": {
312 struct ItemDeselectSignal {
313 POINTERS {
314 item: *mut libobs::obs_sceneitem_t,
315 }
316 }
317 },
318 "item_transform": {
319 struct ItemTransformSignal {
320 POINTERS {
321 item: *mut libobs::obs_sceneitem_t,
322 }
323 }
324 }
325]);