【问题标题】:How to get the monitor number using X11如何使用 X11 获取监视器编号
【发布时间】:2022-07-18 19:58:12
【问题描述】:

我正在使用XRRGetMonitors 获取有关监视器的信息。 但是,我没有关于监视器数量的信息。 我需要系统中的监视器编号(例如,在 GNOME 设置中) enter image description here

【问题讨论】:

  • 你读过XRRGetMonitors的文档吗?它精确地允许您获得您拥有的显示器数量。
  • 我需要获取每台显示器的索引
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: c++ linux x11 xlib


【解决方案1】:

我认为对于这个数字,我们可以只在 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); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多