term_model/
clipboard.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "fuchsia")))]
use std::ffi::c_void;

#[cfg(not(target_os = "fuchsia"))]
use log::{debug, warn};

#[cfg(not(target_os = "fuchsia"))]
use copypasta::nop_clipboard::NopClipboardContext;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "fuchsia")))]
use copypasta::wayland_clipboard;
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "fuchsia")))]
use copypasta::x11_clipboard::{Primary as X11SelectionClipboard, X11ClipboardContext};
#[cfg(not(target_os = "fuchsia"))]
use copypasta::{ClipboardContext, ClipboardProvider};

pub struct Clipboard {
    #[cfg(not(target_os = "fuchsia"))]
    clipboard: Box<dyn ClipboardProvider>,
    #[cfg(not(target_os = "fuchsia"))]
    selection: Option<Box<dyn ClipboardProvider>>,
}

impl Clipboard {
    #[cfg(any(target_os = "macos", target_os = "windows", target_os = "fuchsia"))]
    pub fn new() -> Self {
        Self::default()
    }

    #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "fuchsia")))]
    pub fn new(display: Option<*mut c_void>) -> Self {
        if let Some(display) = display {
            let (selection, clipboard) =
                unsafe { wayland_clipboard::create_clipboards_from_external(display) };
            return Self { clipboard: Box::new(clipboard), selection: Some(Box::new(selection)) };
        }

        Self {
            clipboard: Box::new(ClipboardContext::new().unwrap()),
            selection: Some(Box::new(X11ClipboardContext::<X11SelectionClipboard>::new().unwrap())),
        }
     }

    // Use for tests and ref-tests
    #[cfg(not(target_os = "fuchsia"))]
    pub fn new_nop() -> Self {
        Self { clipboard: Box::new(NopClipboardContext::new().unwrap()), selection: None }
    }

    #[cfg(target_os = "fuchsia")]
    pub fn new_nop() -> Self {
        Self {}
    }
}

impl Default for Clipboard {
    #[cfg(not(target_os = "fuchsia"))]
    fn default() -> Self {
        Self { clipboard: Box::new(ClipboardContext::new().unwrap()), selection: None }
    }

    #[cfg(target_os = "fuchsia")]
    fn default() -> Self {
        Self {}
    }
}

#[derive(Debug)]
pub enum ClipboardType {
    Clipboard,
    Selection,
}

//
// Fuchsia does not currently support clipboards but the Clipboard struct
// is required for the terminal. For now, we will leave the struct empty until
// we support this functionality.
//

impl Clipboard {
    #[cfg(not(target_os = "fuchsia"))]
    pub fn store(&mut self, ty: ClipboardType, text: impl Into<String>) {
        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| {
            warn!("Unable to store text in clipboard: {}", err);
        });
    }

    #[cfg(target_os = "fuchsia")]
    pub fn store(&mut self, _ty: ClipboardType, _text: impl Into<String>) {
        // Intentionally blank
    }

    #[cfg(not(target_os = "fuchsia"))]
    pub fn load(&mut self, ty: ClipboardType) -> String {
        let clipboard = match (ty, &mut self.selection) {
            (ClipboardType::Selection, Some(provider)) => provider,
            _ => &mut self.clipboard,
        };

        match clipboard.get_contents() {
            Err(err) => {
                debug!("Unable to load text from clipboard: {}", err);
                String::new()
            },
            Ok(text) => text,
        }
    }

    #[cfg(target_os = "fuchsia")]
    pub fn load(&mut self, _ty: ClipboardType) -> String {
        String::new()
    }
}