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?
这似乎是一个简单的问题,但我找不到答案:你如何查询(通过X11)什么监视器存在及其解决方案?
7 个解决方案
#1
13
Check out display macros and screen macros from the Xlib manual.
查看Xlib手册中的显示宏和屏幕宏。
Specifically:
特别:
- From the first link:
ScreenCount()
,ScreenOfDisplay()
- 从第一个链接:ScreenCount(),ScreenOfDisplay()
- From the second link:
WidthOfScreen()
,HeightOfScreen()
- 从第二个链接:WidthOfScreen(),HeightOfScreen()
#2
10
This might be helpfull for cli and scripting
这可能对cli和脚本有用
xwininfo -root
But xRandR might be more accurate, especially, when there is multiple monitor environment:
但是,当存在多个监视器环境时,xRandR可能更准确:
xrandr
#3
5
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.
对于现代X服务器,还有XRandR扩展,它提供了最新的多屏幕布局信息模型,包括重叠屏幕和动态屏幕更改。
Documentation of it is available in the XRandR 1.3.1 Protocol spec and the libXrandr man page.
有关它的文档,请参见XRandR 1.3.1协议规范和libXrandr手册页。
#4
4
If Xinerama is in use, try XineramaQueryScreens
. Otherwise, you may be able to assume a single screen and use (X)WidthOfScreen
/(X)HeightOfScreen.
如果正在使用Xinerama,请尝试使用XineramaQueryScreens。否则,您可以假设单个屏幕并使用(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.)
(另请参阅另一个答案。远程可能有人使用旧的X屏幕模型,其屏幕为:x.0,:x.1等)
#5
1
Clean xrandr
output for imagemagick use
xrandr |grep \* |awk '{print $1}'
#6
1
The library X11 working only with unix-like OS, so it is a not cross-platform solution.
库X11只能使用类似unix的操作系统,因此它不是跨平台的解决方案。
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:
基于:
- https://tronche.com/gui/x/xlib/display/opening.html
- https://tronche.com/gui/x/xlib/display/opening.html
- https://tronche.com/gui/x/xlib/display/display-macros.html
- https://tronche.com/gui/x/xlib/display/display-macros.html
- https://tronche.com/gui/x/xlib/display/screen-information.html
- https://tronche.com/gui/x/xlib/display/screen-information.html
- https://*.com/a/1829747/6003870
- https://*.com/a/1829747/6003870
#7
0
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'}
#1
13
Check out display macros and screen macros from the Xlib manual.
查看Xlib手册中的显示宏和屏幕宏。
Specifically:
特别:
- From the first link:
ScreenCount()
,ScreenOfDisplay()
- 从第一个链接:ScreenCount(),ScreenOfDisplay()
- From the second link:
WidthOfScreen()
,HeightOfScreen()
- 从第二个链接:WidthOfScreen(),HeightOfScreen()
#2
10
This might be helpfull for cli and scripting
这可能对cli和脚本有用
xwininfo -root
But xRandR might be more accurate, especially, when there is multiple monitor environment:
但是,当存在多个监视器环境时,xRandR可能更准确:
xrandr
#3
5
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.
对于现代X服务器,还有XRandR扩展,它提供了最新的多屏幕布局信息模型,包括重叠屏幕和动态屏幕更改。
Documentation of it is available in the XRandR 1.3.1 Protocol spec and the libXrandr man page.
有关它的文档,请参见XRandR 1.3.1协议规范和libXrandr手册页。
#4
4
If Xinerama is in use, try XineramaQueryScreens
. Otherwise, you may be able to assume a single screen and use (X)WidthOfScreen
/(X)HeightOfScreen.
如果正在使用Xinerama,请尝试使用XineramaQueryScreens。否则,您可以假设单个屏幕并使用(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.)
(另请参阅另一个答案。远程可能有人使用旧的X屏幕模型,其屏幕为:x.0,:x.1等)
#5
1
Clean xrandr
output for imagemagick use
xrandr |grep \* |awk '{print $1}'
#6
1
The library X11 working only with unix-like OS, so it is a not cross-platform solution.
库X11只能使用类似unix的操作系统,因此它不是跨平台的解决方案。
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:
基于:
- https://tronche.com/gui/x/xlib/display/opening.html
- https://tronche.com/gui/x/xlib/display/opening.html
- https://tronche.com/gui/x/xlib/display/display-macros.html
- https://tronche.com/gui/x/xlib/display/display-macros.html
- https://tronche.com/gui/x/xlib/display/screen-information.html
- https://tronche.com/gui/x/xlib/display/screen-information.html
- https://*.com/a/1829747/6003870
- https://*.com/a/1829747/6003870
#7
0
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'}