diff --git a/Cargo.lock b/Cargo.lock index 1b2566c33..30757b019 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -547,11 +547,11 @@ dependencies = [ [[package]] name = "ctrlc" -version = "3.1.9" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "232295399409a8b7ae41276757b5a1cc21032848d42bff2352261f958b3ca29a" +checksum = "377c9b002a72a0b2c1a18c62e2f3864bdfea4a015e3683a96e24aa45dd6c02d1" dependencies = [ - "nix 0.20.0", + "nix 0.22.0", "winapi 0.3.9", ] @@ -1727,6 +1727,15 @@ dependencies = [ "libc", ] +[[package]] +name = "memoffset" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" +dependencies = [ + "autocfg 1.0.1", +] + [[package]] name = "miniz_oxide" version = "0.4.4" @@ -1901,6 +1910,19 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1e25ee6b412c2a1e3fcb6a4499a5c1bfe7f43e014bdce9a6b6666e5aa2d187" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "5.1.2" diff --git a/Cargo.toml b/Cargo.toml index 798dd2f13..f0d83cc4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ cpal = { git = "https://github.com/rustaudio/cpal" } machine-uid = "0.2" mac_address = "1.1" sciter-rs = { git = "https://github.com/open-trade/rust-sciter", branch = "dyn" } +ctrlc = "3.2" [target.'cfg(target_os = "windows")'.dependencies] #systray = { git = "https://github.com/open-trade/systray-rs" } @@ -68,7 +69,6 @@ core-graphics = "0.22" libpulse-simple-binding = "2.16" libpulse-binding = "2.16" rust-pulsectl = { git = "https://github.com/open-trade/pulsectl" } -ctrlc = "3.1" [target.'cfg(not(any(target_os = "windows", target_os = "android", target_os = "ios")))'.dependencies] psutil = "3.2" diff --git a/src/server/input_service.rs b/src/server/input_service.rs index e4c4fbc0c..056267c5b 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -3,7 +3,11 @@ use super::*; use dispatch::Queue; use enigo::{Enigo, Key, KeyboardControllable, MouseButton, MouseControllable}; use hbb_common::{config::COMPRESS_LEVEL, protobuf::ProtobufEnumOrUnknown}; -use std::{convert::TryFrom, time::Instant}; +use std::{ + convert::TryFrom, + sync::atomic::{AtomicBool, Ordering}, + time::Instant, +}; #[derive(Default)] struct StateCursor { @@ -160,9 +164,9 @@ fn run_cursor(sp: MouseCursorService, state: &mut StateCursor) -> ResultType<()> lazy_static::lazy_static! { static ref ENIGO: Arc> = Arc::new(Mutex::new(Enigo::new())); static ref KEYS_DOWN: Arc>> = Default::default(); - static ref EXITING: Arc> = Default::default(); static ref LATEST_INPUT: Arc> = Default::default(); } +static EXITING: AtomicBool = AtomicBool::new(false); // mac key input must be run in main thread, otherwise crash on >= osx 10.15 #[cfg(target_os = "macos")] @@ -220,26 +224,23 @@ pub fn fix_key_down_timeout_loop() { std::thread::sleep(std::time::Duration::from_millis(300)); fix_key_down_timeout(false); }); - unsafe { - libc::signal(libc::SIGINT, fix_key_down_timeout_at_signal as _); + if let Err(err) = ctrlc::set_handler(move || { + fix_key_down_timeout_at_exit(); + std::process::exit(0); // will call atexit on posix, but not on Windows + }) { + log::error!("Failed to set Ctrl-C handler: {}", err); } } -pub extern "C" fn fix_key_down_timeout_at_exit() { - let mut exiting = EXITING.lock().unwrap(); - if *exiting { +pub fn fix_key_down_timeout_at_exit() { + if EXITING.load(Ordering::SeqCst) { return; } - *exiting = true; + EXITING.store(true, Ordering::SeqCst); fix_key_down_timeout(true); log::info!("fix_key_down_timeout_at_exit"); } -extern "C" fn fix_key_down_timeout_at_signal(_: libc::c_int) { - fix_key_down_timeout_at_exit(); - std::process::exit(0); // will call atexit on posix, but not on Windows -} - fn fix_key_down_timeout(force: bool) { if KEYS_DOWN.lock().unwrap().is_empty() { return; @@ -317,8 +318,7 @@ fn fix_modifiers(modifiers: &[ProtobufEnumOrUnknown], en: &mut Enigo } fn handle_mouse_(evt: &MouseEvent, conn: i32) { - let exiting = EXITING.lock().unwrap(); - if *exiting { + if EXITING.load(Ordering::SeqCst) { return; } #[cfg(windows)] @@ -523,8 +523,7 @@ pub fn handle_key(evt: &KeyEvent) { } fn handle_key_(evt: &KeyEvent) { - let exiting = EXITING.lock().unwrap(); - if *exiting { + if EXITING.load(Ordering::SeqCst) { return; } #[cfg(windows)]