gpt4 book ai didi

c - X11 - Xrandr 给了我假监视器

转载 作者:行者123 更新时间:2023-12-04 17:01:54 32 4
gpt4 key购买 nike

我试图找到所有显示器及其坐标(宽度 w ,高度 h ,x 原点/top-left-most x 和 y 原点/top-left-most y )并且正在使用它代码,它在某些系统上运行良好。但在其他系统上,我得到错误和重复的条目。如果我测试监视器是否为镜像,我能否避免这些重复/错误的监视器条目?如何测试它的镜像?

所以这是我的代码:

// start - get all monitor resolutions
var screen = XRRGetScreenResources(getXOpenDisplay(), getDefaultRootWindow(getXOpenDisplay()));

var noutputs = screen.noutput;

for (var i=noutputs-1; i>=0; i--) {
var info = XRRGetOutputInfo(getXOpenDisplay(), screen, screen.outputs[i]);
if (info.connection == RR_Connected) {
var ncrtcs = info.ncrtc;
for (var j=ncrtcs-1; j>=0; j--) {
var crtc_info = XRRGetCrtcInfo(getXOpenDisplay(), screen, infoCrtcs[j]);
console.info('screen #' + i + ' mon#' + j + ' details:', crtc_info.x, crtc_info.y, crtc_info.width, crtc_info.height);

collMonInfos.push({
x: crtc_info.x,
y: crtc_info.y,
w: crtc_info.width,
h: crtc_info.height
});

XRRFreeCrtcInfo(crtc_info);
}
}
XRRFreeOutputInfo(info);
}
XRRFreeScreenResources(screen);
console.info('JSON:', JSON.stringify(collMonInfos));
// end - get all monitor resolutions

这会将其输出到日志中:
"screen #4 mon#0 details:" 0 0 0 0
"screen #3 mon#1 details:" 0 0 1920 1200
"screen #3 mon#0 details:" 1920 469 1366 768
"screen #2 mon#1 details:" 0 0 1920 1200
"screen #2 mon#0 details:" 1920 469 1366 768
"screen #1 mon#1 details:" 0 0 1920 1200
"screen #1 mon#0 details:" 1920 469 1366 768
"screen #0 mon#1 details:" 0 0 1920 1200
"screen #0 mon#0 details:" 1920 469 1366 768

这是 JSON 格式的:
[{
"x": 0,
"y": 0,
"w": 0,
"h": 0
}, {
"x": 0,
"y": 0,
"w": 1920,
"h": 1200
}, {
"x": 1920,
"y": 469,
"w": 1366,
"h": 768
}, {
"x": 0,
"y": 0,
"w": 1920,
"h": 1200
}, {
"x": 1920,
"y": 469,
"w": 1366,
"h": 768
}, {
"x": 0,
"y": 0,
"w": 1920,
"h": 1200
}, {
"x": 1920,
"y": 469,
"w": 1366,
"h": 768
}, {
"x": 0,
"y": 0,
"w": 1920,
"h": 1200
}, {
"x": 1920,
"y": 469,
"w": 1366,
"h": 768
}]

我真的只有两台显示器,一台是 1920x1200,一台是 1366x768。为什么所有其他条目以及如何进行测试以避免(而不是根据重复或 0 h/w 回顾过滤掉)?

最佳答案

您不必要地遍历每个 输出 然后在每个 监控 .因此,您会收到重复的条目。您不必调用 XRRGetOutputInfo对于每个输出,因为您需要的所有数据(监视器数量)都可以在 XRRGetScreenResources 返回的结构中找到.
这是有效的C代码(至少对我来说):

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>

int main(void) {
Display *d = XOpenDisplay(getenv("DISPLAY"));
Window w = DefaultRootWindow(d);
XRRScreenResources *xrrr = XRRGetScreenResources(d, w);
XRRCrtcInfo *xrrci;
int i;
int ncrtc = xrrr->ncrtc;
for (i = 0; i < ncrtc; ++i) {
xrrci = XRRGetCrtcInfo(d, xrrr, xrrr->crtcs[i]);
printf("%dx%d+%d+%d\n", xrrci->width, xrrci->height, xrrci->x, xrrci->y);
XRRFreeCrtcInfo(xrrci);
}
XRRFreeScreenResources(xrrr);
return 0;
}

关于c - X11 - Xrandr 给了我假监视器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523446/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com