Implementing clipboard functionality on X (*nix) given the XWindow*?

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Implementing clipboard functionality on X (*nix) given the XWindow*? on this date .

I’m using the Allegro5 API to create my window and manage it. It allows me access to the XWindow which it creates with XCreateWindow

I have access to d->window but that’s about it. From this, is there a way I could interact with the XEvents sent to this window? I want to implement clipboard functionality. I’m just not sure how I can deal with XSelection events.

Answer

You need to create the xevent loop, and to handle specific xevents. Here is a xlib hello world example, and that is how you can do it.

The example is copied from here :

 #include<X11/Xlib.h>
 #include<stdio.h>
 #include<stdlib.h>

 int main()
 {
    Display *dpy;
    Window rootwin;
    Window win;
    Colormap cmap;
    XEvent e;
    int scr;
    GC gc;


    if(!(dpy=XOpenDisplay(NULL))) {
        fprintf(stderr, "ERROR: could not open displayn");
        exit(1);
    }

    scr = DefaultScreen(dpy);
    rootwin = RootWindow(dpy, scr);
    cmap = DefaultColormap(dpy, scr);


    win=XCreateSimpleWindow(dpy, rootwin, 1, 1, 100, 50, 0, 
            BlackPixel(dpy, scr), BlackPixel(dpy, scr));

    XStoreName(dpy, win, "hello");


    gc=XCreateGC(dpy, win, 0, NULL);
    XSetForeground(dpy, gc, WhitePixel(dpy, scr));

    XSelectInput(dpy, win, ExposureMask|ButtonPressMask);

    XMapWindow(dpy, win);

    while(1) {
        XNextEvent(dpy, &e);
        if(e.type==Expose && e.xexpose.count<1)
            XDrawString(dpy, win, gc, 10, 10, "Hello World!", 12);
        else if(e.type==ButtonPress) break;
    }

    XCloseDisplay(dpy);
 }

To build, create a Makefile :

all: hello

hello: hello.o
    cc -o hello -Wall -L/usr/X11R6/lib -lX11 hello.o

hello.o: hello.c
    cc -o hello.o -Wall -I/usr/X11R6/include -c hello.c