Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xrandr related, C programming

Tags:

c

screen

x11

xrandr

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; ) {
            --iscres;

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

                    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);

                    XRRFreeCrtcInfo(crtc_info);
                }
            }
            XRRFreeOutputInfo (info);
        }
        XRRFreeScreenResources(screen);

        return 0;
    }

like image 385
Paolo.Bolzoni Avatar asked Dec 04 '12 15:12

Paolo.Bolzoni


2 Answers

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

like image 166
Ivi Avatar answered Oct 05 '22 14:10

Ivi


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.

like image 24
s-m-e Avatar answered Oct 05 '22 16:10

s-m-e