diff --git a/Cargo.lock b/Cargo.lock index 7a6eb11..cb779a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,18 +1,30 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anyhow" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "cc" -version = "1.2.1" +version = "1.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf" dependencies = [ "shlex", ] @@ -97,18 +109,18 @@ checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "hashbrown" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "indexmap" -version = "2.6.0" +version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" dependencies = [ "equivalent", - "hashbrown 0.15.1", + "hashbrown 0.15.2", ] [[package]] @@ -129,9 +141,19 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.162" +version = "0.2.168" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] [[package]] name = "log" @@ -161,11 +183,34 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.5.8", + "smallvec", + "windows-targets 0.52.6", +] + [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -185,6 +230,15 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "redox_syscall" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" +dependencies = [ + "bitflags", +] + [[package]] name = "rust-ini" version = "0.21.1" @@ -205,26 +259,32 @@ dependencies = [ "semver", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "semver" -version = "1.0.23" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba" [[package]] name = "serde" -version = "1.0.215" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.215" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", @@ -257,11 +317,17 @@ dependencies = [ "thread-id", ] +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + [[package]] name = "syn" -version = "2.0.87" +version = "2.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" dependencies = [ "proc-macro2", "quote", @@ -275,7 +341,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7fbf4c9d56b320106cd64fd024dadfa0be7cb4706725fc44a7d7ce952d820c1" dependencies = [ "libc", - "redox_syscall", + "redox_syscall 0.1.57", "winapi", ] @@ -330,9 +396,9 @@ checksum = "343e926fc669bc8cde4fa3129ab681c63671bae288b1f1081ceee6d9d37904fc" [[package]] name = "unicode-ident" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "vswhom" @@ -391,6 +457,7 @@ dependencies = [ "indexmap", "log", "once_cell", + "parking_lot", "rust-ini", "simple-logging", "windows", @@ -621,6 +688,6 @@ dependencies = [ [[package]] name = "xml-rs" -version = "0.8.23" +version = "0.8.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af310deaae937e48a26602b730250b4949e125f468f11e6990be3e5304ddd96f" +checksum = "ea8b391c9a790b496184c29f7f93b9ed5b16abb306c05415b68bcc16e4d06432" diff --git a/Cargo.toml b/Cargo.toml index 73bee64..9555353 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ anyhow = "1" indexmap = "2.2.3" log = "0.4.20" once_cell = "1.18.0" +parking_lot = "0.12.3" rust-ini = "0.21.0" simple-logging = "2.0.2" xml = "0.8.10" diff --git a/src/foreground.rs b/src/foreground.rs index 10fe020..7dd4d14 100644 --- a/src/foreground.rs +++ b/src/foreground.rs @@ -76,6 +76,7 @@ unsafe extern "system" fn win_event_proc( Some(v) => v.to_lowercase(), None => return, }; - IS_FOREGROUND_IN_BLACKLIST = BLACKLIST.get().unwrap().contains(&exe); - debug!("foreground {exe} {IS_FOREGROUND_IN_BLACKLIST}"); + let is_in_blacklist = BLACKLIST.get().unwrap().contains(&exe); + IS_FOREGROUND_IN_BLACKLIST = is_in_blacklist; + debug!("foreground {exe} {is_in_blacklist}"); } diff --git a/src/keyboard.rs b/src/keyboard.rs index 9d21580..fc3c24b 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -8,6 +8,8 @@ use crate::{ }; use anyhow::{anyhow, Result}; +use parking_lot::Mutex; +use std::sync::LazyLock; use windows::Win32::{ Foundation::{HWND, LPARAM, LRESULT, WPARAM}, System::LibraryLoader::GetModuleHandleW, @@ -20,7 +22,7 @@ use windows::Win32::{ }, }; -static mut KEYBOARD_STATE: Vec = vec![]; +static KEYBOARD_STATE: LazyLock>> = LazyLock::new(|| Mutex::new(Vec::new())); static mut WINDOW: HWND = HWND(0 as _); static mut IS_SHIFT_PRESSED: bool = false; static mut PREVIOUS_KEYCODE: u16 = 0; @@ -33,6 +35,16 @@ pub struct KeyboardListener { impl KeyboardListener { pub fn init(hwnd: HWND, hotkeys: &[&Hotkey]) -> Result { unsafe { WINDOW = hwnd } + + let keyboard_state = hotkeys + .iter() + .map(|hotkey| HotKeyState { + hotkey: (*hotkey).clone(), + is_modifier_pressed: false, + }) + .collect(); + *KEYBOARD_STATE.lock() = keyboard_state; + let hook = unsafe { let hinstance = { GetModuleHandleW(None) } .map_err(|err| anyhow!("Failed to get module handle, {err}"))?; @@ -40,15 +52,6 @@ impl KeyboardListener { } .map_err(|err| anyhow!("Failed to set windows hook, {err}"))?; info!("keyboard listener start"); - unsafe { - KEYBOARD_STATE = hotkeys - .iter() - .map(|hotkey| HotKeyState { - hotkey: (*hotkey).clone(), - is_modifier_pressed: false, - }) - .collect() - } Ok(Self { hook }) } @@ -78,7 +81,7 @@ unsafe extern "system" fn keyboard_proc(code: i32, w_param: WPARAM, l_param: LPA if [VK_LSHIFT, VK_RSHIFT].contains(&vk_code) { IS_SHIFT_PRESSED = is_key_pressed(); } - for state in KEYBOARD_STATE.iter_mut() { + for state in KEYBOARD_STATE.lock().iter_mut() { if state.hotkey.modifier.contains(&vk_code) { is_modifier = true; if is_key_pressed() { @@ -101,7 +104,7 @@ unsafe extern "system" fn keyboard_proc(code: i32, w_param: WPARAM, l_param: LPA } } if !is_modifier { - for state in KEYBOARD_STATE.iter_mut() { + for state in KEYBOARD_STATE.lock().iter_mut() { if is_key_pressed() && state.is_modifier_pressed { let id = state.hotkey.id; if vk_code.0 == state.hotkey.code {