cleanup + crossbeam-channel feature

main
isark 5 days ago
parent 60f3bf7bf3
commit 434c0f68e6

@ -13,10 +13,17 @@ path = "src/lib.rs"
name = "test" name = "test"
path = "src/examples/main.rs" path = "src/examples/main.rs"
[features]
default = []
crossbeam = ["crossbeam-channel"]
[dependencies] [dependencies]
crossbeam = "0.8.2"
log = "0.4.19" log = "0.4.19"
[dependencies.crossbeam-channel]
version = "0.5"
optional = true
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
windows = {version = "0.48.0", features = ["Win32_UI_Accessibility", "Win32_Foundation", "Win32_UI_WindowsAndMessaging", "Win32_Graphics_Gdi", "Win32_System_Com", "Win32_System_Ole"]} windows = {version = "0.48.0", features = ["Win32_UI_Accessibility", "Win32_Foundation", "Win32_UI_WindowsAndMessaging", "Win32_Graphics_Gdi", "Win32_System_Com", "Win32_System_Ole"]}

@ -1,8 +1,12 @@
use std::sync::mpsc::Receiver; #[cfg(feature = "crossbeam")]
use crossbeam_channel::{unbounded, Receiver};
#[cfg_attr(unix, path = "linux/mod.rs")] #[cfg(not(feature = "crossbeam"))]
#[cfg_attr(windows, path = "windows/mod.rs")] use std::sync::mpsc::{channel as unbounded, Receiver};
#[cfg_attr(mac, path = "windows/mod.rs")]
#[cfg_attr(target_os = "linux", path = "linux/mod.rs")]
#[cfg_attr(target_os = "windows", path = "windows/mod.rs")]
#[cfg_attr(target_os = "macos", path = "macos/mod.rs")]
pub mod platform_impl; pub mod platform_impl;
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
@ -34,7 +38,7 @@ pub fn focus_target() {
} }
pub fn register<T: Into<String>>(window_title: T) -> Receiver<UnderlayEvent> { pub fn register<T: Into<String>>(window_title: T) -> Receiver<UnderlayEvent> {
let (tx, rx) = std::sync::mpsc::channel(); let (tx, rx) = unbounded();
platform_impl::init(window_title.into(), tx).expect("Catastrophe"); platform_impl::init(window_title.into(), tx).expect("Catastrophe");

@ -1,7 +1,10 @@
use std::{ use std::sync::{Arc, Mutex, OnceLock};
cell::OnceCell,
sync::{atomic::AtomicPtr, Arc, Mutex, OnceLock}, #[cfg(feature = "crossbeam")]
}; use crossbeam_channel::Sender;
#[cfg(not(feature = "crossbeam"))]
use std::sync::mpsc::Sender;
use x11rb::{ use x11rb::{
connect, connect,
@ -65,35 +68,33 @@ 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 { if let Some(target) = TARGET.get() {
if let Some(target) = TARGET.get() { if let Ok(target) = target.lock() {
if let Ok(target) = target.lock() { if *target != x11rb::NONE {
if *target != x11rb::NONE { if let Ok((conn, _screen_num)) = connect(None) {
if let Ok((conn, _screen_num)) = connect(None) { let net_active_window = get_atom(&conn, "_NET_ACTIVE_WINDOW").unwrap();
let net_active_window = get_atom(&conn, "_NET_ACTIVE_WINDOW").unwrap();
let event = ClientMessageEvent {
let event = ClientMessageEvent { response_type: xproto::CLIENT_MESSAGE_EVENT,
response_type: xproto::CLIENT_MESSAGE_EVENT, format: 32,
format: 32, sequence: 0,
sequence: 0, window: *target,
window: *target, type_: net_active_window,
type_: net_active_window, data: ClientMessageData::from([1, x11rb::CURRENT_TIME, 0, 0, 0]),
data: ClientMessageData::from([1, x11rb::CURRENT_TIME, 0, 0, 0]), };
};
if let Ok(cookie) = conn.send_event(
if let Ok(cookie) = conn.send_event( false,
false, conn.setup().roots[_screen_num].root,
conn.setup().roots[_screen_num].root, EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT,
EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT, event,
event, ) {
) { if let Err(e) = cookie.check() {
if let Err(e) = cookie.check() { log::error!("Error focusing target: {e:?}");
log::error!("Error focusing target: {e:?}");
}
} }
conn.flush().ok();
} }
conn.flush().ok();
} }
} }
} }
@ -105,7 +106,7 @@ struct Runtime {
root: Window, root: Window,
target: Target, target: Target,
atoms: AtomCollection, atoms: AtomCollection,
underlay_tx: std::sync::mpsc::Sender<UnderlayEvent>, underlay_tx: Sender<UnderlayEvent>,
active: Window, active: Window,
} }
@ -133,11 +134,9 @@ impl Runtime {
///Using this instead to allow setting the globally accessible target too for focus_target. ///Using this instead to allow setting the globally accessible target too for focus_target.
fn set_target(&mut self, wid: Window) { fn set_target(&mut self, wid: Window) {
self.target.wid = wid; self.target.wid = wid;
unsafe { if let Some(target) = TARGET.get() {
if let Some(target) = TARGET.get() { if let Ok(mut target) = target.lock() {
if let Ok(mut target) = target.lock() { *target = wid;
*target = wid;
}
} }
} }
} }
@ -360,10 +359,7 @@ impl Runtime {
} }
} }
pub fn init( pub fn init(window_title: String, tx: Sender<crate::UnderlayEvent>) -> Result<(), String> {
window_title: String,
tx: std::sync::mpsc::Sender<crate::UnderlayEvent>,
) -> Result<(), String> {
TARGET TARGET
.set(Arc::new(Mutex::new(Window::default()))) .set(Arc::new(Mutex::new(Window::default())))
.map_err(|e| log::error!("Could not init global TARGET storage {e:?}")) .map_err(|e| log::error!("Could not init global TARGET storage {e:?}"))

@ -22,6 +22,13 @@ use windows::Win32::{
UI::Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK}, UI::Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK},
}; };
#[cfg(feature = "crossbeam")]
use crossbeam_channel::{Sender};
#[cfg(not(feature = "crossbeam"))]
use std::sync::mpsc::{Sender};
use crate::{Bounds, UnderlayEvent}; use crate::{Bounds, UnderlayEvent};
static GLOBAL_RUNTIME: AtomicPtr<Arc<Mutex<Runtime>>> = AtomicPtr::new(std::ptr::null_mut()); static GLOBAL_RUNTIME: AtomicPtr<Arc<Mutex<Runtime>>> = AtomicPtr::new(std::ptr::null_mut());
@ -213,7 +220,7 @@ struct Runtime {
target: TargetWindow, target: TargetWindow,
underlay_tx: std::sync::mpsc::Sender<UnderlayEvent>, underlay_tx: Sender<UnderlayEvent>,
uipi_msg_id: u32, uipi_msg_id: u32,
} }
@ -474,7 +481,7 @@ const OW_FOREGROUND_TIMER_MS: u32 = 83;
pub fn init( pub fn init(
window_title: String, window_title: String,
tx: std::sync::mpsc::Sender<crate::UnderlayEvent>, tx: Sender<crate::UnderlayEvent>,
) -> Result<(), String> { ) -> Result<(), String> {
std::thread::spawn(move || { std::thread::spawn(move || {
let uipi_msg_id = register_message("UNDERLAY_UIPI_TEST"); let uipi_msg_id = register_message("UNDERLAY_UIPI_TEST");

Loading…
Cancel
Save