Detect if HDMI Monitor is switched off

6,952

I don't see anything wrong with parsing the output of ddccontrol. DDC is the right way to get the information you want. Unlike with VGA, where DDC was created, the HDMI connector was designed to include DDC from the start. They even went back and modified the DDC standard to add more features for HDMI, calling it E-DDC.

On Linux, the userland tool for accessing DDC info is ddccontrol, so the fact that it doesn't have a flag that makes it do what you want out of the box is no reason to avoid using what's currently provided. If anything, it's an invitation to crack the code open and provide a patch.

Meanwhile, here's a short Perl script to limp by with:

#!/usr/bin/perl
# monitor-on.pl
my $CMD = open '-|', 'ddclient -p' or die "Could not run ddclient: $!\n";
local $/ = undef;   # slurp command output
my $out = <$CMD>;
if ($out =~ m/> Power control/) {
    if ($out =~ m/id=dpms/) {
        print "asleep\n";
    }
    elsif ($out =~ m/id=on/) {
        print "on\n";
    }
    elsif ($out =~ m/id=standby/) {
        print "off\n";
    }
    else {
        print "missing?\n";
    }
}
else {
   # Monitor is either a) not DDC capable; or b) unplugged
   print "missing!\n";
}

This script is untested. I don't have any non-headless ("headed"?) Linux boxes to test with here. If it doesn't work, the fix should be obvious.

It could be made smarter. It won't cope with multiple monitors right now, and it's possible its string parsing could be confused, since it doesn't check that the power status strings it searches for are within the > Power control section.

Share:
6,952

Related videos on Youtube

darnir
Author by

darnir

Updated on September 18, 2022

Comments

  • darnir
    darnir over 1 year

    I have a Monitor connected to my machine through HDMI.
    Now if anyone were to switch off the Monitor, through either the Soft Buttons on it, or by removing it's Power Cord, I wish to be notified and run a Shell Script.

    I tried many ways to identify when a monitor is switched on or off (It's always connected). The only technique that comes close is:

    # ddccontrol -p

    When the external monitor is connected, this returns all kinds of details about the monitor. I could write a script to parse the output for that. However this seems like a unreliable technique for un-supervised usage.

    Is there any way through which I could obtain a Yes/No answer to whether the Monitor is Switched On/Off?

    EDIT: It would be preferable if I can get a message on status change. Since this will be running continuously for days, I do not wish to poll for the status of the monitor. Instead in case it is switched off, I would like to be informed through a message.

  • darnir
    darnir over 11 years
    The output I get is a tad different. I however modified the script to work with it. Thanks a lot! I do have another issue though, using ddccontrol will require polling it every x seconds to get the status. I was hoping that wouldn't be required.
  • Warren Young
    Warren Young over 11 years
    @darnir: Regarding the script changes, feel free to edit my answer. You don't have enough reputation to get instant acceptance, but that's actually a good thing because in this stage, you get points for approved edits.
  • Warren Young
    Warren Young over 11 years
    @darnir: Regarding polling, I don't see a way around that, unless you find out what ddccontrol is reading, and find that that data source works with select(2) or similar, so you can transition to an event-driven paradigm. And again, if so, you'd want to start with the ddccontrol source code.