Skip to content

Instantly share code, notes, and snippets.

@geekpete
Forked from gservat/display-switcher.sh
Created April 14, 2019 22:30
Show Gist options
  • Save geekpete/02b930eb8f6cd15d508ac61089b17d01 to your computer and use it in GitHub Desktop.
Save geekpete/02b930eb8f6cd15d508ac61089b17d01 to your computer and use it in GitHub Desktop.
Display profile switcher using nvidia-settings
#!/bin/bash
exec > >(tee -a "/tmp/display-switcher.log") 2>&1
# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
# separately; see below.
usage() {
cat << HELP >&2
Usage: $0 [option ...] {profile}
OPTIONS:
-h, --help This help message
-v, --verbose Enable verbose mode
-w N, --wait N Seconds to wait before changing profile
-m TEXT, --message TEXT Text to display as a notification
-t SECS, --message-timeout SECS How long (seconds) to display the notification for
-u NAME, --user NAME User to use for X11 notifications
HELP
}
msg() {
if $VERBOSE; then
echo "[$(date)] $1"
fi
}
if ! TEMP=$(getopt -o hvt:u:m:w: --long verbose,help,user:,message:,message-timeout:,wait: -n 'display-switcher' -- "$@"); then
usage
exit 1
fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
VERBOSE=false
WAIT=0
MESSAGE="Switching display profile to %s soon..."
NOTIFY_USER=
NOTIFY_UID=
NOTIFY_ARGS=
ICON="/usr/share/icons/Pop/48x48/devices/gnome-dev-computer.svg"
while true; do
case "$1" in
-v | --verbose ) VERBOSE=true; shift ;;
-w | --wait ) WAIT="$2"; shift 2 ;;
-m | --message ) MESSAGE="$2"; shift 2 ;;
-t | --message-timeout ) NOTIFY_ARGS="${NOTIFY_ARGS} -t ${2}000"; shift 2 ;;
-u | --user )
NOTIFY_USER="$2"
shift 2
if ! NOTIFY_UID="$(id -u ${NOTIFY_USER} 2>/dev/null)"; then
echo "ERROR: Unable to get UID for ${NOTIFY_USER}."
exit 1
fi
;;
-h | --help ) usage; exit 0 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [[ -z ${NOTIFY_USER} ]]; then
echo "ERROR: You need to specify the username for the authorized X user to use for notifications."
usage
exit 1
fi
NOTIFY_CMD="sudo -u ${NOTIFY_USER} DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${NOTIFY_UID}/bus dunstify -I ${ICON}"
# nvidia-settings -q dpys
BENQ_4K_EDID="DPY-EDID-ead0ac88-15bf-e410-e2a5-8538dcd8450d"
LG_5K_EDID="DPY-EDID-79885171-ff0f-23e3-0566-988d08123861"
NOTEBOOK_EDID="DPY-EDID-703b6c15-6e68-abf3-2744-8a1b0abf4d39"
# nvidia-settings -q CurrentMetaMode | tr '\n' ' '
PROFILE=${1}
case "${PROFILE}" in
laptop-only)
MODE="${NOTEBOOK_EDID}: 3840x2160 @3840x2160 +0+0 {ViewPortIn=3840x2160, ViewPortOut=3840x2160+0+0}"
;;
5k-4k-vertical-laptop)
MODE="${BENQ_4K_EDID}: 3840x2160 @2160x3840 +4096+0 {ViewPortIn=2160x3840, ViewPortOut=3840x2160+0+0, Rotation=90}, ${LG_5K_EDID}: 4096x2304 @4096x2304 +0+0 {ViewPortIn=4096x2304, ViewPortOut=4096x2304+0+0}, ${NOTEBOOK_EDID}: 3840x2160 @3840x2160 +6256+0 {ViewPortIn=3840x2160, ViewPortOut=3840x2160+0+0}"
;;
5k-4k-vertical-no-laptop)
MODE="${BENQ_4K_EDID}: 3840x2160 @2160x3840 +4096+0 {ViewPortIn=2160x3840, ViewPortOut=3840x2160+0+0, Rotation=90}, ${LG_5K_EDID}: 4096x2304 @4096x2304 +0+0 {ViewPortIn=4096x2304, ViewPortOut=4096x2304+0+0}"
;;
*)
usage
echo -e "\\nERROR: You need to specify a valid profile to switch to."
exit 1
;;
esac
msg Starting...
msg "Notify command: ${NOTIFY_CMD}"
FINAL_MSG=$(printf "${MESSAGE}" ${PROFILE})
msg "Sending notification: ${NOTIFY_CMD} ${NOTIFY_ARGS} \"${FINAL_MSG}\""
${NOTIFY_CMD} ${NOTIFY_ARGS} "${FINAL_MSG}"
if [[ ${WAIT} -gt 0 ]]; then
msg "Sleeping for ${WAIT} seconds"
sleep "${WAIT}"
fi
# Finally, run nvidia-settings.
msg "Running: nvidia-settings --assign \"CurrentMetaMode=${MODE}\""
nvidia-settings --assign "CurrentMetaMode=${MODE}"
${NOTIFY_CMD} -t 3000 "Display profile has been switched"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment