use super::__private as sealed;
use crate::event_loop::{EventLoopBuilder, EventLoopWindowTarget};
use crate::sync::ThreadSafety;
use crate::window::{Window, WindowBuilder};
use std::os::raw;
use winit::dpi::Size;
use winit::platform::x11::{EventLoopBuilderExtX11 as _, WindowExtX11 as _};
#[doc(inline)]
pub use winit::platform::x11::{register_xlib_error_hook, XWindowType, XlibErrorHook};
pub trait EventLoopWindowTargetExtX11: sealed::EventLoopWindowTargetPrivate {
fn is_x11(&self) -> bool;
}
impl<TS: ThreadSafety> EventLoopWindowTargetExtX11 for EventLoopWindowTarget<TS> {
#[inline]
fn is_x11(&self) -> bool {
!self.is_wayland
}
}
pub trait EventLoopBuilderExtX11: sealed::EventLoopBuilderPrivate {
fn with_x11(&mut self) -> &mut Self;
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
}
impl EventLoopBuilderExtX11 for EventLoopBuilder {
#[inline]
fn with_x11(&mut self) -> &mut Self {
self.inner.with_x11();
self
}
#[inline]
fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
self.inner.with_any_thread(any_thread);
self
}
}
pub trait WindowExtX11: sealed::WindowPrivate {
fn xlib_window(&self) -> Option<raw::c_ulong>;
fn xlib_display(&self) -> Option<*mut raw::c_void>;
fn xlib_screen_id(&self) -> Option<raw::c_int>;
fn xcb_connection(&self) -> Option<*mut raw::c_void>;
}
impl<TS: ThreadSafety> WindowExtX11 for Window<TS> {
fn xcb_connection(&self) -> Option<*mut raw::c_void> {
self.window().xcb_connection()
}
fn xlib_display(&self) -> Option<*mut raw::c_void> {
self.window().xlib_display()
}
fn xlib_screen_id(&self) -> Option<raw::c_int> {
self.window().xlib_screen_id()
}
fn xlib_window(&self) -> Option<raw::c_ulong> {
self.window().xlib_window()
}
}
pub trait WindowBuilderExtX11: sealed::WindowBuilderPrivate {
fn with_x11_screen(self, screen_id: i32) -> Self;
fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
fn with_override_redirect(self, override_redirect: bool) -> Self;
fn with_x11_window_type(self, x11_window_type: Vec<XWindowType>) -> Self;
fn with_base_size<S: Into<Size>>(self, base_size: S) -> Self;
}
impl WindowBuilderExtX11 for WindowBuilder {
fn with_x11_screen(mut self, screen_id: i32) -> Self {
self.platform.set_x11_screen_id(screen_id);
self
}
fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
self.platform
.set_x11_name((general.into(), instance.into()));
self
}
fn with_override_redirect(mut self, override_redirect: bool) -> Self {
self.platform.set_x11_override_redirect(override_redirect);
self
}
fn with_x11_window_type(mut self, x11_window_type: Vec<XWindowType>) -> Self {
self.platform.set_x11_window_type(x11_window_type);
self
}
fn with_base_size<S: Into<Size>>(mut self, base_size: S) -> Self {
self.platform.set_x11_base_size(base_size.into());
self
}
}
#[derive(Default)]
pub(crate) struct PlatformSpecific {
pub x11_window_type: Vec<XWindowType>,
pub x11_name: Option<(String, String)>,
pub x11_screen_id: Option<i32>,
pub x11_override_redirect: bool,
pub x11_base_size: Option<Size>,
}
impl PlatformSpecific {
pub(crate) fn set_x11_window_type(&mut self, x11_window_type: Vec<XWindowType>) {
self.x11_window_type = x11_window_type;
}
pub(crate) fn set_x11_name(&mut self, x11_name: (String, String)) {
self.x11_name = Some(x11_name);
}
pub(crate) fn set_x11_screen_id(&mut self, x11_screen_id: i32) {
self.x11_screen_id = Some(x11_screen_id);
}
pub(crate) fn set_x11_override_redirect(&mut self, x11_override_redirect: bool) {
self.x11_override_redirect = x11_override_redirect;
}
pub(crate) fn set_x11_base_size(&mut self, x11_base_size: Size) {
self.x11_base_size = Some(x11_base_size);
}
pub(crate) fn apply_to(
self,
window_builder: winit::window::WindowBuilder,
) -> winit::window::WindowBuilder {
use winit::platform::x11::WindowBuilderExtX11 as _;
let mut window_builder = window_builder;
if let Some(screen_id) = self.x11_screen_id {
window_builder = window_builder.with_x11_screen(screen_id);
}
if self.x11_override_redirect {
window_builder = window_builder.with_override_redirect(true);
}
if !self.x11_window_type.is_empty() {
window_builder = window_builder.with_x11_window_type(self.x11_window_type);
}
if let Some(base_size) = self.x11_base_size {
window_builder = window_builder.with_base_size(base_size);
}
window_builder
}
}