Skip to content

Commit

Permalink
feat: add get_image_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlock-Holo committed Apr 24, 2022
1 parent a36ba4f commit ca9914e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
.vscode
*.png
.idea
13 changes: 11 additions & 2 deletions src/common_linux.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(feature = "image-data")]
use std::{cell::RefCell, rc::Rc};

#[cfg(feature = "wayland-data-control")]
use crate::wayland_data_control_clipboard::WaylandDataControlClipboardContext;
#[cfg(feature = "wayland-data-control")]
use log::{info, warn};

#[cfg(feature = "wayland-data-control")]
use crate::wayland_data_control_clipboard::WaylandDataControlClipboardContext;
#[cfg(feature = "image-data")]
use crate::ImageData;
use crate::{x11_clipboard::X11ClipboardContext, Error};
Expand Down Expand Up @@ -202,6 +202,15 @@ impl LinuxClipboard {
}
}

pub fn get_image_raw(&mut self) -> Result<Vec<u8>, Error> {
match self {
Self::X11(cb) => cb.get_image_raw(),

#[cfg(feature = "wayland-data-control")]
Self::WlDataControl(cb) => cb.get_image_raw(),
}
}

/// Places an image to the clipboard.
///
/// The chosen output format, depending on the platform is the following:
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ and conditions of the chosen license apply to this file.
#![crate_name = "arboard"]
#![crate_type = "lib"]

mod common;
pub use common::Error;
#[cfg(feature = "image-data")]
pub use common::ImageData;
#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),
))]
pub use common_linux::{ClipboardExtLinux, LinuxClipboardKind};

mod common;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),))]
pub(crate) mod common_linux;

Expand All @@ -42,12 +47,6 @@ type PlatformClipboard = windows_clipboard::WindowsClipboardContext;
#[cfg(target_os = "macos")]
type PlatformClipboard = osx_clipboard::OSXClipboardContext;

#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),
))]
pub use common_linux::{ClipboardExtLinux, LinuxClipboardKind};

/// The OS independent struct for accessing the clipboard.
///
/// Any number of `Clipboard` instances are allowed to exist at a single point in time. Note however
Expand Down Expand Up @@ -89,6 +88,10 @@ impl Clipboard {
self.platform.get_image()
}

pub fn get_image_raw(&mut self) -> Result<Vec<u8>, Error> {
self.platform.get_image_raw()
}

/// Places an image to the clipboard.
///
/// The chosen output format, depending on the platform is the following:
Expand Down
24 changes: 24 additions & 0 deletions src/wayland_data_control_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ impl WaylandDataControlClipboardContext {
}
}

pub fn get_image_raw(&mut self) -> Result<Vec<u8>, Error> {
use wl_clipboard_rs::paste::MimeType;

let result = get_contents(
paste::ClipboardType::Regular,
Seat::Unspecified,
MimeType::Specific(MIME_PNG),
);
match result {
Ok((mut pipe, _mime_type)) => {
let mut buffer = vec![];
pipe.read_to_end(&mut buffer).map_err(into_unknown)?;

Ok(buffer)
}

Err(PasteError::ClipboardEmpty) | Err(PasteError::NoMimeType) => {
Err(Error::ContentNotAvailable)
}

Err(err) => return Err(Error::Unknown { description: format!("{}", err) }),
}
}

#[cfg(feature = "image-data")]
pub fn set_image(&mut self, image: ImageData) -> Result<(), Error> {
use wl_clipboard_rs::copy::MimeType;
Expand Down
6 changes: 6 additions & 0 deletions src/x11_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,12 @@ impl X11ClipboardContext {
Ok(image_data)
}

pub fn get_image_raw(&self) -> Result<Vec<u8>> {
let formats = [self.inner.atoms.PNG_MIME];

Ok(self.inner.read(&formats, LinuxClipboardKind::Clipboard)?.bytes)
}

#[cfg(feature = "image-data")]
pub fn set_image(&self, image: ImageData) -> Result<()> {
let encoded = encode_as_png(&image)?;
Expand Down

0 comments on commit ca9914e

Please sign in to comment.