gpt4 book ai didi

linux - 不断检查显示器是否在 Linux 上工作

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:18 25 4
gpt4 key购买 nike

是否有可能检查显示(监视器)是否正常工作并将该数据导入代码?我假设有一些命令行技巧或设备可能会“泄露”有关它的信息。使用 Linux。

最佳答案

您可以使用 X11 扩展 XRandR(X 分辨率和旋转 或类似的东西)。

您可以使用命令 xrandr 查看输出显示的状态。例如,在我的 PC 中你会得到:

$ xrandr | grep connected
DVI-I-0 disconnected (normal left inverted right x axis y axis)
DVI-I-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 531mm x 299mm
....

当然,输出的名称是特定于设备的。

如果您想从 C 程序访问数据,Xrandr 扩展很容易编程。此示例代码将打印所有输出的连接状态(省略错误检查):

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

int main()
{
Display *dsp = XOpenDisplay(NULL);
Window root = XRootWindow(dsp, 0);
XRRScreenResources *sres = XRRGetScreenResources(dsp, root);
printf("N outputs %d\n", sres->noutput);
for (int i = 0; i < sres->noutput; ++i)
{
XRROutputInfo *info = XRRGetOutputInfo(dsp, sres, sres->outputs[i]);
printf(" %d: '%s' %s\n", i, info->name, info->connection == RR_Connected ? "connected" : "");
XRRFreeOutputInfo(info);

}
XRRFreeScreenResources(sres);
XCloseDisplay(dsp);
return 0;
}

如果你想获得更改的实时通知,你可以使用 XRROutputChangeNotifyEvent X 事件,但这会有点复杂:你需要一个事件循环或使用一个小部件工具包并 Hook X 事件处理程序...

关于linux - 不断检查显示器是否在 Linux 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212503/

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