Search code examples
clinuxgtkx11

How can I use GTK4 of C to get the pixels of the screen?


How can I use GTK4 of C to get the pixels of the screen?

If GTK4 does not have an API, how can I get the pixels of the screen from Xlib?


Solution

  • I don't know about GTK4 but in X11:

    Example:

    //open display and get the root window of the default screen
    //note: there could be more than one screen
    Display *dpy = XOpenDisplay(NULL);
    Window root = RootWindow(dpy, DefaultScreen(dpy));
    
    //get the window attributes of the root window
    XWindowAttributes attr;
    XGetWindowAttributes(dpy, root, &attr);
    
    //get the image of the root window
    XImage* image = XGetImage(
        dpy, 
        root, 
        0, 0, //attr.x, attr.y
        attr.width, attr.height, 
        AllPlanes, 
        ZPixmap
    );
    

    Hint: Include X11/Xlib.h and compile with -lX11