#!/bin/sh # stateful script to manage dual monitor setups read_cfg() { if [ -f ~/.config/xrandr ]; then . ~/.config/xrandr fi if [ -z "${PRIMARY_MONITOR}" ]; then PRIMARY_MONITOR=$(get_default_monitor) fi if [ -z "${MONITOR_RELATION}" ]; then MONITOR_RELATION="solo" fi } show_cfg() { printf "PRIMARY_MONITOR=${PRIMARY_MONITOR}\n" printf "SECONDARY_MONITOR=${SECONDARY_MONITOR}\n" printf "MONITOR_RELATION=${MONITOR_RELATION}\n" } get_default_monitor() { xrandr --listmonitors | grep -v ^Monitors | head -n 1 | awk '{print $NF}' } apply_monitor_cfg() { xrandr --output ${PRIMARY_MONITOR} --auto --primary if [ ! -z "${SECONDARY_MONITOR}" ] && [ "${SECONDARY_MONITOR}" != "${PRIMARY_MONITOR}" ]; then case ${MONITOR_RELATION} in solo) xrandr --output ${SECONDARY_MONITOR} --off ;; left-of|right-of|above|below) xrandr --output ${SECONDARY_MONITOR} --auto --${MONITOR_RELATION} ${PRIMARY_MONITOR} ;; esac else echo "SECONDARY_MONITOR is not set, or same as PRIMARY_MONITOR:" show_cfg fi } helpme() { printf "$0 [list | setup | primary [NAME] | secondary [NAME] | relation [NAME] | reconfigure]\n" printf " commands:\n" printf " list: show monitor info\n" printf " setup: show current setup\n" printf " primary: set primary monitorp\n" printf " secondary: set secondary monitor\n" printf " relation: set monitor relation\n" printf " reconfigure: set monitor config based on the relation\n" printf " relations:\n" printf " solo: disable secondary monitor\n" printf " left-of,right-of,above,below: secondary monitor has named relation to primary\n" } read_cfg case "$1" in list) xrandr --listmonitors ;; setup) show_cfg ;; primary) PRIMARY_MONITOR=${2} show_cfg > ~/.config/xrandr ;; secondary) SECONDARY_MONITOR=${2} show_cfg > ~/.config/xrandr ;; relation) MONITOR_RELATION=${2} show_cfg > ~/.config/xrandr ;; reconfigure) apply_monitor_cfg ;; *) helpme esac