Refactor Windows context with new anti self-injection mechanism

This commit is contained in:
Federico Terzi 2020-03-08 09:57:21 +01:00
parent 6235377b86
commit fb032deb56
2 changed files with 19 additions and 5 deletions

View File

@ -45,14 +45,14 @@ pub fn new(send_channel: Sender<Event>) -> Box<dyn Context> {
// LINUX IMPLEMENTATION
#[cfg(target_os = "linux")]
pub fn new(send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>,) -> Box<dyn Context> {
pub fn new(send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<dyn Context> {
linux::LinuxContext::new(send_channel, is_injecting)
}
// WINDOWS IMPLEMENTATION
#[cfg(target_os = "windows")]
pub fn new(send_channel: Sender<Event>) -> Box<dyn Context> {
windows::WindowsContext::new(send_channel)
pub fn new(send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<dyn Context> {
windows::WindowsContext::new(send_channel, is_injecting)
}
// espanso directories

View File

@ -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<Event>,
is_injecting: Arc<AtomicBool>,
}
impl WindowsContext {
pub fn new(send_channel: Sender<Event>) -> Box<WindowsContext> {
pub fn new(send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<WindowsContext> {
// 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