projects/aports·blob master · refs

159 lines · 6.9 KiB

1Patch-Source: https://gitlab.com/timvisee/ffsend/-/merge_requests/40
2--
3From 1de1b3a8572b48fb5041ba36af81495efcf81515 Mon Sep 17 00:00:00 2001
4From: Jakub Jirutka <[email protected]>
5Date: Sun, 9 Oct 2022 19:49:46 +0200
6Subject: [PATCH] Add support for clipboard on Wayland using wl-copy(1)
7
8wl-copy(1) from the wl-clipboard package is equivalent of xcopy(1)
9on Wayland desktops.
10---
11 Cargo.toml | 2 +-
12 README.md | 8 ++++----
13 build.rs | 3 ++-
14 src/util.rs | 36 +++++++++++++++++++++++++++++++-----
15 4 files changed, 38 insertions(+), 11 deletions(-)
16
17diff --git a/Cargo.toml b/Cargo.toml
18index b91b9cd..e434248 100644
19--- a/Cargo.toml
20+++ b/Cargo.toml
21@@ -98,7 +98,7 @@ infer-command = []
22 # Compile without colored output support
23 no-color = ["colored/no-color"]
24
25-# Automatic using build.rs: use xclip/xsel binary method for clipboard support
26+# Automatic using build.rs: use wl-copy/xclip/xsel binary method for clipboard support
27 clipboard-bin = ["clipboard"]
28
29 # Automatic using build.rs: use native clipboard crate for clipboard support
30diff --git a/README.md b/README.md
31index cc9caf0..257cfee 100644
32--- a/README.md
33+++ b/README.md
34@@ -140,10 +140,10 @@ all available subcommands.
35 - Linux:
36 - OpenSSL & CA certificates:
37 - Ubuntu, Debian and derivatives: `apt install openssl ca-certificates`
38- - Optional: `xclip` or `xsel` for clipboard support
39- - Ubuntu, Debian and derivatives: `apt install xclip`
40- - CentOS/Red Hat/openSUSE/Fedora: `yum install xclip`
41- - Arch: `pacman -S xclip`
42+ - Optional: `wl-copy` (on Wayland), `xclip` (on Xorg) or `xsel` (on Xorg) for clipboard support
43+ - Ubuntu, Debian and derivatives: `apt install wl-clipboard` or `apt install xclip`
44+ - CentOS/Red Hat/openSUSE/Fedora: `yum install wl-clipboard` or `yum install xclip`
45+ - Arch: `pacman -S wl-clipboard` or `pacman -S xclip`
46 - Windows specific:
47 - Optional OpenSSL with `crypto-openssl` feature: [» Installer][openssl-windows-installer] (`v1.1.0j` or above)
48 - macOS specific:
49diff --git a/build.rs b/build.rs
50index 32b3d32..dd4006c 100644
51--- a/build.rs
52+++ b/build.rs
53@@ -14,7 +14,8 @@ fn main() {
54 #[cfg(not(feature = "clipboard-crate"))]
55 println!("cargo:rustc-cfg=feature=\"clipboard-bin\"");
56
57- // xclip and xsel paths are inserted at compile time
58+ // wl-copy, xclip and xsel paths are inserted at compile time
59+ println!("cargo:rerun-if-env-changed=WL_COPY_PATH");
60 println!("cargo:rerun-if-env-changed=XCLIP_PATH");
61 println!("cargo:rerun-if-env-changed=XSEL_PATH");
62 }
63diff --git a/src/util.rs b/src/util.rs
64index d4dad97..d7e6237 100644
65--- a/src/util.rs
66+++ b/src/util.rs
67@@ -327,6 +327,12 @@ pub enum ClipboardType {
68 #[cfg(feature = "clipboard-crate")]
69 Native,
70
71+ /// Manage clipboard through `wl-copy` (wl-clipboard) on Linux.
72+ ///
73+ /// May contain a binary path if specified at compile time through the `WL_COPY_PATH` variable.
74+ #[cfg(feature = "clipboard-bin")]
75+ Wlcopy(Option<String>),
76+
77 /// Manage clipboard through `xclip` on Linux.
78 ///
79 /// May contain a binary path if specified at compile time through the `XCLIP_PATH` variable.
80@@ -351,10 +357,14 @@ impl ClipboardType {
81
82 #[cfg(feature = "clipboard-bin")]
83 {
84- if let Some(path) = option_env!("XCLIP_PATH") {
85+ if let Some(path) = option_env!("WL_COPY_PATH") {
86+ ClipboardType::Wlcopy(Some(path.to_owned()))
87+ } else if let Some(path) = option_env!("XCLIP_PATH") {
88 ClipboardType::Xclip(Some(path.to_owned()))
89 } else if let Some(path) = option_env!("XSEL_PATH") {
90 ClipboardType::Xsel(Some(path.to_owned()))
91+ } else if which("wl-copy").is_ok() {
92+ ClipboardType::Wlcopy(None)
93 } else if which("xclip").is_ok() {
94 ClipboardType::Xclip(None)
95 } else if which("xsel").is_ok() {
96@@ -372,6 +382,8 @@ impl ClipboardType {
97 #[cfg(feature = "clipboard-crate")]
98 ClipboardType::Native => Self::native_set(content),
99 #[cfg(feature = "clipboard-bin")]
100+ ClipboardType::Wlcopy(path) => Self::wlcopy_set(path.clone(), &content),
101+ #[cfg(feature = "clipboard-bin")]
102 ClipboardType::Xclip(path) => Self::xclip_set(path.clone(), &content),
103 #[cfg(feature = "clipboard-bin")]
104 ClipboardType::Xsel(path) => Self::xsel_set(path.clone(), &content),
105@@ -389,6 +401,15 @@ impl ClipboardType {
106 .map_err(ClipboardError::Native)
107 }
108
109+ #[cfg(feature = "clipboard-bin")]
110+ fn wlcopy_set(path: Option<String>, content: &str) -> Result<(), ClipboardError> {
111+ Self::sys_cmd_set(
112+ "wl-copy",
113+ &mut Command::new(path.unwrap_or_else(|| "wl-copy".into())),
114+ content,
115+ )
116+ }
117+
118 #[cfg(feature = "clipboard-bin")]
119 fn xclip_set(path: Option<String>, content: &str) -> Result<(), ClipboardError> {
120 Self::sys_cmd_set(
121@@ -456,6 +477,11 @@ impl fmt::Display for ClipboardType {
122 #[cfg(feature = "clipboard-crate")]
123 ClipboardType::Native => write!(f, "native"),
124 #[cfg(feature = "clipboard-bin")]
125+ ClipboardType::Wlcopy(path) => match path {
126+ None => write!(f, "wl-copy"),
127+ Some(path) => write!(f, "wl-copy ({})", path),
128+ },
129+ #[cfg(feature = "clipboard-bin")]
130 ClipboardType::Xclip(path) => match path {
131 None => write!(f, "xclip"),
132 Some(path) => write!(f, "xclip ({})", path),
133@@ -479,19 +505,19 @@ pub enum ClipboardError {
134 #[fail(display = "failed to access clipboard")]
135 Native(#[cause] Compat<Error>),
136
137- /// The `xclip` or `xsel` binary could not be found on the system, required for clipboard support.
138+ /// The `wl-copy`, `xclip` or `xsel` binary could not be found on the system, required for clipboard support.
139 #[cfg(feature = "clipboard-bin")]
140- #[fail(display = "failed to access clipboard, xclip or xsel is not installed")]
141+ #[fail(display = "failed to access clipboard, neither wl-copy, xclip nor xsel is installed")]
142 NoBinary,
143
144- /// An error occurred while using `xclip` or `xsel` to set the clipboard contents.
145+ /// An error occurred while using `wl-copy`, `xclip` or `xsel` to set the clipboard contents.
146 /// This problem probably occurred when starting, or while piping the clipboard contents to
147 /// the process.
148 #[cfg(feature = "clipboard-bin")]
149 #[fail(display = "failed to access clipboard using {}", _0)]
150 BinaryIo(&'static str, #[cause] IoError),
151
152- /// `xclip` or `xsel` unexpectedly exited with a non-successful status code.
153+ /// `wl-copy`, `xclip` or `xsel` unexpectedly exited with a non-successful status code.
154 #[cfg(feature = "clipboard-bin")]
155 #[fail(
156 display = "failed to use clipboard, {} exited with status code {}",
157--
158GitLab
159