From 9919acbd78952c76e9bcef5b8a99bbe0425281eb Mon Sep 17 00:00:00 2001 From: Guy Chronister Date: Thu, 21 Nov 2024 10:50:30 -0600 Subject: [PATCH 1/2] Improve error handling. --- src/lib.rs | 12 +++++++++++- src/wayland.rs | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 13ade74..9b4c6cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,18 @@ pub struct Display { _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, } +use crate::wayland::DisplaySize; // Import DisplaySize from wayland module + static DEVICE: Lazy> = Lazy::new(|| { - let size = get_axes_range(); + let size = match get_axes_range() { + Ok(size) => size, + Err(e) => { + eprintln!("Error getting axes range: {}", e); + // Handle the error by returning a default size + DisplaySize { width: 0, height: 0 } + } + }; + Mutex::new( VirtualDeviceBuilder::new() .unwrap() diff --git a/src/wayland.rs b/src/wayland.rs index bee5202..b3b7638 100644 --- a/src/wayland.rs +++ b/src/wayland.rs @@ -7,6 +7,7 @@ use wayland_protocols::xdg::xdg_output::zv1::client::{ zxdg_output_manager_v1::ZxdgOutputManagerV1, zxdg_output_v1, }; +use std::error::Error; #[derive(Copy, Clone, Default)] pub struct DisplaySize { @@ -22,24 +23,24 @@ struct OutputData { } /// Returns the largest dimensions of the detected monitors -#[must_use] -pub fn get_axes_range() -> DisplaySize { - let conn = Connection::connect_to_env().unwrap(); +pub fn get_axes_range() -> Result> { + let conn = Connection::connect_to_env()?; let display = conn.display(); let mut queue = conn.new_event_queue(); let handle = queue.handle(); display.get_registry(&handle, ()); let mut data = OutputData::default(); - queue.roundtrip(&mut data).unwrap(); + queue.roundtrip(&mut data)?; for output in data.wl_outputs.iter() { - data.output_man - .as_ref() - .unwrap() - .get_xdg_output(output, &handle, ()); + if let Some(output_man) = &data.output_man { + output_man.get_xdg_output(output, &handle, ()); + } else { + return Err("XDG Output Manager not found".into()); + } } - queue.roundtrip(&mut data).unwrap(); - data.max_size + queue.roundtrip(&mut data)?; + Ok(data.max_size) } impl Dispatch for OutputData { From cd2bbee6957f064149f481845f6f1dcbcc821685 Mon Sep 17 00:00:00 2001 From: Guy Chronister Date: Thu, 21 Nov 2024 10:52:18 -0600 Subject: [PATCH 2/2] Fixed extra space. --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9b4c6cb..3eb0314 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,6 @@ static DEVICE: Lazy> = Lazy::new(|| { DisplaySize { width: 0, height: 0 } } }; - Mutex::new( VirtualDeviceBuilder::new() .unwrap()