Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically determining individual screen widths/heights in Linux (w/Xinerama, TwinView, and/or BigDesktop)

I'm developing a little side-project to display multiple wallpapers on multiple screens under GNOME (something that apparently can't be done by GNOME itself or anything else). I've figured out how to do the main part of it (using the ImageMagick components, for the curious); I'm trying to automate the configuration system.

To do that, I need a way to determine the dimensions of the individual screens are. Can anyone give me a hint where to look for that? I presume the X server itself has the information, but I'm not sure how my program can ask for it.

like image 327
Head Geek Avatar asked Dec 07 '22 07:12

Head Geek


2 Answers

It looks like there's a libXinerama API that can retrieve that information. I haven't found any detailed information on it yet though.

General X.org programming information can be found here (PDF file). Information on the functions provided by libXinerama can be found here (online copy of a manpage, not a lot of information in it).

Here's a small C++ program that I whipped up from those references to retrieve the dimensions and offsets of each of the monitors hooked into Xinerama. It also works for nVidia TwinView; I don't presently have an ATI card to test it on their BigDesktop system, but I suspect it would work on it as well.

#include <cstdlib>
#include <iostream>

#include <X11/extensions/Xinerama.h>

using std::cout;
using std::endl;

int main(int argc, char *argv[]) {
    bool success=false;
    Display *d=XOpenDisplay(NULL);
    if (d) {
        int dummy1, dummy2;
        if (XineramaQueryExtension(d, &dummy1, &dummy2)) {
            if (XineramaIsActive(d)) {
                int heads=0;
                XineramaScreenInfo *p=XineramaQueryScreens(d, &heads);
                if (heads>0) {
                    for (int x=0; x<heads; ++x)
                        cout << "Head " << x+1 << " of " << heads << ": " <<
                            p[x].width << "x" << p[x].height << " at " <<
                            p[x].x_org << "," << p[x].y_org << endl;
                    success=true;
                } else cout << "XineramaQueryScreens says there aren't any" << endl;
                XFree(p);
            } else cout << "Xinerama not active" << endl;
        } else cout << "No Xinerama extension" << endl;
        XCloseDisplay(d);
    } else cout << "Can't open display" << endl;

    return (success ? EXIT_SUCCESS : EXIT_FAILURE);
}
like image 175
Head Geek Avatar answered Jan 26 '23 11:01

Head Geek


Try something like

GdkScreen *screen;
int num_monitors;
int i;

screen = gdk_screen_get_default ();
num_monitors = gdk_screen_get_n_monitors ();

for (i = 0; i < num_monitors; i++) {
    GdkRectangle rect;

    gdk_screen_get_monitor_geometry (screen, i, &rect);
    printf ("monitor %d: offsets (%d, %d), size (%d, %d)\n",
        i,
        rect.x, rect.y,
        rect.width, rect.height);
}

Internally this uses the libXrandr API. Xinerama is more or less deprecated, but still works; RANDR is the new way to handle multiple monitors in X.

like image 27
Federico Mena-Quintero Avatar answered Jan 26 '23 12:01

Federico Mena-Quintero