From e95707fe70ad08f1f65e76de0af001e5db149a3f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 2 Apr 2022 22:44:51 -0700 Subject: [PATCH] flesh out % based and named display for positioning on X11 refs: #1794 --- window/Cargo.toml | 2 +- window/src/os/x11/connection.rs | 7 ++ window/src/os/x11/window.rs | 136 ++++++++++++++++++++++++++------ 3 files changed, 120 insertions(+), 25 deletions(-) diff --git a/window/Cargo.toml b/window/Cargo.toml index 85dad682f..c35552486 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -68,7 +68,7 @@ winreg = "0.6" dirs-next = "2.0" filedescriptor = { version="0.8", path = "../filedescriptor" } x11 = {version ="2.18", features = ["xlib_xcb"]} -xcb = {version="0.9", features=["render", "xkb", "xlib_xcb"]} +xcb = {version="0.9", features=["render", "randr", "xkb", "xlib_xcb"]} xcb-util = { features = [ "cursor", "image", "icccm", "ewmh", "keysyms"], version = "0.3" } xkbcommon = { version = "0.5", features = ["x11", "wayland"], git="https://github.com/wez/xkbcommon-rs.git", rev="5cc8f233ac2b8bfa0d7a26fd981b77e68c3f2219"} #xkbcommon = { version = "0.5", features = ["x11", "wayland"], path="../../xkbcommon-rs" } diff --git a/window/src/os/x11/connection.rs b/window/src/os/x11/connection.rs index 25400b8ac..9f644fee6 100644 --- a/window/src/os/x11/connection.rs +++ b/window/src/os/x11/connection.rs @@ -48,6 +48,7 @@ pub struct XConnection { pub(crate) gl_connection: RefCell>>, pub(crate) ime: RefCell>>, pub(crate) ime_process_event_result: RefCell>, + pub(crate) has_randr: bool, } impl std::ops::Deref for XConnection { @@ -414,6 +415,11 @@ impl XConnection { .get_reply()? .atom(); + let has_randr = conn + .get_extension_data(xcb::randr::id()) + .map(|info| info.present()) + .unwrap_or(false); + let keysyms = unsafe { xcb_key_symbols_alloc((*conn).get_raw_conn()) }; let screen = conn @@ -519,6 +525,7 @@ impl XConnection { gl_connection: RefCell::new(None), ime: RefCell::new(ime), ime_process_event_result: RefCell::new(Ok(())), + has_randr, }); { diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 08ee850b8..86a9abb42 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -9,11 +9,12 @@ use crate::{ }; use anyhow::{anyhow, Context as _}; use async_trait::async_trait; -use config::{ConfigHandle, DimensionContext}; +use config::{ConfigHandle, DimensionContext, GeometryOrigin}; use promise::{Future, Promise}; use raw_window_handle::unix::XcbHandle; use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; use std::any::Any; +use std::collections::HashMap; use std::convert::TryInto; use std::rc::{Rc, Weak}; use std::sync::{Arc, Mutex}; @@ -839,30 +840,14 @@ impl XWindow { })? .x11(); - let mut events = WindowEventSender::new(event_handler); + let ResolvedGeometry { + x, + y, + width, + height, + } = resolve_geometry(&conn, geometry)?; - // TODO: populate these dimension contexts based on xrandr info - let dpi = conn.default_dpi(); - let width_context = DimensionContext { - dpi: dpi as f32, - pixel_max: 65535., - pixel_cell: 65535., - }; - let height_context = DimensionContext { - dpi: dpi as f32, - pixel_max: 65535., - pixel_cell: 65535., - }; - let width = geometry.width.evaluate_as_pixels(width_context) as usize; - let height = geometry.height.evaluate_as_pixels(height_context) as usize; - let x = geometry - .x - .map(|x| x.evaluate_as_pixels(width_context) as i16) - .unwrap_or(0); - let y = geometry - .y - .map(|y| y.evaluate_as_pixels(height_context) as i16) - .unwrap_or(0); + let mut events = WindowEventSender::new(event_handler); let window_id; let window = { @@ -1308,3 +1293,106 @@ impl WindowOps for XWindow { }); } } + +#[derive(Debug)] +struct ResolvedGeometry { + x: i16, + y: i16, + width: usize, + height: usize, +} + +fn resolve_geometry( + conn: &XConnection, + geometry: RequestedWindowGeometry, +) -> anyhow::Result { + let bounds = if conn.has_randr { + let res = xcb::randr::get_screen_resources(conn.conn(), conn.root) + .get_reply() + .context("get_screen_resources")?; + + let mut virtual_screen: Rect = euclid::rect(0, 0, 0, 0); + let mut main_screen: Rect = euclid::rect(0, 0, 0, 0); + let mut by_name = HashMap::new(); + + for &o in res.outputs() { + let info = xcb::randr::get_output_info(conn.conn(), o, res.config_timestamp()) + .get_reply() + .context("get_output_info")?; + let name = String::from_utf8_lossy(info.name()).to_string(); + let c = info.crtc(); + if let Ok(cinfo) = + xcb::randr::get_crtc_info(conn.conn(), c, res.config_timestamp()).get_reply() + { + let bounds = euclid::rect( + cinfo.x() as isize, + cinfo.y() as isize, + cinfo.width() as isize, + cinfo.height() as isize, + ); + virtual_screen = virtual_screen.union(&bounds); + if bounds.origin.x == 0 && bounds.origin.y == 0 { + main_screen = bounds; + } + by_name.insert(name, bounds); + } + } + log::trace!("{:?}", by_name); + log::trace!("virtual: {:?}", virtual_screen); + log::trace!("main: {:?}", main_screen); + + match geometry.origin { + GeometryOrigin::ScreenCoordinateSystem => virtual_screen, + GeometryOrigin::MainScreen => main_screen, + GeometryOrigin::ActiveScreen => { + // TODO: find focused window and resolve it! + // Maybe something like + // but ported to Rust? + main_screen + } + GeometryOrigin::Named(name) => match by_name.get(&name) { + Some(bounds) => bounds.clone(), + None => { + log::error!( + "Requested display {} was not found; available displays are: {:?}. \ + Using primary display instead", + name, + by_name, + ); + main_screen + } + }, + } + } else { + euclid::rect(0, 0, 65535, 65535) + }; + + let dpi = conn.default_dpi(); + let width_context = DimensionContext { + dpi: dpi as f32, + pixel_max: bounds.width() as f32, + pixel_cell: bounds.width() as f32, + }; + let height_context = DimensionContext { + dpi: dpi as f32, + pixel_max: bounds.height() as f32, + pixel_cell: bounds.height() as f32, + }; + let width = geometry.width.evaluate_as_pixels(width_context) as usize; + let height = geometry.height.evaluate_as_pixels(height_context) as usize; + let x = geometry + .x + .map(|x| x.evaluate_as_pixels(width_context) as i16 + bounds.origin.x as i16) + .unwrap_or(0); + let y = geometry + .y + .map(|y| y.evaluate_as_pixels(height_context) as i16 + bounds.origin.y as i16) + .unwrap_or(0); + + Ok(ResolvedGeometry { + x, + y, + width, + height, + }) +}