Skip to content

Commit

Permalink
feat: Add support for Wayland clipboard (#233)
Browse files Browse the repository at this point in the history
Co-authored-by: Aloxaf <[email protected]>
  • Loading branch information
Krapaince and Aloxaf authored Nov 22, 2023
1 parent 1a9077a commit 1f5f0e8
Showing 1 changed file with 38 additions and 12 deletions.
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

0 comments on commit 1f5f0e8

Please sign in to comment.