不断检查显示是否正常 Linux

Constantly checking if display is working on Linux

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

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

您可以使用命令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 事件,但这会有点复杂:你将需要一个事件循环或使用一个小部件工具包和挂钩 X 事件处理程序...