Take the 2-minute tour ×

Here is an example call to xrandr:

$ xrandr --output LVDS --mode 1680x1050 --pos 0x0 --rotate normal --output S-video --off --output DVI-0 --mode 1024x768 --pos 1680x104 --rotate normal

Think about a system where that call has success; there are two screens (LVDS and DVI-0) working with different resolutions. The DVI-0 one is on the right placed in the middle.

How can I get all this informations in a C program? I checked the xrandr source code, but I found it difficult to read and there is no apparent way to query the --pos value (edit: it is hidden in plain sight, thanks to ernestopheles' answer I got it).

I know I can ask a _NET_WORKAREA with XGetWindowProperty, but as far as I saw it does not tell the screen positions, just the size of the ideal rectangle that contains them all.

After some other study of xrandr code, this code seems a step forward the solution. Yet I am not convinced, xrandr.c around line 2940 assumes that crtc_info might be unavailable. I still miss the other way to get resolution and position.

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

    int main() {
        Display *disp;
        XRRScreenResources *screen;
        XRROutputInfo *info;
        XRRCrtcInfo *crtc_info;
        int iscres;
        int icrtc;

        disp = XOpenDisplay(0);
        screen = XRRGetScreenResources (disp, DefaultRootWindow(disp));
        for (iscres = screen->noutput; iscres > 0; ) {

            info = XRRGetOutputInfo (disp, screen, screen->outputs[iscres]);
            if (info->connection == RR_Connected) {
                for (icrtc = info->ncrtc; icrtc > 0;) {

                    crtc_info = XRRGetCrtcInfo (disp, screen, screen->crtcs[icrtc]);
                    fprintf(stderr, "==> %dx%d+%dx%d\n", crtc_info->x, crtc_info->y, crtc_info->width, crtc_info->height);

            XRRFreeOutputInfo (info);

        return 0;

share|improve this question

2 Answers 2

I am not sure, whether I understand the question correctly. Assuming, you want to read out the parameters of the current state of the x-server, use the following command:

xrandr -q
and parse its output:

LVDS connected 1680x1050+0+0 (normal left inverted right x axis y axis) 123mm x 123mm 

for the first screen and

TV_SVIDEO connected 1024x768+1680x104 (normal left inverted right x axis y axis) 123mm x 123mm

for the second. Running the command and parsing it can be done within a program written in C.

share|improve this answer
Thanks to your answer I noticed the position value is just near the resolution. At least I can check the xrandr code about how it determines it. –  Paolo.Bolzoni Dec 4 '12 at 18:05

You can get each screen resolution by doing this:

Display *dpy;
XRRScreenResources *screen;
XRRCrtcInfo *crtc_info;

dpy = XOpenDisplay(":0");
screen = XRRGetScreenResources (dpy, DefaultRootWindow(dpy));
//0 to get the first monitor   
crtc_info = XRRGetCrtcInfo (dpy, screen, screen->crtcs[0]);     

After that crtc_info->width will contain the width of the monitor and crtc_info->x the x position.

dont forget the includes:

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

and compile with -lX11 -lXrandr to link the libraries

share|improve this answer

Your Answer


