diff --git a/Cargo.toml b/Cargo.toml index f38b522..309c505 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "Underlayer" -version = "0.2.2" +version = "0.2.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/linux/mod.rs b/src/linux/mod.rs index bab9a66..20a0e8d 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -1,4 +1,7 @@ -use std::sync::{atomic::AtomicPtr, Arc, Mutex}; +use std::{ + cell::OnceCell, + sync::{atomic::AtomicPtr, Arc, Mutex, OnceLock}, +}; use x11rb::{ connect, @@ -17,7 +20,7 @@ use crate::{Bounds, UnderlayEvent}; const WM_NAME_BUFSIZE: u32 = 512; -static TARGET: AtomicPtr>> = AtomicPtr::new(std::ptr::null_mut()); +static TARGET: OnceLock>> = OnceLock::new(); struct Target { title: String, @@ -63,32 +66,34 @@ fn get_atom>(conn: &RustConnection, name: T) -> Option { //Not very pretty and a bit wasteful making a new connection and getting the atom every time.. pub fn focus_target() { unsafe { - if let Ok(target) = (*TARGET.load(std::sync::atomic::Ordering::SeqCst)).lock() { - if *target != x11rb::NONE { - if let Ok((conn, _screen_num)) = connect(None) { - let net_active_window = get_atom(&conn, "_NET_ACTIVE_WINDOW").unwrap(); - - let event = ClientMessageEvent { - response_type: xproto::CLIENT_MESSAGE_EVENT, - format: 32, - sequence: 0, - window: *target, - type_: net_active_window, - data: ClientMessageData::from([1, x11rb::CURRENT_TIME, 0, 0, 0]), - }; - - if let Ok(cookie) = conn.send_event( - false, - conn.setup().roots[_screen_num].root, - EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT, - event, - ) { - if let Err(e) = cookie.check() { - log::error!("Error focusing target: {e:?}"); + if let Some(target) = TARGET.get() { + if let Ok(target) = target.lock() { + if *target != x11rb::NONE { + if let Ok((conn, _screen_num)) = connect(None) { + let net_active_window = get_atom(&conn, "_NET_ACTIVE_WINDOW").unwrap(); + + let event = ClientMessageEvent { + response_type: xproto::CLIENT_MESSAGE_EVENT, + format: 32, + sequence: 0, + window: *target, + type_: net_active_window, + data: ClientMessageData::from([1, x11rb::CURRENT_TIME, 0, 0, 0]), + }; + + if let Ok(cookie) = conn.send_event( + false, + conn.setup().roots[_screen_num].root, + EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT, + event, + ) { + if let Err(e) = cookie.check() { + log::error!("Error focusing target: {e:?}"); + } } - } - conn.flush().ok(); + conn.flush().ok(); + } } } } @@ -129,8 +134,10 @@ impl Runtime { fn set_target(&mut self, wid: Window) { self.target.wid = wid; unsafe { - if let Ok(mut target) = (*TARGET.load(std::sync::atomic::Ordering::Relaxed)).lock() { - *target = wid; + if let Some(target) = TARGET.get() { + if let Ok(mut target) = target.lock() { + *target = wid; + } } } } @@ -357,6 +364,11 @@ pub fn init( window_title: String, tx: std::sync::mpsc::Sender, ) -> Result<(), String> { + TARGET + .set(Arc::new(Mutex::new(Window::default()))) + .map_err(|e| log::error!("Could not init global TARGET storage {e:?}")) + .ok(); + std::thread::spawn(move || { let (conn, screen_num) = match connect(None) { Ok(conn) => conn, @@ -410,11 +422,6 @@ pub fn init( runtime.conn.flush().ok(); - TARGET.store( - Box::into_raw(Box::new(Arc::new(Mutex::new(x11rb::NONE)))), - std::sync::atomic::Ordering::Relaxed, - ); - log::trace!("Initialized, listning for events"); while let Ok(event) = runtime.conn.wait_for_event() { log::trace!("Got event");