4

Sometimes I connect my laptop to my TV over HDMI to have a bigger screen. Unfortunately, it doesn't automatically switch the audio output, so I have to do that myself every single time I plug or unplug it, with either of those two, to have the sound come from where I want it to come from.

  • pacmd set-card-profile 0 output:hdmi-stereo-extra1
  • pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo

Is there any way to detect if HDMI is plugged in, or at least if a change has occurred? Thanks!

Linux Mint 18.2 Xfce x64, Asus P756U

Katembers
  • 103
  • 1
  • 1
  • 8
  • 1
    This Q is not about programming as defined for StackOverflow. It **may** be more appropriate on https://superuser.com OR https://unix.stackexchange.com . Use the `flag` link at the bottom of your Q and ask the moderator to move it. Please don't post the same Q on 2 different sites. Please read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/how-to-ask , https://stackoverflow.com/help/dont-ask and https://stackoverflow.com/help/mcve before posting more Qs here.Good Luck – shellter Dec 24 '17 at 17:04
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Dec 24 '17 at 20:36

1 Answers1

13

I am using two different ways to determine if HDMI is plugged in:

a) Using xrandr
A simple xrandr will report your hdmi monitor as connected To use this in a script you can do something like:

hdmi_active=$(xrandr |grep ' connected' |grep 'HDMI' |awk '{print $1}')

Above will return the connected hdmi port (i.e HDMI-1) or will return nothing if no HDMI is connected.

You can then use something like

[[ ! -z "$hdmi_active" ]] && do_your_stuff 

z becomes true if $hdmi_active is not set . ! z reverts this behavior and returns true if hdmi_active has a value = hdmi is connected

b) Using the HDMI status file:

$ cat /sys/class/drm/card0/*HDMI*/status

This returns connected / disconnected for your hdmi ports:

$ cat /sys/class/drm/card0/*HDMI*/status
disconnected
disconnected

You can then test against that result with something like:

hdmi_active="$(cat /sys/class/drm/card0/*HDMI*/status |grep '^connected')" #Using ^ we avoind matching disconnected from the regex match, since ^ in an anchor to the beginning of the line
[[ ! -z "$hdmi_active" ]] && do_your_stuff #hdmi is active
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • What is the status if the HDMI cable is plugged in to the laptop, but on the screen/TV another HDMI input is selected? I'm wondering if I can detect input change on the screen. – nagy.zsolt.hun Jul 25 '21 at 12:02
  • 1
    I tell you know. It does not work, and as of now, it is an unsolved problem. – Juanse Cora Nov 03 '21 at 14:40
  • @JuanseCora When you say "it does not work" you refer to my solution or to the question posted by nagy.zsolt.hun ? – George Vasiliou Nov 03 '21 at 16:56
  • 1
    @GeorgeVasiliou I refer to nagy.zsolt.hun comment in which he asks if it can detect input change on the scrren. – Juanse Cora Nov 03 '21 at 17:05