如何用X11复制到剪贴板?
使用 OS X 上的框架,我可以使用以下命令将 PNG 复制到粘贴板(在 C 语言中 — 显然我可以将 NSPasteboard 与 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 复制这个?
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;
}
I'm interested in porting this functionality to Linux/*BSD platforms. How can I replicate this using X?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在进行任何操作之前,请先阅读X 选择、剪切缓冲区和 Kill Rings别的。 X11 有一个相当独特的系统,似乎没有其他人可以复制。
与大多数其他系统不同的一个奇怪之处是:如果拥有选择的程序(剪贴板)消失,那么选择也会消失。因此,当您的程序显示“我有一个选择(恰好是一个图像)”然后退出时,没有人能够向您请求该图像的副本。为了有用,剪贴板所有者需要至少坚持到另一个程序进行选择为止。
还在这里吗?这是一个简短的程序,可以使用 PyGTK 执行您想要的操作(因为 C 很痛苦)。
幕后发生了什么:
如果剪贴板管理器正在运行,该程序可能会立即退出。否则,它将等待,直到在另一个程序中执行“剪切/复制”。
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.
Still here? Here's a short program that does what you want, using PyGTK (because C is a pain).
What happens under the hood:
If a clipboard manager is running, this program may exit immediately. Otherwise, it will wait until "cut/copy" is performed in another program.
程序终止后在 GTK 剪贴板上存储数据的功能没有得到很好的支持。 GTK.clipboard.store 可能无法存储较大的图像(大于几百 kB),并且像 compiz 这样的高级桌面功能可能会与此机制发生冲突。没有这些缺点的一种解决方案是在后台运行一个简单的 gtk 应用程序。以下Python服务器应用程序使用Pyro包公开ImageToClipboard的方法:
将此程序作为后台进程启动,即
gclipboard-imaged.py &
以下示例客户端应用程序使用命令行中给定的文件名设置剪贴板图像:
要将图像复制到剪贴板,请运行
gclipboard-setimage.py picname.png
The ability to store data on the GTK clipboard after a program terminates is not well supported. GTK.clipboard.store may fail to store larger images (greater than several hundred kB), and advanced desktop features like compiz may conflict with this mechanism. One solution without these drawbacks is to run a simple gtk application in the background. The following Python server application uses the Pyro package to expose the methods of ImageToClipboard:
Start this program as a background process, i.e.
gclipboard-imaged.py &
The following example client application sets the clipboard image using a filename given at the command line:
To copy an image to the clipboard, run
gclipboard-setimage.py picname.png