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)

相關內容