Created
April 27, 2020 11:50
-
-
Save dingram/af01bb1d76df609cb2200b14b45733b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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