diff --git a/src/keyboard.rs b/src/keyboard.rs index eb39301f3..a6e1a8ee9 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -12,7 +12,7 @@ use std::{ collections::{HashMap, HashSet}, sync::{ atomic::{AtomicBool, Ordering}, - mpsc, Arc, Mutex, + Arc, Mutex, }, thread, time::SystemTime, @@ -21,10 +21,6 @@ use std::{ static mut IS_ALT_GR: bool = false; static KEYBOARD_HOOKED: AtomicBool = AtomicBool::new(false); -lazy_static::lazy_static! { - static ref GRAB_SENDER: Arc>>> = Default::default(); -} - #[cfg(feature = "flutter")] lazy_static::lazy_static! { static ref CUR_SESSION: Arc>>> = Default::default(); @@ -67,20 +63,32 @@ pub mod client { } pub fn start_grab_loop() { - let (sender, receiver) = mpsc::channel::(); - - grab_loop(receiver); - *GRAB_SENDER.lock().unwrap() = Some(sender); - - change_grab_status(GrabState::Ready); + thread::spawn(grab_loop); } pub fn change_grab_status(state: GrabState) { - if GrabState::Wait == state { - release_remote_keys(); - } - if let Some(sender) = &*GRAB_SENDER.lock().unwrap() { - sender.send(state).ok(); + match state { + GrabState::Ready => {} + GrabState::Run => { + #[cfg(any(target_os = "windows", target_os = "macos"))] + KEYBOARD_HOOKED.swap(true, Ordering::SeqCst); + + #[cfg(target_os = "linux")] + rdev::enable_grab().ok(); + } + GrabState::Wait => { + release_remote_keys(); + + #[cfg(any(target_os = "windows", target_os = "macos"))] + KEYBOARD_HOOKED.swap(false, Ordering::SeqCst); + + #[cfg(target_os = "linux")] + rdev::disable_grab().ok(); + } + GrabState::Exit => { + #[cfg(target_os = "linux")] + rdev::exit_grab_listen().ok(); + } } } @@ -171,67 +179,41 @@ pub mod client { } } -pub fn grab_loop(recv: mpsc::Receiver) { - thread::spawn(move || loop { - if let Some(state) = recv.recv().ok() { - match state { - GrabState::Ready => { - #[cfg(any(target_os = "windows", target_os = "macos"))] - std::thread::spawn(move || { - let func = move |event: Event| match event.event_type { - EventType::KeyPress(key) | EventType::KeyRelease(key) => { - // fix #2211:CAPS LOCK don't work - if key == Key::CapsLock || key == Key::NumLock { - return Some(event); - } - if KEYBOARD_HOOKED.load(Ordering::SeqCst) { - client::process_event(event); - return None; - } else { - return Some(event); - } - } - _ => Some(event), - }; - if let Err(error) = rdev::grab(func) { - log::error!("rdev Error: {:?}", error) - } - }); - - #[cfg(target_os = "linux")] - rdev::start_grab_listen(move |event: Event| match event.event_type { - EventType::KeyPress(key) | EventType::KeyRelease(key) => { - if let Key::Unknown(keycode) = key { - log::error!("rdev get unknown key, keycode is : {:?}", keycode); - } else { - client::process_event(event); - } - None - } - _ => Some(event), - }); +pub fn grab_loop() { + #[cfg(any(target_os = "windows", target_os = "macos"))] + std::thread::spawn(move || { + let func = move |event: Event| match event.event_type { + EventType::KeyPress(key) | EventType::KeyRelease(key) => { + // fix #2211:CAPS LOCK don't work + if key == Key::CapsLock || key == Key::NumLock { + return Some(event); } - GrabState::Run => { - #[cfg(any(target_os = "windows", target_os = "macos"))] - KEYBOARD_HOOKED.swap(true, Ordering::SeqCst); - - #[cfg(target_os = "linux")] - rdev::enable_grab().ok(); - } - GrabState::Wait => { - #[cfg(any(target_os = "windows", target_os = "macos"))] - KEYBOARD_HOOKED.swap(false, Ordering::SeqCst); - - #[cfg(target_os = "linux")] - rdev::disable_grab().ok(); - } - GrabState::Exit => { - #[cfg(target_os = "linux")] - rdev::exit_grab_listen().ok(); + if KEYBOARD_HOOKED.load(Ordering::SeqCst) { + client::process_event(event); + return None; + } else { + return Some(event); } } + _ => Some(event), + }; + if let Err(error) = rdev::grab(func) { + log::error!("rdev Error: {:?}", error) } }); + + #[cfg(target_os = "linux")] + rdev::start_grab_listen(move |event: Event| match event.event_type { + EventType::KeyPress(key) | EventType::KeyRelease(key) => { + if let Key::Unknown(keycode) = key { + log::error!("rdev get unknown key, keycode is : {:?}", keycode); + } else { + client::process_event(event); + } + None + } + _ => Some(event), + }); } pub fn is_long_press(event: &Event) -> bool {