X11 - Xrandr 给我假显示器 [英] X11 - Xrandr giving me false monitors
问题描述
我试图找到所有显示器及其坐标(宽度 w
、高度 h
、x 原点/左上角 x
, 和 y origin/top-left-most y
) 并且正在使用此代码,它在某些系统上运行良好.但在其他系统上,我得到错误和重复的条目.如果我测试监视器是否为镜像,我是否能够避免这些重复/错误监视器条目?如何测试它是否是镜像?
I was trying to find all monitors and their coordinates (width w
, height h
, x origin/top-left-most x
, and y origin/top-left-most y
) and was using this code, it works good on some systems. But on other systems I get false and duplicate entries. Would I be able to avoid these dupilcate/false monitor entries if I tested if monitor is mirror? How to test if its mirror?
这是我的代码:
// 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
这会将其输出到日志中:
And this outputs this to log:
"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 格式:
This is it in JSON format:
[{
"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 回溯过滤掉)?
I really only have 2 monitors, the 1920x1200 one and the 1366x768 one. How come all the other entries and how to test to avoid (rather then filter out in retrospect based on duplicates or 0 h/w)?
推荐答案
您不必要地遍历每个输出,然后遍历每个监视器.所以你会收到一个重复的条目.您不必为每个输出调用 XRRGetOutputInfo
,因为您需要的所有数据(监视器数量)都可以在 XRRGetScreenResources
返回的结构中找到.这是有效的 C 代码(至少对我而言):
You are unnecessarily iterating over each output and then over each monitor. So you receive a duplicate entries. You do not have to call XRRGetOutputInfo
for each output, since all data you need (number of monitors) can be found in structure returned by XRRGetScreenResources
.
Here is the C code that works (at least for me):
#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;
}
这篇关于X11 - Xrandr 给我假显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!