Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining List of all Xorg Displays

I would like to know how I can obtain a list of all Xorg displays on my system, along with a list of screens associated with each display. I spent some time looking through the Xlib documentation, but was not able to find a function that does what I want. Please assume that I have no other dependencies other than a POSIX-complaint OS and X (e.g., no GTK). If what I ask is not possible assuming these minimal dependencies, then a solution using other libraries is fine.

Thank you very much for your help!

like image 763
void-pointer Avatar asked Jul 06 '12 17:07

void-pointer


People also ask

How do I see displays in Linux?

Getting the monitor resolution using the Linux command lineThe xrandr command is used on Linux and Unix-like system such as FreeBSD to set the size, orientation and/or reflection of the outputs for a screen. It can also get or set the screen size.

How can I check XORG status?

If you want to check whether x11 is installed, run dpkg -l | grep xorg . If you want to check if x11 is currently running (if logged in) then run echo $XDG_SESSION_TYPE . Paste the output.

What is Xrandr in Linux?

xrandr is an official configuration utility to the RandR (Resize and Rotate) X Window System extension. It can be used to set the size, orientation or reflection of the outputs for a screen. For configuring multiple monitors see the Multihead page.


1 Answers

The only way I know of to get a list of displays is to check the /tmp/.X11-unix directory.

Once you do that, you can use Xlib to query each display for more information.

Per example:

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <X11/Xlib.h>

int main(void) {
    DIR* d = opendir("/tmp/.X11-unix");

    if (d != NULL) {
        struct dirent *dr;
        while ((dr = readdir(d)) != NULL) {
            if (dr->d_name[0] != 'X')
                continue;

            char display_name[64] = ":";
            strcat(display_name, dr->d_name + 1);

            Display *disp = XOpenDisplay(display_name);
            if (disp != NULL) {
                int count = XScreenCount(disp);
                printf("Display %s has %d screens\n",
                    display_name, count);

                int i;
                for (i=0; i<count; i++)
                    printf(" %d: %dx%d\n",
                        i, XDisplayWidth(disp, i), XDisplayHeight(disp, i));

                XCloseDisplay(disp);
            }
        }
        closedir(d);
    }

    return 0;
}

Running the above gives me this output with my current displays/screens:

Display :0 has 1 screens
 0: 3046x1050
Display :1 has 2 screens
 0: 1366x768
 1: 1680x1050

Never found a better way of listing X displays other than that. I'd very much like to know if any better alternative exists.

like image 118
netcoder Avatar answered Sep 19 '22 16:09

netcoder