Reconnecting HDMI
Switching my media PC from Windows to Bazzite went awfully uneventful. No issues whatsoever. At least for a while.
My media computer is connected to TV courtesy of type-C HDMI output. And I verified heck out of it. Computer’s output worked flawlessly with TV.
However, once TV was off for a while, turning on TV would result in “No Signal” message. At first I though it was sleep, but no. Computer was still reachable via network and it would all start working after reboot. A lot of troubleshooting later and I found a pattern, courtesy of DP status:
cat /sys/class/drm/card1-DP-2/status
If I run it while TV was on, I would see connected
status. If I turn TV off, I would still see connected
status. But, once I turned TV back on (after some time has passed), status would change to disconnected
. And nothing I’ve tried to do over network helped. Well, nothing but one thing.
If you ever used Ctrl+Alt+Fx
keys, you saw virtual terminals in action. Most of the time we only do stuff on the first terminal, but other terminals are sometime handy too. Switching to other terminal would actually reset connection every time. And that is something you can do from the command line.
chvt 2
sleep 1
chvt 1
Figuring it out was the first part. While I now knew how to recover connection to my TV, having to do it every time via network was annoying. I wanted something automatic.
The first step was creating script in /usr/local/bin/dp-reconnect
:
#!/bin/bash
function status() {
for DP_PATH in /sys/class/drm/card1-DP-*; do
DP_STATUS=$( cat "$DP_PATH/status" | grep '^connected$' | xargs )
if [[ "$DP_STATUS" == "connected" ]]; then
echo "Display $( basename "$DP_PATH" ) connected"
exit 0
fi
done
}
status
echo "No connected displays"
chvt 2
sleep 1
chvt 1
status
exit 1
Next step is to create service definition at /etc/systemd/system/dp-reconnect.service
:
[Unit]
Description=Switch terminal if no DP is connected
[Service]
Type=oneshot
ExecStart=/usr/local/bin/dp-reconnect
And lastly, we can create timer in /etc/systemd/system/dp-reconnect.timer
:
[Unit]
Description=Switch terminal if no DP is connected
[Timer]
OnCalendar=*-*-* *:*:00
Persistent=true
AccuracySec=1s
[Install]
WantedBy=timers.target
With all files in place, the only remaining task is to enable the timer.
sudo systemctl enable --now dp-reconnect.timer
With all in place, if display gets disconnected, script will reconnect it within a minute. Not perfect but lightweight enough not to be a serious hassle.