加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:30:39 所属栏目:百科 来源:网络整理
导读:我一直将我的系统移植到X11,我的剪贴板复制有问题(粘贴已经工作).我跟着 this.过了一会儿我发现他的例子也不行.问题是当我尝试将它粘贴到某个地方时,而不是XA_STRING请求的原子id是434.我无法找到这个原子的用途.当我将XA_STRING更改为434时,我得到一个不同
我一直将我的系统移植到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,&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),200,100,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,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,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 %in",(int)e.xselection.requestor,(int)w);
        printf ("prop:%i tar:%i sel:%in",req->property,req->target,req->selection);
        if (req->target == XA_STRING)
        {
            XChangeProperty (dpy,req->requestor,8,(unsigned char*) "It Works",8);
            respond.xselection.property=req->property;
        }
        else // Strings only please
        {
            printf ("No String %in",(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,&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,XA_TARGETS,(unsigned char *)(&supported),sizeof(supported)
    );
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读