Refactor Windows context with new anti self-injection mechanism
This commit is contained in:
parent
6235377b86
commit
fb032deb56
|
@ -45,14 +45,14 @@ pub fn new(send_channel: Sender<Event>) -> Box<dyn Context> {
|
||||||
|
|
||||||
// LINUX IMPLEMENTATION
|
// LINUX IMPLEMENTATION
|
||||||
#[cfg(target_os = "linux")]
|
#[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)
|
linux::LinuxContext::new(send_channel, is_injecting)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WINDOWS IMPLEMENTATION
|
// WINDOWS IMPLEMENTATION
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
pub fn new(send_channel: Sender<Event>) -> Box<dyn Context> {
|
pub fn new(send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<dyn Context> {
|
||||||
windows::WindowsContext::new(send_channel)
|
windows::WindowsContext::new(send_channel, is_injecting)
|
||||||
}
|
}
|
||||||
|
|
||||||
// espanso directories
|
// espanso directories
|
||||||
|
|
|
@ -24,17 +24,21 @@ use crate::event::KeyModifier::*;
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
use std::{fs};
|
use std::{fs};
|
||||||
use widestring::{U16CString, U16CStr};
|
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 BMP_BINARY : &[u8] = include_bytes!("../res/win/espanso.bmp");
|
||||||
const ICO_BINARY : &[u8] = include_bytes!("../res/win/espanso.ico");
|
const ICO_BINARY : &[u8] = include_bytes!("../res/win/espanso.ico");
|
||||||
|
|
||||||
pub struct WindowsContext {
|
pub struct WindowsContext {
|
||||||
send_channel: Sender<Event>,
|
send_channel: Sender<Event>,
|
||||||
|
is_injecting: Arc<AtomicBool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowsContext {
|
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
|
// Initialize image resources
|
||||||
|
|
||||||
let espanso_dir = super::get_data_dir();
|
let espanso_dir = super::get_data_dir();
|
||||||
|
@ -68,6 +72,7 @@ impl WindowsContext {
|
||||||
|
|
||||||
let context = Box::new(WindowsContext{
|
let context = Box::new(WindowsContext{
|
||||||
send_channel,
|
send_channel,
|
||||||
|
is_injecting,
|
||||||
});
|
});
|
||||||
|
|
||||||
unsafe {
|
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) {
|
is_modifier: i32, key_code: i32, is_key_down: i32) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let _self = _self as *mut WindowsContext;
|
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_key_down != 0 { // KEY DOWN EVENT
|
||||||
if is_modifier == 0 { // Char event
|
if is_modifier == 0 { // Char event
|
||||||
// Convert the received buffer to a string
|
// Convert the received buffer to a string
|
||||||
|
|
Loading…
Reference in New Issue
Block a user