wl_clipboard_rs/lib.rs
1//! A safe Rust crate for working with the Wayland clipboard.
2//!
3//! This crate is intended to be used by terminal applications, clipboard managers and other
4//! utilities which don't spawn Wayland surfaces (windows). If your application has a window,
5//! please use the appropriate Wayland protocols for interacting with the Wayland clipboard
6//! (`wl_data_device` from the core Wayland protocol, the `primary_selection` protocol for the
7//! primary selection), for example via the
8//! [smithay-clipboard](https://crates.io/crates/smithay-clipboard) crate.
9//!
10//! The protocol used for clipboard interaction is `ext-data-control` or `wlr-data-control`. When
11//! using the regular clipboard, the compositor must support any version of either protocol. When
12//! using the "primary" clipboard, the compositor must support any version of `ext-data-control`,
13//! or the second version of the `wlr-data-control` protocol.
14//!
15//! For example applications using these features, see `wl-clipboard-rs-tools/src/bin/wl_copy.rs`
16//! and `wl-clipboard-rs-tools/src/bin/wl_paste.rs` which implement terminal apps similar to
17//! [wl-clipboard](https://github.com/bugaevc/wl-clipboard) or
18//! `wl-clipboard-rs-tools/src/bin/wl_clip.rs` which implements a Wayland version of `xclip`.
19//!
20//! The Rust implementation of the Wayland client is used by default; use the `native_lib` feature
21//! to link to `libwayland-client.so` for communication instead. A `dlopen` feature is also
22//! available for loading `libwayland-client.so` dynamically at runtime rather than linking to it.
23//!
24//! The code of the crate itself (and the code of the example utilities) is 100% safe Rust. This
25//! doesn't include the dependencies.
26//!
27//! # Examples
28//!
29//! Copying to the regular clipboard:
30//! ```no_run
31//! # extern crate wl_clipboard_rs;
32//! # fn foo() -> Result<(), Box<dyn std::error::Error>> {
33//! use wl_clipboard_rs::copy::{MimeType, Options, Source};
34//!
35//! let opts = Options::new();
36//! opts.copy(Source::Bytes("Hello world!".to_string().into_bytes().into()), MimeType::Autodetect)?;
37//! # Ok(())
38//! # }
39//! ```
40//!
41//! Pasting plain text from the regular clipboard:
42//! ```no_run
43//! # extern crate wl_clipboard_rs;
44//! # fn foo() -> Result<(), Box<dyn std::error::Error>> {
45//! use std::io::Read;
46//! use wl_clipboard_rs::{paste::{get_contents, ClipboardType, Error, MimeType, Seat}};
47//!
48//! let result = get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Text);
49//! match result {
50//! Ok((mut pipe, _)) => {
51//! let mut contents = vec![];
52//! pipe.read_to_end(&mut contents)?;
53//! println!("Pasted: {}", String::from_utf8_lossy(&contents));
54//! }
55//!
56//! Err(Error::NoSeats) | Err(Error::ClipboardEmpty) | Err(Error::NoMimeType) => {
57//! // The clipboard is empty or doesn't contain text, nothing to worry about.
58//! }
59//!
60//! Err(err) => Err(err)?
61//! }
62//! # Ok(())
63//! # }
64//! ```
65//!
66//! Checking if the "primary" clipboard is supported (note that this might be unnecessary depending
67//! on your crate usage, the regular copying and pasting functions do report if the primary
68//! selection is unsupported when it is requested):
69//!
70//! ```no_run
71//! # extern crate wl_clipboard_rs;
72//! # fn foo() -> Result<(), Box<dyn std::error::Error>> {
73//! use wl_clipboard_rs::utils::{is_primary_selection_supported, PrimarySelectionCheckError};
74//!
75//! match is_primary_selection_supported() {
76//! Ok(supported) => {
77//! // We have our definitive result. False means that ext/wlr-data-control is present
78//! // and did not signal the primary selection support, or that only wlr-data-control
79//! // version 1 is present (which does not support primary selection).
80//! },
81//! Err(PrimarySelectionCheckError::NoSeats) => {
82//! // Impossible to give a definitive result. Primary selection may or may not be
83//! // supported.
84//!
85//! // The required protocol (ext-data-control, or wlr-data-control version 2) is there,
86//! // but there are no seats. Unfortunately, at least one seat is needed to check for the
87//! // primary clipboard support.
88//! },
89//! Err(PrimarySelectionCheckError::MissingProtocol) => {
90//! // The data-control protocol (required for wl-clipboard-rs operation) is not
91//! // supported by the compositor.
92//! },
93//! Err(_) => {
94//! // Some communication error occurred.
95//! }
96//! }
97//! # Ok(())
98//! # }
99//! ```
100//!
101//! # Included terminal utilities
102//!
103//! - `wl-paste`: implements `wl-paste` from
104//! [wl-clipboard](https://github.com/bugaevc/wl-clipboard).
105//! - `wl-copy`: implements `wl-copy` from [wl-clipboard](https://github.com/bugaevc/wl-clipboard).
106//! - `wl-clip`: a Wayland version of `xclip`.
107
108#![doc(html_root_url = "https://docs.rs/wl-clipboard-rs/0.9.2")]
109#![deny(unsafe_code)]
110
111mod common;
112mod data_control;
113mod seat_data;
114
115#[cfg(test)]
116#[allow(unsafe_code)] // It's more convenient for testing some stuff.
117mod tests;
118
119pub mod copy;
120pub mod paste;
121pub mod utils;