【问题标题】:How to Determine if LCD Monitor is Turned on From Linux Command Line如何从 Linux 命令行确定 LCD 显示器是否已打开
【发布时间】:2010-08-08 05:26:10
【问题描述】:

如何从 Linux 的命令行中判断计算机的显示器是否打开/关闭?我传统上将监视器视为仅输出设备,但我注意到 Gnome Monitor Preferences 对话框具有“检测监视器”功能。这可以概括为确定监视器是否物理关闭?

【问题讨论】:

  • echo 'Quick! Press a key if you see this :-)'
  • 虽然从技术上讲不是“从命令行”,但有一个 python 脚本On this SO question 可以很好地解决问题

标签: linux shell command-line


【解决方案1】:

VESA DDC 连接是一个I2C 连接,可用于查询显示器是否存在。

Linux exposes the I2C device 和用户态程序可以使用http://jaffar.cs.msu.su/oleg/ddcci/ 等代码直接与监视器通信

请注意以下内容:Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]

# ddcci-tool /dev/i2c-2 -e -c -d


ddcci-tool version 0.03

Reading EDID : 0x50@/dev/i2c-2
    Plug and Play ID: SAM00BA
    Input type: Analog

Using ddc/ci : 0x37@/dev/i2c-2

Capabilities:
(type(LCD)vcp(04 05 10 12 60(1 3) B0(1 2) B6 C6 C8 C9 D6(1 4) DC(1 2 3 4) DF))

Controls (valid/current/max):
Control 0x04: +/0/1 [Reset Factory Defaults]
Control 0x05: +/0/1 [SAM: Reset Brightness and Contrast]
Control 0x06: +/0/1 [Reset Factory Geometry]
Control 0x08: +/0/1 [Reset Factory Default Color]
Control 0x0e: +/60/120 [SAM: Image Lock Coarse]
Control 0x10: +/0/100 [Brightness]
Control 0x12: +/50/100 [Contrast]
Control 0x16: +/8/16 [Red Video Gain]
Control 0x18: +/8/16 [Green Video Gain]
Control 0x1a: +/8/16 [Blue Video Gain]
Control 0x1e: +/0/2 [SAM: Auto Size Center]
Control 0x20: +/50/100 [Horizontal Position]
Control 0x30: +/25/54 [Vertical Position]
Control 0x3e: +/39/50 [SAM: Image Lock Fine]
Control 0x60: +/1/3 [Input Source Select]
Control 0x62: +/0/100 [Audio Speaker Volume Adjust]
Control 0x6c: +/140/255 [Red Video Black Level]
Control 0x6e: +/127/255 [Green Video Black Level]
Control 0x70: +/121/255 [Blue Video Black Level]
Control 0xb0: +/0/2 [Settings]
Control 0xb6: +/3/8 [???]
Control 0xc6: +/1/1 [???]
Control 0xc8: +/5/16 [???]
Control 0xc9: +/1/0 [???]
Control 0xca: +/2/2 [On Screen Display]
Control 0xcc: +/2/11 [SAM: On Screen Display Language]
Control 0xd6: +/1/4 [SAM: DPMS control (1 - on/4 - stby)]
Control 0xdc: +/4/4 [SAM: MagicBright (1 - text/2 - internet/3 - entertain/4 - custom)]
Control 0xdf: +/512/0 [VCP Version]
Control 0xe0: +/0/2 [SAM: Color preset (0 - normal/1 - warm/2 - cool)]
Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]
Control 0xe2: +/0/1 [???]
Control 0xed: +/108/255 [SAM: Red Video Black Level]
Control 0xee: +/101/255 [SAM: Green Video Black Level]
Control 0xef: +/103/255 [SAM: Blue Video Black Level]

一个有趣的问题是您的监视器是否返回该数据,如果没有,如果当前关闭,它是否会响应。

【讨论】:

  • 确实如此。甚至可以从命令行打开和关闭显示器。
  • ddcci 现在是 ddccontrol:sourceforge.net/projects/ddccontrol 正如 Eren 所说,它确实如此,您甚至可以使用 ddccontrol -r 0xe1 -w 0 自己做
  • 在我的情况下,当显示器打开并连接时,ddccontrol 会出错(抱怨不支持显示器),但 ddccontrol 在未连接或关闭时是静默的。仍在寻找一种方法来确定监视器(电视)是否在另一个输入上。但这可能超出了可行范围。此外,要让它在 Ubuntu 12.04 下运行,请参见此处:techytalk.info/ubuntu/ddccontrol,您可能需要执行 sudo modprobe i2c-dev 才能使其正常工作
  • 如果您愿意花一些时间在上面,那么您可以开始查看 i2cdetect 和 i2cdump,并探测所有内容(可能有风险?),然后将结果输出到文本文件,然后进行一些更改,例如输入选择...然后再次输出结果,然后进行比较,您也许可以辨别字节的含义...
  • 如果您有 Intel 显卡,您将如何启用所需的模块?
【解决方案2】:

来自systembash.com,这是从链接中获取的代码,以防有一天它会关闭:

#!/bin/bash
export DISPLAY=:0.0

if [ $# -eq 0 ]; then
  echo usage: $(basename $0) "on|off|status"
  exit 1
fi

if [ $1 = "off" ]; then
  echo -en "Turning monitor off..."
  xset dpms force off
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "on" ]; then
  echo -en "Turning monitor on..."
  xset dpms force on
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "status" ]; then
  xset -q|sed -ne 's/^[ ]*Monitor is //p'
else 
  echo usage: $(basename $0) "on|off|status"
fi

【讨论】:

  • 问题是关于确定不这样做
  • 可能会出现一些显示器被电源按钮关闭而另一些打开的情况。您的回答没有说​​明哪些显示器已打开。 xrandr 告知是否连接了监视器以及是否设置了显示模式。
  • 即使xrandr 也无法判断显示器是否已通过电源按钮关闭。我不知道如何通过命令行来确定。
【解决方案3】:

如果您的视频驱动程序支持此扩展,您可以使用 xrandr 命令行实用程序获取一些信息。

【讨论】:

  • 这似乎给了我从 xset 得到的相同错误读数。
【解决方案4】:

并非所有显示器都支持 vesa DDC。如果您使用扩展坞,事情可能会变得更加复杂。

另一方面,有一种方法可以通过监视内核/udev 事件来检查您的操作是否被检测到。 为此,对于 Fedora 和 RHEL,输入以下命令:

sudo udevadm monitor --property

它将显示它检测到的每个内核和 udev 事件。 由此,您可以尝试插拔显示器数据线;插拔显示器电源线;按电源按钮切换待机/开机状态。

如果一个动作后没有产生输出,那么你的系统就无法检测到它。

【讨论】:

    【解决方案5】:

    你可能想看看

    的输出
    $ xset -q
    

    我不确定它是否会起作用,但我认为“Monitor is (on|off)”这行应该会告诉你答案。

    【讨论】:

    • 即使我把它关掉了,仍然给我Monitor is On
    • 这可能只是显示显示器是否已插入。我认为没有办法查看它是否真的打开了。如果它已插入,则操作系统假定它已打开。 Gnome 可能只是在插入显示器时检测到它,而不是在它打开/关闭时检测到它。我想尝试这个的唯一方法是输入前面的命令,拔下你的显示器,按回车键,重新插入显示器,看看它说了什么。我无法测试这个 b/c 我在笔记本电脑上。
    • “即使我关闭它,它仍然让我显示器处于开启状态” - 你怎么知道?也许它足够聪明,可以在检测到您已打开显示器进行检查时返回并更改输出。现在我只需要弄清楚当你关上门时冰箱灯是否真的会熄灭。
    • 使用来自不同终端的 ssh,其监视器未关闭?
    • 即使显示器关闭,VESA DDC 控制器也必须通电。
    【解决方案6】:

    当使用 xset 时它总是返回 xset: 无法打开显示“”

    但是,“xset dpms force off”和“xset dpms force off”命令实际上会关闭和打开我的显示器。我正在使用此处列出的脚本 -

    http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/

    【讨论】:

      【解决方案7】:

      xset -q是树莓派的最佳选择。检查回复是否包含“显示器已开启”是使用 gpio 引脚关闭 LCD 背光的好方法;

      if(runOSCommand("xset -q").contains("Monitor is On")){
                  System.out.println("Monitor is On");
                  if screenLight.isHigh()) {
                      screenLight.low();
                  }
              }else{
                  System.out.println("Monitor is Off");
                  if (screenLight.isLow()) {
                      screenLight.high();
                  }
              }
      
      
           public static String runOSCommand(String command){
           String s = null;
           String string = "";
           Process p;
           try {
               p = Runtime.getRuntime().exec(command);
               BufferedReader br = new BufferedReader(
                   new InputStreamReader(p.getInputStream()));
               while ((s = br.readLine()) != null){
       //            System.out.println("line: " + s);
                   string += s;
               }
               p.waitFor();
      //         System.out.println ("exit: " + p.exitValue());
               p.destroy();
           } catch (Exception e) {}
           return string;
       }
      

      【讨论】:

        【解决方案8】:

        首先,找到您要检查的显示器的名称:

        xrandr -q
        

        然后将“THE-MONITOR”更改为正确的名称:

        #!/bin/sh    
        is_on="`xrandr -q | grep -A 1 'THE-MONITOR' | tail -1 | sed 's/[^\*]//g';`";
        

        管道:

        • xrandr -q 列出所有监视器;
        • grep -A 1 'THE-MONITOR' 过滤到两行,一行包含您的显示器的名称,以及连续的一行,如果显示器打开,旁边将有一个“*”;
        • tail -1 丢弃第一行;
        • sed 's/[^\*]//g' 过滤掉除“*”之外的所有内容;

        现在 "$is_on" 是一个布尔字符串,如 "*" 或空。

        这只有在您的首选模式位于模式列表顶部时才有效,这很常见。

        开启和关闭的整个脚本:

        #!/bin/bash
        is_on="`xrandr | grep -A 1 'DVI-I-1' | tail -1 | sed 's/[^\*]//g';`";
        if [ "$is_on" ]
        then
            xrandr --output DVI-I-1 --off
        else
            xrandr --output DVI-I-1 --auto --left-of HDMI-0 
        fi
        

        【讨论】:

          猜你喜欢
          • 2015-01-27
          • 1970-01-01
          • 1970-01-01
          • 2013-01-09
          • 2012-02-12
          • 2012-02-25
          • 1970-01-01
          • 2021-07-08
          • 1970-01-01
          相关资源
          最近更新 更多