move sleep from main thread

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-12-29 18:16:06 +08:00
parent 94cecb1860
commit 0b9b71e4fc

View File

@ -223,8 +223,7 @@ lazy_static::lazy_static! {
static ref IS_SERVER: bool = std::env::args().nth(1) == Some("--server".to_owned()); static ref IS_SERVER: bool = std::env::args().nth(1) == Some("--server".to_owned());
} }
// virtual_input must be used in main thread. static mut VIRTUAL_INPUT_MTX: Mutex<()> = Mutex::new(());
// No need to wrap mutex.
static mut VIRTUAL_INPUT: Option<VirtualInput> = None; static mut VIRTUAL_INPUT: Option<VirtualInput> = None;
// First call set_uinput() will create keyboard and mouse clients. // First call set_uinput() will create keyboard and mouse clients.
@ -687,17 +686,21 @@ pub fn handle_key(evt: &KeyEvent) {
// having GUI, run main GUI thread, otherwise crash // having GUI, run main GUI thread, otherwise crash
let evt = evt.clone(); let evt = evt.clone();
QUEUE.exec_async(move || handle_key_(&evt)); QUEUE.exec_async(move || handle_key_(&evt));
std::thread::sleep(Duration::from_millis(20));
return; return;
} }
#[cfg(windows)] #[cfg(windows)]
crate::portable_service::client::handle_key(evt); crate::portable_service::client::handle_key(evt);
#[cfg(not(windows))] #[cfg(not(windows))]
handle_key_(evt); handle_key_(evt);
#[cfg(target_os = "macos")]
std::thread::sleep(Duration::from_millis(20));
} }
#[inline] #[inline]
fn reset_input() { fn reset_input() {
unsafe { unsafe {
let _lock = VIRTUAL_INPUT_MTX.lock();
VIRTUAL_INPUT = VirtualInput::new( VIRTUAL_INPUT = VirtualInput::new(
CGEventSourceStateID::Private, CGEventSourceStateID::Private,
CGEventTapLocation::AnnotatedSession, CGEventTapLocation::AnnotatedSession,
@ -738,9 +741,9 @@ fn sim_rdev_rawkey(code: u32, keydown: bool) {
#[inline] #[inline]
fn simulate_(event_type: &EventType) { fn simulate_(event_type: &EventType) {
unsafe { unsafe {
let _lock = VIRTUAL_INPUT_MTX.lock();
if let Some(virtual_input) = &VIRTUAL_INPUT { if let Some(virtual_input) = &VIRTUAL_INPUT {
let _ = virtual_input.simulate(&event_type); let _ = virtual_input.simulate(&event_type);
std::thread::sleep(Duration::from_millis(20));
} }
} }
} }