From 828f4b0355c1f1d347495dec5b33c0fd1f4eb0b4 Mon Sep 17 00:00:00 2001 From: isark Date: Sun, 16 Jul 2023 14:39:53 +0200 Subject: [PATCH] Changed focus target to sending a message event instead, seemingly more reliable... --- Cargo.toml | 2 +- src/linux/mod.rs | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b556ea1..52c1035 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "Underlayer" -version = "0.2.0" +version = "0.2.1" 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 b6686a7..1f0d1ee 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -6,7 +6,7 @@ use x11rb::{ protocol::{ xproto::{ Atom, AtomEnum, ChangeWindowAttributesAux, - ConnectionExt, EventMask, Window, InputFocus, + ConnectionExt, EventMask, Window, InputFocus, ClientMessageData, ClientMessageEvent, self, }, Event, }, @@ -60,12 +60,31 @@ 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::Relaxed)).lock() { + if let Ok(target) = (*TARGET.load(std::sync::atomic::Ordering::SeqCst)).lock() { if *target != x11rb::NONE { if let Ok((conn, _screen_num)) = connect(None) { - conn.set_input_focus(InputFocus::PARENT, *target, x11rb::CURRENT_TIME).ok(); + + 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, + 0, + 0, + 0, + 0, + ]), + }; + + conn.send_event(false, conn.setup().roots[_screen_num].root, EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT, event).ok(); conn.flush().ok(); } }