From bc2c34eb7f9eea251822d94ea534a1d2de03c5a1 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sun, 9 Jun 2019 11:46:31 +0000 Subject: Add wayland primary selection clipboard support --- alacritty_terminal/src/clipboard.rs | 57 +++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 21 deletions(-) (limited to 'alacritty_terminal/src/clipboard.rs') diff --git a/alacritty_terminal/src/clipboard.rs b/alacritty_terminal/src/clipboard.rs index a310c991..827559bb 100644 --- a/alacritty_terminal/src/clipboard.rs +++ b/alacritty_terminal/src/clipboard.rs @@ -15,16 +15,19 @@ #[cfg(not(any(target_os = "macos", target_os = "windows")))] use std::ffi::c_void; -use clipboard::nop_clipboard::NopClipboardContext; +use copypasta::nop_clipboard::NopClipboardContext; #[cfg(not(any(target_os = "macos", target_os = "windows")))] -use clipboard::wayland_clipboard::WaylandClipboardContext; +use copypasta::wayland_clipboard::{ + Clipboard as WaylandClipboardClipboard, Primary as WaylandPrimaryClipboard, + WaylandClipboardContext, +}; #[cfg(not(any(target_os = "macos", target_os = "windows")))] -use clipboard::x11_clipboard::{Primary as X11SecondaryClipboard, X11ClipboardContext}; -use clipboard::{ClipboardContext, ClipboardProvider}; +use copypasta::x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext}; +use copypasta::{ClipboardContext, ClipboardProvider}; pub struct Clipboard { - primary: Box, - secondary: Option>, + clipboard: Box, + selection: Option>, } impl Clipboard { @@ -37,41 +40,53 @@ impl Clipboard { pub fn new(display: Option<*mut c_void>) -> Self { if let Some(display) = display { return Self { - primary: unsafe { Box::new(WaylandClipboardContext::new_from_external(display)) }, - secondary: None, + clipboard: unsafe { + Box::new( + WaylandClipboardContext::::new_from_external( + display, + ), + ) + }, + selection: unsafe { + Some(Box::new( + WaylandClipboardContext::::new_from_external( + display, + ), + )) + }, }; } Self { - primary: Box::new(ClipboardContext::new().unwrap()), - secondary: Some(Box::new(X11ClipboardContext::::new().unwrap())), + clipboard: Box::new(ClipboardContext::new().unwrap()), + selection: Some(Box::new(X11ClipboardContext::::new().unwrap())), } } // Use for tests and ref-tests pub fn new_nop() -> Self { - Self { primary: Box::new(NopClipboardContext::new().unwrap()), secondary: None } + Self { clipboard: Box::new(NopClipboardContext::new().unwrap()), selection: None } } } impl Default for Clipboard { fn default() -> Self { - Self { primary: Box::new(ClipboardContext::new().unwrap()), secondary: None } + Self { clipboard: Box::new(ClipboardContext::new().unwrap()), selection: None } } } #[derive(Debug)] pub enum ClipboardType { - Primary, - Secondary, + Clipboard, + Selection, } impl Clipboard { pub fn store(&mut self, ty: ClipboardType, text: impl Into) { - let clipboard = match (ty, &mut self.secondary) { - (ClipboardType::Secondary, Some(provider)) => provider, - (ClipboardType::Secondary, None) => return, - _ => &mut self.primary, + let clipboard = match (ty, &mut self.selection) { + (ClipboardType::Selection, Some(provider)) => provider, + (ClipboardType::Selection, None) => return, + _ => &mut self.clipboard, }; clipboard.set_contents(text.into()).unwrap_or_else(|err| { @@ -80,9 +95,9 @@ impl Clipboard { } pub fn load(&mut self, ty: ClipboardType) -> String { - let clipboard = match (ty, &mut self.secondary) { - (ClipboardType::Secondary, Some(provider)) => provider, - _ => &mut self.primary, + let clipboard = match (ty, &mut self.selection) { + (ClipboardType::Selection, Some(provider)) => provider, + _ => &mut self.clipboard, }; match clipboard.get_contents() { -- cgit v1.2.3-54-g00ecf