如何复制与X11到剪贴板? [英] How to copy to clipboard with X11?
问题描述
使用OS X上的框架,我可以使用以下为PNG复制到剪贴板(在C - 很明显,我可以用NSPasteboard与可可):
Using the frameworks on OS X, I can use the following to copy a PNG to the pasteboard (in C — obviously I could use NSPasteboard with Cocoa):
#include <ApplicationServices/ApplicationServices.h>
int copyThatThing(void)
{
PasteboardRef clipboard;
if (PasteboardCreate(kPasteboardClipboard, &clipboard) != noErr) {
return -1;
}
if (PasteboardClear(clipboard) != noErr) {
CFRelease(clipboard);
return -1;
}
size_t len;
char *pngbuf = createMyPNGBuffer(&len); /* Defined somewhere else */
if (pngbuf == NULL) {
CFRelease(clipboard);
return -1;
}
CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, pngbuf,
len, kCFAllocatorNull);
if (data == NULL) {
CFRelease(clipboard);
free(pngbuf);
return -1;
}
OSStatus err;
err = PasteboardPutItemFlavor(clipboard, NULL, kUTTypePNG, data, 0);
CFRelease(clipboard);
CFRelease(data);
free(pngbuf);
return 0;
}
我感兴趣的移植此功能的Linux / * BSD平台。我怎样才能复制此用X?
I'm interested in porting this functionality to Linux/*BSD platforms. How can I replicate this using X?
推荐答案
去读一读 X选中,剪切缓冲区,杀戒指先天下之忧。 X11有一个相当独特的系统,没有其他人似乎已经被复制。
Go read X Selections, Cut Buffers, and Kill Rings before anything else. X11 has a rather unique system that nobody else seems to have copied.
一个古怪的是大多数其他系统不同:如果程序拥有的选择(剪贴板)消失了,所以没有选择。 我有一个选择(这恰好是一个图像)所以,当你的程序说,然后退出,没有人会能够从您请求图像的副本。为了有用,剪贴板所有者需要坚持围绕至少要到另一个程序需要的选择。
One oddity that is different from most other systems: if the program owning the selection (clipboard) goes away, so does the selection. So when your program says "I have a selection (which happens to be an image)" and then exits, nobody will be able to request a copy of that image from you. In order to be useful, the clipboard owner needs to stick around at least until another program takes the selection.
还在这里?下面是一个简短的程序,你想要做什么,使用PyGTK的(因为C是一种痛苦)。
Still here? Here's a short program that does what you want, using PyGTK (because C is a pain).
#!/usr/bin/env python
import gtk
import sys
count = 0
def handle_owner_change(clipboard, event):
global count
print 'clipboard.owner-change(%r, %r)' % (clipboard, event)
count += 1
if count > 1:
sys.exit(0)
image = gtk.gdk.pixbuf_new_from_file(sys.argv[1])
clipboard = gtk.clipboard_get()
clipboard.connect('owner-change', handle_owner_change)
clipboard.set_image(image)
clipboard.store()
gtk.main()
引擎盖下会发生什么:
What happens under the hood:
- GDK加载图像。
- 的Gtk声称剪贴板选择的所有权。
- 的是,CLIPBOARD_MANAGER副本,并采取选择。 (有可能不是一个运行,所以这可能不会发生。)
- 当另一个程序从我们的选择请求数据,GTK处理到目标的转换和从图像数据的传输。
- 第一OWNER_CHANGE事件对应于我们取得所有权;等下一个对应于我们失去所有权,并退出。
- Gdk loads an image.
- Gtk claims ownership of the CLIPBOARD selection.
- Gtk requests that the CLIPBOARD_MANAGER copy and take the selection. (There might not be one running, so this might not happen.)
- When another program requests data from our selection, Gtk handles the conversion and transfer of data from the image to the target.
- The first OWNER_CHANGE event corresponds to us taking ownership; wait for the next one corresponding to us losing ownership, and exit.
如果剪贴板管理器正在运行,这个程序可能会立即退出。否则,它将等到另一个程序正在执行剪切/复制。
If a clipboard manager is running, this program may exit immediately. Otherwise, it will wait until "cut/copy" is performed in another program.
这篇关于如何复制与X11到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!