From fb032deb56e9fd9a12ac89aa84ed15063455f974 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 8 Mar 2020 09:57:21 +0100 Subject: [PATCH] Refactor Windows context with new anti self-injection mechanism --- src/context/mod.rs | 6 +++--- src/context/windows.rs | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index ac9f9fe..dec7eca 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -45,14 +45,14 @@ pub fn new(send_channel: Sender) -> Box { // LINUX IMPLEMENTATION #[cfg(target_os = "linux")] -pub fn new(send_channel: Sender, is_injecting: Arc,) -> Box { +pub fn new(send_channel: Sender, is_injecting: Arc) -> Box { linux::LinuxContext::new(send_channel, is_injecting) } // WINDOWS IMPLEMENTATION #[cfg(target_os = "windows")] -pub fn new(send_channel: Sender) -> Box { - windows::WindowsContext::new(send_channel) +pub fn new(send_channel: Sender, is_injecting: Arc) -> Box { + windows::WindowsContext::new(send_channel, is_injecting) } // espanso directories diff --git a/src/context/windows.rs b/src/context/windows.rs index c6ecf57..a0cf529 100644 --- a/src/context/windows.rs +++ b/src/context/windows.rs @@ -24,17 +24,21 @@ use crate::event::KeyModifier::*; use std::ffi::c_void; use std::{fs}; use widestring::{U16CString, U16CStr}; -use log::{info, error}; +use log::{info, error, debug}; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; +use std::sync::atomic::Ordering::Acquire; const BMP_BINARY : &[u8] = include_bytes!("../res/win/espanso.bmp"); const ICO_BINARY : &[u8] = include_bytes!("../res/win/espanso.ico"); pub struct WindowsContext { send_channel: Sender, + is_injecting: Arc, } impl WindowsContext { - pub fn new(send_channel: Sender) -> Box { + pub fn new(send_channel: Sender, is_injecting: Arc) -> Box { // Initialize image resources let espanso_dir = super::get_data_dir(); @@ -68,6 +72,7 @@ impl WindowsContext { let context = Box::new(WindowsContext{ send_channel, + is_injecting, }); unsafe { @@ -106,6 +111,15 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32 is_modifier: i32, key_code: i32, is_key_down: i32) { unsafe { let _self = _self as *mut WindowsContext; + + // If espanso is currently injecting text, we should avoid processing + // external events, as it could happen that espanso reinterpret its + // own input. + if (*_self).is_injecting.load(Acquire) { + debug!("Input ignored while espanso is injecting text..."); + return; + } + if is_key_down != 0 { // KEY DOWN EVENT if is_modifier == 0 { // Char event // Convert the received buffer to a string