7

We build an applications that supports a server mode and a GUI mode. Now the XServer and the application should start, if a HDMI Cable is connected otherwise it should only run the server.

So whats the best way to detect HDMI? Read the resolution and if it's not default HDMI resolution it's connected? But this wouldn't be possible if the PI had booted already, wouldn't it?

  • It may be simpler to test the runlevel using the runlevel command. if running in runlevel 5 would impy a gui supported setup. Then you could launch the GUI as part of the GUI startup. – Steve Robillard Apr 20 '16 at 22:08
  • @SteveRobillard Checking the runlevel would be A) Depreciated, B) Not necessarily indicative of whether X is running. I didn't check, but I'd bet what happens with a GUI system with no display attached is X starts, finds no display, and exits. However, the runlevel (or non-depreciated equivalent) would still remain the same, so if you wanted a system that could be configured to use a GUI when available that's not a useful test... – goldilocks Apr 21 '16 at 1:12
  • ...You can definitely get detailed information from the output of the X server -- however in fact the output of systemctl status ______ would probably be the easiest and most decisive thing to parse here. Probably there's a python API :P And to query the X server for the displays and resolution (also see man xargs). – goldilocks Apr 21 '16 at 1:12
10

There's a command line utility, tvservice built into Raspbian that will tell you the state of the HDMI.

To detect the full state, you can use the command: tvservice -s. On my RPi this currently outputs:

state 0x40001 [NTSC 4:3], 720x480 @ 60.00Hz, interlaced

The states (that I've gathered) are as follows:

  • 0x40001 Not initialized and HDMI cable is disconnected
  • 0x40002 Not initialized but HDMI cable is connected
  • 0x120002 Standby mode
  • 0x120005 HDMI disconnected
  • 0x120016 Active

If you're less interested in the additional details tvservice -n may be slightly simpler to parse. It gives the current name of connected device. Example:

[E] No device present

Additionally, if connectivity is touch and go, you may find the -M flag useful. It monitors for events concerned with the HDMI port.

Sources:

Edit:

As Ghanima pointed out in the comments, tvservice is also available in Arch under /opt/vc/bin/tvservice.

  • 2
    On a side note, it's not just available an Raspbian. Arch has it to, it sits in /opt/vc/bin/tvservice and comes as part of the raspberrypi-firmware-tools package that is installed by default. – Ghanima Apr 21 '16 at 6:11
2

The tvservice command does not consistently sense when HDMI is disconnected. Say you boot the Pi with HDMI, then manaully disconnect the HDMI cable. The tvservice command will still report that HDMI device is connected, in that -n, -s, etc. commands still report the last previously connected monitor via HDMI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.