RPI: count-conntected-screens-rpi-i3

Introduction

I used to use the tvservice command to count the number of connected screens in Rasbperry Pi OS. This stopped working just recently after an update. Not really sure why. The output on a raspberry pi said that it is not supported with the display driver.

Old behavior:

pi@raspberrypi:~ $ tvservice -l
1 attached device(s), display IDs are : 
Display Number 2, type HDMI 0

#--- count the number of displays
pi@raspberrypi:~ $ tvservice -l | head -1 | awk '{ print $1}'
1

New behavior, on a freshly installed Raspbian OS:

dashboard@raspberrypi:~ $ tvservice -l
tvservice is not supported when using the vc4-kms-v3d driver.
Similar features are available with standard linux tools
such as modetest from libdrm-tests.

I rely on knowing how many screens are attached to a Raspberry Pi in one of my projects, so I borrowed a snippet from reddit and adapted it to my liking to replace the lost funcitonality:

$ DISPLAY=:0 xprop -notype -root _NET_DESKTOP_NAMES 
_NET_DESKTOP_NAMES = "1", "2"

#--- count the number of displays
$ DISPLAY=:0 xprop -notype -root _NET_DESKTOP_NAMES | awk -F= '{print $2}' | awk -F, '{print NF}'
2

Update: 2022-05-05

I just figured out that tvservice works if you tell your pi to use vc4-fkms-v3d instead of the vc4-kms-v3d driver in the /boot/config.txt

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

After a reboot, tvservice -l works again:

pi@raspberrypi:~ $ tvservice -l
2 attached device(s), display ID's are : 
Display Number 2, type HDMI 0
Display Number 7, type HDMI 1

References