From cb8471828ac444fb1d4c41688c161ebf1be173bb Mon Sep 17 00:00:00 2001 From: Mohammad AlSaleh Date: Thu, 18 Jan 2024 07:12:22 +0300 Subject: [PATCH] Add primary clipboard support for X11 and wayland The X11 code already had the functionality. Just didn't expose it. Signed-off-by: Mohammad AlSaleh --- src/lib.rs | 18 ++++++++++++++++++ src/platform/linux.rs | 16 ++++++++++++++++ wayland/src/lib.rs | 10 ++++++++++ x11/src/lib.rs | 32 ++++++++++++++++++++++++++------ 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 016ed0f..a9857e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,8 +72,26 @@ impl Clipboard { } } +impl Clipboard { + pub fn read_primary(&self) -> Option>> { + self.raw.read_primary() + } + + pub fn write_primary(&mut self, contents: String) -> Option>> { + self.raw.write_primary(contents) + } +} + pub trait ClipboardProvider { fn read(&self) -> Result>; fn write(&mut self, contents: String) -> Result<(), Box>; + + fn read_primary(&self) -> Option>> { + None + } + + fn write_primary(&mut self, _contents: String) -> Option>> { + None + } } diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 02b6da9..12d8c67 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -24,9 +24,17 @@ impl ClipboardProvider for wayland::Clipboard { self.read() } + fn read_primary(&self) -> Option>> { + Some(self.read_primary()) + } + fn write(&mut self, contents: String) -> Result<(), Box> { self.write(contents) } + + fn write_primary(&mut self, contents: String) -> Option>> { + Some(self.write_primary(contents)) + } } impl ClipboardProvider for x11::Clipboard { @@ -34,7 +42,15 @@ impl ClipboardProvider for x11::Clipboard { self.read().map_err(Box::from) } + fn read_primary(&self) -> Option>> { + Some(self.read_primary().map_err(Box::from)) + } + fn write(&mut self, contents: String) -> Result<(), Box> { self.write(contents).map_err(Box::from) } + + fn write_primary(&mut self, contents: String) -> Option>> { + Some(self.write_primary(contents).map_err(Box::from)) + } } diff --git a/wayland/src/lib.rs b/wayland/src/lib.rs index 801ce0f..fdf62c0 100644 --- a/wayland/src/lib.rs +++ b/wayland/src/lib.rs @@ -33,9 +33,19 @@ impl Clipboard { Ok(self.context.lock().unwrap().load()?) } + pub fn read_primary(&self) -> Result> { + Ok(self.context.lock().unwrap().load_primary()?) + } + pub fn write(&mut self, data: String) -> Result<(), Box> { self.context.lock().unwrap().store(data); Ok(()) } + + pub fn write_primary(&mut self, data: String) -> Result<(), Box> { + self.context.lock().unwrap().store_primary(data); + + Ok(()) + } } diff --git a/x11/src/lib.rs b/x11/src/lib.rs index 95247fe..0a1e310 100644 --- a/x11/src/lib.rs +++ b/x11/src/lib.rs @@ -45,10 +45,9 @@ impl Clipboard { }) } - /// Read the current [`Clipboard`] value. - pub fn read(&self) -> Result { + fn read_selection(&self, selection: Atom) -> Result { Ok(String::from_utf8(self.load( - self.reader.atoms.clipboard, + selection, self.reader.atoms.utf8_string, self.reader.atoms.property, std::time::Duration::from_secs(3), @@ -56,9 +55,18 @@ impl Clipboard { .map_err(Error::InvalidUtf8)?) } - /// Write a new value to the [`Clipboard`]. - pub fn write(&mut self, contents: String) -> Result<(), Error> { - let selection = self.writer.atoms.clipboard; + /// Read the current CLIPBOARD [`Clipboard`] value. + pub fn read(&self) -> Result { + self.read_selection(self.reader.atoms.clipboard) + } + + + /// Read the current PRIMARY [`Clipboard`] value. + pub fn read_primary(&self) -> Result { + self.read_selection(self.reader.atoms.primary) + } + + fn write_selection(&mut self, selection: Atom, contents: String) -> Result<(), Error> { let target = self.writer.atoms.utf8_string; self.selections @@ -87,6 +95,18 @@ impl Clipboard { } } + /// Write a new value to the CLIPBOARD [`Clipboard`]. + pub fn write(&mut self, contents: String) -> Result<(), Error> { + let selection = self.writer.atoms.clipboard; + self.write_selection(selection, contents) + } + + /// Write a new value to the PRIMARY [`Clipboard`]. + pub fn write_primary(&mut self, contents: String) -> Result<(), Error> { + let selection = self.writer.atoms.primary; + self.write_selection(selection, contents) + } + /// load value. fn load( &self,