You are not logged in.

#1 2023-04-29 17:29:30

diniamo
Member
Registered: 2022-06-15
Posts: 107

[SOLVED] HDMI hotplug script failing

I've been trying to create a script that automatically sets the video and audio output to HDMI-1 when I plug the cable in. This is what I currently have:

/usr/local/bin/hdmi_toggle.sh:

#!/bin/bash

export PATH=/usr/bin

USER_NAME=$(who | awk -v vt=tty$(fgconsole) '$0 ~ vt {print $1}')
USER_ID=$(id -u "$USER_NAME")
CARD_PATH="/sys/class/drm/card0/"
AUDIO_OUTPUT="analog-stereo" VIDEO_OUTPUT="eDP-1" VIDEO_OUTPUT_OFF="HDMI-1"
PULSE_SERVER="unix:/run/user/"$USER_ID"/pulse/native"

for OUTPUT in $(cd "$CARD_PATH" && echo card*); do
  OUT_STATUS=$(<"$CARD_PATH"/"$OUTPUT"/status)
  if [[ $OUT_STATUS == connected ]]
  then
    echo $OUTPUT connected
    case "$OUTPUT" in
      "card0-HDMI-A-1")
        AUDIO_OUTPUT="hdmi-stereo" # Digital Stereo (HDMI 1)
		VIDEO_OUTPUT="HDMI-1"
     ;;
      "card0-HDMI-A-2")
        AUDIO_OUTPUT="hdmi-stereo-extra1" # Digital Stereo (HDMI 2)
		VIDEO_OUTPUT="HDMI-2"
     ;;
    esac

	VIDEO_OUTPUT_OFF="eDP-1"
  fi
done

echo selecting output $AUDIO_OUTPUT
# sudo -u "$USER_NAME" pactl --server "$PULSE_SERVER" set-card-profile alsa_card.pci-0000_00_1f.3 output:$AUDIO_OUTPUT+input:analog-stereo
pactl --server "$PULSE_SERVER" set-card-profile alsa_card.pci-0000_00_1f.3 output:$AUDIO_OUTPUT+input:analog-stereo

# while ! xrandr | grep 'HDMI1 connected' ; do sleep 1; done;

echo selecting output $VIDEO_OUTPUT, unselecting output $VIDEO_OUTPUT_OFF
# sudo -u "$USER_NAME" xrandr --output "$VIDEO_OUTPUT" --auto
# sudo -u "$USER_NAME" xrandr --output "$VIDEO_OUTPUT_OFF" --off
xrandr --output "$VIDEO_OUTPUT" --auto && xrandr --output "$VIDEO_OUTPUT_OFF" --off}}

/etc/udev/rules.d/99-hdmi_sound.rules:

KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="$HOME/.Xauthority", RUN+="/usr/local/bin/hdmi_toggle.sh"

Result: when I plug the cable in, my laptop's screen goes black, but nothing happens on the HDMI monitor. When I unplug the cable, my laptop's screen flashes for a brief second, only to then remain black. (the audio part of it works fine)

Last edited by diniamo (2023-07-26 09:13:58)

Offline

#2 2023-04-29 20:02:25

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,337

Re: [SOLVED] HDMI hotplug script failing

If you're not sold on udev, https://aur.archlinux.org/packages/x-on-resize
Does the manual invocation of the script work?

Offline

#3 2023-04-30 08:13:31

diniamo
Member
Registered: 2022-06-15
Posts: 107

Re: [SOLVED] HDMI hotplug script failing

Thanks for the pointer, the script was indeed broken, I simplified it to this (now it works if I run it from my terminal):

#!/bin/bash

export PATH=/usr/bin

if [[ $(</sys/class/drm/card0/card0-HDMI-A-1/status) == "connected" ]]
then
	pactl set set-card-profile alsa_card.pci-0000_00_1f.3 output:hdmi-stereo
	xrandr --output HDMI-1 --auto && xrandr --output eDP-1 --off
else
	pactl set set-card-profile alsa_card.pci-0000_00_1f.3 output:analog-stereo
	xrandr --output eDP-1 --auto && xrandr --output HDMI-1 --off
fi

if I run it from udev however, it only affects my laptop's screen (as intended), but doesn't do anything on the HDMI display. I'm not sure how to fix that. Also, it seems to kill my polybar when unplugging.

Last edited by diniamo (2023-04-30 08:14:21)

Offline

#4 2023-04-30 14:11:26

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,337

Re: [SOLVED] HDMI hotplug script failing

The udev rule probably fires before the output is registered on the server, try x-on-resize (in case you worry about the provenance, Keith drafted and wrote most of the randr extension…)

If this doesn't implicitly handle the status of the polybar, try to delay the "--off" call (… && sleep 1 && xrandr …) and check whether there're coredumps, https://wiki.archlinux.org/title/Core_d … _core_dump

Offline

#5 2023-05-01 07:52:53

diniamo
Member
Registered: 2022-06-15
Posts: 107

Re: [SOLVED] HDMI hotplug script failing

Nice, x-on-resize worked. It also solved the polybar issue - somewhat. The process itself doesn't die anymore, but here is what happens: whenever I plug or unplug, I get put on xworkspace 1, but I don't see the contents of it until I switch to another desktop and back, and polybar is only visible on said workspace. I've noticed similar issues (polybar only visible on the desktop where I turned it back on) when trying to toggle polybar, which I solved by doing:

super + alt + p
	{ killall polybar && bspc config top_padding +0, polybar &; sleep 0.1; bspc wm -r }

Also, this might be related: I added this settings to bspwm, so I don't get that "Desktop" workspace when using the HDMI display.

bspc config merge_overlapping_monitors true

One more thing, is there no service + config file for x-on-resize? I couldn't find any, so I'd assumed that I have to make my own, but when I try to start it, it gives me an error:

XOpenDisplay  failed

or when I try to make it a user unit:

x-on-resize.service: Failed at step GROUP spawning x-on-resize: Operation not permitted

Last edited by diniamo (2023-05-01 11:27:38)

Offline

#6 2023-05-01 14:08:32

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,337

Re: [SOLVED] HDMI hotplug script failing

I don't see the contents of it until I switch to another desktop and back

Do you run a compositor (picom, xcompmgr)?
Can you interact w/ the "blind" polybar?

(I run neither bspwm nor polybar, you should test their behavior when xrandr-changing the layout w/o de- or attaching outputs and in doubt file an upstream bug)

Offline

#7 2023-05-01 21:23:55

diniamo
Member
Registered: 2022-06-15
Posts: 107

Re: [SOLVED] HDMI hotplug script failing

No, I'm pretty sure I don't run a compositor.

Offline

#8 2023-05-02 15:20:46

diniamo
Member
Registered: 2022-06-15
Posts: 107

Re: [SOLVED] HDMI hotplug script failing

And no, I can't interact with polybar, because even the space where it should be is gone (the open windows occupy it instead). I also just noticed that polybar doesn't disappear on desktops where I don't have anything open.

Offline

Board footer

Powered by FluxBB