1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use obs_sys::{obs_output_t, obs_output_create, obs_output_release, obs_output_get_ref};

use crate::{string::ObsString, hotkey::Hotkey, prelude::DataObj, wrapper::PtrWrapper};

/// Context wrapping an OBS output - video / audio elements which are displayed to the screen.
///
/// See [OBS documentation](https://obsproject.com/docs/reference-outputs.html#c.obs_output_t)
pub struct OutputContext {
    pub(crate) inner: *mut obs_output_t,
}

impl OutputContext {
    pub fn from_raw(output: *mut obs_output_t) -> Self {
        Self {
            inner: unsafe { obs_output_get_ref(output) }
        }
    }
}

impl Clone for OutputContext {
    fn clone(&self) -> Self {
        Self::from_raw(self.inner)
    }
}

impl OutputContext {
    pub fn new(id: ObsString, name: ObsString, settings: Option<DataObj<'_>>) -> Self {
        let settings = match settings {
            Some(mut data) => data.as_ptr_mut(),
            None => std::ptr::null_mut(),
        };
        let output = unsafe {
            obs_output_create(id.as_ptr(), name.as_ptr(), settings, std::ptr::null_mut())
        };
        Self::from_raw(output)
    }
}

impl Drop for OutputContext {
    fn drop(&mut self) {
        unsafe { obs_output_release(self.inner) }
    }
}

pub struct CreatableOutputContext<'a, D> {
    pub(crate) hotkey_callbacks: Vec<(
        ObsString,
        ObsString,
        Box<dyn FnMut(&mut Hotkey, &mut D)>,
    )>,
    pub settings: DataObj<'a>,
}

impl<'a, D> CreatableOutputContext<'a, D> {
    pub fn from_raw(settings: DataObj<'a>) -> Self {
        Self { hotkey_callbacks: vec![], settings }
    }

    pub fn register_hotkey<F: FnMut(&mut Hotkey, &mut D) + 'static>(
        &mut self,
        name: ObsString,
        description: ObsString,
        func: F,
    ) {
        self.hotkey_callbacks
            .push((name, description, Box::new(func)));
    }
}