2020-06-15 17:15:27 +10:00
|
|
|
use std::os::raw;
|
2023-01-10 19:46:48 +11:00
|
|
|
use std::ptr;
|
2018-06-15 09:42:18 +10:00
|
|
|
|
2019-06-22 01:33:15 +10:00
|
|
|
use crate::{
|
2022-02-17 08:09:03 +11:00
|
|
|
event_loop::{EventLoopBuilder, EventLoopWindowTarget},
|
2019-06-22 01:33:15 +10:00
|
|
|
monitor::MonitorHandle,
|
|
|
|
window::{Window, WindowBuilder},
|
|
|
|
};
|
2019-02-21 20:51:43 +11:00
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
use crate::dpi::Size;
|
2019-06-18 04:27:00 +10:00
|
|
|
use crate::platform_impl::{
|
2023-01-10 19:46:48 +11:00
|
|
|
x11::ffi::XVisualInfo, ApplicationName, Backend, Window as LinuxWindow, XLIB_ERROR_HOOKS,
|
2018-06-15 09:42:18 +10:00
|
|
|
};
|
2019-04-28 02:06:51 +10:00
|
|
|
|
2019-06-22 01:33:15 +10:00
|
|
|
pub use crate::platform_impl::{x11::util::WindowType as XWindowType, XNotSupported};
|
2017-03-15 20:11:43 +11:00
|
|
|
|
2022-07-26 23:02:09 +10:00
|
|
|
/// The first argument in the provided hook will be the pointer to `XDisplay`
|
|
|
|
/// and the second one the pointer to [`XErrorEvent`]. The returned `bool` is an
|
|
|
|
/// indicator whether the error was handled by the callback.
|
|
|
|
///
|
|
|
|
/// [`XErrorEvent`]: https://linux.die.net/man/3/xerrorevent
|
2022-07-23 03:21:28 +10:00
|
|
|
pub type XlibErrorHook =
|
|
|
|
Box<dyn Fn(*mut std::ffi::c_void, *mut std::ffi::c_void) -> bool + Send + Sync>;
|
|
|
|
|
|
|
|
/// Hook to winit's xlib error handling callback.
|
|
|
|
///
|
|
|
|
/// This method is provided as a safe way to handle the errors comming from X11 when using xlib
|
|
|
|
/// in external crates, like glutin for GLX access. Trying to handle errors by speculating with
|
|
|
|
/// `XSetErrorHandler` is [`unsafe`].
|
|
|
|
///
|
|
|
|
/// [`unsafe`]: https://www.remlab.net/op/xlib.shtml
|
|
|
|
#[inline]
|
|
|
|
pub fn register_xlib_error_hook(hook: XlibErrorHook) {
|
|
|
|
// Append new hook.
|
|
|
|
unsafe {
|
2022-09-01 02:32:19 +10:00
|
|
|
XLIB_ERROR_HOOKS.lock().unwrap().push(hook);
|
2022-07-23 03:21:28 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
/// Additional methods on [`EventLoopWindowTarget`] that are specific to X11.
|
|
|
|
pub trait EventLoopWindowTargetExtX11 {
|
2022-06-12 02:57:19 +10:00
|
|
|
/// True if the [`EventLoopWindowTarget`] uses X11.
|
2017-09-23 17:36:30 +10:00
|
|
|
fn is_x11(&self) -> bool;
|
2017-09-01 19:04:57 +10:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
impl<T> EventLoopWindowTargetExtX11 for EventLoopWindowTarget<T> {
|
2017-09-23 17:36:30 +10:00
|
|
|
#[inline]
|
|
|
|
fn is_x11(&self) -> bool {
|
2019-08-09 07:50:22 +10:00
|
|
|
!self.p.is_wayland()
|
2017-09-23 17:36:30 +10:00
|
|
|
}
|
2017-01-08 00:34:38 +11:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
/// Additional methods on [`EventLoopBuilder`] that are specific to X11.
|
|
|
|
pub trait EventLoopBuilderExtX11 {
|
2022-02-17 08:09:03 +11:00
|
|
|
/// Force using X11.
|
|
|
|
fn with_x11(&mut self) -> &mut Self;
|
2019-08-09 07:50:22 +10:00
|
|
|
|
2022-02-17 08:09:03 +11:00
|
|
|
/// Whether to allow the event loop to be created off of the main thread.
|
2019-10-19 02:51:06 +11:00
|
|
|
///
|
2022-02-17 08:09:03 +11:00
|
|
|
/// By default, the window is only allowed to be created on the main
|
|
|
|
/// thread, to make platform compatibility easier.
|
|
|
|
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
|
2019-10-19 02:51:06 +11:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
impl<T> EventLoopBuilderExtX11 for EventLoopBuilder<T> {
|
2019-10-19 02:51:06 +11:00
|
|
|
#[inline]
|
2022-02-17 08:09:03 +11:00
|
|
|
fn with_x11(&mut self) -> &mut Self {
|
|
|
|
self.platform_specific.forced_backend = Some(Backend::X);
|
|
|
|
self
|
2019-10-19 02:51:06 +11:00
|
|
|
}
|
|
|
|
|
2019-08-09 07:50:22 +10:00
|
|
|
#[inline]
|
2022-02-17 08:09:03 +11:00
|
|
|
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
|
|
|
|
self.platform_specific.any_thread = any_thread;
|
|
|
|
self
|
2019-08-09 07:50:22 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
/// Additional methods on [`Window`] that are specific to X11.
|
|
|
|
pub trait WindowExtX11 {
|
2022-06-12 02:57:19 +10:00
|
|
|
/// Returns the ID of the [`Window`] xlib object that is used by this window.
|
2015-09-26 02:04:55 +10:00
|
|
|
///
|
|
|
|
/// Returns `None` if the window doesn't use xlib (if it uses wayland for example).
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xlib_window(&self) -> Option<raw::c_ulong>;
|
2015-09-26 02:04:55 +10:00
|
|
|
|
|
|
|
/// Returns a pointer to the `Display` object of xlib that is used by this window.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the window doesn't use xlib (if it uses wayland for example).
|
|
|
|
///
|
2022-06-12 02:57:19 +10:00
|
|
|
/// The pointer will become invalid when the [`Window`] is destroyed.
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xlib_display(&self) -> Option<*mut raw::c_void>;
|
2016-10-20 03:11:02 +11:00
|
|
|
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xlib_screen_id(&self) -> Option<raw::c_int>;
|
2016-10-20 03:11:02 +11:00
|
|
|
|
2016-07-31 07:58:28 +10:00
|
|
|
/// This function returns the underlying `xcb_connection_t` of an xlib `Display`.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the window doesn't use xlib (if it uses wayland for example).
|
|
|
|
///
|
2022-06-12 02:57:19 +10:00
|
|
|
/// The pointer will become invalid when the [`Window`] is destroyed.
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xcb_connection(&self) -> Option<*mut raw::c_void>;
|
2015-09-26 02:04:55 +10:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
impl WindowExtX11 for Window {
|
2015-09-26 02:04:55 +10:00
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xlib_window(&self) -> Option<raw::c_ulong> {
|
2017-03-04 07:41:51 +11:00
|
|
|
match self.window {
|
2019-05-30 11:29:54 +10:00
|
|
|
LinuxWindow::X(ref w) => Some(w.xlib_window()),
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wayland_platform)]
|
2019-06-22 01:33:15 +10:00
|
|
|
_ => None,
|
2015-09-26 02:04:55 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xlib_display(&self) -> Option<*mut raw::c_void> {
|
2017-03-04 07:41:51 +11:00
|
|
|
match self.window {
|
2019-05-30 11:29:54 +10:00
|
|
|
LinuxWindow::X(ref w) => Some(w.xlib_display()),
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wayland_platform)]
|
2019-06-22 01:33:15 +10:00
|
|
|
_ => None,
|
2015-09-26 02:04:55 +10:00
|
|
|
}
|
|
|
|
}
|
2016-05-08 17:28:54 +10:00
|
|
|
|
2018-05-21 00:47:22 +10:00
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
fn xlib_screen_id(&self) -> Option<raw::c_int> {
|
2017-03-04 07:41:51 +11:00
|
|
|
match self.window {
|
2019-05-30 11:29:54 +10:00
|
|
|
LinuxWindow::X(ref w) => Some(w.xlib_screen_id()),
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wayland_platform)]
|
2019-06-22 01:33:15 +10:00
|
|
|
_ => None,
|
2016-10-20 03:11:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 00:47:22 +10:00
|
|
|
#[inline]
|
2019-09-24 00:10:33 +10:00
|
|
|
fn xcb_connection(&self) -> Option<*mut raw::c_void> {
|
|
|
|
match self.window {
|
|
|
|
LinuxWindow::X(ref w) => Some(w.xcb_connection()),
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(wayland_platform)]
|
2019-09-24 00:10:33 +10:00
|
|
|
_ => None,
|
2019-04-28 02:06:51 +10:00
|
|
|
}
|
2018-05-21 00:47:22 +10:00
|
|
|
}
|
2015-09-26 02:04:55 +10:00
|
|
|
}
|
2016-01-08 02:01:18 +11:00
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
/// Additional methods on [`WindowBuilder`] that are specific to X11.
|
|
|
|
pub trait WindowBuilderExtX11 {
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_x11_visual<T>(self, visual_infos: *const T) -> Self;
|
2022-04-20 08:56:56 +10:00
|
|
|
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_x11_screen(self, screen_id: i32) -> Self;
|
2018-05-03 23:41:11 +10:00
|
|
|
|
2022-04-20 08:56:56 +10:00
|
|
|
/// Build window with the given `general` and `instance` names.
|
|
|
|
///
|
2022-09-01 15:05:32 +10:00
|
|
|
/// The `general` sets general class of `WM_CLASS(STRING)`, while `instance` set the
|
2022-04-20 08:56:56 +10:00
|
|
|
/// instance part of it. The resulted property looks like `WM_CLASS(STRING) = "general", "instance"`.
|
|
|
|
///
|
|
|
|
/// For details about application ID conventions, see the
|
|
|
|
/// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
|
|
|
|
fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
|
|
|
|
|
2018-05-21 00:47:22 +10:00
|
|
|
/// Build window with override-redirect flag; defaults to false. Only relevant on X11.
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_override_redirect(self, override_redirect: bool) -> Self;
|
2022-04-20 08:56:56 +10:00
|
|
|
|
2019-09-24 00:10:33 +10:00
|
|
|
/// Build window with `_NET_WM_WINDOW_TYPE` hints; defaults to `Normal`. Only relevant on X11.
|
|
|
|
fn with_x11_window_type(self, x11_window_type: Vec<XWindowType>) -> Self;
|
2022-04-20 08:56:56 +10:00
|
|
|
|
2018-05-03 23:41:11 +10:00
|
|
|
/// Build window with base size hint. Only implemented on X11.
|
2022-01-02 14:56:13 +11:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use winit::dpi::{LogicalSize, PhysicalSize};
|
|
|
|
/// # use winit::window::WindowBuilder;
|
2022-09-01 15:05:32 +10:00
|
|
|
/// # use winit::platform::x11::WindowBuilderExtX11;
|
2022-01-02 14:56:13 +11:00
|
|
|
/// // Specify the size in logical dimensions like this:
|
|
|
|
/// WindowBuilder::new().with_base_size(LogicalSize::new(400.0, 200.0));
|
|
|
|
///
|
|
|
|
/// // Or specify the size in physical dimensions like this:
|
|
|
|
/// WindowBuilder::new().with_base_size(PhysicalSize::new(400, 200));
|
|
|
|
/// ```
|
2020-01-04 17:14:11 +11:00
|
|
|
fn with_base_size<S: Into<Size>>(self, base_size: S) -> Self;
|
2016-01-08 02:01:18 +11:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
impl WindowBuilderExtX11 for WindowBuilder {
|
2017-01-07 07:59:35 +11:00
|
|
|
#[inline]
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_x11_visual<T>(mut self, visual_infos: *const T) -> Self {
|
2020-06-15 17:15:27 +10:00
|
|
|
{
|
|
|
|
self.platform_specific.visual_infos =
|
|
|
|
Some(unsafe { ptr::read(visual_infos as *const XVisualInfo) });
|
|
|
|
}
|
2017-01-07 07:59:35 +11:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_x11_screen(mut self, screen_id: i32) -> Self {
|
2017-01-07 07:59:35 +11:00
|
|
|
self.platform_specific.screen_id = Some(screen_id);
|
|
|
|
self
|
|
|
|
}
|
2018-05-03 23:41:11 +10:00
|
|
|
|
2018-05-21 00:47:22 +10:00
|
|
|
#[inline]
|
2022-04-20 08:56:56 +10:00
|
|
|
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
|
|
|
|
self.platform_specific.name = Some(ApplicationName::new(general.into(), instance.into()));
|
2018-05-21 00:47:22 +10:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-10-10 07:12:23 +11:00
|
|
|
#[inline]
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_override_redirect(mut self, override_redirect: bool) -> Self {
|
2018-05-21 00:47:22 +10:00
|
|
|
self.platform_specific.override_redirect = override_redirect;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-28 02:06:51 +10:00
|
|
|
#[inline]
|
2019-09-24 00:10:33 +10:00
|
|
|
fn with_x11_window_type(mut self, x11_window_types: Vec<XWindowType>) -> Self {
|
|
|
|
self.platform_specific.x11_window_types = x11_window_types;
|
2019-04-28 02:06:51 +10:00
|
|
|
self
|
|
|
|
}
|
2018-05-21 00:47:22 +10:00
|
|
|
|
2018-09-21 07:00:04 +10:00
|
|
|
#[inline]
|
2020-01-04 17:14:11 +11:00
|
|
|
fn with_base_size<S: Into<Size>>(mut self, base_size: S) -> Self {
|
2019-09-24 00:10:33 +10:00
|
|
|
self.platform_specific.base_size = Some(base_size.into());
|
2018-09-21 07:00:04 +10:00
|
|
|
self
|
|
|
|
}
|
2016-01-08 02:01:18 +11:00
|
|
|
}
|
2017-08-30 16:49:18 +10:00
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
/// Additional methods on `MonitorHandle` that are specific to X11.
|
|
|
|
pub trait MonitorHandleExtX11 {
|
2017-08-30 16:49:18 +10:00
|
|
|
/// Returns the inner identifier of the monitor.
|
|
|
|
fn native_id(&self) -> u32;
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:05:32 +10:00
|
|
|
impl MonitorHandleExtX11 for MonitorHandle {
|
2017-08-30 16:49:18 +10:00
|
|
|
#[inline]
|
|
|
|
fn native_id(&self) -> u32 {
|
2019-05-30 11:29:54 +10:00
|
|
|
self.inner.native_identifier()
|
2017-08-30 16:49:18 +10:00
|
|
|
}
|
|
|
|
}
|