From 08853451c0c705e42322067c71f5a6859d251d94 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 21 Nov 2021 19:40:53 +0100 Subject: [PATCH] feat(core): wire up alternative x11 xclip clipboard backend and create patch for gedit. Fix #882 --- espanso/src/cli/worker/config.rs | 15 ++++++- .../dispatch/executor/clipboard_injector.rs | 38 +++++++++++++---- espanso/src/cli/worker/engine/mod.rs | 2 +- .../middleware/render/extension/clipboard.rs | 23 +++++++++-- espanso/src/patch/mod.rs | 1 + espanso/src/patch/patches/linux/gedit_x11.rs | 41 +++++++++++++++++++ espanso/src/patch/patches/linux/mod.rs | 1 + espanso/src/patch/patches/mod.rs | 1 + 8 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 espanso/src/patch/patches/linux/gedit_x11.rs diff --git a/espanso/src/cli/worker/config.rs b/espanso/src/cli/worker/config.rs index 408782a..dd69176 100644 --- a/espanso/src/cli/worker/config.rs +++ b/espanso/src/cli/worker/config.rs @@ -25,7 +25,10 @@ use espanso_config::{ }; use espanso_info::{AppInfo, AppInfoProvider}; -use super::builtin::is_builtin_match; +use super::{ + builtin::is_builtin_match, + engine::process::middleware::render::extension::clipboard::ClipboardOperationOptionsProvider, +}; pub struct ConfigManager<'a> { config_store: &'a dyn ConfigStore, @@ -139,6 +142,16 @@ impl<'a> super::engine::dispatch::executor::clipboard_injector::ClipboardParamsP disable_x11_fast_inject: active.disable_x11_fast_inject(), restore_clipboard: active.preserve_clipboard(), restore_clipboard_delay: active.restore_clipboard_delay(), + x11_use_xclip_backend: active.x11_use_xclip_backend(), + } + } +} + +impl<'a> ClipboardOperationOptionsProvider for ConfigManager<'a> { + fn get_operation_options(&self) -> espanso_clipboard::ClipboardOperationOptions { + let active = self.active(); + espanso_clipboard::ClipboardOperationOptions { + use_xclip_backend: active.x11_use_xclip_backend(), } } } diff --git a/espanso/src/cli/worker/engine/dispatch/executor/clipboard_injector.rs b/espanso/src/cli/worker/engine/dispatch/executor/clipboard_injector.rs index 8ebfd54..163245b 100644 --- a/espanso/src/cli/worker/engine/dispatch/executor/clipboard_injector.rs +++ b/espanso/src/cli/worker/engine/dispatch/executor/clipboard_injector.rs @@ -19,7 +19,7 @@ use std::{convert::TryInto, path::PathBuf}; -use espanso_clipboard::Clipboard; +use espanso_clipboard::{Clipboard, ClipboardOperationOptions}; use espanso_inject::{keys::Key, InjectionOptions, Injector}; use log::error; @@ -39,6 +39,7 @@ pub struct ClipboardParams { pub disable_x11_fast_inject: bool, pub restore_clipboard: bool, pub restore_clipboard_delay: usize, + pub x11_use_xclip_backend: bool, } pub struct ClipboardInjectorAdapter<'a> { @@ -103,11 +104,19 @@ impl<'a> ClipboardInjectorAdapter<'a> { Some(ClipboardRestoreGuard::lock( self.clipboard, params.restore_clipboard_delay.try_into().unwrap(), + self.get_operation_options(), )) } else { None } } + + fn get_operation_options(&self) -> ClipboardOperationOptions { + let params = self.params_provider.get(); + ClipboardOperationOptions { + use_xclip_backend: params.x11_use_xclip_backend, + } + } } impl<'a> TextInjector for ClipboardInjectorAdapter<'a> { @@ -118,7 +127,9 @@ impl<'a> TextInjector for ClipboardInjectorAdapter<'a> { fn inject_text(&self, text: &str) -> anyhow::Result<()> { let _guard = self.restore_clipboard_guard(); - self.clipboard.set_text(text)?; + self + .clipboard + .set_text(text, &self.get_operation_options())?; self.send_paste_combination()?; @@ -130,7 +141,9 @@ impl<'a> HtmlInjector for ClipboardInjectorAdapter<'a> { fn inject_html(&self, html: &str, fallback_text: &str) -> anyhow::Result<()> { let _guard = self.restore_clipboard_guard(); - self.clipboard.set_html(html, Some(fallback_text))?; + self + .clipboard + .set_html(html, Some(fallback_text), &self.get_operation_options())?; self.send_paste_combination()?; @@ -153,7 +166,9 @@ impl<'a> ImageInjector for ClipboardInjectorAdapter<'a> { let _guard = self.restore_clipboard_guard(); - self.clipboard.set_image(&path)?; + self + .clipboard + .set_image(&path, &self.get_operation_options())?; self.send_paste_combination()?; @@ -165,16 +180,22 @@ struct ClipboardRestoreGuard<'a> { clipboard: &'a dyn Clipboard, content: Option, restore_delay: u64, + clipboard_operation_options: ClipboardOperationOptions, } impl<'a> ClipboardRestoreGuard<'a> { - pub fn lock(clipboard: &'a dyn Clipboard, restore_delay: u64) -> Self { - let clipboard_content = clipboard.get_text(); + pub fn lock( + clipboard: &'a dyn Clipboard, + restore_delay: u64, + clipboard_operation_options: ClipboardOperationOptions, + ) -> Self { + let clipboard_content = clipboard.get_text(&clipboard_operation_options); Self { clipboard, content: clipboard_content, restore_delay, + clipboard_operation_options, } } } @@ -186,7 +207,10 @@ impl<'a> Drop for ClipboardRestoreGuard<'a> { // A delay is needed to mitigate the problem std::thread::sleep(std::time::Duration::from_millis(self.restore_delay)); - if let Err(error) = self.clipboard.set_text(&content) { + if let Err(error) = self + .clipboard + .set_text(&content, &self.clipboard_operation_options) + { error!( "unable to restore clipboard content after expansion: {}", error diff --git a/espanso/src/cli/worker/engine/mod.rs b/espanso/src/cli/worker/engine/mod.rs index 7449bda..513bff5 100644 --- a/espanso/src/cli/worker/engine/mod.rs +++ b/espanso/src/cli/worker/engine/mod.rs @@ -181,7 +181,7 @@ pub fn initialize_and_spawn( let clipboard = espanso_clipboard::get_clipboard(Default::default()) .expect("failed to initialize clipboard module"); // TODO: handle options - let clipboard_adapter = ClipboardAdapter::new(&*clipboard); + let clipboard_adapter = ClipboardAdapter::new(&*clipboard, &config_manager); let clipboard_extension = espanso_render::extension::clipboard::ClipboardExtension::new(&clipboard_adapter); let date_extension = espanso_render::extension::date::DateExtension::new(); diff --git a/espanso/src/cli/worker/engine/process/middleware/render/extension/clipboard.rs b/espanso/src/cli/worker/engine/process/middleware/render/extension/clipboard.rs index 57b2c91..999b308 100644 --- a/espanso/src/cli/worker/engine/process/middleware/render/extension/clipboard.rs +++ b/espanso/src/cli/worker/engine/process/middleware/render/extension/clipboard.rs @@ -17,21 +17,36 @@ * along with espanso. If not, see . */ -use espanso_clipboard::Clipboard; +use espanso_clipboard::{Clipboard, ClipboardOperationOptions}; use espanso_render::extension::clipboard::ClipboardProvider; +pub trait ClipboardOperationOptionsProvider { + fn get_operation_options(&self) -> ClipboardOperationOptions; +} + pub struct ClipboardAdapter<'a> { clipboard: &'a dyn Clipboard, + clipboard_operation_options_provider: &'a dyn ClipboardOperationOptionsProvider, } impl<'a> ClipboardAdapter<'a> { - pub fn new(clipboard: &'a dyn Clipboard) -> Self { - Self { clipboard } + pub fn new( + clipboard: &'a dyn Clipboard, + clipboard_operation_options_provider: &'a dyn ClipboardOperationOptionsProvider, + ) -> Self { + Self { + clipboard, + clipboard_operation_options_provider, + } } } impl<'a> ClipboardProvider for ClipboardAdapter<'a> { fn get_text(&self) -> Option { - self.clipboard.get_text() + self.clipboard.get_text( + &self + .clipboard_operation_options_provider + .get_operation_options(), + ) } } diff --git a/espanso/src/patch/mod.rs b/espanso/src/patch/mod.rs index f1ce634..fa6d065 100644 --- a/espanso/src/patch/mod.rs +++ b/espanso/src/patch/mod.rs @@ -42,6 +42,7 @@ fn get_builtin_patches() -> Vec { return vec![ patches::linux::alacritty_terminal_x11::patch(), patches::linux::emacs_x11::patch(), + patches::linux::gedit_x11::patch(), patches::linux::generic_terminal_x11::patch(), patches::linux::kitty_terminal_x11::patch(), patches::linux::konsole_terminal_x11::patch(), diff --git a/espanso/src/patch/patches/linux/gedit_x11.rs b/espanso/src/patch/patches/linux/gedit_x11.rs new file mode 100644 index 0000000..458f9c9 --- /dev/null +++ b/espanso/src/patch/patches/linux/gedit_x11.rs @@ -0,0 +1,41 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use std::sync::Arc; + +use crate::patch::patches::{PatchedConfig, Patches}; +use crate::patch::PatchDefinition; + +pub fn patch() -> PatchDefinition { + PatchDefinition { + name: module_path!().split(':').last().unwrap_or("unknown"), + is_enabled: || cfg!(target_os = "linux") && !super::util::is_wayland(), + should_patch: |app| app.class.unwrap_or_default().contains("Gedit"), + apply: |base, name| { + Arc::new(PatchedConfig::patch( + base, + name, + Patches { + x11_use_xclip_backend: Some(true), + ..Default::default() + }, + )) + }, + } +} diff --git a/espanso/src/patch/patches/linux/mod.rs b/espanso/src/patch/patches/linux/mod.rs index 3d42011..1e0bc94 100644 --- a/espanso/src/patch/patches/linux/mod.rs +++ b/espanso/src/patch/patches/linux/mod.rs @@ -19,6 +19,7 @@ pub mod alacritty_terminal_x11; pub mod emacs_x11; +pub mod gedit_x11; pub mod generic_terminal_x11; pub mod kitty_terminal_x11; pub mod konsole_terminal_x11; diff --git a/espanso/src/patch/patches/mod.rs b/espanso/src/patch/patches/mod.rs index c6dd4f7..e45567d 100644 --- a/espanso/src/patch/patches/mod.rs +++ b/espanso/src/patch/patches/mod.rs @@ -50,5 +50,6 @@ generate_patchable_config!( undo_backspace -> bool, win32_exclude_orphan_events -> bool, win32_keyboard_layout_cache_interval -> i64, + x11_use_xclip_backend -> bool, keyboard_layout -> Option );