DarkElvenAngel
Posts: 125
Joined: Tue Mar 20, 2018 9:53 pm

Need a script to detect available displays.

Mon Jun 04, 2018 8:57 pm

Hello,

I have a software framework I wrote for a kiosk display this works very well at detecting during boot if an HDMI display is connected or not. Giving the framework the ability to run in different modes. Ie kiosk or headless.

My problem is I don't know how to detect other types of displays ie DSI namely the Official Touch Screen or RCA.

Is there a simple way to detect these other display modes?

If multiple displays are attached?

If one is added after booting like a daemon.(in this case name HDMI, I've considered polling the vcgencmd to detect HDMI hotplug)

The point of my script is to be able to take the SD from one environment and place it into another and have the framework launch the appropriate actions.

If it helps this is my screentype script it very simple but it works.

Code: Select all

#!/bin/bash
 
video="$(tvservice -s | sed "s/^.*\[\([^ ]*\) .*$/\1/" )"
 
if [ "$video" != "HDMI" ];
then
  echo "headless"
else
  echo "HDMI"
fi
Thanks for any and all insights

DarkElvenAngel
Posts: 125
Joined: Tue Mar 20, 2018 9:53 pm

Re: Need a script to detect available displays.

Mon Jun 04, 2018 9:06 pm

I felt I should add this snippet of code as well

Code: Select all

tvservice -s | awk '{print $2}'
I get 0x40001 while headless on a pi zero.

I remember seeing something about this value and what some of the bits mean in this output.

DarkElvenAngel
Posts: 125
Joined: Tue Mar 20, 2018 9:53 pm

Re: Need a script to detect available displays.

Wed Jun 06, 2018 3:30 pm

I've done more poking about and have some output examples from tvservice -s

Code: Select all

Active HDMI
0x12000a [HDMI CEA (4) RGB lim 16:9], 1280x720 @ 60.00Hz, p
Unpluged after Boot HDMI
0x120009 [HDMI CEA (4) RGB lim 16:9], 1280x720 @ 60.00Hz, p
Headless
0x40001 [NTSC 4:3], 720x480 @ 60.00Hz, i
Headless with HDMI Pluged after
0x40002 [NTSC 4:3], 720x480 @ 60.00Hz, i
Headless after running tvservice -p
0x40005 [DVI CEA (1) RGB lim 4:3], 640x480 @ 60.00Hz, p
With Offical Touch Screen
0x400000 [LCD], 800x480 @ 60.00Hz, p
The text output is enough for me to detect 2 modes reliable (LCD & HDMI) the output hex values however seem to be infinitely more useful if I can decode them.

I see a pattern in bits 1 and 0.
If bit 1 is set then HDMI is pluged in
If bit 0 is set then HDMI is absent or not hotplug compatible.
(There is a setting in config.txt that forces hotplug to always be set would fool my script)

tvservice -M is able to pickup HDMI hotplug events is it possible to daemonize this?

Justin Buist
Posts: 9
Joined: Thu Jan 18, 2018 10:43 pm

Re: Need a script to detect available displays.

Tue Jun 12, 2018 3:23 pm

You could try looking in /sys/class/graphics. For example the directory /sys/class/graphics/fb0 contains information about the onboard display. Checking out /sys/class/graphics/fb0/virtual_size on a machine with an actual display connected shows me the proper resolution of 1600x900. If I check that file on a headless box I get a weird 656x416 resolution. You might be able to use that information, or something else in that folder, to detect all sorts of displays.

DarkElvenAngel
Posts: 125
Joined: Tue Mar 20, 2018 9:53 pm

Re: Need a script to detect available displays.

Wed Jun 13, 2018 12:15 am

Justin Buist wrote:
Tue Jun 12, 2018 3:23 pm
You could try looking in /sys/class/graphics. For example the directory /sys/class/graphics/fb0 contains information about the onboard display. Checking out /sys/class/graphics/fb0/virtual_size on a machine with an actual display connected shows me the proper resolution of 1600x900. If I check that file on a headless box I get a weird 656x416 resolution. You might be able to use that information, or something else in that folder, to detect all sorts of displays.
Thanks for that tip and a novel approach

DarkElvenAngel
Posts: 125
Joined: Tue Mar 20, 2018 9:53 pm

Re: Need a script to detect available displays.

Tue Jun 25, 2019 3:08 am

DarkElvenAngel wrote:
Mon Jun 04, 2018 8:57 pm
Hello,

I have a software framework I wrote for a kiosk display this works very well at detecting during boot if an HDMI display is connected or not. Giving the framework the ability to run in different modes. Ie kiosk or headless.

My problem is I don't know how to detect other types of displays ie DSI namely the Official Touch Screen or RCA.

Is there a simple way to detect these other display modes?

If multiple displays are attached?

If one is added after booting like a daemon.(in this case name HDMI, I've considered polling the vcgencmd to detect HDMI hotplug)

The point of my script is to be able to take the SD from one environment and place it into another and have the framework launch the appropriate actions.

If it helps this is my screentype script it very simple but it works.

Code: Select all

#!/bin/bash
 
video="$(tvservice -s | sed "s/^.*\[\([^ ]*\) .*$/\1/" )"
 
if [ "$video" != "HDMI" ];
then
  echo "headless"
else
  echo "HDMI"
fi
Thanks for any and all insights
I'm revisiting this with a new question specifically for the RPI 4B as it truly has multiple monitor support, does tvservice still work? ( I checked the git page and the source is not changed )

Anyone have a RPI 4B I'm interested in the output you are getting.

Return to “Advanced users”