rio_winit_fork/platform/
x11.rs

1//! # X11
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5use crate::event_loop::{ActiveEventLoop, EventLoopBuilder};
6use crate::monitor::MonitorHandle;
7use crate::window::{Window, WindowAttributes};
8
9use crate::dpi::Size;
10
11/// X window type. Maps directly to
12/// [`_NET_WM_WINDOW_TYPE`](https://specifications.freedesktop.org/wm-spec/wm-spec-1.5.html).
13#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15pub enum WindowType {
16    /// A desktop feature. This can include a single window containing desktop icons with the same
17    /// dimensions as the screen, allowing the desktop environment to have full control of the
18    /// desktop, without the need for proxying root window clicks.
19    Desktop,
20    /// A dock or panel feature. Typically a Window Manager would keep such windows on top of all
21    /// other windows.
22    Dock,
23    /// Toolbar windows. "Torn off" from the main application.
24    Toolbar,
25    /// Pinnable menu windows. "Torn off" from the main application.
26    Menu,
27    /// A small persistent utility window, such as a palette or toolbox.
28    Utility,
29    /// The window is a splash screen displayed as an application is starting up.
30    Splash,
31    /// This is a dialog window.
32    Dialog,
33    /// A dropdown menu that usually appears when the user clicks on an item in a menu bar.
34    /// This property is typically used on override-redirect windows.
35    DropdownMenu,
36    /// A popup menu that usually appears when the user right clicks on an object.
37    /// This property is typically used on override-redirect windows.
38    PopupMenu,
39    /// A tooltip window. Usually used to show additional information when hovering over an object
40    /// with the cursor. This property is typically used on override-redirect windows.
41    Tooltip,
42    /// The window is a notification.
43    /// This property is typically used on override-redirect windows.
44    Notification,
45    /// This should be used on the windows that are popped up by combo boxes.
46    /// This property is typically used on override-redirect windows.
47    Combo,
48    /// This indicates the window is being dragged.
49    /// This property is typically used on override-redirect windows.
50    Dnd,
51    /// This is a normal, top-level window.
52    #[default]
53    Normal,
54}
55
56/// The first argument in the provided hook will be the pointer to `XDisplay`
57/// and the second one the pointer to [`XErrorEvent`]. The returned `bool` is an
58/// indicator whether the error was handled by the callback.
59///
60/// [`XErrorEvent`]: https://linux.die.net/man/3/xerrorevent
61pub type XlibErrorHook =
62    Box<dyn Fn(*mut std::ffi::c_void, *mut std::ffi::c_void) -> bool + Send + Sync>;
63
64/// A unique identifier for an X11 visual.
65pub type XVisualID = u32;
66
67/// A unique identifier for an X11 window.
68pub type XWindow = u32;
69
70/// Hook to winit's xlib error handling callback.
71///
72/// This method is provided as a safe way to handle the errors coming from X11
73/// when using xlib in external crates, like glutin for GLX access. Trying to
74/// handle errors by speculating with `XSetErrorHandler` is [`unsafe`].
75///
76/// **Be aware that your hook is always invoked and returning `true` from it will
77/// prevent `winit` from getting the error itself. It's wise to always return
78/// `false` if you're not initiated the `Sync`.**
79///
80/// [`unsafe`]: https://www.remlab.net/op/xlib.shtml
81#[inline]
82pub fn register_xlib_error_hook(hook: XlibErrorHook) {
83    // Append new hook.
84    unsafe {
85        crate::platform_impl::XLIB_ERROR_HOOKS.lock().unwrap().push(hook);
86    }
87}
88
89/// Additional methods on [`ActiveEventLoop`] that are specific to X11.
90pub trait ActiveEventLoopExtX11 {
91    /// True if the [`ActiveEventLoop`] uses X11.
92    fn is_x11(&self) -> bool;
93}
94
95impl ActiveEventLoopExtX11 for ActiveEventLoop {
96    #[inline]
97    fn is_x11(&self) -> bool {
98        !self.p.is_wayland()
99    }
100}
101
102/// Additional methods on [`EventLoopBuilder`] that are specific to X11.
103pub trait EventLoopBuilderExtX11 {
104    /// Force using X11.
105    fn with_x11(&mut self) -> &mut Self;
106
107    /// Whether to allow the event loop to be created off of the main thread.
108    ///
109    /// By default, the window is only allowed to be created on the main
110    /// thread, to make platform compatibility easier.
111    fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
112}
113
114impl<T> EventLoopBuilderExtX11 for EventLoopBuilder<T> {
115    #[inline]
116    fn with_x11(&mut self) -> &mut Self {
117        self.platform_specific.forced_backend = Some(crate::platform_impl::Backend::X);
118        self
119    }
120
121    #[inline]
122    fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
123        self.platform_specific.any_thread = any_thread;
124        self
125    }
126}
127
128/// Additional methods on [`Window`] that are specific to X11.
129pub trait WindowExtX11 {}
130
131impl WindowExtX11 for Window {}
132
133/// Additional methods on [`WindowAttributes`] that are specific to X11.
134pub trait WindowAttributesExtX11 {
135    /// Create this window with a specific X11 visual.
136    fn with_x11_visual(self, visual_id: XVisualID) -> Self;
137
138    fn with_x11_screen(self, screen_id: i32) -> Self;
139
140    /// Build window with the given `general` and `instance` names.
141    ///
142    /// The `general` sets general class of `WM_CLASS(STRING)`, while `instance` set the
143    /// instance part of it. The resulted property looks like `WM_CLASS(STRING) = "instance",
144    /// "general"`.
145    ///
146    /// For details about application ID conventions, see the
147    /// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
148    fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
149
150    /// Build window with override-redirect flag; defaults to false.
151    fn with_override_redirect(self, override_redirect: bool) -> Self;
152
153    /// Build window with `_NET_WM_WINDOW_TYPE` hints; defaults to `Normal`.
154    fn with_x11_window_type(self, x11_window_type: Vec<WindowType>) -> Self;
155
156    /// Build window with base size hint.
157    ///
158    /// ```
159    /// # use winit::dpi::{LogicalSize, PhysicalSize};
160    /// # use winit::window::Window;
161    /// # use winit::platform::x11::WindowAttributesExtX11;
162    /// // Specify the size in logical dimensions like this:
163    /// Window::default_attributes().with_base_size(LogicalSize::new(400.0, 200.0));
164    ///
165    /// // Or specify the size in physical dimensions like this:
166    /// Window::default_attributes().with_base_size(PhysicalSize::new(400, 200));
167    /// ```
168    fn with_base_size<S: Into<Size>>(self, base_size: S) -> Self;
169
170    /// Embed this window into another parent window.
171    ///
172    /// # Example
173    ///
174    /// ```no_run
175    /// use winit::window::Window;
176    /// use winit::event_loop::ActiveEventLoop;
177    /// use winit::platform::x11::{XWindow, WindowAttributesExtX11};
178    /// # fn create_window(event_loop: &ActiveEventLoop) -> Result<(), Box<dyn std::error::Error>> {
179    /// let parent_window_id = std::env::args().nth(1).unwrap().parse::<XWindow>()?;
180    /// let window_attributes = Window::default_attributes().with_embed_parent_window(parent_window_id);
181    /// let window = event_loop.create_window(window_attributes)?;
182    /// # Ok(()) }
183    /// ```
184    fn with_embed_parent_window(self, parent_window_id: XWindow) -> Self;
185}
186
187impl WindowAttributesExtX11 for WindowAttributes {
188    #[inline]
189    fn with_x11_visual(mut self, visual_id: XVisualID) -> Self {
190        self.platform_specific.x11.visual_id = Some(visual_id);
191        self
192    }
193
194    #[inline]
195    fn with_x11_screen(mut self, screen_id: i32) -> Self {
196        self.platform_specific.x11.screen_id = Some(screen_id);
197        self
198    }
199
200    #[inline]
201    fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
202        self.platform_specific.name =
203            Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into()));
204        self
205    }
206
207    #[inline]
208    fn with_override_redirect(mut self, override_redirect: bool) -> Self {
209        self.platform_specific.x11.override_redirect = override_redirect;
210        self
211    }
212
213    #[inline]
214    fn with_x11_window_type(mut self, x11_window_types: Vec<WindowType>) -> Self {
215        self.platform_specific.x11.x11_window_types = x11_window_types;
216        self
217    }
218
219    #[inline]
220    fn with_base_size<S: Into<Size>>(mut self, base_size: S) -> Self {
221        self.platform_specific.x11.base_size = Some(base_size.into());
222        self
223    }
224
225    #[inline]
226    fn with_embed_parent_window(mut self, parent_window_id: XWindow) -> Self {
227        self.platform_specific.x11.embed_window = Some(parent_window_id);
228        self
229    }
230}
231
232/// Additional methods on `MonitorHandle` that are specific to X11.
233pub trait MonitorHandleExtX11 {
234    /// Returns the inner identifier of the monitor.
235    fn native_id(&self) -> u32;
236}
237
238impl MonitorHandleExtX11 for MonitorHandle {
239    #[inline]
240    fn native_id(&self) -> u32 {
241        self.inner.native_identifier()
242    }
243}