Skip to main content

libobs_wrapper\data\properties\types/
mod.rs

1//! # Important Notice
2//! All structs in this module use direct obs calls to get the data from the obs_property_t struct. **ALWAYS MAKE SURE THIS IS RUNNING ON THE OBS THREAD**
3
4mod button;
5impl_general_property!(Color);
6mod editable_list;
7impl_general_property!(Font);
8impl_general_property!(FrameRate);
9impl_general_property!(Group);
10impl_general_property!(ColorAlpha);
11mod list;
12mod number;
13mod path;
14mod text;
15
16pub(crate) struct PropertyCreationInfo {
17    name: String,
18    description: Option<String>,
19    pointer: Sendable<*mut libobs::obs_property>,
20    runtime: ObsRuntime,
21}
22
23impl PropertyCreationInfo {
24    /// # Safety
25    /// `pointer` must be non-null and borrowed from a live obs_properties_t tree.
26    /// The caller must retain that tree, without mutation or destruction, until the
27    /// returned info has been consumed by its TryFrom conversion. All native access
28    /// and conversion must run on `runtime`'s actor (nested actor calls run inline).
29    pub(crate) unsafe fn new(
30        name: String,
31        description: Option<String>,
32        pointer: Sendable<*mut libobs::obs_property>,
33        runtime: ObsRuntime,
34    ) -> Self {
35        Self {
36            name,
37            description,
38            pointer,
39            runtime,
40        }
41    }
42}
43
44use std::ffi::CStr;
45
46pub use button::*;
47pub use editable_list::*;
48use libobs::obs_property;
49pub use list::*;
50pub use number::*;
51pub use path::*;
52pub use text::*;
53
54use crate::{run_with_obs, runtime::ObsRuntime, unsafe_send::Sendable, utils::ObsError};
55
56use super::{macros::impl_general_property, ObsProperty, ObsPropertyType};
57
58impl ObsPropertyType {
59    /// # Safety
60    /// Run on `runtime`'s actor. `pointer` must belong to a live property tree retained
61    /// without mutation or destruction throughout this call and all nested conversions.
62    unsafe fn inner_to_property_struct(
63        &self,
64        runtime: &ObsRuntime,
65        pointer: Sendable<*mut obs_property>,
66    ) -> Result<ObsProperty, ObsError> {
67        let (name, description) = run_with_obs!(runtime, (pointer), move || {
68            let name = unsafe {
69                // SAFETY: The caller retains the parent tree throughout this actor conversion.
70                libobs::obs_property_name(pointer.0)
71            };
72            if name.is_null() {
73                return Err(ObsError::NullPointer(Some(
74                    "Property name pointer is null".to_string(),
75                )));
76            }
77
78            let name = unsafe {
79                // SAFETY: obs_property_name returns a tree-owned NUL-terminated string;
80                // the caller retains the unchanged tree until this copy completes.
81                CStr::from_ptr(name)
82            };
83            let name = name.to_string_lossy().to_string();
84
85            let description = unsafe { libobs::obs_property_description(pointer.0) };
86            let description = if description.is_null() {
87                None
88            } else {
89                let description = unsafe {
90                    // SAFETY: The non-null description is a tree-owned NUL-terminated string;
91                    // the caller retains the unchanged tree until this copy completes.
92                    CStr::from_ptr(description)
93                };
94                Some(description.to_string_lossy().to_string())
95            };
96
97            Ok((name, description))
98        })??;
99
100        let info = PropertyCreationInfo::new(
101            name,
102            description,
103            pointer,
104            runtime.clone(), //
105        );
106
107        let data = match self {
108            ObsPropertyType::Invalid => ObsProperty::Invalid,
109            ObsPropertyType::Bool => ObsProperty::Bool,
110            ObsPropertyType::Int => ObsProperty::Int(ObsNumberProperty::<i32>::try_from(info)?),
111            ObsPropertyType::Float => ObsProperty::Float(ObsNumberProperty::<f64>::try_from(info)?),
112            ObsPropertyType::Text => ObsProperty::Text(ObsTextProperty::try_from(info)?),
113            ObsPropertyType::Path => ObsProperty::Path(ObsPathProperty::try_from(info)?),
114            ObsPropertyType::List => ObsProperty::List(ObsListProperty::try_from(info)?),
115            ObsPropertyType::Color => ObsProperty::Color(ObsColorProperty::try_from(info)?),
116            ObsPropertyType::Button => ObsProperty::Button(ObsButtonProperty::try_from(info)?),
117            ObsPropertyType::Font => ObsProperty::Font(ObsFontProperty::try_from(info)?),
118            ObsPropertyType::EditableList => {
119                ObsProperty::EditableList(ObsEditableListProperty::try_from(info)?)
120            }
121            ObsPropertyType::FrameRate => {
122                ObsProperty::FrameRate(ObsFrameRateProperty::try_from(info)?)
123            }
124            ObsPropertyType::Group => ObsProperty::Group(ObsGroupProperty::try_from(info)?),
125            ObsPropertyType::ColorAlpha => {
126                ObsProperty::ColorAlpha(ObsColorAlphaProperty::try_from(info)?)
127            }
128        };
129
130        Ok(data)
131    }
132
133    /// # Safety
134    /// Run on `runtime`'s actor. `pointer` is a non-null iterator borrow, not an owned
135    /// reference; retain its parent property tree unchanged until conversion finishes.
136    pub(in crate::data::properties) unsafe fn get_property_struct(
137        &self,
138        runtime: &ObsRuntime,
139        pointer: Sendable<*mut obs_property>,
140    ) -> Result<ObsProperty, ObsError> {
141        self.inner_to_property_struct(runtime, pointer)
142    }
143}