Skip to content

Instantly share code, notes, and snippets.

@dingram
Created April 27, 2020 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingram/af01bb1d76df609cb2200b14b45733b1 to your computer and use it in GitHub Desktop.
Save dingram/af01bb1d76df609cb2200b14b45733b1 to your computer and use it in GitHub Desktop.
#!/bin/zsh
SCREEN_SERIAL="1WSZMS2"
INPUT_DP1="0f"
INPUT_HDMI1="11"
if [[ ! -e "/dev/i2c-1" ]]; then
echo "Module i2c-dev is not loaded; loading..."
sudo modprobe i2c-dev
if [[ ! -e "/dev/i2c-1" ]]; then
echo "No i2c devices available" >&2
exit 1
fi
fi
if [[ ! -w "/dev/i2c-1" ]]; then
echo "No permission to write to i2c devices; enabling sudo mode."
USE_SUDO=1
fi
DDCUTIL="$(which ddcutil)"
function ddcutil() {
if [[ "$USE_SUDO" == 1 ]]; then
sudo ${DDCUTIL:?} --nodetect --sn "${SCREEN_SERIAL:?}" "$@"
else
${DDCUTIL:?} --nodetect --sn "${SCREEN_SERIAL:?}" "$@"
fi
}
function read_sl() {
sed -re 's/^.*sl=0x([0-9a-fA-F]{2}).*$/\1/'
}
CURRENT_INPUT="$(ddcutil getvcp 60 | read_sl)"
if [[ "$1" == "hdmi" ]]; then
NEW_INPUT="${INPUT_HDMI1}"
elif [[ "$1" == "dp" || "$1" == "displayport" ]]; then
NEW_INPUT="${INPUT_DP1}"
elif [[ "${CURRENT_INPUT}" == "${INPUT_DP1}" ]]; then
NEW_INPUT="${INPUT_HDMI1}"
else
NEW_INPUT="${INPUT_DP1}"
fi
if [[ "${CURRENT_INPUT}" == "${NEW_INPUT}" ]]; then
echo "Nothing to do"
exit
fi
if [[ "${NEW_INPUT}" == "${INPUT_DP1}" ]]; then
echo "Switching to DisplayPort input"
elif [[ "${NEW_INPUT}" == "${INPUT_HDMI1}" ]]; then
echo "Switching to HDMI input"
else
echo "Unknown desired input" >&2
exit 1
fi
ddcutil setvcp 60 "0x${NEW_INPUT:?}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment