Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for Wayland clipboard #233

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions src/bin/silicon/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use {
};
#[cfg(target_os = "macos")]
use {image::ImageOutputFormat, pasteboard::Pasteboard};

#[cfg(target_os = "linux")]
use {image::ImageOutputFormat, std::process::Command};

Expand All @@ -24,18 +25,43 @@ use silicon::directories::PROJECT_DIRS;

#[cfg(target_os = "linux")]
pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
let mut temp = tempfile::NamedTempFile::new()?;
image.write_to(&mut temp, ImageOutputFormat::Png)?;
Command::new("xclip")
.args([
"-sel",
"clip",
"-t",
"image/png",
temp.path().to_str().unwrap(),
])
.status()
.map_err(|e| format_err!("Failed to copy image to clipboard : {} (Tip: do you have xclip installed ?)", e))?;
use std::io::{Cursor, Write};

match std::env::var(r#"XDG_SESSION_TYPE"#).ok() {
Some(x) if x == "wayland" => {
let mut command = Command::new("wl-copy")
.args(["--type", "image/png"])
.stdin(std::process::Stdio::piped())
.spawn()?;

let mut cursor = Cursor::new(Vec::new());
image.write_to(&mut cursor, ImageOutputFormat::Png)?;

{
let stdin = command.stdin.as_mut().unwrap();
stdin.write_all(cursor.get_ref())?;
}

command
.wait()
.map_err(|e| format_err!("Failed to copy image to clipboard: {}", e))?;
}
_ => {
let mut temp = tempfile::NamedTempFile::new()?;
image.write_to(&mut temp, ImageOutputFormat::Png)?;

Command::new(r#"xclip"#)
.args([
"-sel",
"clip",
"-t",
"image/png",
temp.path().to_str().unwrap(),
])
.status()
.map_err(|e| format_err!("Failed to copy image to clipboard: {} (Tip: do you have xclip installed ?)", e))?;
}
};
Ok(())
}

Expand Down
Loading