4

I have been porting my system to X11 and I have a problem with clipboard copy (paste is already working). I have followed this. After a while I noticed that his example does not work too. Problem is when I try to paste it to somewhere, instead of XA_STRING requested atom id is 434. I couldnt find what this atom is for. When I change XA_STRING to 434 I get a different error. The following is the code.

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;

Error I got for replacing XA_STRING with 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

If relevant, I use KDE 4.8 and klipper is currently closed.

EDIT: Example from the website.

#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);
    }
}
}

Compile using

gcc copytest.c -o copytest -std=c99 -lX11
jww
  • 97,681
  • 90
  • 411
  • 885
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
  • 1
    Can you please post full source code that can be compiled? – Pavel Strakhov May 13 '12 at 08:49
  • @Riateche: that could be an issue, its a fairly large framework containing 100s of files. However, I might create a minimal working sample, or even send the application that I used as reference. Its not working as well. – Cem Kalyoncu May 13 '12 at 08:59
  • The example works for me on Ubuntu 12.04 with Unity. I don't know what can cause these errors. – Pavel Strakhov May 13 '12 at 09:10
  • It seems getting error with 434 is unrelated, I have worked on the initial application. And it seems that pasting to gedit works while pasting to Kwrite or any kde application does not. – Cem Kalyoncu May 13 '12 at 09:20
  • @Riateche: I found what 434 is, it is target list. It seems that KDE requires some sort of target list and we should implement it as well. I found a sample in here: http://www.mail-archive.com/cygwin-xfree@cygwin.com/msg00897.html – Cem Kalyoncu May 13 '12 at 09:44
  • The other error is the format, it should be a fixed 8. – Cem Kalyoncu May 13 '12 at 10:00
  • You'll better use Qt or Gtk for that. Both have extensive clipboard support. If you insist on coding directly in X11 (which IMHO is a loss of your time), study and follow [EWMH](http://standards.freedesktop.org/wm-spec/wm-spec-latest.html) and [ICCCM](https://en.wikipedia.org/wiki/Inter-Client_Communication_Conventions_Manual) – Basile Starynkevitch Sep 20 '14 at 09:45
  • My system is a library much like Qt or GTK – Cem Kalyoncu Sep 21 '14 at 09:38

2 Answers2

2

Following is requied to work for KDE applications

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)
    );
}
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
1

You have passed copiedtext.length() to format param for XChangeProperty; this is wrong.

You should pass 8 for char* and 16 for short* and 32 for int*.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • According to `man XChangeProperty` 32 is for `long *`, not `int *`: _If the specified format is 8, the property data must be a __char__ array. If the specified format is 16, the property data must be a __short__ array. If the specified format is 32, the property data must be a __long__ array._ – uli42 Nov 17 '19 at 13:04