winit/platform_impl/linux/x11/
xdisplay.rs

1use 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 x11rb::connection::Connection;
8use x11rb::protocol::randr::ConnectionExt as _;
9use x11rb::protocol::render;
10use x11rb::protocol::xproto::{self, ConnectionExt};
11use x11rb::resource_manager;
12use x11rb::xcb_ffi::XCBConnection;
13
14use super::atoms::Atoms;
15use super::ffi;
16use super::monitor::MonitorHandle;
17use crate::window::CursorIcon;
18
19/// A connection to an X server.
20pub struct XConnection {
21    pub xlib: ffi::Xlib,
22
23    // TODO(notgull): I'd like to remove this, but apparently Xlib and Xinput2 are tied together
24    // for some reason.
25    pub xinput2: ffi::XInput2,
26
27    pub display: *mut ffi::Display,
28
29    /// The manager for the XCB connection.
30    ///
31    /// The `Option` ensures that we can drop it before we close the `Display`.
32    xcb: Option<XCBConnection>,
33
34    /// The atoms used by `winit`.
35    ///
36    /// This is a large structure, so I've elected to Box it to make accessing the fields of
37    /// this struct easier. Feel free to unbox it if you like kicking puppies.
38    atoms: Box<Atoms>,
39
40    /// The index of the default screen.
41    default_screen: usize,
42
43    /// The last timestamp received by this connection.
44    timestamp: AtomicU32,
45
46    /// List of monitor handles.
47    pub monitor_handles: Mutex<Option<Vec<MonitorHandle>>>,
48
49    /// The resource database.
50    database: RwLock<resource_manager::Database>,
51
52    /// RandR version.
53    randr_version: (u32, u32),
54
55    /// Atom for the XSettings screen.
56    xsettings_screen: Option<xproto::Atom>,
57
58    /// XRender format information.
59    render_formats: render::QueryPictFormatsReply,
60
61    pub latest_error: Mutex<Option<XError>>,
62    pub cursor_cache: Mutex<HashMap<Option<CursorIcon>, xproto::Cursor>>,
63}
64
65unsafe impl Send for XConnection {}
66unsafe impl Sync for XConnection {}
67
68pub type XErrorHandler =
69    Option<unsafe extern "C" fn(*mut ffi::Display, *mut ffi::XErrorEvent) -> std::os::raw::c_int>;
70
71impl XConnection {
72    pub fn new(error_handler: XErrorHandler) -> Result<XConnection, XNotSupported> {
73        // opening the libraries
74        let xlib = ffi::Xlib::open()?;
75        let xlib_xcb = ffi::Xlib_xcb::open()?;
76        let xinput2 = ffi::XInput2::open()?;
77
78        unsafe { (xlib.XInitThreads)() };
79        unsafe { (xlib.XSetErrorHandler)(error_handler) };
80
81        // calling XOpenDisplay
82        let display = unsafe {
83            let display = (xlib.XOpenDisplay)(ptr::null());
84            if display.is_null() {
85                return Err(XNotSupported::XOpenDisplayFailed);
86            }
87            display
88        };
89
90        // Open the x11rb XCB connection.
91        let xcb = {
92            // Get a pointer to the underlying XCB connection
93            let xcb_connection =
94                unsafe { (xlib_xcb.XGetXCBConnection)(display as *mut ffi::Display) };
95            assert!(!xcb_connection.is_null());
96
97            // Wrap the XCB connection in an x11rb XCB connection
98            let conn =
99                unsafe { XCBConnection::from_raw_xcb_connection(xcb_connection.cast(), false) };
100
101            conn.map_err(|e| XNotSupported::XcbConversionError(Arc::new(WrapConnectError(e))))?
102        };
103
104        // Get the default screen.
105        let default_screen = unsafe { (xlib.XDefaultScreen)(display) } as usize;
106
107        // Load the database.
108        let database = resource_manager::new_from_default(&xcb)
109            .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;
110
111        // Load the RandR version.
112        let randr_version = xcb
113            .randr_query_version(1, 3)
114            .expect("failed to request XRandR version")
115            .reply()
116            .expect("failed to query XRandR version");
117
118        let xsettings_screen = Self::new_xsettings_screen(&xcb, default_screen);
119        if xsettings_screen.is_none() {
120            tracing::warn!("error setting XSETTINGS; Xft options won't reload automatically")
121        }
122
123        // Start getting the XRender formats.
124        let formats_cookie = render::query_pict_formats(&xcb)
125            .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;
126
127        // Fetch atoms.
128        let atoms = Atoms::new(&xcb)
129            .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?
130            .reply()
131            .map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;
132
133        // Finish getting everything else.
134        let formats =
135            formats_cookie.reply().map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;
136
137        Ok(XConnection {
138            xlib,
139            xinput2,
140            display,
141            xcb: Some(xcb),
142            atoms: Box::new(atoms),
143            default_screen,
144            timestamp: AtomicU32::new(0),
145            latest_error: Mutex::new(None),
146            monitor_handles: Mutex::new(None),
147            database: RwLock::new(database),
148            cursor_cache: Default::default(),
149            randr_version: (randr_version.major_version, randr_version.minor_version),
150            render_formats: formats,
151            xsettings_screen,
152        })
153    }
154
155    fn new_xsettings_screen(xcb: &XCBConnection, default_screen: usize) -> Option<xproto::Atom> {
156        // Fetch the _XSETTINGS_S[screen number] atom.
157        let xsettings_screen = xcb
158            .intern_atom(false, format!("_XSETTINGS_S{}", default_screen).as_bytes())
159            .ok()?
160            .reply()
161            .ok()?
162            .atom;
163
164        // Get PropertyNotify events from the XSETTINGS window.
165        // TODO: The XSETTINGS window here can change. In the future, listen for DestroyNotify on
166        // this window in order to accommodate for a changed window here.
167        let selector_window = xcb.get_selection_owner(xsettings_screen).ok()?.reply().ok()?.owner;
168
169        xcb.change_window_attributes(
170            selector_window,
171            &xproto::ChangeWindowAttributesAux::new()
172                .event_mask(xproto::EventMask::PROPERTY_CHANGE),
173        )
174        .ok()?
175        .check()
176        .ok()?;
177
178        Some(xsettings_screen)
179    }
180
181    /// Checks whether an error has been triggered by the previous function calls.
182    #[inline]
183    pub fn check_errors(&self) -> Result<(), XError> {
184        let error = self.latest_error.lock().unwrap().take();
185        if let Some(error) = error {
186            Err(error)
187        } else {
188            Ok(())
189        }
190    }
191
192    #[inline]
193    pub fn randr_version(&self) -> (u32, u32) {
194        self.randr_version
195    }
196
197    /// Get the underlying XCB connection.
198    #[inline]
199    pub fn xcb_connection(&self) -> &XCBConnection {
200        self.xcb.as_ref().expect("xcb_connection somehow called after drop?")
201    }
202
203    /// Get the list of atoms.
204    #[inline]
205    pub fn atoms(&self) -> &Atoms {
206        &self.atoms
207    }
208
209    /// Get the index of the default screen.
210    #[inline]
211    pub fn default_screen_index(&self) -> usize {
212        self.default_screen
213    }
214
215    /// Get the default screen.
216    #[inline]
217    pub fn default_root(&self) -> &xproto::Screen {
218        &self.xcb_connection().setup().roots[self.default_screen]
219    }
220
221    /// Get the resource database.
222    #[inline]
223    pub fn database(&self) -> RwLockReadGuard<'_, resource_manager::Database> {
224        self.database.read().unwrap_or_else(|e| e.into_inner())
225    }
226
227    /// Reload the resource database.
228    #[inline]
229    pub fn reload_database(&self) -> Result<(), super::X11Error> {
230        let database = resource_manager::new_from_default(self.xcb_connection())?;
231        *self.database.write().unwrap_or_else(|e| e.into_inner()) = database;
232        Ok(())
233    }
234
235    /// Get the latest timestamp.
236    #[inline]
237    pub fn timestamp(&self) -> u32 {
238        self.timestamp.load(Ordering::Relaxed)
239    }
240
241    /// Set the last witnessed timestamp.
242    #[inline]
243    pub fn set_timestamp(&self, timestamp: u32) {
244        // Store the timestamp in the slot if it's greater than the last one.
245        let mut last_timestamp = self.timestamp.load(Ordering::Relaxed);
246        loop {
247            let wrapping_sub = |a: xproto::Timestamp, b: xproto::Timestamp| (a as i32) - (b as i32);
248
249            if wrapping_sub(timestamp, last_timestamp) <= 0 {
250                break;
251            }
252
253            match self.timestamp.compare_exchange(
254                last_timestamp,
255                timestamp,
256                Ordering::Relaxed,
257                Ordering::Relaxed,
258            ) {
259                Ok(_) => break,
260                Err(x) => last_timestamp = x,
261            }
262        }
263    }
264
265    /// Get the atom for Xsettings.
266    #[inline]
267    pub fn xsettings_screen(&self) -> Option<xproto::Atom> {
268        self.xsettings_screen
269    }
270
271    /// Get the data containing our rendering formats.
272    #[inline]
273    pub fn render_formats(&self) -> &render::QueryPictFormatsReply {
274        &self.render_formats
275    }
276
277    /// Do we need to do an endian swap?
278    #[inline]
279    pub fn needs_endian_swap(&self) -> bool {
280        #[cfg(target_endian = "big")]
281        let endian = xproto::ImageOrder::MSB_FIRST;
282        #[cfg(not(target_endian = "big"))]
283        let endian = xproto::ImageOrder::LSB_FIRST;
284
285        self.xcb_connection().setup().image_byte_order != endian
286    }
287}
288
289impl fmt::Debug for XConnection {
290    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291        self.display.fmt(f)
292    }
293}
294
295impl Drop for XConnection {
296    #[inline]
297    fn drop(&mut self) {
298        self.xcb = None;
299        unsafe { (self.xlib.XCloseDisplay)(self.display) };
300    }
301}
302
303/// Error triggered by xlib.
304#[derive(Debug, Clone)]
305pub struct XError {
306    pub description: String,
307    pub error_code: u8,
308    pub request_code: u8,
309    pub minor_code: u8,
310}
311
312impl Error for XError {}
313
314impl fmt::Display for XError {
315    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
316        write!(
317            formatter,
318            "X error: {} (code: {}, request code: {}, minor code: {})",
319            self.description, self.error_code, self.request_code, self.minor_code
320        )
321    }
322}
323
324/// Error returned if this system doesn't have XLib or can't create an X connection.
325#[derive(Clone, Debug)]
326pub enum XNotSupported {
327    /// Failed to load one or several shared libraries.
328    LibraryOpenError(ffi::OpenError),
329
330    /// Connecting to the X server with `XOpenDisplay` failed.
331    XOpenDisplayFailed, // TODO: add better message.
332
333    /// We encountered an error while converting the connection to XCB.
334    XcbConversionError(Arc<dyn Error + Send + Sync + 'static>),
335}
336
337impl From<ffi::OpenError> for XNotSupported {
338    #[inline]
339    fn from(err: ffi::OpenError) -> XNotSupported {
340        XNotSupported::LibraryOpenError(err)
341    }
342}
343
344impl XNotSupported {
345    fn description(&self) -> &'static str {
346        match self {
347            XNotSupported::LibraryOpenError(_) => "Failed to load one of xlib's shared libraries",
348            XNotSupported::XOpenDisplayFailed => "Failed to open connection to X server",
349            XNotSupported::XcbConversionError(_) => "Failed to convert Xlib connection to XCB",
350        }
351    }
352}
353
354impl Error for XNotSupported {
355    #[inline]
356    fn source(&self) -> Option<&(dyn Error + 'static)> {
357        match *self {
358            XNotSupported::LibraryOpenError(ref err) => Some(err),
359            XNotSupported::XcbConversionError(ref err) => Some(&**err),
360            _ => None,
361        }
362    }
363}
364
365impl fmt::Display for XNotSupported {
366    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
367        formatter.write_str(self.description())
368    }
369}
370
371/// A newtype wrapper around a `ConnectError` that can't be accessed by downstream libraries.
372///
373/// Without this, `x11rb` would become a public dependency.
374#[derive(Debug)]
375struct WrapConnectError(x11rb::rust_connection::ConnectError);
376
377impl fmt::Display for WrapConnectError {
378    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
379        fmt::Display::fmt(&self.0, f)
380    }
381}
382
383impl Error for WrapConnectError {
384    // We can't implement `source()` here or otherwise risk exposing `x11rb`.
385}