Skip to content

Commit

Permalink
Fix lock contention
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Apr 27, 2022
1 parent 18f9af0 commit 39279db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ simplelog = "0.8.0"
anyhow = "1"
parking_lot = "0.12"
crossbeam-channel = "0.5"
once_cell = "1"

# Using my personal fork for tap_hold_interval and Sequence
keyberon = { git = "https://github.com/jtroo/keyberon", rev = "6e110b7935" }
Expand All @@ -28,7 +29,6 @@ evdev-rs = "0.4.0"
uinput-sys = "0.1.7"

[target.'cfg(target_os = "windows")'.dependencies]
once_cell = "1"
encode_unicode = "0.3.6"
winapi = { version = "0.3.9", features = ["consoleapi", "wincon"] }
native-windows-gui = { version = "1.0.12", features = [
Expand Down
25 changes: 17 additions & 8 deletions src/kanata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pub struct Kanata {
last_tick: time::Instant,
}

#[cfg(target_os = "windows")]
use once_cell::sync::Lazy;

static MAPPED_KEYS: Lazy<Mutex<cfg::MappedKeys>> = Lazy::new(|| Mutex::new([false; 256]));

#[cfg(target_os = "windows")]
static PRESSED_KEYS: Lazy<Mutex<HashSet<OsCode>>> = Lazy::new(|| Mutex::new(HashSet::new()));

Expand Down Expand Up @@ -153,7 +155,8 @@ impl Kanata {
}
Ok(cfg) => {
self.layout = cfg.layout;
self.mapped_keys = cfg.mapped_keys;
let mut mapped_keys = MAPPED_KEYS.lock();
*mapped_keys = cfg.mapped_keys;
self.key_outputs = cfg.key_outputs;
log::info!("Live reload successful")
}
Expand Down Expand Up @@ -241,6 +244,10 @@ impl Kanata {
#[cfg(target_os = "linux")]
pub fn event_loop(kanata: Arc<Mutex<Self>>, tx: Sender<KeyEvent>) -> Result<()> {
info!("Kanata: entering the event loop");
{
let mut mapped_keys = MAPPED_KEYS.lock();
*mapped_keys = kanata.lock().mapped_keys;
}

let kbd_in = match KbdIn::new(&kanata.lock().kbd_in_path) {
Ok(kbd_in) => kbd_in,
Expand All @@ -265,12 +272,10 @@ impl Kanata {
// Check if this keycode is mapped in the configuration. If it hasn't been mapped, send
// it immediately.
let kc: usize = key_event.code.into();
{
if kc >= cfg::MAPPED_KEYS_LEN || !MAPPED_KEYS.lock()[kc] {
let mut kanata = kanata.lock();
if kc >= cfg::MAPPED_KEYS_LEN || !kanata.mapped_keys[kc] {
kanata.kbd_out.write_key(key_event.code, key_event.value)?;
continue;
}
kanata.kbd_out.write_key(key_event.code, key_event.value)?;
continue;
}

// Send key events to the processing loop
Expand All @@ -292,6 +297,10 @@ impl Kanata {
}
};
native_windows_gui::init()?;
{
let mut mapped_keys = MAPPED_KEYS.lock();
*mapped_keys = kanata.lock().mapped_keys;
}

// This callback should return `false` if the input event is **not** handled by the
// callback and `true` if the input event **is** handled by the callback. Returning false
Expand All @@ -301,7 +310,7 @@ impl Kanata {
if input_event.code as usize >= cfg::MAPPED_KEYS_LEN {
return false;
}
if !kanata.lock().mapped_keys[input_event.code as usize] {
if !MAPPED_KEYS.lock()[input_event.code as usize] {
return false;
}

Expand Down

0 comments on commit 39279db

Please sign in to comment.