diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 06391ad7cd5..bd28971903e 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -457,7 +457,7 @@ static RECORD_CURSOR_POS_RUNNING: AtomicBool = AtomicBool::new(false); // We need to do some special handling for macOS when using the legacy mode. #[cfg(target_os = "macos")] static LAST_KEY_LEGACY_MODE: AtomicBool = AtomicBool::new(true); -// We use enigo to +// We use enigo to // 1. Simulate mouse events // 2. Simulate the legacy mode key events // 3. Simulate the functioin key events, like LockScreen @@ -667,7 +667,17 @@ fn is_pressed(key: &Key, en: &mut Enigo) -> bool { #[inline] #[cfg(target_os = "macos")] fn key_sleep() { - std::thread::sleep(Duration::from_millis(20)); + // https://www.reddit.com/r/rustdesk/comments/1kn1w5x/typing_lags_when_connecting_to_macos_clients/ + // + // There's a strange bug when running by `launchctl load -w /Library/LaunchAgents/abc.plist` + // `std::thread::sleep(Duration::from_millis(20));` may sleep 90ms or more. + // Though `/Applications/RustDesk.app/Contents/MacOS/rustdesk --server` in terminal is ok. + let now = Instant::now(); + // This workaround results `21~24ms` sleep time in my tests. + // But it works well in my tests. + while now.elapsed() < Duration::from_millis(12) { + std::thread::sleep(Duration::from_millis(1)); + } } #[inline] @@ -691,8 +701,8 @@ fn get_modifier_state(key: Key, en: &mut Enigo) -> bool { pub fn handle_mouse(evt: &MouseEvent, conn: i32) { #[cfg(target_os = "macos")] - if !is_server() { - // having GUI, run main GUI thread, otherwise crash + { + // having GUI (--server has tray, it is GUI too), run main GUI thread, otherwise crash let evt = evt.clone(); QUEUE.exec_async(move || handle_mouse_(&evt, conn)); return; @@ -706,7 +716,7 @@ pub fn handle_mouse(evt: &MouseEvent, conn: i32) { // to-do: merge handle_mouse and handle_pointer pub fn handle_pointer(evt: &PointerDeviceEvent, conn: i32) { #[cfg(target_os = "macos")] - if !is_server() { + { // having GUI, run main GUI thread, otherwise crash let evt = evt.clone(); QUEUE.exec_async(move || handle_pointer_(&evt, conn)); @@ -1205,11 +1215,7 @@ fn reset_input() { #[cfg(target_os = "macos")] pub fn reset_input_ondisconn() { - if !is_server() { - QUEUE.exec_async(reset_input); - } else { - reset_input(); - } + QUEUE.exec_async(reset_input); } fn sim_rdev_rawkey_position(code: KeyCode, keydown: bool) {