Fixed some timing issues on Linux

main
isark 2 years ago
parent f4858eba43
commit 60f3bf7bf3

@ -1,6 +1,6 @@
[package] [package]
name = "Underlayer" name = "Underlayer"
version = "0.2.2" version = "0.2.3"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

@ -1,4 +1,7 @@
use std::sync::{atomic::AtomicPtr, Arc, Mutex}; use std::{
cell::OnceCell,
sync::{atomic::AtomicPtr, Arc, Mutex, OnceLock},
};
use x11rb::{ use x11rb::{
connect, connect,
@ -17,7 +20,7 @@ use crate::{Bounds, UnderlayEvent};
const WM_NAME_BUFSIZE: u32 = 512; const WM_NAME_BUFSIZE: u32 = 512;
static TARGET: AtomicPtr<Arc<Mutex<Window>>> = AtomicPtr::new(std::ptr::null_mut()); static TARGET: OnceLock<Arc<Mutex<Window>>> = OnceLock::new();
struct Target { struct Target {
title: String, title: String,
@ -63,32 +66,34 @@ fn get_atom<T: Into<String>>(conn: &RustConnection, name: T) -> Option<Atom> {
//Not very pretty and a bit wasteful making a new connection and getting the atom every time.. //Not very pretty and a bit wasteful making a new connection and getting the atom every time..
pub fn focus_target() { pub fn focus_target() {
unsafe { unsafe {
if let Ok(target) = (*TARGET.load(std::sync::atomic::Ordering::SeqCst)).lock() { if let Some(target) = TARGET.get() {
if *target != x11rb::NONE { if let Ok(target) = target.lock() {
if let Ok((conn, _screen_num)) = connect(None) { if *target != x11rb::NONE {
let net_active_window = get_atom(&conn, "_NET_ACTIVE_WINDOW").unwrap(); 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, let event = ClientMessageEvent {
format: 32, response_type: xproto::CLIENT_MESSAGE_EVENT,
sequence: 0, format: 32,
window: *target, sequence: 0,
type_: net_active_window, window: *target,
data: ClientMessageData::from([1, x11rb::CURRENT_TIME, 0, 0, 0]), type_: net_active_window,
}; data: ClientMessageData::from([1, x11rb::CURRENT_TIME, 0, 0, 0]),
};
if let Ok(cookie) = conn.send_event(
false, if let Ok(cookie) = conn.send_event(
conn.setup().roots[_screen_num].root, false,
EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT, conn.setup().roots[_screen_num].root,
event, EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT,
) { event,
if let Err(e) = cookie.check() { ) {
log::error!("Error focusing target: {e:?}"); 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) { fn set_target(&mut self, wid: Window) {
self.target.wid = wid; self.target.wid = wid;
unsafe { unsafe {
if let Ok(mut target) = (*TARGET.load(std::sync::atomic::Ordering::Relaxed)).lock() { if let Some(target) = TARGET.get() {
*target = wid; if let Ok(mut target) = target.lock() {
*target = wid;
}
} }
} }
} }
@ -357,6 +364,11 @@ pub fn init(
window_title: String, window_title: String,
tx: std::sync::mpsc::Sender<crate::UnderlayEvent>, tx: std::sync::mpsc::Sender<crate::UnderlayEvent>,
) -> Result<(), String> { ) -> 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 || { std::thread::spawn(move || {
let (conn, screen_num) = match connect(None) { let (conn, screen_num) = match connect(None) {
Ok(conn) => conn, Ok(conn) => conn,
@ -410,11 +422,6 @@ pub fn init(
runtime.conn.flush().ok(); 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"); log::trace!("Initialized, listning for events");
while let Ok(event) = runtime.conn.wait_for_event() { while let Ok(event) = runtime.conn.wait_for_event() {
log::trace!("Got event"); log::trace!("Got event");

Loading…
Cancel
Save