Crates.io | wayland-clipboard-listener |
lib.rs | wayland-clipboard-listener |
version | 0.2.6 |
source | src |
created_at | 2023-06-16 07:05:31.649752 |
updated_at | 2024-10-24 10:34:09.401899 |
description | impl wlr-data-control-unstable-v1, listen for clipboard |
homepage | https://github.com/Decodetalkers/wayland-clipboard-listener |
repository | |
max_upload_size | |
id | 891984 |
size | 76,893 |
I would rather to use animate girl to name the crate, but it will cannot show how the crate do
it impl wlr-data-control-unstable-v1
, you can see the protocol under
you can use it on sway or kde
it is GPL-v3, if you use the code, you need to privide your code, you should not fuck to use the code to listen on people's clipboard and upload it to you company or government, you must fuck to opensource your code.
impl wlr-data-control-unstable-v1, handle the clipboard on sway, hyperland or kde animpl the protocol. You can view the protocol in wlr-data-control-unstable-v1. Here we simply explain it.
This protocol involves there register: WlSeat, ZwlrDataControlManagerV1, ZwlrDataControlDeviceV1, and zwlrDataControlOfferV1, seat is used to create a device, and the device will handle the copy and paste,
when you want to use this protocol, you need to init these first, then enter the eventloop, you
can view our code, part of init()
Copy is mainly in the part of device dispatch and dataoffer one, there are two road to finished a copy event, this is decided by the time you send the receive request of ZwlrDataControlDeviceV1;
it is similar with Road 1, but send receive request when receive selection event, this time you will receive mimetype. Here you can only receive the data which is by copy
Paste with wlr-data-control-unstable-v1, need data provider alive, you can make an experiment, if you copy a text from firefox, and kill firefox, you will find, you cannot paste! It is amazing, doesn't it? So the copy event need to keep alive if the data is still available. You will find that if you copy a text with wl-copy, it will always alive in htop, if you view the code, you will find it fork itself, and live in the backend, until you copy another text from other place, it will die.
Then the problem is, how to copy the data, and when to kill the progress?
Copy event involves ZwlrDataControlDeviceV1 and ZwlrDataControlSourceV1.
offer
request. You can set muti times,A simple example to create a clipboard listener is following:
use wayland_clipboard_listener::WlClipboardPasteStream;
use wayland_clipboard_listener::WlListenType;
fn main() {
let mut stream = WlClipboardPasteStream::init(WlListenType::ListenOnCopy).unwrap();
for context in stream.paste_stream().flatten().flatten() {
println!("{context:?}");
}
}
A simple example to create a wl-copy is following:
use wayland_clipboard_listener::{WlClipboardCopyStream, WlClipboardListenerError};
fn main() -> Result<(), WlClipboardListenerError> {
let args = std::env::args();
if args.len() != 2 {
println!("You need to pass a string to it");
return Ok(());
}
let context: &str = &args.last().unwrap();
let mut stream = WlClipboardCopyStream::init()?;
stream.copy_to_clipboard(context.as_bytes().to_vec())?;
Ok(())
}
Thanks to wl-clipboard-rs, and smithay.
You can take a look to the repo following: