From 56f492438f6f06ca9c55b8aad272f5e9f85b1802 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Sun, 23 Apr 2023 18:50:32 -0400 Subject: [PATCH] Improve x11 error texts --- src/kwin_window.rs | 19 +++++++++++++------ src/x11_connection.rs | 32 ++++++++++++++++++++++++-------- src/x11_window.rs | 5 ++++- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/kwin_window.rs b/src/kwin_window.rs index 2686988..20b505b 100644 --- a/src/kwin_window.rs +++ b/src/kwin_window.rs @@ -19,14 +19,18 @@ const KWIN_SCRIPT: &str = include_str!("kwin_window.js"); struct KWinScript { dbus_connection: Connection, + is_loaded: bool, } impl KWinScript { fn new(dbus_connection: Connection) -> Self { - KWinScript { dbus_connection } + KWinScript { + dbus_connection, + is_loaded: false, + } } - fn load(&self) -> Result<(), BoxedError> { + fn load(&mut self) -> Result<(), BoxedError> { let path = temp_dir().join("kwin_window.js"); std::fs::write(&path, KWIN_SCRIPT).unwrap(); @@ -34,6 +38,7 @@ impl KWinScript { .get_registered_number(&path) .and_then(|number| self.start(number)); std::fs::remove_file(&path)?; + self.is_loaded = true; result } @@ -99,10 +104,12 @@ impl KWinScript { impl Drop for KWinScript { fn drop(&mut self) { - debug!("Unloading KWin script"); - if let Err(e) = self.unload() { - error!("Problem during stopping KWin script: {e}"); - }; + if self.is_loaded { + debug!("Unloading KWin script"); + if let Err(e) = self.unload() { + error!("Problem during stopping KWin script: {e}"); + }; + } } } diff --git a/src/x11_connection.rs b/src/x11_connection.rs index 8342740..470bc43 100644 --- a/src/x11_connection.rs +++ b/src/x11_connection.rs @@ -48,13 +48,15 @@ impl X11Connection { let name = self.get_property( focus, - self.intern_atom(b"_NET_WM_NAME")?, - self.intern_atom(b"UTF8_STRING")?, + self.intern_atom("_NET_WM_NAME")?, + "_NET_WM_NAME", + self.intern_atom("UTF8_STRING")?, u32::MAX, )?; let class = self.get_property( focus, AtomEnum::WM_CLASS.into(), + "WM_CLASS", AtomEnum::STRING.into(), u32::MAX, )?; @@ -71,23 +73,37 @@ impl X11Connection { &self, window: Window, property: Atom, + property_name: &str, property_type: Atom, long_length: u32, ) -> Result { self.connection - .get_property(false, window, property, property_type, 0, long_length)? + .get_property(false, window, property, property_type, 0, long_length) + .map_err(|e| format!("GetPropertyRequest[{property_name}] failed: {e}"))? .reply() - .map_err(std::convert::Into::into) + .map_err(|e| format!("GetPropertyReply[{property_name}] failed: {e}").into()) } - fn intern_atom(&self, name: &[u8]) -> Result { - Ok(self.connection.intern_atom(false, name)?.reply()?.atom) + fn intern_atom(&self, name: &str) -> Result { + Ok(self + .connection + .intern_atom(false, name.as_bytes()) + .map_err(|_| format!("InternAtomRequest[{name}] failed"))? + .reply() + .map_err(|_| format!("InternAtomReply[{name}] failed"))? + .atom) } fn find_active_window(&self) -> Result { let window: Atom = AtomEnum::WINDOW.into(); - let net_active_window = self.intern_atom(b"_NET_ACTIVE_WINDOW")?; - let active_window = self.get_property(self.screen_root, net_active_window, window, 1)?; + let net_active_window = self.intern_atom("_NET_ACTIVE_WINDOW")?; + let active_window = self.get_property( + self.screen_root, + net_active_window, + "_NET_ACTIVE_WINDOW", + window, + 1, + )?; if active_window.format == 32 && active_window.length == 1 { active_window diff --git a/src/x11_window.rs b/src/x11_window.rs index ef4b3f3..6957c76 100644 --- a/src/x11_window.rs +++ b/src/x11_window.rs @@ -29,8 +29,11 @@ impl WindowWatcher { impl Watcher for WindowWatcher { fn new() -> Result { + let connection = X11Connection::new()?; + connection.active_window_data()?; + Ok(WindowWatcher { - connection: X11Connection::new()?, + connection, last_title: String::new(), last_app_id: String::new(), })