繁体   English   中英

如何使用 X11 获取监视器编号

[英]How to get the monitor number using X11

我正在使用XRRGetMonitors获取有关监视器的信息。 但是,我没有关于监视器数量的信息。 我需要系统中的监视器编号(例如,在 GNOME 设置中)在此处输入图像描述

我认为对于这个数字,我们可以只在 XRRGetMonitors(..) 返回的列表中获取监视器信息结构的位置

#include <X11/Xlib.h>
#include <iostream>
#include <X11/extensions/Xrandr.h>

using namespace std;

int main() 
{
    int monitor_count = 0;

    auto display = XOpenDisplay(NULL);
    if (display == NULL)
    {
        cout << "Failed to open display!" << endl;
        return -1;
    }

    auto wnd = XDefaultRootWindow(display);

    // 0 = active, 1 = inactive, call it twice to get a full list, if there are 
    // inactive monitors
    XRRMonitorInfo *info = XRRGetMonitors(display, wnd, 0, &monitor_count);
    cout << "There are " << monitor_count << " monitor(s) in system." << endl;

    int i = 0;
    while (i < monitor_count)
    {
        cout << "Monitor " << i++ << " : " << XGetAtomName(display, info->name) << endl;
        info++;
    }
    XCloseDisplay(display); 
}

upd:不,这不是一个正确的例子,因为 XRRGetMonitors 在我的配置中没有显示非活动监视器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM