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