如何确定是否从Linux命令行打开LCD监视器

时间:2010-08-08 05:26:11

标签: linux shell command-line

如何判断计算机的显示器是否在Linux中从命令行打开/关闭?我传统上认为监视器只是输出设备,但我注意到Gnome Monitor Preferences对话框有一个“detect monitor”功能。是否可以推广以确定显示器是否已实际关闭?

8 个答案:

答案 0 :(得分:13)

VESA DDC连接是I2C连接,可用于查询监视器的存在。

Linux exposes the I2C device和userland程序可以使用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]

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

答案 1 :(得分:10)

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

答案 2 :(得分:5)

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

答案 3 :(得分:3)

并非所有监视器都支持vesa DDC。如果你使用码头,事情会变得更加复杂。

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

sudo udevadm monitor --property

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

如果操作后没有生成输出,则系统无法检测到它。

答案 4 :(得分:2)

使用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/

答案 5 :(得分:1)

您可能想查看

的输出
$ xset -q

我不确定它是否会起作用,但我认为“显示器是(开|关)”这一行应该告诉你答案。

答案 6 :(得分:1)

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;
 }

答案 7 :(得分:1)

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

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