Skip to content

Commit

Permalink
Merge pull request #886 from federico-terzi/dev
Browse files Browse the repository at this point in the history
v2.1.1-alpha
  • Loading branch information
federico-terzi committed Nov 27, 2021
2 parents f7fdb06 + 73214cb commit 283b858
Show file tree
Hide file tree
Showing 60 changed files with 2,660 additions and 889 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions espanso-clipboard/src/cocoa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{
path::PathBuf,
};

use crate::Clipboard;
use crate::{Clipboard, ClipboardOperationOptions};
use anyhow::Result;
use log::error;
use thiserror::Error;
Expand All @@ -38,7 +38,7 @@ impl CocoaClipboard {
}

impl Clipboard for CocoaClipboard {
fn get_text(&self) -> Option<String> {
fn get_text(&self, _: &ClipboardOperationOptions) -> Option<String> {
let mut buffer: [i8; 2048] = [0; 2048];
let native_result =
unsafe { ffi::clipboard_get_text(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) };
Expand All @@ -50,7 +50,7 @@ impl Clipboard for CocoaClipboard {
}
}

fn set_text(&self, text: &str) -> anyhow::Result<()> {
fn set_text(&self, text: &str, _: &ClipboardOperationOptions) -> anyhow::Result<()> {
let string = CString::new(text)?;
let native_result = unsafe { ffi::clipboard_set_text(string.as_ptr()) };
if native_result > 0 {
Expand All @@ -60,7 +60,11 @@ impl Clipboard for CocoaClipboard {
}
}

fn set_image(&self, image_path: &std::path::Path) -> anyhow::Result<()> {
fn set_image(
&self,
image_path: &std::path::Path,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
if !image_path.exists() || !image_path.is_file() {
return Err(CocoaClipboardError::ImageNotFound(image_path.to_path_buf()).into());
}
Expand All @@ -75,7 +79,12 @@ impl Clipboard for CocoaClipboard {
}
}

fn set_html(&self, html: &str, fallback_text: Option<&str>) -> anyhow::Result<()> {
fn set_html(
&self,
html: &str,
fallback_text: Option<&str>,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
let html_string = CString::new(html)?;
let fallback_string = CString::new(fallback_text.unwrap_or_default())?;
let fallback_ptr = if fallback_text.is_some() {
Expand Down
30 changes: 24 additions & 6 deletions espanso-clipboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,28 @@ mod wayland;
mod cocoa;

pub trait Clipboard {
fn get_text(&self) -> Option<String>;
fn set_text(&self, text: &str) -> Result<()>;
fn set_image(&self, image_path: &Path) -> Result<()>;
fn set_html(&self, html: &str, fallback_text: Option<&str>) -> Result<()>;
fn get_text(&self, options: &ClipboardOperationOptions) -> Option<String>;
fn set_text(&self, text: &str, options: &ClipboardOperationOptions) -> Result<()>;
fn set_image(&self, image_path: &Path, options: &ClipboardOperationOptions) -> Result<()>;
fn set_html(
&self,
html: &str,
fallback_text: Option<&str>,
options: &ClipboardOperationOptions,
) -> Result<()>;
}

#[allow(dead_code)]
pub struct ClipboardOperationOptions {
pub use_xclip_backend: bool,
}

impl Default for ClipboardOperationOptions {
fn default() -> Self {
Self {
use_xclip_backend: false,
}
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -74,8 +92,8 @@ pub fn get_clipboard(_: ClipboardOptions) -> Result<Box<dyn Clipboard>> {
#[cfg(target_os = "linux")]
#[cfg(not(feature = "wayland"))]
pub fn get_clipboard(_: ClipboardOptions) -> Result<Box<dyn Clipboard>> {
info!("using X11NativeClipboard");
Ok(Box::new(x11::native::X11NativeClipboard::new()?))
info!("using X11Clipboard");
Ok(Box::new(x11::X11Clipboard::new()?))
}

#[cfg(target_os = "linux")]
Expand Down
19 changes: 14 additions & 5 deletions espanso-clipboard/src/wayland/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{
process::Stdio,
};

use crate::{Clipboard, ClipboardOptions};
use crate::{Clipboard, ClipboardOperationOptions, ClipboardOptions};
use anyhow::Result;
use log::{error, warn};
use std::process::Command;
Expand Down Expand Up @@ -74,7 +74,7 @@ impl WaylandFallbackClipboard {
}

impl Clipboard for WaylandFallbackClipboard {
fn get_text(&self) -> Option<String> {
fn get_text(&self, _: &ClipboardOperationOptions) -> Option<String> {
let timeout = std::time::Duration::from_millis(self.command_timeout);
match Command::new("wl-paste")
.arg("--no-newline")
Expand Down Expand Up @@ -116,11 +116,15 @@ impl Clipboard for WaylandFallbackClipboard {
}
}

fn set_text(&self, text: &str) -> anyhow::Result<()> {
fn set_text(&self, text: &str, _: &ClipboardOperationOptions) -> anyhow::Result<()> {
self.invoke_command_with_timeout(&mut Command::new("wl-copy"), text.as_bytes(), "wl-copy")
}

fn set_image(&self, image_path: &std::path::Path) -> anyhow::Result<()> {
fn set_image(
&self,
image_path: &std::path::Path,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
if !image_path.exists() || !image_path.is_file() {
return Err(WaylandFallbackClipboardError::ImageNotFound(image_path.to_path_buf()).into());
}
Expand All @@ -137,7 +141,12 @@ impl Clipboard for WaylandFallbackClipboard {
)
}

fn set_html(&self, html: &str, _fallback_text: Option<&str>) -> anyhow::Result<()> {
fn set_html(
&self,
html: &str,
_fallback_text: Option<&str>,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
self.invoke_command_with_timeout(
&mut Command::new("wl-copy").arg("--type").arg("text/html"),
html.as_bytes(),
Expand Down
19 changes: 14 additions & 5 deletions espanso-clipboard/src/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod ffi;

use std::{ffi::CString, path::PathBuf};

use crate::Clipboard;
use crate::{Clipboard, ClipboardOperationOptions};
use anyhow::Result;
use log::error;
use thiserror::Error;
Expand All @@ -36,7 +36,7 @@ impl Win32Clipboard {
}

impl Clipboard for Win32Clipboard {
fn get_text(&self) -> Option<String> {
fn get_text(&self, _: &ClipboardOperationOptions) -> Option<String> {
let mut buffer: [u16; 2048] = [0; 2048];
let native_result =
unsafe { ffi::clipboard_get_text(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) };
Expand All @@ -48,7 +48,7 @@ impl Clipboard for Win32Clipboard {
}
}

fn set_text(&self, text: &str) -> anyhow::Result<()> {
fn set_text(&self, text: &str, _: &ClipboardOperationOptions) -> anyhow::Result<()> {
let string = U16CString::from_str(text)?;
let native_result = unsafe { ffi::clipboard_set_text(string.as_ptr()) };
if native_result > 0 {
Expand All @@ -58,7 +58,11 @@ impl Clipboard for Win32Clipboard {
}
}

fn set_image(&self, image_path: &std::path::Path) -> anyhow::Result<()> {
fn set_image(
&self,
image_path: &std::path::Path,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
if !image_path.exists() || !image_path.is_file() {
return Err(Win32ClipboardError::ImageNotFound(image_path.to_path_buf()).into());
}
Expand All @@ -73,7 +77,12 @@ impl Clipboard for Win32Clipboard {
}
}

fn set_html(&self, html: &str, fallback_text: Option<&str>) -> anyhow::Result<()> {
fn set_html(
&self,
html: &str,
fallback_text: Option<&str>,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
let html_descriptor = generate_html_descriptor(html);
let html_string = CString::new(html_descriptor)?;
let fallback_string = U16CString::from_str(fallback_text.unwrap_or_default())?;
Expand Down
64 changes: 63 additions & 1 deletion espanso-clipboard/src/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,66 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/

pub(crate) mod native;
use anyhow::Result;

use crate::{Clipboard, ClipboardOperationOptions};

mod native;
mod xclip;

pub(crate) struct X11Clipboard {
native_backend: native::X11NativeClipboard,
xclip_backend: xclip::XClipClipboard,
}

impl X11Clipboard {
pub fn new() -> Result<Self> {
Ok(Self {
native_backend: native::X11NativeClipboard::new()?,
xclip_backend: xclip::XClipClipboard::new(),
})
}
}

impl Clipboard for X11Clipboard {
fn get_text(&self, options: &ClipboardOperationOptions) -> Option<String> {
if options.use_xclip_backend {
self.xclip_backend.get_text(options)
} else {
self.native_backend.get_text(options)
}
}

fn set_text(&self, text: &str, options: &ClipboardOperationOptions) -> anyhow::Result<()> {
if options.use_xclip_backend {
self.xclip_backend.set_text(text, options)
} else {
self.native_backend.set_text(text, options)
}
}

fn set_image(
&self,
image_path: &std::path::Path,
options: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
if options.use_xclip_backend {
self.xclip_backend.set_image(image_path, options)
} else {
self.native_backend.set_image(image_path, options)
}
}

fn set_html(
&self,
html: &str,
fallback_text: Option<&str>,
options: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
if options.use_xclip_backend {
self.xclip_backend.set_html(html, fallback_text, options)
} else {
self.native_backend.set_html(html, fallback_text, options)
}
}
}
19 changes: 14 additions & 5 deletions espanso-clipboard/src/x11/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
path::PathBuf,
};

use crate::Clipboard;
use crate::{Clipboard, ClipboardOperationOptions};
use anyhow::Result;
use std::os::raw::c_char;
use thiserror::Error;
Expand All @@ -39,7 +39,7 @@ impl X11NativeClipboard {
}

impl Clipboard for X11NativeClipboard {
fn get_text(&self) -> Option<String> {
fn get_text(&self, _: &ClipboardOperationOptions) -> Option<String> {
let mut buffer: [c_char; 2048] = [0; 2048];
let native_result =
unsafe { ffi::clipboard_x11_get_text(buffer.as_mut_ptr(), (buffer.len() - 1) as i32) };
Expand All @@ -51,7 +51,7 @@ impl Clipboard for X11NativeClipboard {
}
}

fn set_text(&self, text: &str) -> anyhow::Result<()> {
fn set_text(&self, text: &str, _: &ClipboardOperationOptions) -> anyhow::Result<()> {
let string = CString::new(text)?;
let native_result = unsafe { ffi::clipboard_x11_set_text(string.as_ptr()) };
if native_result > 0 {
Expand All @@ -61,7 +61,11 @@ impl Clipboard for X11NativeClipboard {
}
}

fn set_image(&self, image_path: &std::path::Path) -> anyhow::Result<()> {
fn set_image(
&self,
image_path: &std::path::Path,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
if !image_path.exists() || !image_path.is_file() {
return Err(X11NativeClipboardError::ImageNotFound(image_path.to_path_buf()).into());
}
Expand All @@ -80,7 +84,12 @@ impl Clipboard for X11NativeClipboard {
}
}

fn set_html(&self, html: &str, fallback_text: Option<&str>) -> anyhow::Result<()> {
fn set_html(
&self,
html: &str,
fallback_text: Option<&str>,
_: &ClipboardOperationOptions,
) -> anyhow::Result<()> {
let html_string = CString::new(html)?;
let fallback_string = CString::new(fallback_text.unwrap_or_default())?;
let fallback_ptr = if fallback_text.is_some() {
Expand Down

0 comments on commit 283b858

Please sign in to comment.