我想知道如何获得我系统上所有Xorg显示器的列表,以及与每个显示器相关的屏幕列表。 我花了一些时间浏览Xlib文档,但无法find我想要的function。 请假设除了POSIX投诉OS和X(例如,没有GTK),我没有其他依赖项。 如果我所要求的是不可能假设这些最小的依赖关系,那么使用其他库的解决scheme是好的。
非常感谢您的帮助!
我知道获取显示列表的唯一方法是检查/tmp/.X11-unix
目录。
一旦你这样做,你可以使用Xlib来查询每个显示更多的信息。
每个例子:
#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; }
运行上面给我这个输出与我目前的显示/屏幕:
Display :0 has 1 screens 0: 3046x1050 Display :1 has 2 screens 0: 1366x768 1: 1680x1050
从来没有找到一个更好的方式列出除此之外的X显示。 我非常想知道是否有更好的选择。
像netcoder写道,这个问题有两个不同的部分:
连接到X服务器
该进程使用XOpenDisplay()
建立到X服务器的连接。 连接被拆除使用XCloseDisplay()
。 在这个线程netcoders代码是一个很好的例子,如何正确地做到这一点。
正如netcoder提到的那样,问题是没有可靠的方法来找出哪个X服务器可以连接到一个进程。 他的代码检查X套接字的典型位置/tmp/.X11-unix/
。 如果用户是远程连接的,那么这种方法根本不起作用,例如通过SSH(启用X转发)。 在这种情况下,真的只有DISPLAY
环境变量(也许有一些欺骗wrt。〜 ~/.Xauthority
文件)。
不幸的是,我也不知道更好的方法。 我个人更喜欢使用每个用户的配置文件 – 比如说~/.application/displays
– ,其中用户可以列出应用程序应该尝试以与DISPLAY
环境变量相同的格式连接的服务器名称,另外默认的一个。 这不是自动的(netcoder的代码是),但这种方法更适合我。
查找X服务器提供的屏幕
XScreenCount()
将返回进程当前连接的X服务器提供的屏幕数量。 如果您只需要屏幕尺寸,请按照网络代码示例。 有关更多详细信息,请使用XScreenOfDisplay(Display,index)
获取Screen
指针; 0
<= index
< XScreenCount(Display)
。
在C代码中,宏ScreenCount()
和ScreenOfDisplay()
通常比实际的函数调用更高效。