You are not logged in.
Pages: 1
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
If you're not sold on udev, https://aur.archlinux.org/packages/x-on-resize
Does the manual invocation of the script work?
Offline
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
fiif 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
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
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 trueOne 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 failedor when I try to make it a user unit:
x-on-resize.service: Failed at step GROUP spawning x-on-resize: Operation not permittedLast edited by diniamo (2023-05-01 11:27:38)
Offline
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
No, I'm pretty sure I don't run a compositor.
Offline
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
Pages: 1