diff --git a/.gitignore b/.gitignore index bdf7846..138a81a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ target .devenv .direnv + .pre-commit-config.yaml + diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 120000 index bcdfa1a..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1 +0,0 @@ -/nix/store/5yxsak504bvfrbqkfyavq0y5gpvkm9db-pre-commit-config.json \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c84f24a..08da108 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,26 @@ use std::error::Error; -use x11rb::protocol::Event; use x11rb::{ connection::Connection, - protocol::xproto::{self, AtomEnum, ConnectionExt, EventMask, Window}, - rust_connection::RustConnection, + protocol::{ + xproto::{self, AtomEnum, ConnectionExt, EventMask, Window}, + Event, + }, + rust_connection::{ConnectionError, RustConnection}, }; fn get_active_window( connection: &RustConnection, root: Window, atom: xproto::Atom, -) -> Result { +) -> Result> { let response = connection - .get_property::<_, u32>(false, root, atom, AtomEnum::WINDOW.into(), 0, 1) - .unwrap() - .reply() - .unwrap(); + .get_property::<_, u32>(false, root, atom, AtomEnum::WINDOW.into(), 0, 1)? + .reply()?; - if response.value32().is_none() { - return Err(()); - } - - Ok(response.to_owned().value32().unwrap().next().unwrap()) + response + .value32() + .ok_or_else(|| "No active window found".into()) + .map(|mut val| val.next().unwrap()) } fn main() -> Result<(), Box> { @@ -29,22 +28,11 @@ fn main() -> Result<(), Box> { let screen = &connection.setup().roots[screen_num]; let root = screen.root; let net_active_window = connection - .intern_atom(false, b"_NET_ACTIVE_WINDOW") - .unwrap() - .reply() - .unwrap() + .intern_atom(false, b"_NET_ACTIVE_WINDOW")? + .reply()? .atom; - let active_window = get_active_window(&connection, root, net_active_window); - if active_window.is_err() { - eprintln!("Error getting initial active window, exiting program."); - return Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - "Error getting initial active window, exiting program.", - ))); - } - - let window_id = active_window.unwrap(); + let window_id = get_active_window(&connection, root, net_active_window)?; xproto::change_window_attributes( &connection, @@ -55,35 +43,37 @@ fn main() -> Result<(), Box> { connection.flush()?; loop { - let event = connection.wait_for_event(); - - if let Ok(Event::PropertyNotify(e)) = event { - if e.atom != net_active_window { - continue; - } - - let active_window = get_active_window(&connection, root, net_active_window); - - if active_window.is_err() { - eprintln!("Error getting active window"); - continue; - } - - if active_window.unwrap() == window_id { - continue; - } + match connection.wait_for_event() { + Ok(Event::PropertyNotify(e)) if e.atom == net_active_window => { + let active_window = get_active_window(&connection, root, net_active_window)?; - if let Err(err) = connection.unmap_window(window_id) { - eprintln!("Error unmapping window: {:?}", err); - } else { - connection.flush()?; + if active_window != window_id { + if let Err(err) = connection.unmap_window(window_id) { + eprintln!("Error unmapping window: {:?}", err); + } else { + connection.flush()?; + } + } } - } else { - eprintln!("X11 server has crashed, exiting program."); - return Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - "X11 server has crashed, exiting program.", - ))); + Ok(_) => (), + Err(e) => match e { + ConnectionError::UnknownError => eprintln!("An unknown error occurred."), + ConnectionError::UnsupportedExtension => { + eprintln!("An X11 extension was not supported by the server.") + } + ConnectionError::MaximumRequestLengthExceeded => { + eprintln!("A request larger than the maximum request length was sent.") + } + ConnectionError::FdPassingFailed => eprintln!("File descriptor passing failed."), + ConnectionError::ParseError(err) => { + eprintln!("Error while parsing some data: {:?}", err) + } + ConnectionError::InsufficientMemory => eprintln!("Out of memory."), + ConnectionError::IoError(_) => { + return Err("X11 server has crashed, exiting program.".into()) + } + _ => eprintln!("An unexpected error occurred."), + }, } } }