Skip to content

Commit

Permalink
On X11, cache the XRandR extension version
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull authored and kchibisov committed Dec 31, 2023
1 parent 3f82a6a commit 2998bbf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/platform_impl/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ impl XConnection {

fn query_monitor_list(&self) -> Result<Vec<MonitorHandle>, X11Error> {
let root = self.default_root();
let resources = ScreenResources::from_connection(self.xcb_connection(), root)?;
let resources =
ScreenResources::from_connection(self.xcb_connection(), root, self.randr_version())?;

// Pipeline all of the get-crtc requests.
let mut crtc_cookies = Vec::with_capacity(resources.crtcs().len());
Expand Down Expand Up @@ -343,10 +344,9 @@ impl ScreenResources {
pub(crate) fn from_connection(
conn: &impl x11rb::connection::Connection,
root: &x11rb::protocol::xproto::Screen,
(major_version, minor_version): (u32, u32),
) -> Result<Self, X11Error> {
let version = conn.randr_query_version(1, 3)?.reply()?;

if (version.major_version == 1 && version.minor_version >= 3) || version.major_version > 1 {
if (major_version == 1 && minor_version >= 3) || major_version > 1 {
let reply = conn
.randr_get_screen_resources_current(root.root)?
.reply()?;
Expand Down
23 changes: 22 additions & 1 deletion src/platform_impl/linux/x11/xdisplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ use std::{
use crate::window::CursorIcon;

use super::{atoms::Atoms, ffi, monitor::MonitorHandle};
use x11rb::{connection::Connection, protocol::xproto, resource_manager, xcb_ffi::XCBConnection};
use x11rb::{
connection::Connection,
protocol::{randr::ConnectionExt as _, xproto},
resource_manager,
xcb_ffi::XCBConnection,
};

/// A connection to an X server.
pub(crate) struct XConnection {
Expand Down Expand Up @@ -47,6 +52,9 @@ pub(crate) struct XConnection {
/// The resource database.
database: RwLock<resource_manager::Database>,

/// RandR version.
randr_version: (u32, u32),

pub latest_error: Mutex<Option<XError>>,
pub cursor_cache: Mutex<HashMap<Option<CursorIcon>, ffi::Cursor>>,
}
Expand Down Expand Up @@ -104,6 +112,13 @@ impl XConnection {
let database = resource_manager::new_from_default(&xcb)
.map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;

// Load the RandR version.
let randr_version = xcb
.randr_query_version(1, 3)
.expect("failed to request XRandR version")
.reply()
.expect("failed to query XRandR version");

Ok(XConnection {
xlib,
xcursor,
Expand All @@ -117,6 +132,7 @@ impl XConnection {
monitor_handles: Mutex::new(None),
database: RwLock::new(database),
cursor_cache: Default::default(),
randr_version: (randr_version.major_version, randr_version.minor_version),
})
}

Expand All @@ -131,6 +147,11 @@ impl XConnection {
}
}

#[inline]
pub fn randr_version(&self) -> (u32, u32) {
self.randr_version
}

/// Get the underlying XCB connection.
#[inline]
pub fn xcb_connection(&self) -> &XCBConnection {
Expand Down

0 comments on commit 2998bbf

Please sign in to comment.