Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X11: Implement putting things into the clipboard #1851

Merged
merged 9 commits into from
Jul 8, 2021

Conversation

psychon
Copy link
Contributor

@psychon psychon commented Jul 4, 2021

This PR fixes #937: The X11 platform now fully supports the clipboard.

When putting things into the clipboard, a new (invisible (= not mapped)) window is created and this window becomes the selection owner. Nothing else "x11-y" happens, so the clipboard contents are just kept in memory. Later, we might get SelectionRequest events, which are replied to by setting a window property and sending an event. INCR requests (for "incremental" (for "lots of data")) are again more complicated, just as when getting the clipboard contents.

Another complication are self-transfer: If we own the CLIPBOARD selection and request the clipboard contents, we would hang: The X11 server sends us an event that something wants a selection transfer, but the code would just put the event into a queue and not answer it. I briefly tried to implement "we still have to handle these requests", but ran into borrowing problems (the code for getting the selection and handling selection requests had conflicting borrows). Since it is way easier this way anyway, I implement a short-circuit: When we are the selection owner, "nothing X11" is done and we just directly provide the requested data.

Testing done with the following program and pasting into gvim (I also tested without the while loop for non-INCR transfers):

fn main() {
    tracing_subscriber::fmt().init();
    let app = druid_shell::Application::new().unwrap();
    let mut contents = String::from("1234567890");
    while contents.len() < 17 * 1024 * 1024 {
        let copy = contents.clone();
        contents = contents + &copy;
    }
    app.clipboard().put_string(contents);
    println!("{:?}", app.clipboard().available_type_names());
    println!("{:?}", app.clipboard().preferred_format(&["FOOBAR", "UTF8_STRING"]));
    println!("{:?}", app.clipboard().get_string().map(|s| s.len()));
    println!("Running!");
    app.run(None);
}

This commit renames the current Clipboard to ClipboardState and adds a
new Clipboard(Rc<RefCell<ClipboardState>>). This is in preparation for
setting the clipboard contents where we need to keep the clipboard
contents in ClipboardState.

Signed-off-by: Uli Schlachter <psychon@znc.in>
Signed-off-by: Uli Schlachter <psychon@znc.in>
Fixes: linebender#937
Signed-off-by: Uli Schlachter <psychon@znc.in>
Signed-off-by: Uli Schlachter <psychon@znc.in>
Signed-off-by: Uli Schlachter <psychon@znc.in>
Lots of cases of:

Error:    --> druid-shell/src/platform/x11/clipboard.rs:428:49
    |
428 |     fn handle_property_notify(&mut self, event: &PropertyNotifyEvent) -> Result<(), ReplyOrIdError> {
    |                                                 ^^^^^^^^^^^^^^^^^^^^ help: consider passing by value instead: `PropertyNotifyEvent`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref

error: this argument (16 byte) is passed by reference, but would be more efficient if passed by value (limit: 16 byte)

Signed-off-by: Uli Schlachter <psychon@znc.in>
Seems like fixing the clippy warnings made rustfmt change its mind on
the proper formatting...

Signed-off-by: Uli Schlachter <psychon@znc.in>
Copy link
Collaborator

@maan2003 maan2003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Really happy to see x11 backend moving forward. Just some minor nits:

druid-shell/src/platform/x11/clipboard.rs Outdated Show resolved Hide resolved
druid-shell/src/platform/x11/clipboard.rs Outdated Show resolved Hide resolved
Signed-off-by: Uli Schlachter <psychon@znc.in>
CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@maan2003 maan2003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@maan2003 maan2003 merged commit 177f3d6 into linebender:master Jul 8, 2021
@psychon psychon deleted the x11-put-clipboard2 branch July 8, 2021 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

X11 shell does not support clipboards.
2 participants