Page 1 of 1

Auto detection of HDMI (and eth) post boot?

Posted: Tue Jul 24, 2012 3:43 pm
by factoid
Playing with my pi headless yesterday, I noticed that upon plugging it into my HDMI tv I wasn't getting a signal immediately. After playing around I found a work around, but I was wondering what would be involved in making this automagic?

from my ssh session, running /opt/vc/bin/tvsession -p will wake up the HDMI port, and then from the PI's local KB, hitting a CTRL+ALT+F# key to switch terminals will start outputting something (i.e. CTRL+ALT+F7 will get the desktop back, if it was set to start on boot). Alternatively I could do a 'startx' from the ssh session and end up with two desktops, but that would force the display to update on the TV.

What is it about the startx process that causes the video to start displaying (without requiring local keyboard input). Is there a way we can setup a dbus or other event watcher to respond properly to HDMI state changes?

As an aside, I've also found that if I use my ubuntu netbook's internet sharing, the pi only acquires an IP if the network sharing is in place before I power on the pi. I assumed the point of the magnetic Ethernet port was to be notified when a lan becomes available? Is there a fix for this issue as well?

Re: Auto detection of HDMI (and eth) post boot?

Posted: Tue Jul 24, 2012 3:53 pm
by PaulCheffus
factoid wrote:Playing with my pi headless yesterday, I noticed that upon plugging it into my HDMI tv I wasn't getting a signal immediately. After playing around I found a work around, but I was wondering what would be involved in making this automagic?

from my ssh session, running /opt/vc/bin/tvsession -p will wake up the HDMI port, and then from the PI's local KB, hitting a CTRL+ALT+F# key to switch terminals will start outputting something (i.e. CTRL+ALT+F7 will get the desktop back, if it was set to start on boot). Alternatively I could do a 'startx' from the ssh session and end up with two desktops, but that would force the display to update on the TV.

What is it about the startx process that causes the video to start displaying (without requiring local keyboard input). Is there a way we can setup a dbus or other event watcher to respond properly to HDMI state changes?

As an aside, I've also found that if I use my ubuntu netbook's internet sharing, the pi only acquires an IP if the network sharing is in place before I power on the pi. I assumed the point of the magnetic Ethernet port was to be notified when a lan becomes available? Is there a fix for this issue as well?
Hi

Have you tried

hdmi_force_hotplug=1

in your config.txt file?

Cheers

Paul

Re: Auto detection of HDMI (and eth) post boot?

Posted: Tue Jul 24, 2012 4:44 pm
by factoid
Yes, that doesn't cause the system to respond to changes in the state of the HDMI cable post boot. I'm talking about finding ways to make the OS aware of changes to the HDMI and ethernet port states, much the same way it would with USB.

The tvservice utility included with the videocore stuff seems to have what we need on the HDMI side, and I imagine there must be something simple that can be setup to respond to ethernet state changes, since that's already common elsewhere.

I'm willing to look into it further, I was just wondering if anyone else is working on/has solved this problem?

Re: Auto detection of HDMI (and eth) post boot?

Posted: Tue Jul 24, 2012 6:06 pm
by PaulCheffus
factoid wrote:Yes, that doesn't cause the system to respond to changes in the state of the HDMI cable post boot. I'm talking about finding ways to make the OS aware of changes to the HDMI and ethernet port states, much the same way it would with USB.
Hi

Sorry I don't get what you mean. With hdmi_force_hotplug=1 I can switch my TV away from the AV channel of the PI and back and the PI display then comes back. Prior to adding that I couldn't do this. Surely this is the same as wanting to plug in your screen after booting?

Cheers

Paul

Re: Auto detection of HDMI (and eth) post boot?

Posted: Tue Jul 24, 2012 6:13 pm
by factoid
Have you tried booting the RPI headless, and then plugging in into a TV? I'll have to double check with the hotplug forced on, but I don't think it worked for me. My recollection was that if you were connected to a TV on boot, then you were fine after unplugging and re-plugging. But if you started it up without an HDMI connected, then plugged it in, the display isn't active.

In either case, having to force users to force HDMI always on defeats the purpose. No harm in having something that makes the system more self aware and self configuring, no matter when you plug stuff in.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Mon Aug 13, 2012 11:01 am
by NormanDunbar
Just a thought. I'm pretty sure I read somewhere that the HDMI is not hot-pluggable and plugging in or removing the cable, after boot and with power on, can render the Pi deader than a dead thing.

Apparently only the Audio 3.5mm output, the USB and the Network are hot-plug, the HDMI isn't.I've no idea about composite.

Cheers,
Norm.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Tue Aug 14, 2012 6:15 am
by factoid
It'd be helpful if you could link to where you heard that. That doesn't make any sense to me, since HDMI is an inherently hot pluggable device. The config.txt file even has options for hdmi_force_hotplug and hdmi_ignore_hotplug.

I've pulled the HDMI cable after boot and re-attached with no apparent damage, and I'd be very surprised if a device intended to be used by children couldn't handle being plugged/unplugged from a TV while running. Any other HDMI device I've ever used (XBox, PS3, etc...) has had no issue with that.

Also, I've been able to deactivate and re-activate the hdmi connection after boot, I just haven't looked into determining how to have the kernel or udev be notified of the state change and respond appropriately.

But my pi has been running headless for the last while, while I build stuff and hack around with accelerated X support (haven't gotten to the point where it needs to actually display anything).

Re: Auto detection of HDMI (and eth) post boot?

Posted: Tue Aug 14, 2012 9:36 am
by dom
NormanDunbar wrote:Just a thought. I'm pretty sure I read somewhere that the HDMI is not hot-pluggable and plugging in or removing the cable, after boot and with power on, can render the Pi deader than a dead thing.
There's certainly no damage that can occur from hot plugging.

Suppose you remove HDMI cable and replug into a different display. It might not support the resolution of the first display. What do you do with your console framebuffer, and the video app playing on top of it?

It's complicated, so we say it is unsupported.

In fact, I believe the API allows the host (e.g. Linux) to detect the hotplug removal, power off HDMI output. Power it on again at a new resolution, recreate the framebuffer console (with fbset at new resolution). (If you were running a full screen SDL app at that time what do you do?)

Someone could write a HDMI service to try and do the right thing, but currently I'm not sure what the right thing is.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Wed Aug 15, 2012 2:03 pm
by NormanDunbar
factoid wrote:It'd be helpful if you could link to where you heard that. That doesn't make any sense to me, since HDMI is an inherently hot pluggable device. The config.txt file even has options for hdmi_force_hotplug and hdmi_ignore_hotplug.
No problem, Element 14's Quick Start Guide is one place I read it. I also think I saw it in Linux Format (or Linux User & Developer) as well.

The following is from the Quick Start Guide which you can find here: http://www.farnell.com/datasheets/1524403.pdf
It is always recommended to connect the MicroUSB Power to the unit last (while most
connections can be made live, it is best practice to connect items such as displays/h/w pin
connections with the power turned off).
There's also a forum answer at Element 14 which advises not plugging in the HDMi under power because the pins are small and close together and wrongly inserting could lead to power going up/down the wrong wires. (Sorry, lost the link.)

Cheers,
Norm.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Wed Aug 15, 2012 2:15 pm
by bredman
I wouldn't trust anything that Element14 writes. They have a habit of scraping unverified crap from wikis and then publishing it as fact.

I had a good laugh at that bit about mixing up the pins while plugging in a HDMI cable.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Wed Aug 15, 2012 2:20 pm
by NormanDunbar
bredman wrote:I wouldn't trust anything that Element14 writes. They have a habit of scraping unverified crap from wikis and then publishing it as fact.
I'm an old git, ;) , and I started computing with a ZX-81. Nothing on that was hot-plug! It was a sure fire way to trash the computer. I also remember the first PCs which couldn't be hot plugged either, even the monitors. I did an HP Unix Administrators course years ago and I can remember being very wary of the fact that HP allowed kit to be plugged in and out willy nilly.
bredman wrote:I had a good laugh at that bit about mixing up the pins while plugging in a HDMI cable.
Not so much mixing up the pins, but getting the plug in slightly off centre could, I assume, lead to a short? When I got my HDMI cable (£1.00 from Amazon.co.uk) I remember thinking that there was a lot of very small pins in it.

Cheers,
Norm.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Wed Aug 15, 2012 2:37 pm
by factoid
I'm from the era of Amiga and the 386, and while there was a lot of stuff that didn't hot plug then either, it also didn't damage anything. It usually just didn't work. HDs and anything that needed a safe shutdown was of course an exception, but most things involving data transfer were fine. Printers and modems didn't care when you connected them, just that link was established before you started pushing data.

Anyway, E14 is just manufacturing the device, and probably trying to CYA (T, in their case). Given that all the external jacks on the pi are by design hot pluggable, this thing would have never passed certification if it ran the risk of damaging itself or things connected to it after power on.

I'll put more stock in what Dom says than anything from E14 (regarding the pi), especially as it agrees with my own direct experience with the HDMI, and also with how HDMI is suppose to be used.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Wed Aug 15, 2012 2:41 pm
by NormanDunbar
Factoid, Dom,

thanks.

Cheers,
Norm.

Re: Auto detection of HDMI (and eth) post boot?

Posted: Wed Apr 13, 2016 7:00 pm
by nharris
I know this is a very old thread but I'm sure someone will find this useful. I created two scripts that together allow headless booting and hotplugging the HDMI to different monitors as needed. I never intend to use the composite so it is effectively disabled by these scripts. I'm running a Pi3 as a portable media player/server. OSMC can play to any available DVI/HDMI tv or monitor while Plex Media Server streams to devices. When no HDMI is connected Kodi is shutdown to save resources. The first script is "activateHDMI" and is called by rc.local. The last command in this script fires off "hdmiPlugged" which monitors the plugged/unplugged state of the HDMI port.

activateHDMI
_______________________________________________________________________
#!/bin/bash
# sleep a few seconds for bootup to complete
sleep 10
# Check for active HDMI. If HDMI is not active then shutdown the tv output
# and bring it back up with HDMI active and start the HDMI monitoring script
if /opt/vc/bin/tvservice -s | grep 'NTSC';then
/opt/vc/bin/tvservice -o
/opt/vc/bin/tvservice -p
nohup /home/osmc/hdmiPlugged &
else
nohup /home/osmc/hdmiPlugged &
fi
___________________________________________________________________

hdmiPlugged
__________________________________________________________________
#!/bin/bash
# Name of service or app that needs the display. In my case it is OSMC's instance of Kodi. I'm sure this could be tweaked for X11.
service=mediacenter
######################
# Check if the HDMI is unplugged. If it is unplugged, we make sure mediacenter is not running. If mediacenter is running,
# we stop its service to save resources and call the plugged function.
unplugged () {
while /opt/vc/bin/tvservice -s | grep '0x120009\|0x120005\|0x40005' ; do
if (($(ps -ef | grep -v grep | grep $service | wc -l) > 0 )); then
systemctl stop mediacenter
#printf '\n unplugged' >> /home/osmc/hdmi.log
fi
sleep 5
plugged
done
}

#####################
# Check to see if the HDMI is plugged up. If it is then we make sure that mediacenter service is running. If mediacenter is not running,
# we start it and call the unplugged function.
plugged () {
while /opt/vc/bin/tvservice -s | grep '0x12000a\|0x120006\|0x40006' ; do
if ! (($(ps -ef | grep -v grep | grep $service | wc -l) > 0 )); then
sleep 5
# Reset HDMI out by switching it off and back on and then set the framebuffer.
/opt/vc/bin/tvservice -o
sleep 5
/opt/vc/bin/tvservice -p
fbset -depth 8
fbset -depth 16
sleep 1
# Kodi was not running so we start it.
systemctl start mediacenter
fi
sleep 5
unplugged
done
}
# Sleep for 30 seconds to allow for boot up to complete.
sleep 30

plugged
unplugged
_________________________________________________________________

Re: Auto detection of HDMI (and eth) post boot?

Posted: Mon May 30, 2016 2:40 pm
by srburrito
nharris wrote:I know this is a very old thread but I'm sure someone will find this useful. I created two scripts that together allow headless booting and hotplugging the HDMI to different monitors as needed. I never intend to use the composite so it is effectively disabled by these scripts. I'm running a Pi3 as a portable media player/server. OSMC can play to any available DVI/HDMI tv or monitor while Plex Media Server streams to devices. When no HDMI is connected Kodi is shutdown to save resources. The first script is "activateHDMI" and is called by rc.local. The last command in this script fires off "hdmiPlugged" which monitors the plugged/unplugged state of the HDMI port.

activateHDMI
_______________________________________________________________________
#!/bin/bash
# sleep a few seconds for bootup to complete
sleep 10
# Check for active HDMI. If HDMI is not active then shutdown the tv output
# and bring it back up with HDMI active and start the HDMI monitoring script
if /opt/vc/bin/tvservice -s | grep 'NTSC';then
/opt/vc/bin/tvservice -o
/opt/vc/bin/tvservice -p
nohup /home/osmc/hdmiPlugged &
else
nohup /home/osmc/hdmiPlugged &
fi
___________________________________________________________________

hdmiPlugged
__________________________________________________________________
#!/bin/bash
# Name of service or app that needs the display. In my case it is OSMC's instance of Kodi. I'm sure this could be tweaked for X11.
service=mediacenter
######################
# Check if the HDMI is unplugged. If it is unplugged, we make sure mediacenter is not running. If mediacenter is running,
# we stop its service to save resources and call the plugged function.
unplugged () {
while /opt/vc/bin/tvservice -s | grep '0x120009\|0x120005\|0x40005' ; do
if (($(ps -ef | grep -v grep | grep $service | wc -l) > 0 )); then
systemctl stop mediacenter
#printf '\n unplugged' >> /home/osmc/hdmi.log
fi
sleep 5
plugged
done
}

#####################
# Check to see if the HDMI is plugged up. If it is then we make sure that mediacenter service is running. If mediacenter is not running,
# we start it and call the unplugged function.
plugged () {
while /opt/vc/bin/tvservice -s | grep '0x12000a\|0x120006\|0x40006' ; do
if ! (($(ps -ef | grep -v grep | grep $service | wc -l) > 0 )); then
sleep 5
# Reset HDMI out by switching it off and back on and then set the framebuffer.
/opt/vc/bin/tvservice -o
sleep 5
/opt/vc/bin/tvservice -p
fbset -depth 8
fbset -depth 16
sleep 1
# Kodi was not running so we start it.
systemctl start mediacenter
fi
sleep 5
unplugged
done
}
# Sleep for 30 seconds to allow for boot up to complete.
sleep 30

plugged
unplugged
_________________________________________________________________
This is exactly what I want to do, but forgive me, I don't know where to put it. Is it mean to go in config.txt?

Re: Auto detection of HDMI (and eth) post boot?

Posted: Mon May 30, 2016 5:05 pm
by nharris
For simplicity I keep the scripts in my profile folder. i.e. /home/osmc/ I added "/bin/sh /home/osmc/activateHdmi &" (without quotes) to /etc/rc.local to run it.