4

我一直在将我的系统移植到 X11,但我遇到了剪贴板复制问题(粘贴已经在工作)。我遵循了这个。过了一会儿,我注意到他的例子也不起作用。问题是当我尝试将它粘贴到某个地方时,而不是 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
4

2 回答 2

2

以下是 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)
    );
}
于 2012-05-13T09:58:24.560 回答
1

您已传递copiedtext.length()给 XChangeProperty 的格式参数;这是错误的。

你应该通过8forchar*16forshort*32for int*

于 2014-09-20T09:09:40.770 回答