Search code examples
cscreenshotx11

Taking screenshot with libx11


I'm currently trying to take a screenshot using libx11

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

int main(void) {
XImage* pic;
Display* dpl;
unsigned int buffer_size;

dpl = XOpenDisplay("127.0.0.1:0.0");

pic = XGetImage(dpl, RootWindow(dpl, DefaultScreen(dpl)), 10, 10, 201, 201,
        AllPlanes, ZPixmap);
}

if I compile the code using -lX11 and run it I keep getting a segmentation fault. Any ideas?


Solution

  • The X11 server does not usually listen on TCP/IP localhost, but on a Unix socket. At any rate, you should not hard-code the address of the X11 server. Try this:

    dpl = XOpenDisplay(NULL);
    assert(dpl);