How to query X11 display resolution?
Asked Answered
H

8

20

It seems like an simple problem, but I can't find the answer: How do you query (via X11) what monitors exist and their resolutions?

Hankins answered 1/12, 2009 at 23:35 Comment(0)
H
14

Check out display macros and screen macros from the Xlib manual.

Specifically:

  • From the first link: ScreenCount(), ScreenOfDisplay()
  • From the second link: WidthOfScreen(), HeightOfScreen()
Harday answered 1/12, 2009 at 23:42 Comment(0)
C
13

This might be helpfull for cli and scripting

xwininfo -root

But xRandR might be more accurate, especially, when there is multiple monitor environment:

xrandr
Clingy answered 16/6, 2011 at 11:10 Comment(2)
It seem xrandr compared to xwininfo -root doesn't tell you about the monitor that is currently active though. However it's telling you about the monitor by proxy of the active root window.Discriminant
xprop -root is also an alternative to xwininfo, but every -root option requires a root window. During .xinitrc you can not rely on the existence of the root window.Hwang
V
5

If Xinerama is in use, try XineramaQueryScreens. Otherwise, you may be able to assume a single screen and use (X)WidthOfScreen/(X)HeightOfScreen.

(Also see the other answer. It's remotely possible someone is using the old X screen model where your screens are :x.0, :x.1, etc.)

Voussoir answered 1/12, 2009 at 23:43 Comment(0)
B
4

For modern X servers, there's also the XRandR extension, which provides the most up-to-date model of multi screen layout information, including overlapping screens and dynamic screen changes.

Documentation of it is available in the XRandR 1.3.1 Protocol spec and the libXrandr man page.

Barbosa answered 2/12, 2009 at 0:59 Comment(0)
S
4

The library X11 working only with unix-like OS, so it is a not cross-platform solution.

A full code

#include <stdio.h>

#include <X11/Xlib.h>

int
main(const int argc, const char *argv[])
{

    Display *display;
    Screen *screen;

    // open a display
    display = XOpenDisplay(NULL);

    // return the number of available screens
    int count_screens = ScreenCount(display);

    printf("Total count screens: %d\n", count_screens);


    for (int i = 0; i < count_screens; ++i) {
        screen = ScreenOfDisplay(display, i);
        printf("\tScreen %d: %dX%d\n", i + 1, screen->width, screen->height);
    }

    // close the display
    XCloseDisplay(display);

   return 0;
}

A compilation

gcc -o setup setup.c -std=c11 `pkg-config --cflags --libs x11`

A result (actual for my computer)

Total count screens: 1
    Screen 1: 1366X768

Based on:

  1. https://tronche.com/gui/x/xlib/display/opening.html
  2. https://tronche.com/gui/x/xlib/display/display-macros.html
  3. https://tronche.com/gui/x/xlib/display/screen-information.html
  4. https://mcmap.net/q/614259/-how-to-query-x11-display-resolution
Stlaurent answered 9/2, 2017 at 14:58 Comment(3)
Thank you for providing the code. Do you know the answer to my question: #42988432. I am asking if the screens you are finding in this code is the same as what Gtk3 3.22 calls monitors.Highsounding
Any opinion/experience on Cygwin/Xming/etc.? X11 might not be portable, but if it's ported/adapted, it's as good as portable, no? Not my area of expertise - I'm used to using libraries like SDL2 that sweep this stuff under the rug.Flaming
This doesn't work with multiple monitors, at least on my Ubuntu setup. The question used the word "monitors", with an "s". The results this code gives me: Total count screens: 1 Screen 1: 3520X1200 I'm using two "Join Displays", as denoted by the Display GUI settings. The first is 1920x1200. The second is a smaller, laptop display. The code results give you the total area covered by the two displays side-by-side. I want to programmatically determine the resolution of the primary display.Venireman
S
3

Python

import os
from Xlib import X, display
d = display.Display()
s = d.screen().root
output = os.popen("xrandr --listmonitors | grep '*' | awk {'print $4'}").read().splitlines()
num_sc = s.xinerama_get_screen_count().screen_count
width = s.get_geometry().width
height = s.get_geometry().height
print("Total count screens: %s" % num_sc)
for i in range(num_sc):
    print("\tScreen %s(%s): %sX%s" % (i, output[i], width, height))

Bash

$ xrandr --listmonitors
$ xrandr
$ xrandr | grep '*' | awk {'print $1'}
Salespeople answered 24/6, 2018 at 5:35 Comment(0)
B
2

Clean xrandr output for imagemagick use

$ xrandr |grep \* |awk '{print $1}'

Results here in:

1920x1080
Barcellona answered 22/5, 2016 at 10:58 Comment(1)
It is not related with the X11?Homo
H
0

The program xdpyinfo tells you almost everything about your X11 server.

$ xdpyinfo | sed -n '/^screen #0/{n;p}'
  dimensions:    3840x2160 pixels (696x391 millimeters)

For ffmpeg:

$ xdpyinfo | sed -n '/^screen #0/{n;s/^[ a-z:]*//;s/ .*//p}'
3840x2160
Hwang answered 17/12, 2022 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.