获取所有Xorg显示的列表 [英] Obtaining List of all Xorg Displays

查看:123
本文介绍了获取所有Xorg显示的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何获取系统上所有Xorg显示器的列表以及与每个显示器关联的屏幕的列表.我花了一些时间浏览Xlib文档,但找不到能满足我需要的功能.请假设除了POSIX兼容的OS和X外,我没有其他依赖项(例如,没有GTK).如果我要问的是假设这些最小的依赖关系是不可能的,那么使用其他库的解决方案就可以了.

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.

非常感谢您的帮助!

推荐答案

我知道获取显示列表的唯一方法是检查/tmp/.X11-unix目录.

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

完成此操作后,您可以使用Xlib查询每个显示以获取更多信息.

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

每个示例:

#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

除此以外,没有找到更好的方法来列出X显示.我非常想知道是否还有更好的选择.

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

这篇关于获取所有Xorg显示的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆