Skip to main content

libobs_wrapper\data\traits/
getters.rs

1use std::ffi::CStr;
2
3use crate::{
4    data::ObsDataPointers,
5    run_with_obs,
6    unsafe_send::SmartPointerSendable,
7    utils::{ObsError, ObsString},
8};
9
10/// # Safety
11/// This function must be called on the OBS runtime.
12#[allow(unknown_lints)]
13#[allow(ensure_obs_call_in_runtime)]
14unsafe fn has_value(
15    data_ptr: SmartPointerSendable<*mut libobs::obs_data_t>,
16    key: &ObsString,
17) -> bool {
18    libobs::obs_data_has_user_value(data_ptr.get_ptr(), key.as_ptr().0)
19        || libobs::obs_data_has_default_value(data_ptr.get_ptr(), key.as_ptr().0)
20}
21
22#[allow(private_bounds)]
23pub trait ObsDataGetters: ObsDataPointers {
24    fn get_string<T: Into<ObsString> + Send + Sync>(
25        &self,
26        key: T,
27    ) -> Result<Option<String>, ObsError> {
28        let key = key.into();
29        let data_ptr = self.as_ptr();
30
31        run_with_obs!(self.runtime(), (data_ptr, key), move || {
32            let has_value = unsafe {
33                // SAFETY: data_ptr retains the native data; key owns its CString throughout this
34                // actor call.
35                has_value(data_ptr.clone(), &key)
36            };
37
38            if has_value {
39                let result = unsafe {
40                    // SAFETY: data_ptr retains the data reference on the actor; key owns a live
41                    // CString through this synchronous lookup.
42                    libobs::obs_data_get_string(data_ptr.get_ptr(), key.as_ptr().0)
43                };
44
45                if result.is_null() {
46                    Err(ObsError::NullPointer(None))
47                } else {
48                    let result = unsafe {
49                        // SAFETY: libobs returns a data-owned NUL-terminated string; data_ptr
50                        // retains it and the actor copies it before any mutation.
51                        CStr::from_ptr(result)
52                    };
53                    let result = result
54                        .to_str()
55                        .map_err(|_| ObsError::StringConversionError)?
56                        .to_string();
57
58                    Ok(Some(result))
59                }
60            } else {
61                Ok(None)
62            }
63        })?
64    }
65    fn get_int<T: Into<ObsString> + Sync + Send>(&self, key: T) -> Result<Option<i64>, ObsError> {
66        let key = key.into();
67        let data_ptr = self.as_ptr();
68
69        run_with_obs!(self.runtime(), (data_ptr, key), move || {
70            let has_value = unsafe {
71                // SAFETY: data_ptr retains the native data; key owns its CString throughout this
72                // actor call.
73                has_value(data_ptr.clone(), &key)
74            };
75
76            if has_value {
77                Some(unsafe {
78                    // SAFETY: data_ptr retains the data reference on the actor; key owns a live
79                    // CString through this synchronous lookup.
80                    libobs::obs_data_get_int(data_ptr.get_ptr(), key.as_ptr().0)
81                })
82            } else {
83                None
84            }
85        })
86    }
87    fn get_bool<T: Into<ObsString> + Sync + Send>(&self, key: T) -> Result<Option<bool>, ObsError> {
88        let key = key.into();
89
90        let data_ptr = self.as_ptr();
91
92        run_with_obs!(self.runtime(), (data_ptr, key), move || {
93            let has_value = unsafe {
94                // SAFETY: data_ptr retains the native data; key owns its CString throughout this
95                // actor call.
96                has_value(data_ptr.clone(), &key)
97            };
98
99            if has_value {
100                Some(unsafe {
101                    // SAFETY: data_ptr retains the data reference on the actor; key owns a live
102                    // CString through this synchronous lookup.
103                    libobs::obs_data_get_bool(data_ptr.get_ptr(), key.as_ptr().0)
104                })
105            } else {
106                None
107            }
108        })
109    }
110    fn get_double<T: Into<ObsString> + Sync + Send>(
111        &self,
112        key: T,
113    ) -> Result<Option<f64>, ObsError> {
114        let key = key.into();
115        let data_ptr = self.as_ptr();
116
117        let result = run_with_obs!(self.runtime(), (key, data_ptr), move || {
118            let has_value = unsafe {
119                // SAFETY: data_ptr retains the native data; key owns its CString throughout this
120                // actor call.
121                has_value(data_ptr.clone(), &key)
122            };
123
124            if has_value {
125                Some(unsafe {
126                    // SAFETY: data_ptr retains the data reference on the actor; key owns a live
127                    // CString through this synchronous lookup.
128                    libobs::obs_data_get_double(data_ptr.get_ptr(), key.as_ptr().0)
129                })
130            } else {
131                None
132            }
133        })?;
134
135        Ok(result)
136    }
137
138    fn get_json(&self) -> Result<String, ObsError> {
139        let data_ptr = self.as_ptr();
140        run_with_obs!(self.runtime(), (data_ptr), move || {
141            let json_ptr = unsafe {
142                // SAFETY: data_ptr retains the data reference throughout actor-side JSON
143                // serialization.
144                libobs::obs_data_get_json(data_ptr.get_ptr())
145            };
146
147            if json_ptr.is_null() {
148                return Err(ObsError::NullPointer(Some(
149                    "Couldn't get json representation of OBS data".into(),
150                )));
151            }
152
153            let json = unsafe {
154                // SAFETY: libobs returns a data-owned NUL-terminated string; data_ptr retains it
155                // and the actor copies it before any mutation.
156                CStr::from_ptr(json_ptr)
157            }
158            .to_str()
159            .map_err(|_| ObsError::JsonParseError)?
160            .to_string();
161
162            Ok(json)
163        })?
164    }
165}