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 | |
15 | use std::marker::PhantomData; |
16 | use std::time::Duration; |
17 | |
18 | use x11_clipboard::Atom; |
19 | use x11_clipboard::{Atoms, Clipboard as X11Clipboard}; |
20 | |
21 | use crate::common::*; |
22 | |
23 | pub trait Selection: Send { |
24 | fn atom(atoms: &Atoms) -> Atom; |
25 | } |
26 | |
27 | pub struct Primary; |
28 | |
29 | impl Selection for Primary { |
30 | fn atom(atoms: &Atoms) -> Atom { |
31 | atoms.primary |
32 | } |
33 | } |
34 | |
35 | pub struct Clipboard; |
36 | |
37 | impl Selection for Clipboard { |
38 | fn atom(atoms: &Atoms) -> Atom { |
39 | atoms.clipboard |
40 | } |
41 | } |
42 | |
43 | pub struct X11ClipboardContext<S = Clipboard>(X11Clipboard, PhantomData<S>) |
44 | where |
45 | S: Selection; |
46 | |
47 | impl<S> X11ClipboardContext<S> |
48 | where |
49 | S: Selection, |
50 | { |
51 | pub fn new() -> Result<X11ClipboardContext<S>> { |
52 | Ok(X11ClipboardContext(X11Clipboard::new()?, PhantomData)) |
53 | } |
54 | } |
55 | |
56 | impl<S> ClipboardProvider for X11ClipboardContext<S> |
57 | where |
58 | S: Selection, |
59 | { |
60 | fn get_contents(&mut self) -> Result<String> { |
61 | Ok(String::from_utf8(self.0.load( |
62 | S::atom(&self.0.getter.atoms), |
63 | self.0.getter.atoms.utf8_string, |
64 | self.0.getter.atoms.property, |
65 | timeout:Duration::from_secs(3), |
66 | )?)?) |
67 | } |
68 | |
69 | fn set_contents(&mut self, data: String) -> Result<()> { |
70 | Ok(self.0.store(S::atom(&self.0.setter.atoms), self.0.setter.atoms.utf8_string, value:data)?) |
71 | } |
72 | } |
73 | |