Skip to content

Instantly share code, notes, and snippets.

@jjrh
Created May 22, 2025 15:50
Show Gist options
  • Save jjrh/15f0dd86d118a022ad775d700b3707e9 to your computer and use it in GitHub Desktop.
Save jjrh/15f0dd86d118a022ad775d700b3707e9 to your computer and use it in GitHub Desktop.
script to switch input on a dell monitor using ddcutil
#!/bin/bash
#################################################
#
# ddcutil detect
# ddcutil --display=2 capabilities
# ddcutil --display=2 getvcp 60
# ddcutil --display=2 setvcp 60 0x0f
#
#################################################
VCP=60
HDMI_1=0x11
HDMI_2=0x12
DP_1=0x0f
#################################################
TARGET_DIS=1
TARGET_INPUT=""
printhelp() {
echo "USAGE --------------------------"
echo " --help|-h|-help|help"
echo " print help"
echo ""
echo " --cur "
echo " show current display"
echo ""
echo " --dis=* "
echo " select display - default 1"
echo ""
echo " HDMI1 | hdmi1 | HDMI_1 | hdmi_1 | HDMI2 | hdmi2 | HDMI_2 | hdmi_1 | DP | dp | DP1 | dp1 | dp_1 | DP_1"
echo " input to change to"
echo "----------------------------------------------------------"
}
while [ "$1" != "" ]; do
case $1 in
--help|-h|-help|help)
printhelp
exit 1
;;
--cur)
GET_CURRENT_INFO=1
;;
--dis=*)
TARGET_DIS=$(echo "$1" | sed 's/--dis=//g')
case $TARGET_DIS in
1)
PLACE_HOLDER=0
;;
2)
PLACE_HOLDER=0
;;
*)
echo "DISPLAY must be 1 or 2"
exit 1
esac
;;
HDMI1|hdmi1|HDMI_1|hdmi_1|HDMI2|hdmi2|HDMI_2|hdmi_1|DP|dp|DP1|dp1|dp_1|DP_1)
D_VAL="${1^^}"
case $D_VAL in
HDMI1|HDMI_1)
TARGET_INPUT=$HDMI_1
;;
HDMI2|HDMI_2)
TARGET_INPUT=$HDMI_2
;;
DP|DP_1|DP1)
TARGET_INPUT=$DP_1
;;
*)
echo "ERROR $D_VAL is not one of HDMI1 HDMI2 or DP"
exit 1
esac
;;
*)
printf "unknown argument! '%s'\n" $1
printf " use --help / -h for usage examples\n"
exit 1
;;
esac
shift
done
if [ ! -z $GET_CURRENT_INFO ]; then
echo "ddcutil --display=$TARGET_DIS getvcp $VCP"
out=$(ddcutil --display=$TARGET_DIS getvcp $VCP)
INP=$(echo "$out" | sed -n "s/.*(sl=\(.*\))/\1/p")
echo "$out"
case $INP in
$HDMI_1)
echo "HDMI 1"
;;
$HDMI_2)
echo "HDMI 2"
;;
$DP_1)
echo "DP 1"
;;
*)
echo "unknown $INP"
;;
esac
exit 1
fi
if [ -z $TARGET_INPUT ]; then
echo "Need target input - one of HDMI1 HDMI2 DP"
exit 1
fi
# echo "TARGET_DIS: $TARGET_DIS"
# echo "TARGET_INPUT: $TARGET_INPUT"
##CUR_INPUT=$(ddcutil --display=$TARGET_DIS getvcp $VCP | awk -F'sl=' '{print $2}' | sed 's/)//g')
CUR_INPUT=$(ddcutil --display=$TARGET_DIS getvcp $VCP | sed -n "s/.*(sl=\(.*\))/\1/p")
if [ "$CUR_INPUT" != "$TARGET_INPUT" ]; then
echo "RUNNING: ddcutil --display=$TARGET_DIS setvcp $VCP $TARGET_INPUT"
ddcutil --display=$TARGET_DIS setvcp $VCP $TARGET_INPUT
exit 1
else
echo "Same input as display doing nothing"
exit 1
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment