async_winit/platform/
x11.rs1use super::__private as sealed;
25use crate::event_loop::{EventLoopBuilder, EventLoopWindowTarget};
26use crate::sync::ThreadSafety;
27use crate::window::{Window, WindowBuilder};
28
29use std::os::raw;
30
31use winit::dpi::Size;
32use winit::platform::x11::{EventLoopBuilderExtX11 as _, WindowExtX11 as _};
33
34#[doc(inline)]
35pub use winit::platform::x11::{register_xlib_error_hook, XWindowType, XlibErrorHook};
36
37pub trait EventLoopWindowTargetExtX11: sealed::EventLoopWindowTargetPrivate {
41 fn is_x11(&self) -> bool;
43}
44
45impl<TS: ThreadSafety> EventLoopWindowTargetExtX11 for EventLoopWindowTarget<TS> {
46 #[inline]
47 fn is_x11(&self) -> bool {
48 !self.is_wayland
49 }
50}
51
52pub trait EventLoopBuilderExtX11: sealed::EventLoopBuilderPrivate {
56 fn with_x11(&mut self) -> &mut Self;
58
59 fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
64}
65
66impl EventLoopBuilderExtX11 for EventLoopBuilder {
67 #[inline]
68 fn with_x11(&mut self) -> &mut Self {
69 self.inner.with_x11();
70 self
71 }
72
73 #[inline]
74 fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
75 self.inner.with_any_thread(any_thread);
76 self
77 }
78}
79
80pub trait WindowExtX11: sealed::WindowPrivate {
84 fn xlib_window(&self) -> Option<raw::c_ulong>;
88
89 fn xlib_display(&self) -> Option<*mut raw::c_void>;
95
96 fn xlib_screen_id(&self) -> Option<raw::c_int>;
97
98 fn xcb_connection(&self) -> Option<*mut raw::c_void>;
104}
105
106impl<TS: ThreadSafety> WindowExtX11 for Window<TS> {
107 fn xcb_connection(&self) -> Option<*mut raw::c_void> {
108 self.window().xcb_connection()
109 }
110
111 fn xlib_display(&self) -> Option<*mut raw::c_void> {
112 self.window().xlib_display()
113 }
114
115 fn xlib_screen_id(&self) -> Option<raw::c_int> {
116 self.window().xlib_screen_id()
117 }
118
119 fn xlib_window(&self) -> Option<raw::c_ulong> {
120 self.window().xlib_window()
121 }
122}
123
124pub trait WindowBuilderExtX11: sealed::WindowBuilderPrivate {
128 fn with_x11_screen(self, screen_id: i32) -> Self;
129
130 fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;
138
139 fn with_override_redirect(self, override_redirect: bool) -> Self;
141
142 fn with_x11_window_type(self, x11_window_type: Vec<XWindowType>) -> Self;
144
145 fn with_base_size<S: Into<Size>>(self, base_size: S) -> Self;
158}
159
160impl WindowBuilderExtX11 for WindowBuilder {
161 fn with_x11_screen(mut self, screen_id: i32) -> Self {
162 self.platform.set_x11_screen_id(screen_id);
163 self
164 }
165
166 fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
167 self.platform
168 .set_x11_name((general.into(), instance.into()));
169 self
170 }
171
172 fn with_override_redirect(mut self, override_redirect: bool) -> Self {
173 self.platform.set_x11_override_redirect(override_redirect);
174 self
175 }
176
177 fn with_x11_window_type(mut self, x11_window_type: Vec<XWindowType>) -> Self {
178 self.platform.set_x11_window_type(x11_window_type);
179 self
180 }
181
182 fn with_base_size<S: Into<Size>>(mut self, base_size: S) -> Self {
183 self.platform.set_x11_base_size(base_size.into());
184 self
185 }
186}
187
188#[derive(Default)]
189pub(crate) struct PlatformSpecific {
190 pub x11_window_type: Vec<XWindowType>,
191 pub x11_name: Option<(String, String)>,
192 pub x11_screen_id: Option<i32>,
193 pub x11_override_redirect: bool,
194 pub x11_base_size: Option<Size>,
195}
196
197impl PlatformSpecific {
198 pub(crate) fn set_x11_window_type(&mut self, x11_window_type: Vec<XWindowType>) {
199 self.x11_window_type = x11_window_type;
200 }
201
202 pub(crate) fn set_x11_name(&mut self, x11_name: (String, String)) {
203 self.x11_name = Some(x11_name);
204 }
205
206 pub(crate) fn set_x11_screen_id(&mut self, x11_screen_id: i32) {
207 self.x11_screen_id = Some(x11_screen_id);
208 }
209
210 pub(crate) fn set_x11_override_redirect(&mut self, x11_override_redirect: bool) {
211 self.x11_override_redirect = x11_override_redirect;
212 }
213
214 pub(crate) fn set_x11_base_size(&mut self, x11_base_size: Size) {
215 self.x11_base_size = Some(x11_base_size);
216 }
217
218 pub(crate) fn apply_to(
219 self,
220 window_builder: winit::window::WindowBuilder,
221 ) -> winit::window::WindowBuilder {
222 use winit::platform::x11::WindowBuilderExtX11 as _;
223
224 let mut window_builder = window_builder;
225 if let Some(screen_id) = self.x11_screen_id {
226 window_builder = window_builder.with_x11_screen(screen_id);
227 }
228 if self.x11_override_redirect {
229 window_builder = window_builder.with_override_redirect(true);
230 }
231 if !self.x11_window_type.is_empty() {
232 window_builder = window_builder.with_x11_window_type(self.x11_window_type);
233 }
234 if let Some(base_size) = self.x11_base_size {
235 window_builder = window_builder.with_base_size(base_size);
236 }
237 window_builder
238 }
239}