Skip to content

Conversation

@nseguin42
Copy link

@nseguin42 nseguin42 commented Apr 5, 2025

This PR adds the other half (host-to-client direction) of clipboard support for the Wayland backend (the client-to-host direction being added in e17aec4). In other words, it allows the gamescope client to paste from the host's clipboard.

--

The implementation is based on Emersion's blogpost from 2020: "Wayland clipboard and drag & drop" . In the post, Emersion writes (emphasis mine):

Note that we’re performing blocking write calls in data_source_handle_send. This could potentially stall the Wayland event loop if we had more data to write (overflowing the kernel buffer). A real client would perform non-blocking writes instead.

I'm not sure how to go about making this change, so I'd appreciate some input here.

--

Only the clipboard is supported (not the primary selection). Since the client-to-host direction supports the primary selection (c.f. ef1e8db), we should probably add this too.

--

Only the "text/plain" MIME type is requested. Should we request different MIME types?

@nseguin42 nseguin42 marked this pull request as draft April 5, 2025 22:45
@MithicSpirit
Copy link

Thanks for doing this, I've been meaning to do it eventually, but never got around to it.

To prevent stalling too much on reads, I just prevented it from reading more than once, and print out a message when it doesn't fit (see line 2516).

I think it would be good for this to support all the same MIME types that the source supports (see lines 1228–1232 and 1240–1244; we should probably refactor these together so we don't triplicate the list of MIME types). However, more importantly, I think it should check the offered MIME types before getting the data. Currently, if I copy an image to the clipboard and try to paste it into gamescope, it just prints out some garbage, while I think it should either empty the internal clipboard or leave it unmodified.

@ninetailedtori
Copy link

ninetailedtori commented Sep 1, 2025

How about we merge this as is into main, renaming to plaintext host-to-client clipboard sync, given it's functional rn? We can work off this for the others after that. :]

(For reference: https://patch-diff.githubusercontent.com/raw/ValveSoftware/gamescope/pull/1797.patch)

@matte-schwartz
Copy link

matte-schwartz commented Sep 1, 2025

The PR is still a draft so it needs to be finalized first before its able to be merged. (note: this is not a guarantee it would get merged on the gamescope side since thats not up to me, thats just the way draft PRs work)

@ninetailedtori
Copy link

ninetailedtori commented Sep 1, 2025

Is there a way to contribute to this PR without write-access to either? I assume I should make the PR to the draft's repo by adding it as a temp secondary upstream? (Not too sure what the correct way to do it is haha)

Also in fairness I should probably start with: do we have a list of ALL the things we need to change before this is a full PR?

@ninetailedtori
Copy link

BTW for reference: #1935 can act as the official ticket for this PR. Close it when this is finalised and merged.

@matte-schwartz
Copy link

@ninetailedtori to work on it, I usually generate a .patch for the MR by adding .patch to the end of the link, apply to a new local branch, and then push commits to that branch. Maintains authorship and lets you add new commits.

I think addressing @MithicSpirit 's comment is probably a good start to get this running again, it can always be exoanded in future MRs.

@LZeugirdor
Copy link

+1 for this

@ninetailedtori
Copy link

I'll start to work on it today :)

ninetailedtori added a commit to ninetailedtori/gamescope that referenced this pull request Oct 2, 2025
…re#1797

Signed-off-by: ninetailedtori <ninetailedtori@uwu.gal>
@ninetailedtori
Copy link

ninetailedtori commented Oct 2, 2025

Replying to #1797 (comment)

Pretty much right now, it's implemented in such a way that it will simply accept whatever is piped to the file descriptor, and there is no handler on the mime type directly.
I'm uncertain, but it seems like even though we advertise it, it seems to send it anyway, when it should be raising a fuss about mime types.

What we can do is expand our data offer handling to callback to complain if unsupported mime type to the source, and destroy the offer on our end, I suppose?

I'll implement it roughly and rework it as I go along, probably in the spirit of traditional draft PR style I assume :]

@ninetailedtori
Copy link

ninetailedtori@500d293 An attempt was made, I just gotta compile and test it, I welcome help from others to test it too though :)

@neonvoidx
Copy link

@nseguin42 do you need help testing this? I'm very much in need of this, as it makes gaming horrid without it in some games like WoW

@nseguin42
Copy link
Author

nseguin42 commented Oct 22, 2025 via email

@neonvoidx
Copy link

@ninetailedtori are you driving this from here?

@ninetailedtori
Copy link

ninetailedtori commented Oct 28, 2025

I am, yes. Working on various projects right now but a functional gamescope Wayland h2c clipboard is one of my prios. The current draft patch from nseguin works but does not correctly decline handling of unsupported clip buffer content, and as a result instead of respecting the advertised mimetypes (as they added in the draft), either:

  1. outputs a garbled mess, or;
  2. CTD when content is attempted to be set.

My existing work on top of it (which can be seen in my fork, linked by me in a prior message) might compile but it is a draft, and may still cause errors. To explain what I've done or intend to do:

Implement a validation stage on the data offer to assess its type, check against supported types, and if unsupported, erase the offer entirely and throw a warn in the log.

I do welcome further testing on my PR, and it would be much appreciated, as I can only test so much on it!

In terms of this draft PR, we can either continue here until my PR is fully functional and therefore moves to its own, or I can do as OP did and make my own draft PR and we may continue testing from there and eventually approve that thread. Up to how everyone would like it handled really, I'm not too sure what the PR etiquette is for handovers here :]

It should be noted again that copy-paste ability of image or file or video content will not be implemented in this patch, as it is outside of the scope of this, and only will occur in a separate general scope issue to add more mimetypes to gamescope as a whole. The current intended behaviour is to log a warn and erase the data offer.

@ninetailedtori
Copy link

Of note, wrt things I need to do:

  • check for CTD, and any other instabilities when invoking the host to client copy;
  • double check whether I actually implemented warn code;
  • I've got to condense down the MIME types to spec so we don't dupe them unnecessarily. This may not be possible unless there's a universal mime list that all Wayland clipboard implementers use? I typically use wl-clipboard specs.

@neonvoidx
Copy link

neonvoidx commented Oct 28, 2025

Replying to #1797 (comment)

Just ping me whenever you need testing, I'm down to test, looking at your diff, I see one issue, which may just be me, but
const std::vector<const char *> &mimeTypes = m_CurrentOfferMimeTypes;

../src/Backends/WaylandBackend.cpp: In member function ‘void gamescope::CWaylandBackend::Wayland_DataDevice_Selection(wl_data_device*, wl_data_offer*)’:
../src/Backends/WaylandBackend.cpp:2778:54: error: invalid initialization of reference of type ‘const std::vector<const char*>&’ from expression of type ‘std::vector<std::__cxx11::basic_string<char> >’
 2778 |         const std::vector<const char *> &mimeTypes = m_CurrentOfferMimeTypes;
      |                                                      ^~~~~~~~~~~~~~~~~~~~~~~
../src/Backends/WaylandBackend.cpp:2782:40: error: cannot convert ‘bool’ to ‘const char*’
 2782 |                 if (strcmp(offeredType == supportedType) == 0) {
      |                            ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
      |                                        |
      |                                        bool
      ```

@ninetailedtori
Copy link

ninetailedtori commented Oct 28, 2025

Replying to #1797 (comment)

Oh good catch, that's a typo, lemme fix that one quickly :]

@ninetailedtori
Copy link

I'll be opening a new PR for this, so it allows everyone to keep a better track of it! Will mention this one when I request :]

@cwildfoerster
Copy link

Just to chime in: just pulled master, merged and your PR works as expected, i can finally copy&paste server connections into cs2. please keep it up

ninetailedtori added a commit to ninetailedtori/gamescope that referenced this pull request Jan 14, 2026
…re#1797

Signed-off-by: ninetailedtori <ninetailedtori@uwu.gal>
@ninetailedtori
Copy link

ninetailedtori commented Jan 15, 2026

Please check my PR revival #2062, to see if it's stable!

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.

7 participants