How to query X11 display resolution?

五迷三道 提交于 2019-12-03 05:11:45

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

Specifically:

  • From the first link: ScreenCount(), ScreenOfDisplay()
  • From the second link: WidthOfScreen(), HeightOfScreen()

This might be helpfull for cli and scripting

xwininfo -root

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

xrandr

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.

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

PADYMKO

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://stackoverflow.com/a/1829747/6003870

Clean xrandr output for imagemagick use

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

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'}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!