c++xlibxcb

Grab the color of a pixel with Xcb instead of Xlib


I use several window manager and if i understand correctly they use xlib. (awesome,openbox,fluxbox...)

I use the following code to detect the amount of "RED" in a pixel :

#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main(int argc, char *argv[]){
    XColor c;
    Display *d = XOpenDisplay((char *) NULL);
    int RED;
    int x=atoi(argv[1]);
    int y=atoi(argv[2]);
    XImage *image;
    image = XGetImage (d, RootWindow (d, DefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
    c.pixel = XGetPixel (image, 0, 0);
    XFree (image);
    XQueryColor (d, DefaultColormap(d, DefaultScreen (d)), &c);
    RED=c.red/256;
    cout << RED;
}

But it always return 0 with my i3-gaps window manager. (works with others wm)

I guess it's because i3 doesn't use Xlib but Xcb instead.

If so, how can i achieve the same thing with Xcb ? (Something backward compatible from Xlib syntax ?)


Solution

  • I've just replaced #include <X11/Xlib.h> by #include <xcb/xcb.h>.

    Amazing...