【问题标题】:Clipboard selection transfer does not work剪贴板选择传输不起作用
【发布时间】:2012-05-13 08:26:09
【问题描述】:

我一直在将我的系统移植到 X11,但我遇到了剪贴板复制问题(粘贴已经在工作)。我关注了this。过了一会儿,我注意到他的例子也不起作用。问题是当我尝试将它粘贴到某个地方时,而不是 XA_STRING 请求的原子 id 是 434。我找不到这个原子的用途。当我将 XA_STRING 更改为 434 时,我得到一个不同的错误。以下是代码。

void SetClipboardText(const std::string &text) {
   XSetSelectionOwner (display, XA_CLIPBOARD, windowhandle, CurrentTime);
   copiedtext=text;
   XFlush(display);
   std::cout<<"COPIED"<<std::endl;
}

...
case SelectionRequest:
    XEvent respond;

    std::cout<<"SELTYPE: "<<event.xselectionrequest.target<<std::endl;

    if(event.xselectionrequest.target==XA_STRING && 
        event.xselectionrequest.selection==XA_CLIPBOARD) {
        std::cout<<"REQUESTED"<<std::endl;

        XChangeProperty (display,
            event.xselectionrequest.requestor,
            event.xselectionrequest.property,
            XA_STRING,
            copiedtext.length(),
            PropModeReplace,
            (unsigned char*) copiedtext.c_str(),
            copiedtext.length()
        );
        respond.xselection.property=event.xselectionrequest.property;
    }
    else {
        respond.xselection.property= None;
    }
    respond.xselection.type= SelectionNotify;
    respond.xselection.display= event.xselectionrequest.display;
    respond.xselection.requestor= event.xselectionrequest.requestor;
    respond.xselection.selection=event.xselectionrequest.selection;
    respond.xselection.target= event.xselectionrequest.target;
    respond.xselection.time = event.xselectionrequest.time;
    XSendEvent (display, event.xselectionrequest.requestor,0,0,&respond);
    XFlush (display);
    break;

将 XA_STRING 替换为 434 时出错:

X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  18 (X_ChangeProperty)
  Value in failed request:  0x4
  Serial number of failed request:  33
  Current serial number in output stream:  36

如果相关,我使用 KDE 4.8 并且 klipper 目前已关闭。

EDIT:来自网站的示例。

#include <X11/Xlib.h> 
#include <X11/Xatom.h>
#include <assert.h>   
#include <unistd.h>   
#include <stdio.h>
#include <stdlib.h>

main()
{
Display *dpy = XOpenDisplay(NULL);
assert(dpy);
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 
                 200, 100, 0, 0, 0);
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
XSelectionRequestEvent *req;
XEvent e, respond;
for(;;) {
    XNextEvent(dpy, &e);
    if (e.type == MapNotify) break;
}
XFlush(dpy);
//
Atom a1, a2, a3, type;
XSelectInput(dpy, w, StructureNotifyMask+ExposureMask);
int format, result;
unsigned long len, bytes_left, dummy;
unsigned char *data;
Window Sown;
for (int ii = 0; ii < 50; ii++) {
    XSetSelectionOwner (dpy, XA_PRIMARY, w, CurrentTime);
    XFlush (dpy);
    XNextEvent (dpy, &e);
    if (e.type == SelectionRequest)
    //
    // Somebody wants our data
    //
    {
        req=&(e.xselectionrequest);
        printf ("Selection Request from Mr %i I am %i\n",
            (int)e.xselection.requestor, (int)w);
        printf ("prop:%i tar:%i sel:%i\n", req->property,
            req->target, req->selection);
        if (req->target == XA_STRING)
        {
            XChangeProperty (dpy,
                req->requestor,
                req->property,
                XA_STRING,
                8,
                PropModeReplace,
                (unsigned char*) "It Works",
                8);
            respond.xselection.property=req->property;
        }
        else // Strings only please
        {
            printf ("No String %i\n",
                (int)req->target);
            respond.xselection.property= None;
        }
        respond.xselection.type= SelectionNotify;
        respond.xselection.display= req->display;
        respond.xselection.requestor= req->requestor;
        respond.xselection.selection=req->selection;
        respond.xselection.target= req->target;
        respond.xselection.time = req->time;
        XSendEvent (dpy, req->requestor,0,0,&respond);
        XFlush (dpy);
    }
}
}

编译使用

gcc copytest.c -o copytest -std=c99 -lX11

【问题讨论】:

  • 能否请您发布可以编译的完整源代码?
  • @Riateche:这可能是个问题,它是一个包含 100 多个文件的相当大的框架。但是,我可能会创建一个最小的工作示例,甚至发送我用作参考的应用程序。它也不能正常工作。
  • 该示例适用于带有 Unity 的 Ubuntu 12.04。我不知道是什么导致了这些错误。
  • 似乎与 434 出现错误无关,我已经处理了初始应用程序。似乎粘贴到 gedit 有效,而粘贴到 Kwrite 或任何 kde 应用程序则无效。
  • @Riateche:我发现 434 是什么,它是目标列表。似乎 KDE 需要某种目标列表,我们也应该实现它。我在这里找到了一个样本:mail-archive.com/cygwin-xfree@cygwin.com/msg00897.html

标签: c++ selection clipboard x11 xlib


【解决方案1】:

以下内容适用于 KDE 应用程序

else if(event.xselectionrequest.target==XA_TARGETS && 
  event.xselectionrequest.selection==XA_CLIPBOARD) {
    Atom supported[]={XA_STRING};
    XChangeProperty (display,
        event.xselectionrequest.requestor,
        event.xselectionrequest.property,
        XA_TARGETS,
        8,
        PropModeReplace,
        (unsigned char *)(&supported),
        sizeof(supported)
    );
}

【讨论】:

    【解决方案2】:

    您已通过copiedtext.length() 来格式化 XChangeProperty 的参数;这是错误的。

    您应该将8 传递给char*,将16 传递给short*,将32 传递给int*

    【讨论】:

    • 根据man XChangeProperty,32是long *,不是int *如果指定格式为8,则属性数据必须是char数组.如果指定格式为 16,则属性数据必须为 short 数组。如果指定格式为32,则属性数据必须为long数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多