Linux 能否从命令行判断显示器是否已连接?

Linux 能否从命令行判断显示器是否已连接?

我希望能够从命令行得知显示器是否已连接到计算机(这样我就可以通过脚本决定是否应运行 X)。我该怎么做?

我不能依赖 X 的存在,因此排除了任何依赖于有效 X 显示的东西,比如xrandr。我发现ddccontrol,但它的输出是多行的,解析起来并不容易。(它确实告诉我,理论上这些信息可以按照我想要的方式获得。)有没有更适合脚本的程序可以告诉我显示器是否连接到系统?

我想要运行此功能的大多数系统都运行 Fedora 20。

答案1

我还没有在 fedora 20 上尝试过这个(只在 Ubuntu 13.04 上尝试过),但我脑子里的理论是正确的。

您可以尝试使用 read-edid (http://www.polypux.org/projects/read-edid/

它应该会给你一个与 XF86 兼容的模式行..你可以解析它。

以便可能工作。也许这更简单,您只需检查退出代码即可。

$ sudo get-edid > edid
get-edid: get-edid version 2.0.0

    Performing real mode VBE call
    Interrupt 0x10 ax=0x4f00 bx=0x0 cx=0x0
    Function supported
    Call successful

    VBE version 300
    VBE string at 0x11100 "NVIDIA"

VBE/DDC service about to be called
    Report DDC capabilities

    Performing real mode VBE call
    Interrupt 0x10 ax=0x4f15 bx=0x0 cx=0x0
    Function supported
    Call successful

    Monitor and video card combination does not support DDC1 transfers
    Monitor and video card combination supports DDC2 transfers
    0 seconds per 128 byte EDID block transfer
    Screen is not blanked during DDC transfer

Reading next EDID block

VBE/DDC service about to be called
    Read EDID

    Performing real mode VBE call
    Interrupt 0x10 ax=0x4f15 bx=0x1 cx=0x0
    Function supported
    Call failed

The EDID data should not be trusted as the VBE call failed
EDID claims 255 more blocks left
EDID blocks left is wrong.
Your EDID is probably invalid.
tom@tom:~$ parse-edid < edid 
parse-edid: parse-edid version 2.0.0
parse-edid: EDID checksum failed - data is corrupt. Continuing anyway.
parse-edid: first bytes don't match EDID version 1 header
parse-edid: do not trust output (if any).

    # EDID version 255 revision 255
Section "Monitor"
    Identifier "___:ffff"
    VendorName "___"
    ModelName "___:ffff"
    # DPMS capabilities: Active off:yes  Suspend:yes  Standby:yes

    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
    Mode    "4095x4095" # vfreq 9.770Hz, hfreq 80.018kHz
        DotClock    655.350000
        HTimings    4095 4350 4605 8190
        VTimings    4095 4158 4221 8190
        Flags   "Interlace" "+HSync" "+VSync"
    EndMode
EndSection

答案2

您可以使用以下方式列出当前连接或“已知”显示器等信息xrandr

# list all known monitors
xrandr --listmonitors --verbose

# list active monitors
xrandr --listactivemonitors

只需进行一点 grepping 等操作即可。值得查看这些man页面,看看是否有更好的功能适合您的用例。

这是我发现对脚本最有用的东西

xrandr | grep " connected " # =>
  # eDP-1
  # HDMI-1 # <-- the monitor I want to detect

# AND -- to check if `HDMI-1` is connected
xrandr | grep " connected " | awk '{ print$1 }' | grep HDMI-1

# store connection status in a var
$hdmi_connection=$(xrandr | grep " connected " | awk '{ print$1 }' | grep HDMI-1)

相关内容