Skip to content

Commit

Permalink
chore: improve code readability and subdivide error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Want-ToBelieve committed Dec 6, 2023
1 parent f06248d commit e9884dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autohide-tdrop"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
description = "100% pure rust implementation that can automatically hide terminals or other applications managed by tdrop when they lose focus"
authors = ["backtolife2021"]
Expand Down
98 changes: 44 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
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<Window, ()> {
) -> Result<Window, Box<dyn Error>> {
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<dyn Error>> {
let (connection, screen_num) = RustConnection::connect(None)?;
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,
Expand All @@ -55,35 +43,37 @@ fn main() -> Result<(), Box<dyn Error>> {
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."),
},
}
}
}

0 comments on commit e9884dd

Please sign in to comment.