raw_window_handle/
unix.rs

1use core::ffi::{c_int, c_ulong, c_void};
2use core::num::NonZeroU32;
3use core::ptr::NonNull;
4
5/// Raw display handle for Xlib.
6#[non_exhaustive]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct XlibDisplayHandle {
9    /// A pointer to an Xlib `Display`.
10    ///
11    /// It is strongly recommended to set this value, however it may be set to
12    /// `None` to request the default display when using EGL.
13    pub display: Option<NonNull<c_void>>,
14
15    /// An X11 screen to use with this display handle.
16    ///
17    /// Note, that X11 could have multiple screens, however
18    /// graphics APIs could work only with one screen at the time,
19    /// given that multiple screens usually reside on different GPUs.
20    pub screen: c_int,
21}
22
23impl XlibDisplayHandle {
24    /// Create a new handle to a display.
25    ///
26    ///
27    /// # Example
28    ///
29    /// ```
30    /// # use core::ffi::c_void;
31    /// # use core::ptr::NonNull;
32    /// # use raw_window_handle::XlibDisplayHandle;
33    /// #
34    /// let display: NonNull<c_void>;
35    /// let screen;
36    /// # display = NonNull::from(&()).cast();
37    /// # screen = 0;
38    /// let handle = XlibDisplayHandle::new(Some(display), screen);
39    /// ```
40    pub fn new(display: Option<NonNull<c_void>>, screen: c_int) -> Self {
41        Self { display, screen }
42    }
43}
44
45/// Raw window handle for Xlib.
46#[non_exhaustive]
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
48pub struct XlibWindowHandle {
49    /// An Xlib `Window`.
50    pub window: c_ulong,
51    /// An Xlib visual ID, or 0 if unknown.
52    pub visual_id: c_ulong,
53}
54
55impl XlibWindowHandle {
56    /// Create a new handle to a window.
57    ///
58    ///
59    /// # Example
60    ///
61    /// ```
62    /// # use core::ffi::c_ulong;
63    /// # use raw_window_handle::XlibWindowHandle;
64    /// #
65    /// let window: c_ulong;
66    /// # window = 0;
67    /// let mut handle = XlibWindowHandle::new(window);
68    /// // Optionally set the visual ID.
69    /// handle.visual_id = 0;
70    /// ```
71    pub fn new(window: c_ulong) -> Self {
72        Self {
73            window,
74            visual_id: 0,
75        }
76    }
77}
78
79/// Raw display handle for Xcb.
80#[non_exhaustive]
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82pub struct XcbDisplayHandle {
83    /// A pointer to an X server `xcb_connection_t`.
84    ///
85    /// It is strongly recommended to set this value, however it may be set to
86    /// `None` to request the default display when using EGL.
87    pub connection: Option<NonNull<c_void>>,
88
89    /// An X11 screen to use with this display handle.
90    ///
91    /// Note, that X11 could have multiple screens, however
92    /// graphics APIs could work only with one screen at the time,
93    /// given that multiple screens usually reside on different GPUs.
94    pub screen: c_int,
95}
96
97impl XcbDisplayHandle {
98    /// Create a new handle to a connection and screen.
99    ///
100    ///
101    /// # Example
102    ///
103    /// ```
104    /// # use core::ffi::c_void;
105    /// # use core::ptr::NonNull;
106    /// # use raw_window_handle::XcbDisplayHandle;
107    /// #
108    /// let connection: NonNull<c_void>;
109    /// let screen;
110    /// # connection = NonNull::from(&()).cast();
111    /// # screen = 0;
112    /// let handle = XcbDisplayHandle::new(Some(connection), screen);
113    /// ```
114    pub fn new(connection: Option<NonNull<c_void>>, screen: c_int) -> Self {
115        Self { connection, screen }
116    }
117}
118
119/// Raw window handle for Xcb.
120#[non_exhaustive]
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
122pub struct XcbWindowHandle {
123    /// An X11 `xcb_window_t`.
124    pub window: NonZeroU32, // Based on xproto.h
125    /// An X11 `xcb_visualid_t`.
126    pub visual_id: Option<NonZeroU32>,
127}
128
129impl XcbWindowHandle {
130    /// Create a new handle to a window.
131    ///
132    ///
133    /// # Example
134    ///
135    /// ```
136    /// # use core::num::NonZeroU32;
137    /// # use raw_window_handle::XcbWindowHandle;
138    /// #
139    /// let window: NonZeroU32;
140    /// # window = NonZeroU32::new(1).unwrap();
141    /// let mut handle = XcbWindowHandle::new(window);
142    /// // Optionally set the visual ID.
143    /// handle.visual_id = None;
144    /// ```
145    pub fn new(window: NonZeroU32) -> Self {
146        Self {
147            window,
148            visual_id: None,
149        }
150    }
151}
152
153/// Raw display handle for Wayland.
154#[non_exhaustive]
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
156pub struct WaylandDisplayHandle {
157    /// A pointer to a `wl_display`.
158    pub display: NonNull<c_void>,
159}
160
161impl WaylandDisplayHandle {
162    /// Create a new display handle.
163    ///
164    ///
165    /// # Example
166    ///
167    /// ```
168    /// # use core::ffi::c_void;
169    /// # use core::ptr::NonNull;
170    /// # use raw_window_handle::WaylandDisplayHandle;
171    /// #
172    /// let display: NonNull<c_void>;
173    /// # display = NonNull::from(&()).cast();
174    /// let handle = WaylandDisplayHandle::new(display);
175    /// ```
176    pub fn new(display: NonNull<c_void>) -> Self {
177        Self { display }
178    }
179}
180
181/// Raw window handle for Wayland.
182#[non_exhaustive]
183#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
184pub struct WaylandWindowHandle {
185    /// A pointer to a `wl_surface`.
186    pub surface: NonNull<c_void>,
187}
188
189impl WaylandWindowHandle {
190    /// Create a new handle to a surface.
191    ///
192    ///
193    /// # Example
194    ///
195    /// ```
196    /// # use core::ffi::c_void;
197    /// # use core::ptr::NonNull;
198    /// # use raw_window_handle::WaylandWindowHandle;
199    /// #
200    /// let surface: NonNull<c_void>;
201    /// # surface = NonNull::from(&()).cast();
202    /// let handle = WaylandWindowHandle::new(surface);
203    /// ```
204    pub fn new(surface: NonNull<c_void>) -> Self {
205        Self { surface }
206    }
207}
208
209/// Raw display handle for the Linux Kernel Mode Set/Direct Rendering Manager.
210#[non_exhaustive]
211#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
212pub struct DrmDisplayHandle {
213    /// The drm file descriptor.
214    // TODO: Use `std::os::fd::RawFd`?
215    pub fd: i32,
216}
217
218impl DrmDisplayHandle {
219    /// Create a new handle to a file descriptor.
220    ///
221    ///
222    /// # Example
223    ///
224    /// ```
225    /// # use raw_window_handle::DrmDisplayHandle;
226    /// #
227    /// let fd: i32;
228    /// # fd = 0;
229    /// let handle = DrmDisplayHandle::new(fd);
230    /// ```
231    pub fn new(fd: i32) -> Self {
232        Self { fd }
233    }
234}
235
236/// Raw window handle for the Linux Kernel Mode Set/Direct Rendering Manager.
237#[non_exhaustive]
238#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
239pub struct DrmWindowHandle {
240    /// The primary drm plane handle.
241    pub plane: u32,
242}
243
244impl DrmWindowHandle {
245    /// Create a new handle to a plane.
246    ///
247    ///
248    /// # Example
249    ///
250    /// ```
251    /// # use raw_window_handle::DrmWindowHandle;
252    /// #
253    /// let plane: u32;
254    /// # plane = 0;
255    /// let handle = DrmWindowHandle::new(plane);
256    /// ```
257    pub fn new(plane: u32) -> Self {
258        Self { plane }
259    }
260}
261
262/// Raw display handle for the Linux Generic Buffer Manager.
263#[non_exhaustive]
264#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
265pub struct GbmDisplayHandle {
266    /// The gbm device.
267    pub gbm_device: NonNull<c_void>,
268}
269
270impl GbmDisplayHandle {
271    /// Create a new handle to a device.
272    ///
273    ///
274    /// # Example
275    ///
276    /// ```
277    /// # use core::ffi::c_void;
278    /// # use core::ptr::NonNull;
279    /// # use raw_window_handle::GbmDisplayHandle;
280    /// #
281    /// let ptr: NonNull<c_void>;
282    /// # ptr = NonNull::from(&()).cast();
283    /// let handle = GbmDisplayHandle::new(ptr);
284    /// ```
285    pub fn new(gbm_device: NonNull<c_void>) -> Self {
286        Self { gbm_device }
287    }
288}
289
290/// Raw window handle for the Linux Generic Buffer Manager.
291#[non_exhaustive]
292#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
293pub struct GbmWindowHandle {
294    /// The gbm surface.
295    pub gbm_surface: NonNull<c_void>,
296}
297
298impl GbmWindowHandle {
299    /// Create a new handle to a surface.
300    ///
301    ///
302    /// # Example
303    ///
304    /// ```
305    /// # use core::ffi::c_void;
306    /// # use core::ptr::NonNull;
307    /// # use raw_window_handle::GbmWindowHandle;
308    /// #
309    /// let ptr: NonNull<c_void>;
310    /// # ptr = NonNull::from(&()).cast();
311    /// let handle = GbmWindowHandle::new(ptr);
312    /// ```
313    pub fn new(gbm_surface: NonNull<c_void>) -> Self {
314        Self { gbm_surface }
315    }
316}