c - How to copy to clipboard with X11? -


Using the framework on OS X, I can use PNG to copy it to the pasteboard (in C - obviously I use NSPasteboard with cocoa):

  # Include & lt; ApplicationServices / ApplicationServices.h & gt; Int replication (zero) {pasteboard clipboard; If (pasteboard creat (clipboard clipboard, and clipboard)! = NoErr) {return -1; } If (Pasteboard Clear (Clipboard)! = NoErr) {CFRYLase (clipboard); Return -1; } Size_t lane; Char * pngbuf = createMyPNGBuffer (& amp; len); / * Somewhere defined * / if (pngbuf == faucet) {CFRYLease (clipboard); Return -1; } CFDataRef data = CFDataCreateWithBytesNoCopy (kCFAllocatorDefault, pngbuf, len, kCFAllocatorNull); If (data == faucet) {CFRYLEGE (clipboard); Free (pngbuf); Return -1; } OSStatus mistake; Err = PasteboardPutItemFlavor (clipboard, NULL, KUTTypePNG, data, 0); CFRelease (clipboard); CFRelease (data); Free (pngbuf); Return 0; }  

I am interested in porting this functionality to Linux / BSD platforms. How do I create it using X?

Read anything before X11 has a unique system, which does not copy any is.

A quaintness that is different from other systems: If the player with the selection is removed, the selection is also done so when your program says "I have a selection (which is an image ) And then exit, no one will be able to request you from the copy of that image. To be useful, the clipboard owner needs to stick at least until any other program selection not do.

Still here? Using PGT (C is a pain), this is a small program that you want. (

  #! / Usr / bin / env python import gtk import sys count = 0 def handle_owner_change (event): global countdown print 'clipboard User-change (% R,% R) '% (clipboard, event) calculation = = 1 if calculation & gt; 1: sys.exit (0) image = gtk.gdk.pixbuf_new_from_file (sys.argv [1]) Clipboard = gtk.clipboard_get () clipboard.connect ('Owner-change', handle_r_range) clipboard .set_iage (image) clipboard . What happens under the hood: 

  • GDK loads an image.
  • CLICKBOARD selection of ownership of GTK claims.
  • Take the CLIPBOARD_MANAGER copy and select Gtk. (It may happen, this may not be one.)
  • When any other program requests data from our selection, then the management of targeting GTK data and the transfer of data Does.
  • The first OWNER_CHANGE event matches our ownership; Wait for the next ownership to lose, and wait for the exit

If a clipboard manager is running, then this program can immediately exit, otherwise it will wait until "cut / copy" is done in any other program.


Comments