Skip to content

Instantly share code, notes, and snippets.

@karlyeurl
Last active November 24, 2022 08:35
Show Gist options
  • Save karlyeurl/890688f8741f2f92bd74a94ca2b49ea2 to your computer and use it in GitHub Desktop.
Save karlyeurl/890688f8741f2f92bd74a94ca2b49ea2 to your computer and use it in GitHub Desktop.
Auto-HDMI on a per-screen basis
ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/systemctl --no-block start hdmi-config.service"
#!/bin/bash
# first let's check that the screen isn't locked so we don't pop arandr unnecessarily
if ps aux | grep i3lock | grep -vE "(xautolock|grep)" > /dev/null;
then
echo "The screen is locked, external monitors may just have been put to sleep, not changing anything."
exit
fi
until xrandr > /dev/null
do
echo "Waiting for X11"
sleep 1
done
# get a unique ID depending on the screen combination that's plugged in
hash_dp1_edid=$(md5sum -b /sys/class/drm/card0-DP-1/edid | awk '{print $1;}')
hash_dp2_edid=$(md5sum -b /sys/class/drm/card0-DP-2/edid | awk '{print $1;}')
hash_edp1_edid=$(md5sum -b /sys/class/drm/card0-eDP-1/edid | awk '{print $1;}')
hash_hdmia1_edid=$(md5sum -b /sys/class/drm/card0-HDMI-A-1/edid | awk '{print $1;}')
hash_edid=$(echo "$hash_dp1_edid $hash_dp2_edid $hash_edp1_edid $hash_hdmia1_edid" | md5sum - | awk '{print $1;}')
if test -f "$hash_edid";
then
if [[ "$(md5sum $hash_edid | awk '{print $1;}')" = "$(unxrandr | md5sum | awk '{print $1;}')" ]];
then :
else
echo "This screen has already been configured in $hash_edid, using that config."
sh $hash_edid
fi
else
echo "The configuration for screen if $hash_edid does not exist, creating it with arandr and saving it."
arandr
unxrandr > $hash_edid
echo "Configuration for $hash_edid has been written."
fi
[Unit]
Description=Automatic HDMI screen configuration
[Service]
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/<USER>/.Xauthority"
WorkingDirectory=/home/<USER>/.hdmi-auto
User=<USER>
ExecStart=/bin/bash /home/<USER>/.hdmi-auto/script.sh
[Install]
WantedBy=graphical.target
@karlyeurl
Copy link
Author

Update: check all the screens available on my card0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment