clip icon indicating copy to clipboard operation
clip copied to clipboard

Custom formats

Open azertyalex opened this issue 2 years ago • 5 comments

I'm trying to write other formats to the clipboard for example image/jpeg or image/jpg but pasting anywhere does not result in a jpeg being pasted.

code:

#include <cassert>
#include <fstream>
#include <iostream>
#include "clip.h"

int main() {
  clip::format int_format = clip::register_format("image/jpeg");
  std::ifstream file("lala.jpg");
  file.get();
  std::string line;
  std::string longline;

  while (std::getline(file, line)) {
    longline.append(line);
  }

  clip::lock l;
  if (l.locked()) {
    l.clear();
    return l.set_data(int_format, longline.c_str(), longline.size());
  } else {
    return false;
  }
}

I also tried hacking it in by getting the atom for image/jpeg and editing https://github.com/dacap/clip/blob/main/clip_x11.cpp#L257 so that m_data always sets a jpeg regardless of me calling a set_data with the text/plain atom. But nothing seems to work.

azertyalex avatar Jan 10 '22 11:01 azertyalex

It seems I can put anything into "image/jpeg", even text

xclip -sel clip -t "image/jpeg" -o
file:///home/amol/Code/Varia/clip/build/examples/lala.jpg

But using my own custom schemes do not result in anything, which is a shame because I need to set text/uri-list

azertyalex avatar Jan 10 '22 14:01 azertyalex

Hi @azertyalex, I think it should work, but you should copy the binary data just exactly as it is (don't use getline() because it is suited only for text-based data and might modify the final result).

dacap avatar Jan 10 '22 15:01 dacap

When using custom formats, as long as the program runs, I can access my custom format with xclip. Is this intentional? After reading up on how the x11 clipboard works this seems logical. However, I wonder why this isn't necessary with text/plain?

azertyalex avatar Jan 10 '22 15:01 azertyalex

On X11, the clipboard is shared directly between programs that owns the clipboard and the one that ask for its content, anyway when the program that own the clipboard selection is closed, it can decide if transfer its data to a possible clipboard manager (if it's available): https://github.com/dacap/clip/blob/c46c49141f72dd753b260eea4e6d7b37413f0ff6/clip_x11.cpp#L122

The selection manager and the SAVE_TARGETS mechanism depends on your Linux distribution/WM. E.g. if the clip library asks to the clipboard manager to save targets, the manager should request to our program the clipboard data and save it.

dacap avatar Jan 10 '22 15:01 dacap

I actually do not need a clipboard manager. I just made an example for debugging purposes. I'm using clip.h in a remote browsing situation, and my browser will always be running :)

Thanks for the help!

azertyalex avatar Jan 10 '22 15:01 azertyalex