Skip to content

Commit

Permalink
Use x11rb's resource_manager instead of Xrm
Browse files Browse the repository at this point in the history
Transfers more functionality from Xlib to x11rb
  • Loading branch information
notgull committed Aug 27, 2023
1 parent f029dcb commit 5bee1cf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ wayland-protocols = { version = "0.30.0", features = [ "staging"], optional = tr
calloop = "0.10.5"
rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "thread", "process"] }
x11-dl = { version = "2.18.5", optional = true }
x11rb = { version = "0.12.0", default-features = false, features = ["allow-unsafe-code", "dl-libxcb", "randr", "xinput", "xkb"], optional = true }
x11rb = { version = "0.12.0", default-features = false, features = ["allow-unsafe-code", "dl-libxcb", "randr", "resource_manager", "xinput", "xkb"], optional = true }
xkbcommon-dl = "0.4.0"
memmap2 = { version = "0.5.0", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ impl<T: 'static> EventProcessor<T> {
let window_id = mkwid(window);
let device_id = mkdid(xev.deviceid as xinput::DeviceId);

if let Some(all_info) = DeviceInfo::get(&wt.xconn, 0) {
if let Some(all_info) = DeviceInfo::get(&wt.xconn, super::ALL_DEVICES) {
let mut devices = self.devices.borrow_mut();
for device_info in all_info.info.iter() {
if device_info.deviceid == xev.sourceid as xinput::DeviceId
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl<T: 'static> EventLoop<T> {
)
.unwrap();

event_processor.init_device(0);
event_processor.init_device(ALL_DEVICES);

EventLoop {
loop_running: false,
Expand Down
21 changes: 5 additions & 16 deletions src/platform_impl/linux/x11/util/randr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,10 @@ pub fn calc_dpi_factor(

impl XConnection {
// Retrieve DPI from Xft.dpi property
pub unsafe fn get_xft_dpi(&self) -> Option<f64> {
(self.xlib.XrmInitialize)();
let resource_manager_str = (self.xlib.XResourceManagerString)(self.display);
if resource_manager_str.is_null() {
return None;
}
if let Ok(res) = ::std::ffi::CStr::from_ptr(resource_manager_str).to_str() {
let name: &str = "Xft.dpi:\t";
for pair in res.split('\n') {
if let Some(stripped) = pair.strip_prefix(name) {
return f64::from_str(stripped).ok();
}
}
}
None
pub fn get_xft_dpi(&self) -> Option<f64> {
self.database()
.get_string("Xfi.dpi", "")
.and_then(|s| f64::from_str(s).ok())
}
pub fn get_output_info(
&self,
Expand Down Expand Up @@ -138,7 +127,7 @@ impl XConnection {
dpi_override
}
EnvVarDPI::NotSet => {
if let Some(dpi) = unsafe { self.get_xft_dpi() } {
if let Some(dpi) = self.get_xft_dpi() {
dpi / 96.
} else {
calc_dpi_factor(
Expand Down
17 changes: 16 additions & 1 deletion src/platform_impl/linux/x11/xdisplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use std::{
use crate::window::CursorIcon;

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

/// A connection to an X server.
pub(crate) struct XConnection {
pub xlib: ffi::Xlib,
pub xcursor: ffi::Xcursor,
pub display: *mut ffi::Display,

/// The manager for the XCB connection.
///
/// The `Option` ensures that we can drop it before we close the `Display`.
Expand All @@ -38,6 +39,9 @@ pub(crate) struct XConnection {
/// List of monitor handles.
pub monitor_handles: Mutex<Option<Vec<MonitorHandle>>>,

/// The resource database.
database: resource_manager::Database,

pub latest_error: Mutex<Option<XError>>,
pub cursor_cache: Mutex<HashMap<Option<CursorIcon>, ffi::Cursor>>,
}
Expand Down Expand Up @@ -90,6 +94,10 @@ impl XConnection {
.reply()
.map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;

// Load the database.
let database = resource_manager::new_from_default(&xcb)
.map_err(|e| XNotSupported::XcbConversionError(Arc::new(e)))?;

Ok(XConnection {
xlib,
xcursor,
Expand All @@ -100,6 +108,7 @@ impl XConnection {
timestamp: AtomicU32::new(0),
latest_error: Mutex::new(None),
monitor_handles: Mutex::new(None),
database,
cursor_cache: Default::default(),
})
}
Expand Down Expand Up @@ -141,6 +150,12 @@ impl XConnection {
&self.xcb_connection().setup().roots[self.default_screen]
}

/// Get the resource database.
#[inline]
pub fn database(&self) -> &resource_manager::Database {
&self.database
}

/// Get the latest timestamp.
#[inline]
pub fn timestamp(&self) -> u32 {
Expand Down

0 comments on commit 5bee1cf

Please sign in to comment.