1use std::ffi::{c_void, CStr};
9
10use crate::{
11 capabilities::PropertyMetadata,
12 data::{object::ObsObjectTrait, ImmutableObsData},
13 encoders::{audio::ObsAudioEncoder, video::ObsVideoEncoder},
14 run_with_obs,
15 services::ObsServiceRef,
16 sources::{ObsFilterRef, ObsSourceRef},
17 unsafe_send::{NativePointer, Sendable, SmartPointerSendable},
18 utils::{ObsError, ObsString},
19};
20
21use crate::data::output::{ObsOutputRef, ObsReplayBufferOutputRef};
22
23#[derive(Debug, Clone)]
24pub struct PropertyActionRefresh {
25 pub properties: Vec<PropertyMetadata>,
27 pub settings: ImmutableObsData,
29}
30
31#[derive(Debug, Clone)]
32pub enum PropertyActionOutcome {
33 Invoked {
37 refresh_requested: bool,
38 refresh: PropertyActionRefresh,
39 },
40 ExternalUrl {
43 url: String,
44 refresh: PropertyActionRefresh,
45 },
46}
47
48impl PropertyActionOutcome {
49 pub fn refresh(&self) -> &PropertyActionRefresh {
50 match self {
51 Self::Invoked { refresh, .. } | Self::ExternalUrl { refresh, .. } => refresh,
52 }
53 }
54}
55
56pub trait ObsPropertyActions: ObsObjectTrait {
62 fn invoke_property_button(&self, name: &str) -> Result<PropertyActionOutcome, ObsError>;
63}
64
65#[allow(unknown_lints)]
66#[allow(ensure_obs_call_in_runtime)]
67fn source_properties(source: *mut libobs::obs_source_t) -> *mut libobs::obs_properties_t {
68 unsafe { libobs::obs_source_properties(source) }
70}
71
72#[allow(unknown_lints)]
73#[allow(ensure_obs_call_in_runtime)]
74fn source_settings(source: *mut libobs::obs_source_t) -> *mut libobs::obs_data_t {
75 unsafe { libobs::obs_source_get_settings(source) }
77}
78
79#[allow(unknown_lints)]
80#[allow(ensure_obs_call_in_runtime)]
81fn output_properties(output: *mut libobs::obs_output_t) -> *mut libobs::obs_properties_t {
82 unsafe { libobs::obs_output_properties(output) }
84}
85
86#[allow(unknown_lints)]
87#[allow(ensure_obs_call_in_runtime)]
88fn output_settings(output: *mut libobs::obs_output_t) -> *mut libobs::obs_data_t {
89 unsafe { libobs::obs_output_get_settings(output) }
91}
92
93#[allow(unknown_lints)]
94#[allow(ensure_obs_call_in_runtime)]
95fn encoder_properties(encoder: *mut libobs::obs_encoder_t) -> *mut libobs::obs_properties_t {
96 unsafe { libobs::obs_encoder_properties(encoder) }
98}
99
100#[allow(unknown_lints)]
101#[allow(ensure_obs_call_in_runtime)]
102fn encoder_settings(encoder: *mut libobs::obs_encoder_t) -> *mut libobs::obs_data_t {
103 unsafe { libobs::obs_encoder_get_settings(encoder) }
105}
106
107#[allow(unknown_lints)]
108#[allow(ensure_obs_call_in_runtime)]
109fn service_properties(service: *mut libobs::obs_service_t) -> *mut libobs::obs_properties_t {
110 unsafe { libobs::obs_service_properties(service) }
112}
113
114#[allow(unknown_lints)]
115#[allow(ensure_obs_call_in_runtime)]
116fn service_settings(service: *mut libobs::obs_service_t) -> *mut libobs::obs_data_t {
117 unsafe { libobs::obs_service_get_settings(service) }
119}
120
121fn invoke_button<P, GP, GS, T>(
122 target: &T,
123 object: SmartPointerSendable<P>,
124 name: &str,
125 get_properties: GP,
126 get_settings: GS,
127) -> Result<PropertyActionOutcome, ObsError>
128where
129 P: NativePointer,
130 T: ObsObjectTrait<Native = P> + ?Sized,
131 GP: Fn(P) -> *mut libobs::obs_properties_t + Send + 'static,
132 GS: Fn(P) -> *mut libobs::obs_data_t + Send + 'static,
133{
134 let runtime = target.runtime().clone();
135 let property_name = ObsString::new(name);
136 let object_for_actor = object.clone();
137 let (refresh_requested, url, properties, settings_ptr) =
138 run_with_obs!(runtime, (object_for_actor, property_name), move || unsafe {
139 let properties = get_properties(object_for_actor.get_ptr());
142 if properties.is_null() {
143 return Err(ObsError::InvalidOperation(
144 "OBS instance returned no property tree".into(),
145 ));
146 }
147
148 let property = libobs::obs_properties_get(properties, property_name.as_ptr().0);
149 if property.is_null() {
150 libobs::obs_properties_destroy(properties);
151 return Err(ObsError::PropertyNotFound {
152 name: property_name.to_string(),
153 });
154 }
155 if libobs::obs_property_get_type(property)
156 != libobs::obs_property_type_OBS_PROPERTY_BUTTON
157 {
158 libobs::obs_properties_destroy(properties);
159 return Err(ObsError::PropertyValueUnsupported {
160 name: property_name.to_string(),
161 property_type: "non-button property".into(),
162 });
163 }
164 if !libobs::obs_property_enabled(property) {
165 libobs::obs_properties_destroy(properties);
166 return Err(ObsError::InvalidOperation(format!(
167 "OBS property '{}' is currently disabled",
168 property_name
169 )));
170 }
171
172 let button_type = libobs::obs_property_button_type(property);
173 let mut url = None;
174 let refresh_requested = if button_type == libobs::obs_button_type_OBS_BUTTON_URL {
175 let raw_url = libobs::obs_property_button_url(property);
176 if !raw_url.is_null() {
177 url = Some(CStr::from_ptr(raw_url).to_string_lossy().into_owned());
178 }
179 false
180 } else {
181 libobs::obs_property_button_clicked(
184 property,
185 object_for_actor.get_ptr().into_addr() as *mut c_void,
186 )
187 };
188 libobs::obs_properties_destroy(properties);
189
190 let refreshed = get_properties(object_for_actor.get_ptr());
193 let properties = crate::capabilities::snapshot_owned_properties(refreshed);
194 let settings = get_settings(object_for_actor.get_ptr());
195 if settings.is_null() {
196 return Err(ObsError::NullPointer(Some(
197 "failed to refresh instance settings after property action".into(),
198 )));
199 }
200 Ok((refresh_requested, url, properties, Sendable(settings)))
201 })??;
202
203 let settings = ImmutableObsData::from_raw_pointer(settings_ptr, target.runtime().clone());
204 target.__internal_replace_settings(settings.clone())?;
205 let refresh = PropertyActionRefresh {
206 properties,
207 settings,
208 };
209 Ok(match url {
210 Some(url) => PropertyActionOutcome::ExternalUrl { url, refresh },
211 None => PropertyActionOutcome::Invoked {
212 refresh_requested,
213 refresh,
214 },
215 })
216}
217
218macro_rules! impl_source_actions {
219 ($ty:ty) => {
220 impl ObsPropertyActions for $ty {
221 fn invoke_property_button(
222 &self,
223 name: &str,
224 ) -> Result<PropertyActionOutcome, ObsError> {
225 invoke_button(
226 self,
227 self.__native_handle(),
228 name,
229 source_properties,
230 source_settings,
231 )
232 }
233 }
234 };
235}
236
237impl_source_actions!(ObsSourceRef);
238impl_source_actions!(ObsFilterRef);
239
240macro_rules! impl_output_actions {
241 ($ty:ty) => {
242 impl ObsPropertyActions for $ty {
243 fn invoke_property_button(
244 &self,
245 name: &str,
246 ) -> Result<PropertyActionOutcome, ObsError> {
247 invoke_button(
248 self,
249 self.__native_handle(),
250 name,
251 output_properties,
252 output_settings,
253 )
254 }
255 }
256 };
257}
258
259impl_output_actions!(ObsOutputRef);
260impl_output_actions!(ObsReplayBufferOutputRef);
261
262impl ObsPropertyActions for ObsVideoEncoder {
263 fn invoke_property_button(&self, name: &str) -> Result<PropertyActionOutcome, ObsError> {
264 invoke_button(
265 self,
266 self.__native_handle(),
267 name,
268 encoder_properties,
269 encoder_settings,
270 )
271 }
272}
273
274impl ObsPropertyActions for ObsAudioEncoder {
275 fn invoke_property_button(&self, name: &str) -> Result<PropertyActionOutcome, ObsError> {
276 invoke_button(
277 self,
278 self.__native_handle(),
279 name,
280 encoder_properties,
281 encoder_settings,
282 )
283 }
284}
285
286impl ObsPropertyActions for ObsServiceRef {
287 fn invoke_property_button(&self, name: &str) -> Result<PropertyActionOutcome, ObsError> {
288 invoke_button(
289 self,
290 self.__native_handle(),
291 name,
292 service_properties,
293 service_settings,
294 )
295 }
296}