Skip to main content

libobs_wrapper\utils\initialization/
mod.rs

1#[cfg(windows)]
2mod windows;
3
4#[cfg(windows)]
5pub(crate) use windows::*;
6
7#[cfg(not(windows))]
8mod other;
9
10#[cfg(not(windows))]
11pub(crate) use other::*;
12
13#[cfg(any(target_os = "linux", doc, feature = "__test_environment"))]
14#[derive(Clone, Debug)]
15pub enum PlatformType {
16    X11,
17    Wayland,
18    Invalid,
19}
20
21/// Raw native display pointer whose lifetime is owned by the caller's GUI toolkit.
22///
23/// Safe code cannot construct this type. The caller must guarantee that the display
24/// outlives the OBS context and that it belongs to the platform selected in
25/// [`NixDisplay`].
26#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
27#[derive(Clone, Copy, Debug)]
28pub struct NativeDisplayHandle(*mut std::os::raw::c_void);
29
30#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
31impl NativeDisplayHandle {
32    /// # Safety
33    /// `raw` must remain valid for the complete OBS context lifetime.
34    pub unsafe fn from_raw(raw: *mut std::os::raw::c_void) -> Self {
35        Self(raw)
36    }
37
38    pub(crate) fn as_ptr(self) -> *mut std::os::raw::c_void {
39        self.0
40    }
41}
42
43// Construction is unsafe and the pointer is never dereferenced outside the serialized
44// OBS/platform initialization paths. This is intentionally specific to display handles,
45// rather than a generic "make anything Send" wrapper.
46unsafe impl Send for NativeDisplayHandle {}
47unsafe impl Sync for NativeDisplayHandle {}
48
49#[derive(Debug, Clone)]
50pub enum NixDisplay {
51    X11(NativeDisplayHandle),
52    Wayland(NativeDisplayHandle),
53}
54
55impl NixDisplay {
56    /// Construct an X11 display descriptor.
57    ///
58    /// # Safety
59    /// The display pointer must be a live X11 display and outlive the OBS context.
60    pub unsafe fn x11(raw: *mut std::os::raw::c_void) -> Self {
61        // SAFETY: This constructor forwards the caller's documented lifetime and X11
62        // validity guarantees unchanged to NativeDisplayHandle.
63        Self::X11(unsafe { NativeDisplayHandle::from_raw(raw) })
64    }
65
66    /// Construct a Wayland display descriptor.
67    ///
68    /// # Safety
69    /// The display pointer must be a live Wayland display and outlive the OBS context.
70    pub unsafe fn wayland(raw: *mut std::os::raw::c_void) -> Self {
71        // SAFETY: This constructor forwards the caller's documented lifetime and
72        // Wayland validity guarantees unchanged to NativeDisplayHandle.
73        Self::Wayland(unsafe { NativeDisplayHandle::from_raw(raw) })
74    }
75}