Skip to content
Advertisement

Implementing clipboard functionality on X (*nix) without access to event loop?

Is it possible to implement copy-paste of text if you have a window but no access to the event loop, and can only make function calls to X. I’ve implemented clipboard on Windows and OSX with nothing but API function calls. Is the same possible on *nix with X? I was told you absolutely need access to the X event loop. Is there a way to implement basic string clipboard functionality with only api calls?

Advertisement

Answer

Short answer is “no”.

Long answer following…

The main difference between Windows and X clipboard (don’t know about OSX) is that:

  • In Windows the data is is copied into a chunk of shared memory when the user does COPY and it is available there for any application that does PASTE.
  • In X, when an application does COPY it just owns the clipboard. Then, when another application (or the same) does a PASTE the data is transferred from one application to the other.

So, in order to implement COPY in an X client, you have to own the clipboard first, and then listen and reply to the get clipboard contents messages. And to implement PASTE you have to send the get clipboard contents message and wait for the reply. Both of these operations require you to mess with the event loop.

That’s why in X the copied data is not available after you kill the source application. Unless you use some kind of clipboard server, that is, an application that listens to the event owner of clipboard is dying and saves a copy of the data for future pastes.

To be fair, Windows clipboard can work also in the direct transfer mode, but it is rarely used, AFAIK, and only for very big pieces of data.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement