• Stars
    star
    195
  • Rank 192,936 (Top 4 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A safe Rust crate for working with the Wayland clipboard.

wl-clipboard-rs

crates.io Build Status Documentation

Documentation (master)

A safe Rust crate for working with the Wayland clipboard.

This crate is intended to be used by terminal applications, clipboard managers and other utilities which don't spawn Wayland surfaces (windows). If your application has a window, please use the appropriate Wayland protocols for interacting with the Wayland clipboard (wl_data_device from the core Wayland protocol, the primary_selection protocol for the primary selection), for example via the smithay-clipboard crate.

The protocol used for clipboard interaction is data-control from wlroots. When using the regular clipboard, the compositor must support the first version of the protocol. When using the "primary" clipboard, the compositor must support the second version of the protocol (or higher).

For example applications using these features, see wl-clipboard-rs-tools/src/bin/wl_copy.rs and wl-clipboard-rs-tools/src/bin/wl_paste.rs which implement terminal apps similar to wl-clipboard or wl-clipboard-rs-tools/src/bin/wl_clip.rs which implements a Wayland version of xclip.

The Rust implementation of the Wayland client is used by default; use the native_lib feature to link to libwayland-client.so for communication instead. A dlopen feature is also available for loading libwayland-client.so dynamically at runtime rather than linking to it.

The code of the crate itself (and the code of the example utilities) is 100% safe Rust. This doesn't include the dependencies.

Examples

Copying to the regular clipboard:

use wl_clipboard_rs::copy::{MimeType, Options, Source};

let opts = Options::new();
opts.copy(Source::Bytes("Hello world!".to_string().into_bytes().into()), MimeType::Autodetect)?;

Pasting plain text from the regular clipboard:

use std::io::Read;
use wl_clipboard_rs::{paste::{get_contents, ClipboardType, Error, MimeType, Seat}};

let result = get_contents(ClipboardType::Regular, Seat::Unspecified, MimeType::Text);
match result {
    Ok((mut pipe, _)) => {
        let mut contents = vec![];
        pipe.read_to_end(&mut contents)?;
        println!("Pasted: {}", String::from_utf8_lossy(&contents));
    }

    Err(Error::NoSeats) | Err(Error::ClipboardEmpty) | Err(Error::NoMimeType) => {
        // The clipboard is empty or doesn't contain text, nothing to worry about.
    }

    Err(err) => Err(err)?
}

Checking if the "primary" clipboard is supported (note that this might be unnecessary depending on your crate usage, the regular copying and pasting functions do report if the primary selection is unsupported when it is requested):

use wl_clipboard_rs::utils::{is_primary_selection_supported, PrimarySelectionCheckError};

match is_primary_selection_supported() {
    Ok(supported) => {
        // We have our definitive result. False means that either data-control version 1
        // is present (which does not support the primary selection), or that data-control
        // version 2 is present and it did not signal the primary selection support.
    },
    Err(PrimarySelectionCheckError::NoSeats) => {
        // Impossible to give a definitive result. Primary selection may or may not be
        // supported.

        // The required protocol (data-control version 2) is there, but there are no seats.
        // Unfortunately, at least one seat is needed to check for the primary clipboard
        // support.
    },
    Err(PrimarySelectionCheckError::MissingProtocol { .. }) => {
        // The data-control protocol (required for wl-clipboard-rs operation) is not
        // supported by the compositor.
    },
    Err(_) => {
        // Some communication error occurred.
    }
}

Included terminal utilities

  • wl-paste: implements wl-paste from wl-clipboard.
  • wl-copy: implements wl-copy from wl-clipboard.
  • wl-clip: a Wayland version of xclip.

Stuff that would be neat to add:

  • Utility that mimics xsel commandline flags.

License: MIT/Apache-2.0

More Repositories

1

niri

A scrollable-tiling Wayland compositor.
Rust
1,414
star
2

BunnymodXT

Speedrun and TAS tool for Half-Life & friends.
C++
196
star
3

OpenAG

An open-source reimplementation of Adrenaline Gamer's client library.
C++
125
star
4

MouseTweaks

A Minecraft mod that enhances inventory management.
Java
94
star
5

SourcePauseTool

This was once just a plugin to prevent game unpausing.
C++
83
star
6

bxt-rs

Fastest video recording and TAS tools for Half-Life & mods.
Rust
36
star
7

circular-queue

A circular buffer-like queue container.
Rust
33
star
8

hl-capture

A tool for capturing Half-Life videos on Linux.
Rust
33
star
9

vapoursynth-rs

A safe Rust wrapper for VapourSynth.
Rust
27
star
10

hldemo-rs

Half-Life demo format parser using nom.
Rust
21
star
11

plitki

An experimental vertical scrolling rhythm game engine.
Rust
20
star
12

hlkreedz

Pawn
18
star
13

BunnymodXT-Injector

A small simple injector for Bunnymod XT.
C++
13
star
14

bxt-launcher

GUI launcher for Bunnymod XT on Linux
Vala
12
star
15

yalter-bot

A simple Discord bot written in Rust.
Rust
10
star
16

minhook-sys

Rust bindings to MinHook.
C
10
star
17

osu-auto-plus

osu! cursor dancing with highest possible scores on 2B maps.
C++
10
star
18

DemTools

A collection of tools operating GoldSource demo files.
C++
9
star
19

HLDemo

A library for parsing GoldSource demo files.
C++
8
star
20

LiveSplit.BunnySplit

An AutoSplit component which works with Bunnymod XT.
C#
7
star
21

dotfiles

Lua
6
star
22

HLSDemo

A library for parsing Half-Life: Source old old engine demo files.
C++
5
star
23

BunnymodXT-Demoplayer

Old version of Bunnymod for Half-Life mods.
C++
5
star
24

SPTLib

A hooking library used by SPT and Bunnymod XT.
C
4
star
25

hls-autopause

Experimenting with hooking in Rust. HLS: OOE autopause as an example.
Rust
4
star
26

blog

https://bxt.rs/
HTML
4
star
27

compress

An implementation of a simple arithmetic coding data compression algorithm.
C++
4
star
28

network-relay

Rust
3
star
29

SteamChatPainter

A C# tool to create smile patterns to be used in Steam chat conversations!
C#
3
star
30

talos-tools

Rust
3
star
31

bunnymod-pro

C++
3
star
32

hlfixperf

A small library to improve Half-Life's load times on Linux.
Rust
3
star
33

DemPack

GoldSource demo steganography.
C++
2
star
34

TASView

A repository of me having fun with C++, with showing stuff from Bunnymod XT as a side goal.
C++
2
star
35

aoc2017

Advent of Code 2017 solutions
Rust
2
star
36

UnexpectedFishing

A Bukkit plugin, that makes fishing slightly more... unexpected.
Java
1
star
37

ForestryFix

A mod that disables Forestry's mod protection on the client.
Scala
1
star
38

demo-reader

Shows information about a GoldSrc demo.
Rust
1
star
39

tas-log-reader

View TAS log files produced by Bunnymod XT. Proof-of-concept GTK 4 version of qconread2.
Rust
1
star
40

quaver-editor-plugin

Random utilities I needed.
Lua
1
star