obs_wrapper/source/
context.rs1use super::audio::AudioRef;
2use crate::hotkey::{Hotkey, HotkeyCallbacks};
3use crate::prelude::DataObj;
4use crate::string::ObsString;
5use obs_sys::obs_get_audio;
6
7pub struct GlobalContext;
8pub struct VideoRenderContext;
9
10impl GlobalContext {
11 pub fn with_audio<T, F: FnOnce(&AudioRef) -> T>(&self, func: F) -> T {
12 let audio = unsafe { AudioRef::from_raw(obs_get_audio()) };
13 func(&audio)
14 }
15}
16
17impl Default for VideoRenderContext {
18 fn default() -> Self {
19 Self
20 }
21}
22
23impl Default for GlobalContext {
24 fn default() -> Self {
25 Self
26 }
27}
28
29pub struct CreatableSourceContext<'a, D> {
30 pub(crate) hotkey_callbacks: HotkeyCallbacks<D>,
31 pub settings: DataObj<'a>,
32 pub global: &'a mut GlobalContext,
33}
34
35impl<'a, D> CreatableSourceContext<'a, D> {
36 pub(crate) unsafe fn from_raw(settings: DataObj<'a>, global: &'a mut GlobalContext) -> Self {
37 Self {
38 hotkey_callbacks: Default::default(),
39 settings,
40 global,
41 }
42 }
43
44 pub fn register_hotkey<F: FnMut(&mut Hotkey, &mut D) + 'static>(
45 &mut self,
46 name: ObsString,
47 description: ObsString,
48 func: F,
49 ) {
50 self.hotkey_callbacks
51 .push((name, description, Box::new(func)));
52 }
53
54 pub fn with_audio<T, F: FnOnce(&AudioRef) -> T>(&self, func: F) -> T {
56 self.global.with_audio(func)
57 }
58}