copypasta/
wayland_clipboard.rs

1// Copyright 2017 Avraham Weinstock
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::ffi::c_void;
16use std::sync::{Arc, Mutex};
17
18use smithay_clipboard::Clipboard as WaylandClipboard;
19
20use crate::common::{ClipboardProvider, Result};
21
22pub struct Clipboard {
23    context: Arc<Mutex<WaylandClipboard>>,
24}
25
26pub struct Primary {
27    context: Arc<Mutex<WaylandClipboard>>,
28}
29
30/// Create new clipboard from a raw display pointer.
31///
32/// # Safety
33///
34/// Since the type of the display is a raw pointer, it's the responsibility of the callee to make
35/// sure that the passed pointer is a valid Wayland display.
36pub unsafe fn create_clipboards_from_external(display: *mut c_void) -> (Primary, Clipboard) {
37    let context = Arc::new(Mutex::new(WaylandClipboard::new(display)));
38
39    (Primary { context: context.clone() }, Clipboard { context })
40}
41
42impl ClipboardProvider for Clipboard {
43    fn get_contents(&mut self) -> Result<String> {
44        Ok(self.context.lock().unwrap().load()?)
45    }
46
47    fn set_contents(&mut self, data: String) -> Result<()> {
48        self.context.lock().unwrap().store(data);
49
50        Ok(())
51    }
52}
53
54impl ClipboardProvider for Primary {
55    fn get_contents(&mut self) -> Result<String> {
56        Ok(self.context.lock().unwrap().load_primary()?)
57    }
58
59    fn set_contents(&mut self, data: String) -> Result<()> {
60        self.context.lock().unwrap().store_primary(data);
61
62        Ok(())
63    }
64}