Skip to content

Commit

Permalink
refactor: update deps and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Dec 18, 2024
1 parent 9d04aa1 commit 6fe07d9
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 41 deletions.
121 changes: 94 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions src/foreground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
27 changes: 15 additions & 12 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,7 +22,7 @@ use windows::Win32::{
},
};

static mut KEYBOARD_STATE: Vec<HotKeyState> = vec![];
static KEYBOARD_STATE: LazyLock<Mutex<Vec<HotKeyState>>> = 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;
Expand All @@ -33,22 +35,23 @@ pub struct KeyboardListener {
impl KeyboardListener {
pub fn init(hwnd: HWND, hotkeys: &[&Hotkey]) -> Result<Self> {
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}"))?;
SetWindowsHookExW(WH_KEYBOARD_LL, Some(keyboard_proc), hinstance, 0)
}
.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 })
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down

0 comments on commit 6fe07d9

Please sign in to comment.