winit/platform_impl/linux/x11/
xdisplay.rs1use std::collections::HashMap;
2use std::error::Error;
3use std::sync::atomic::{AtomicU32, Ordering};
4use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard};
5use std::{fmt, ptr};
6
7use crate::window::CursorIcon;
8
9use super::atoms::Atoms;
10use super::ffi;
11use super::monitor::MonitorHandle;
12use x11rb::connection::Connection;
13use x11rb::protocol::randr::ConnectionExt as _;
14use x11rb::protocol::xproto::{self, ConnectionExt};
15use x11rb::resource_manager;
16use x11rb::xcb_ffi::XCBConnection;
17
18pub struct XConnection {
20 pub xlib: ffi::Xlib,
21 pub xcursor: ffi::Xcursor,
22
23 pub xinput2: ffi::XInput2,
26
27 pub display: *mut ffi::Display,
28
29 xcb: Option<XCBConnection>,
33
34 atoms: Box<Atoms>,
39
40 default_screen: usize,
42
43 timestamp: AtomicU32,
45
46 pub monitor_handles: Mutex<Option<Vec<MonitorHandle>>>,
48
49 database: RwLock<resource_manager::Database>,
51
52 randr_version: (u32, u32),
54
55 xsettings_screen: Option<xproto::Atom>,
57
58 pub latest_error: Mutex<Option<XError>>,
59 pub cursor_cache: Mutex<HashMap<Option<CursorIcon>, ffi::Cursor>>,
60}
61
62unsafe impl Send for XConnection {}
63unsafe impl Sync for XConnection {}
64
65pub type XErrorHandler =
66 Option<unsafe extern "C" fn(*mut ffi::Display, *mut ffi::XErrorEvent) -> std::os::raw::c_int>;
67
68impl XConnection {
69 pub fn new(error_handler: XErrorHandler) -> Result<XConnection, XNotSupported> {
70 let xlib = ffi::Xlib::open()?;
72 let xcursor = ffi::Xcursor::open()?;
73 let xlib_xcb = ffi::Xlib_xcb::open()?;
74 let xinput2 = ffi::XInput2::open()?;
75
76 unsafe { (xlib.XInitThreads)() };
77 unsafe { (xlib.XSetErrorHandler)(error_handler) };
78
79 let display = unsafe {
81 let display = (xlib.XOpenDisplay)(ptr::null());
82 if display.is_null() {
83 return Err(XNotSupported::XOpenDisplayFailed);
84 }
85 display
86 };
87
88 let xcb = {
90 let xcb_connection =
92 unsafe { (xlib_xcb.XGetXCBConnection)(display as *mut ffi::Display) };
93 assert!(!xcb_connection.is_null());
94
95 let conn =
97 unsafe { XCBConnection::from_raw_xcb_connection(xcb_connection.cast(), false) };
98
99 conn.map_err(|e| XNotSupported::XcbConversionError(Arc::new(WrapConnectError(e))))?
100 };
101
102 let default_screen = unsafe { (xlib.XDefaultScreen)(display) } as usize;
104
105 let database = resource_manager::new_from_default(&xcb)
107 .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;
108
109 let randr_version = xcb
111 .randr_query_version(1, 3)
112 .expect("failed to request XRandR version")
113 .reply()
114 .expect("failed to query XRandR version");
115
116 let xsettings_screen = Self::new_xsettings_screen(&xcb, default_screen);
117 if xsettings_screen.is_none() {
118 tracing::warn!("error setting XSETTINGS; Xft options won't reload automatically")
119 }
120
121 let atoms = Atoms::new(&xcb)
123 .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?
124 .reply()
125 .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;
126
127 Ok(XConnection {
128 xlib,
129 xcursor,
130 xinput2,
131 display,
132 xcb: Some(xcb),
133 atoms: Box::new(atoms),
134 default_screen,
135 timestamp: AtomicU32::new(0),
136 latest_error: Mutex::new(None),
137 monitor_handles: Mutex::new(None),
138 database: RwLock::new(database),
139 cursor_cache: Default::default(),
140 randr_version: (randr_version.major_version, randr_version.minor_version),
141 xsettings_screen,
142 })
143 }
144
145 fn new_xsettings_screen(xcb: &XCBConnection, default_screen: usize) -> Option<xproto::Atom> {
146 let xsettings_screen = xcb
148 .intern_atom(false, format!("_XSETTINGS_S{default_screen}").as_bytes())
149 .ok()?
150 .reply()
151 .ok()?
152 .atom;
153
154 let selector_window = xcb.get_selection_owner(xsettings_screen).ok()?.reply().ok()?.owner;
158
159 xcb.change_window_attributes(
160 selector_window,
161 &xproto::ChangeWindowAttributesAux::new()
162 .event_mask(xproto::EventMask::PROPERTY_CHANGE),
163 )
164 .ok()?
165 .check()
166 .ok()?;
167
168 Some(xsettings_screen)
169 }
170
171 #[inline]
173 pub fn check_errors(&self) -> Result<(), XError> {
174 let error = self.latest_error.lock().unwrap().take();
175 if let Some(error) = error {
176 Err(error)
177 } else {
178 Ok(())
179 }
180 }
181
182 #[inline]
183 pub fn randr_version(&self) -> (u32, u32) {
184 self.randr_version
185 }
186
187 #[inline]
189 pub fn xcb_connection(&self) -> &XCBConnection {
190 self.xcb.as_ref().expect("xcb_connection somehow called after drop?")
191 }
192
193 #[inline]
195 pub fn atoms(&self) -> &Atoms {
196 &self.atoms
197 }
198
199 #[inline]
201 pub fn default_screen_index(&self) -> usize {
202 self.default_screen
203 }
204
205 #[inline]
207 pub fn default_root(&self) -> &xproto::Screen {
208 &self.xcb_connection().setup().roots[self.default_screen]
209 }
210
211 #[inline]
213 pub fn database(&self) -> RwLockReadGuard<'_, resource_manager::Database> {
214 self.database.read().unwrap_or_else(|e| e.into_inner())
215 }
216
217 #[inline]
219 pub fn reload_database(&self) -> Result<(), super::X11Error> {
220 let database = resource_manager::new_from_default(self.xcb_connection())?;
221 *self.database.write().unwrap_or_else(|e| e.into_inner()) = database;
222 Ok(())
223 }
224
225 #[inline]
227 pub fn timestamp(&self) -> u32 {
228 self.timestamp.load(Ordering::Relaxed)
229 }
230
231 #[inline]
233 pub fn set_timestamp(&self, timestamp: u32) {
234 let mut last_timestamp = self.timestamp.load(Ordering::Relaxed);
236 loop {
237 if (timestamp as i32).wrapping_sub(last_timestamp as i32) <= 0 {
238 break;
239 }
240
241 match self.timestamp.compare_exchange(
242 last_timestamp,
243 timestamp,
244 Ordering::Relaxed,
245 Ordering::Relaxed,
246 ) {
247 Ok(_) => break,
248 Err(x) => last_timestamp = x,
249 }
250 }
251 }
252
253 #[inline]
255 pub fn xsettings_screen(&self) -> Option<xproto::Atom> {
256 self.xsettings_screen
257 }
258}
259
260impl fmt::Debug for XConnection {
261 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
262 self.display.fmt(f)
263 }
264}
265
266impl Drop for XConnection {
267 #[inline]
268 fn drop(&mut self) {
269 self.xcb = None;
270 unsafe { (self.xlib.XCloseDisplay)(self.display) };
271 }
272}
273
274#[derive(Debug, Clone)]
276pub struct XError {
277 pub description: String,
278 pub error_code: u8,
279 pub request_code: u8,
280 pub minor_code: u8,
281}
282
283impl Error for XError {}
284
285impl fmt::Display for XError {
286 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
287 write!(
288 formatter,
289 "X error: {} (code: {}, request code: {}, minor code: {})",
290 self.description, self.error_code, self.request_code, self.minor_code
291 )
292 }
293}
294
295#[derive(Clone, Debug)]
297pub enum XNotSupported {
298 LibraryOpenError(ffi::OpenError),
300
301 XOpenDisplayFailed, XcbConversionError(Arc<dyn Error + Send + Sync + 'static>),
306}
307
308impl From<ffi::OpenError> for XNotSupported {
309 #[inline]
310 fn from(err: ffi::OpenError) -> XNotSupported {
311 XNotSupported::LibraryOpenError(err)
312 }
313}
314
315impl XNotSupported {
316 fn description(&self) -> &'static str {
317 match self {
318 XNotSupported::LibraryOpenError(_) => "Failed to load one of xlib's shared libraries",
319 XNotSupported::XOpenDisplayFailed => "Failed to open connection to X server",
320 XNotSupported::XcbConversionError(_) => "Failed to convert Xlib connection to XCB",
321 }
322 }
323}
324
325impl Error for XNotSupported {
326 #[inline]
327 fn source(&self) -> Option<&(dyn Error + 'static)> {
328 match *self {
329 XNotSupported::LibraryOpenError(ref err) => Some(err),
330 XNotSupported::XcbConversionError(ref err) => Some(&**err),
331 _ => None,
332 }
333 }
334}
335
336impl fmt::Display for XNotSupported {
337 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
338 formatter.write_str(self.description())
339 }
340}
341
342#[derive(Debug)]
346struct WrapConnectError(x11rb::rust_connection::ConnectError);
347
348impl fmt::Display for WrapConnectError {
349 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
350 fmt::Display::fmt(&self.0, f)
351 }
352}
353
354impl Error for WrapConnectError {
355 }