obs_wrapper/output/
context.rs

1use obs_sys::{obs_output_create, obs_output_get_ref, obs_output_release, obs_output_t};
2
3use crate::{
4    hotkey::{Hotkey, HotkeyCallbacks},
5    prelude::DataObj,
6    string::ObsString,
7    wrapper::PtrWrapper,
8};
9
10/// Context wrapping an OBS output - video / audio elements which are displayed
11/// to the screen.
12///
13/// See [OBS documentation](https://obsproject.com/docs/reference-outputs.html#c.obs_output_t)
14pub struct OutputContext {
15    pub(crate) inner: *mut obs_output_t,
16}
17
18impl OutputContext {
19    /// # Safety
20    ///
21    /// Pointer must be valid.
22    pub unsafe fn from_raw(output: *mut obs_output_t) -> Self {
23        Self {
24            inner: obs_output_get_ref(output),
25        }
26    }
27}
28
29impl Clone for OutputContext {
30    fn clone(&self) -> Self {
31        unsafe { Self::from_raw(self.inner) }
32    }
33}
34
35impl OutputContext {
36    pub fn new(id: ObsString, name: ObsString, settings: Option<DataObj<'_>>) -> Self {
37        let settings = match settings {
38            Some(mut data) => data.as_ptr_mut(),
39            None => std::ptr::null_mut(),
40        };
41        let output = unsafe {
42            obs_output_create(id.as_ptr(), name.as_ptr(), settings, std::ptr::null_mut())
43        };
44
45        unsafe { Self::from_raw(output) }
46    }
47}
48
49impl Drop for OutputContext {
50    fn drop(&mut self) {
51        unsafe { obs_output_release(self.inner) }
52    }
53}
54
55pub struct CreatableOutputContext<'a, D> {
56    pub(crate) hotkey_callbacks: HotkeyCallbacks<D>,
57    pub settings: DataObj<'a>,
58}
59
60impl<'a, D> CreatableOutputContext<'a, D> {
61    pub fn from_raw(settings: DataObj<'a>) -> Self {
62        Self {
63            hotkey_callbacks: vec![],
64            settings,
65        }
66    }
67
68    pub fn register_hotkey<F: FnMut(&mut Hotkey, &mut D) + 'static>(
69        &mut self,
70        name: ObsString,
71        description: ObsString,
72        func: F,
73    ) {
74        self.hotkey_callbacks
75            .push((name, description, Box::new(func)));
76    }
77}