Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

switch-monitors.sh (Source)

#!/bin/sh
# File: switch-monitors.sh
# Location: /usr/local/bin
# Author: bgstack15
# SPDX-License-Identifier: GPL-3.0
# Startdate: 2023-12-05-3 16:15
# Title: Switch monitor config for NVIDIA graphics card
# Purpose: easily switch between 1 monitor and 2 monitors on pcb-009.
# History: attempt 2. The first was attemping to read nvidia-settings -q dpys and parsing enabled/connected, but I didn't know how to use the derived values
# Usage: Called by tray icon
# Reference:
#    https://wiki.archlinux.org/title/NVIDIA#Using_nvidia-settings
# Improve:
# Documentation:
#    Configure `nvidia-settings` as desired. Print metamode value with following command.
#    nvidia-settings -q CurrentMetaMode
#    Use everything after the `::`
test -z "${DESIRED}" && test -n "${1}" && DESIRED="${1}"
desired="$( echo "${DESIRED}" | tr 'A-Z' 'a-z' )"
case "${desired}" in
   "both"|"left+right"|"left,right","double")
      echo "Using both monitors."
      nvidia-settings --assign "CurrentMetaMode=DPY-4: nvidia-auto-select @1920x1080 +1920+0 {ViewPortIn=1920x1080, ViewPortOut=1920x1080+0+0}, DPY-1: nvidia-auto-select @1920x1080 +0+0 {ViewPortIn=1920x1080, ViewPortOut=1920x1080+0+0}"
      ;;
   "one"|"right"|"single")
      nvidia-settings --assign "CurrentMetaMode=DPY-4: nvidia-auto-select @1920x1080 +0+0 {ViewPortIn=1920x1080, ViewPortOut=1920x1080+0+0}"
      ;;
   *) echo "Unknown option: ${desired}. Aborted." ;
      exit 1 ;
      ;;
esac