c – 剪贴板选择传输不起作用

我一直将我的系统移植到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;

我用434替换XA_STRING时遇到错误:

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目前已关闭.

编辑:网站上的例子.

#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

最佳答案 以下内容适用于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)
    );
}
点赞