diff --git a/lua-api-crates/window-funcs/src/lib.rs b/lua-api-crates/window-funcs/src/lib.rs index 9e739bde6..1a6eac5ea 100644 --- a/lua-api-crates/window-funcs/src/lib.rs +++ b/lua-api-crates/window-funcs/src/lib.rs @@ -19,6 +19,7 @@ pub struct ScreenInfo { pub y: isize, pub width: isize, pub height: isize, + pub scale: f64, } impl_lua_conversion_dynamic!(ScreenInfo); @@ -42,6 +43,7 @@ impl From for ScreenInfo { y: info.rect.min_y(), width: info.rect.width(), height: info.rect.height(), + scale: info.scale, } } } diff --git a/window/src/os/macos/connection.rs b/window/src/os/macos/connection.rs index ce1fb3fa5..2e5fcbc32 100644 --- a/window/src/os/macos/connection.rs +++ b/window/src/os/macos/connection.rs @@ -165,7 +165,11 @@ fn nsscreen_to_screen_info(screen: *mut Object) -> ScreenInfo { frame.size.width as isize, frame.size.height as isize, ); - ScreenInfo { name, rect } + ScreenInfo { + name, + rect, + scale: 1.0, + } } extern "C" { diff --git a/window/src/os/wayland/connection.rs b/window/src/os/wayland/connection.rs index 7b92a19ef..2b4f1cd10 100644 --- a/window/src/os/wayland/connection.rs +++ b/window/src/os/wayland/connection.rs @@ -2,6 +2,7 @@ use super::pointer::*; use super::window::*; use crate::connection::ConnectionOps; +use crate::os::wayland::output::OutputHandler; use crate::os::x11::keyboard::Keyboard; use crate::screen::{ScreenInfo, Screens}; use crate::spawn::*; @@ -23,7 +24,13 @@ use toolkit::shm::AutoMemPool; use wayland_client::protocol::wl_keyboard::{Event as WlKeyboardEvent, KeymapFormat, WlKeyboard}; use wayland_client::{EventQueue, Main}; -toolkit::default_environment!(MyEnvironment, desktop); +toolkit::default_environment!(MyEnvironment, desktop, +fields=[ + output_handler: OutputHandler, +], +singles=[ + wayland_protocols::wlr::unstable::output_management::v1::client::zwlr_output_manager_v1::ZwlrOutputManagerV1 => output_handler, +]); pub struct WaylandConnection { should_terminate: RefCell, @@ -61,8 +68,11 @@ pub struct WaylandConnection { impl WaylandConnection { pub fn create_new() -> anyhow::Result { - let (environment, display, event_q) = - toolkit::new_default_environment!(MyEnvironment, desktop)?; + let (environment, display, event_q) = toolkit::new_default_environment!( + MyEnvironment, + desktop, + fields = [output_handler: OutputHandler::new()] + )?; let mut pointer = None; let mut seat_keyboards = HashMap::new(); @@ -368,6 +378,14 @@ impl ConnectionOps for WaylandConnection { } fn screens(&self) -> anyhow::Result { + if let Some(screens) = self + .environment + .borrow() + .with_inner(|env| env.output_handler.screens()) + { + return Ok(screens); + } + let mut by_name = HashMap::new(); let mut virtual_rect: ScreenRect = euclid::rect(0, 0, 0, 0); for output in self.environment.borrow().get_all_outputs() { @@ -392,8 +410,10 @@ impl ConnectionOps for WaylandConnection { height as isize, ); + let scale = info.scale_factor as f64; + virtual_rect = virtual_rect.union(&rect); - by_name.insert(name.clone(), ScreenInfo { name, rect }); + by_name.insert(name.clone(), ScreenInfo { name, rect, scale }); }); } diff --git a/window/src/os/wayland/mod.rs b/window/src/os/wayland/mod.rs index 093618831..60c5e81ee 100644 --- a/window/src/os/wayland/mod.rs +++ b/window/src/os/wayland/mod.rs @@ -1,9 +1,11 @@ #![cfg(all(unix, not(target_os = "macos")))] pub mod connection; +pub mod output; pub mod window; pub use self::window::*; pub use connection::*; +pub use output::*; mod copy_and_paste; mod drag_and_drop; mod frame; diff --git a/window/src/os/wayland/output.rs b/window/src/os/wayland/output.rs new file mode 100644 index 000000000..2aa0195e4 --- /dev/null +++ b/window/src/os/wayland/output.rs @@ -0,0 +1,301 @@ +//! Dealing with Wayland outputs + +use crate::screen::{ScreenInfo, Screens}; +use crate::ScreenRect; +use smithay_client_toolkit::environment::GlobalHandler; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use wayland_client::protocol::wl_output::Transform; +use wayland_client::protocol::wl_registry::WlRegistry; +use wayland_client::{Attached, DispatchData, Interface, Main, Proxy}; +use wayland_protocols::wlr::unstable::output_management::v1::client::zwlr_output_head_v1::{ + Event as ZwlrOutputHeadEvent, ZwlrOutputHeadV1, +}; +use wayland_protocols::wlr::unstable::output_management::v1::client::zwlr_output_manager_v1::{ + Event as ZwlrOutputEvent, ZwlrOutputManagerV1, +}; +use wayland_protocols::wlr::unstable::output_management::v1::client::zwlr_output_mode_v1::{ + Event as ZwlrOutputModeEvent, ZwlrOutputModeV1, +}; + +#[derive(Debug, Default, Clone)] +pub struct ModeInfo { + pub id: u32, + pub width: i32, + pub height: i32, + pub refresh: i32, + pub preferred: bool, +} + +#[derive(Debug, Default, Clone)] +pub struct HeadInfo { + pub id: u32, + pub name: String, + pub description: String, + pub physical_width: i32, + pub physical_height: i32, + /// List of ids that correspond to ModeInfo's + pub mode_ids: Vec, + pub enabled: bool, + pub current_mode_id: Option, + pub x: i32, + pub y: i32, + pub transform: Option, + pub scale: f64, + pub make: String, + pub model: String, + pub serial_number: String, +} + +#[derive(Default, Debug)] +struct Inner { + zwlr_heads: HashMap>, + zwlr_modes: HashMap>, + zwlr_mode_info: HashMap, + zwlr_head_info: HashMap, +} + +fn wl_id(obj: T) -> u32 +where + I: Interface, + T: AsRef>, + I: AsRef>, + I: From>, +{ + let proxy: &Proxy = obj.as_ref(); + proxy.id() +} + +impl Inner { + fn handle_zwlr_mode_event( + &mut self, + mode: Main, + event: ZwlrOutputModeEvent, + _ddata: DispatchData, + _inner: &Arc>, + ) { + log::debug!("{event:?}"); + let id = wl_id(mode.detach()); + let mut info = self.zwlr_mode_info.entry(id).or_insert_with(|| ModeInfo { + id, + ..ModeInfo::default() + }); + + match event { + ZwlrOutputModeEvent::Size { width, height } => { + info.width = width; + info.height = height; + } + ZwlrOutputModeEvent::Refresh { refresh } => { + info.refresh = refresh; + } + ZwlrOutputModeEvent::Preferred => { + info.preferred = true; + } + ZwlrOutputModeEvent::Finished => { + drop(info); + self.zwlr_mode_info.remove(&id); + self.zwlr_modes.remove(&id); + } + _ => {} + } + } + + fn handle_zwlr_head_event( + &mut self, + head: Main, + event: ZwlrOutputHeadEvent, + _ddata: DispatchData, + inner: &Arc>, + ) { + log::debug!("{event:?}"); + let id = wl_id(head.detach()); + let mut info = self.zwlr_head_info.entry(id).or_insert_with(|| HeadInfo { + id, + ..HeadInfo::default() + }); + match event { + ZwlrOutputHeadEvent::Name { name } => { + info.name = name; + } + ZwlrOutputHeadEvent::Description { description } => { + info.description = description; + } + ZwlrOutputHeadEvent::PhysicalSize { width, height } => { + info.physical_width = width; + info.physical_height = height; + } + ZwlrOutputHeadEvent::Mode { mode } => { + let inner = Arc::clone(inner); + mode.quick_assign(move |mode, event, ddata| { + inner + .lock() + .unwrap() + .handle_zwlr_mode_event(mode, event, ddata, &inner); + }); + let mode_id = wl_id(mode.detach()); + info.mode_ids.push(mode_id); + self.zwlr_modes.insert(mode_id, mode.into()); + } + ZwlrOutputHeadEvent::Enabled { enabled } => { + info.enabled = enabled != 0; + } + ZwlrOutputHeadEvent::CurrentMode { mode } => { + let mode_id = wl_id(mode); + info.current_mode_id.replace(mode_id); + } + ZwlrOutputHeadEvent::Position { x, y } => { + info.x = x; + info.y = y; + } + ZwlrOutputHeadEvent::Transform { transform } => { + info.transform.replace(transform); + } + ZwlrOutputHeadEvent::Scale { scale } => { + info.scale = scale; + } + ZwlrOutputHeadEvent::Make { make } => { + info.make = make; + } + ZwlrOutputHeadEvent::Model { model } => { + info.model = model; + } + ZwlrOutputHeadEvent::SerialNumber { serial_number } => { + info.serial_number = serial_number; + } + ZwlrOutputHeadEvent::Finished => { + drop(info); + self.zwlr_heads.remove(&id); + self.zwlr_head_info.remove(&id); + } + + _ => {} + } + } + + fn handle_zwlr_output_event( + &mut self, + _output: Main, + event: ZwlrOutputEvent, + _ddata: DispatchData, + inner: &Arc>, + ) { + log::debug!("{event:?}"); + match event { + ZwlrOutputEvent::Head { head } => { + let inner = Arc::clone(inner); + head.quick_assign(move |output, event, ddata| { + inner + .lock() + .unwrap() + .handle_zwlr_head_event(output, event, ddata, &inner); + }); + self.zwlr_heads.insert(wl_id(head.detach()), head.into()); + } + ZwlrOutputEvent::Done { serial: _ } => {} + ZwlrOutputEvent::Finished => {} + _ => {} + } + } +} + +pub struct OutputHandler { + zwlr: Option>, + inner: Arc>, +} + +impl OutputHandler { + pub fn new() -> Self { + Self { + zwlr: None, + inner: Arc::new(Mutex::new(Inner::default())), + } + } + + pub fn screens(&self) -> Option { + let inner = self.inner.lock().unwrap(); + + let mut by_name = HashMap::new(); + let mut virtual_rect: ScreenRect = euclid::rect(0, 0, 0, 0); + + log::debug!("zwlr_head_info: {:#?}", inner.zwlr_head_info); + + for head in inner.zwlr_head_info.values() { + let name = head.name.clone(); + let (width, height) = match head.current_mode_id { + Some(mode_id) => match inner.zwlr_mode_info.get(&mode_id) { + Some(mode) => (mode.width, mode.height), + None => continue, + }, + None => continue, + }; + + let scale = head.scale; + let rect = euclid::rect( + head.x as isize, + head.y as isize, + width as isize, + height as isize, + ); + virtual_rect = virtual_rect.union(&rect); + by_name.insert(name.clone(), ScreenInfo { name, rect, scale }); + } + + if by_name.is_empty() { + return None; + } + + // The main screen is the one either at the origin of + // the virtual area, or if that doesn't exist for some weird + // reason, the screen closest to the origin. + let main = by_name + .values() + .min_by_key(|screen| { + screen + .rect + .origin + .to_f32() + .distance_to(euclid::Point2D::origin()) + .abs() as isize + })? + .clone(); + + // We don't yet know how to determine the active screen, + // so assume the main screen. + let active = main.clone(); + + Some(Screens { + main, + active, + by_name, + virtual_rect, + }) + } +} + +impl GlobalHandler for OutputHandler { + fn created( + &mut self, + registry: Attached, + id: u32, + version: u32, + _ddata: DispatchData, + ) { + log::debug!("created ZwlrOutputManagerV1 {id} {version}"); + let zwlr = registry.bind::(2, id); + + let inner = Arc::clone(&self.inner); + zwlr.quick_assign(move |output, event, ddata| { + inner + .lock() + .unwrap() + .handle_zwlr_output_event(output, event, ddata, &inner); + }); + + self.zwlr.replace(zwlr.into()); + } + + fn get(&self) -> std::option::Option> { + self.zwlr.clone() + } +} diff --git a/window/src/os/windows/connection.rs b/window/src/os/windows/connection.rs index ded6179b0..a54a5e03b 100644 --- a/window/src/os/windows/connection.rs +++ b/window/src/os/windows/connection.rs @@ -153,6 +153,7 @@ impl ConnectionOps for Connection { mi.rcMonitor.right as isize - mi.rcMonitor.left as isize, mi.rcMonitor.bottom as isize - mi.rcMonitor.top as isize, ), + scale: 1.0, }; info.virtual_rect = info.virtual_rect.union(&screen_info.rect); diff --git a/window/src/os/x11/connection.rs b/window/src/os/x11/connection.rs index 5690b5f41..398dcc2e5 100644 --- a/window/src/os/x11/connection.rs +++ b/window/src/os/x11/connection.rs @@ -180,6 +180,7 @@ impl ConnectionOps for XConnection { let info = ScreenInfo { name: name.clone(), rect: bounds, + scale: 1.0, }; by_name.insert(name, info); } diff --git a/window/src/screen.rs b/window/src/screen.rs index ad9871ca8..335448e33 100644 --- a/window/src/screen.rs +++ b/window/src/screen.rs @@ -13,4 +13,5 @@ pub struct Screens { pub struct ScreenInfo { pub name: String, pub rect: ScreenRect, + pub scale: f64, }