annoyed
annoyed

Reputation: 61

retrieve physical screen size

Xorg starts and creates a virtual screen

Virtual screen size determined to be 3120 x 1050

spanning my 2 physical screens of 1680x1050 and 1440x900, using xinerama I guess.

There is no configuation file and I don't want to change the system settings.

My application was using DisplayWidth and DisplayHeight to retrieve the screen size, which was fine on a single-screen setup.

maxwidth = DisplayWidth (dpy, scrnum);
maxheight = DisplayHeight (dpy, scrnum);

But on the dual-screen setup with a virtual screen automatically created, those functions return the size of the virtual screen.

I've tried different ways to retrieve the physical screen size, with the same result :

maxwidth = XWidthOfScreen (XScreenOfDisplay(dpy, scrnum));
maxheight = XHeightOfScreen (XScreenOfDisplay(dpy, scrnum));

or

XWindowAttributes attr;
XGetWindowAttributes(dpy, RootWindow(dpy, scrnum), &attr);
maxwidth = attr.width;
maxheight = attr.height;

Is it possible to retrieve the size of the physical screens using only Xlib ? I would like to avoid adding more library dependencies just to set the size of a window, but may be this can be achieve using Xrand extension ?

Upvotes: 5

Views: 1962

Answers (1)

Jammerx2
Jammerx2

Reputation: 864

The only way I know of to do this is to use the Xrandr extension as you mentioned. You will want to use XRRGetScreenResources and loop through each Crtc to get the information you want.

#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <stdio.h>

int main()
{
    Display *display = XOpenDisplay(NULL);
    XRRScreenResources *screens = XRRGetScreenResources(display, DefaultRootWindow(display));
    XRRCrtcInfo *info = NULL;
    int i = 0;

    for (i = 0; i < screens->ncrtc; i++) {
        info = XRRGetCrtcInfo(display, screens, screens->crtcs[i]);
        printf("%dx%d\n", info->width, info->height);
        XRRFreeCrtcInfo(info);
    }
    XRRFreeScreenResources(screens);

    return 0;
}

Upvotes: 6

Related Questions