2024-09-26 04:15:43 +00:00
|
|
|
#!/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
|
2024-09-26 13:35:39 +00:00
|
|
|
if [ ! -z "${SECONDARY_MONITOR}" ] && [ "${SECONDARY_MONITOR}" != "${PRIMARY_MONITOR}" ]; then
|
2024-09-26 04:15:43 +00:00
|
|
|
case ${MONITOR_RELATION} in
|
|
|
|
solo)
|
|
|
|
xrandr --output ${SECONDARY_MONITOR} --off ;;
|
2024-09-26 13:35:39 +00:00
|
|
|
left-of|right-of|above|below)
|
|
|
|
xrandr --output ${SECONDARY_MONITOR} --auto --${MONITOR_RELATION} ${PRIMARY_MONITOR} ;;
|
2024-09-26 04:15:43 +00:00
|
|
|
esac
|
2024-09-26 13:35:39 +00:00
|
|
|
else
|
|
|
|
echo "SECONDARY_MONITOR is not set, or same as PRIMARY_MONITOR:"
|
|
|
|
show_cfg
|
2024-09-26 04:15:43 +00:00
|
|
|
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"
|
2024-09-26 13:35:39 +00:00
|
|
|
printf " solo: disable secondary monitor\n"
|
|
|
|
printf " left-of,right-of,above,below: secondary monitor has named relation to primary\n"
|
2024-09-26 04:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|