Skip to content

Instantly share code, notes, and snippets.

@nikku
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save nikku/ab722a632181fa5568dd to your computer and use it in GitHub Desktop.

Select an option

Save nikku/ab722a632181fa5568dd to your computer and use it in GitHub Desktop.
Monitor switching script for Optimus (Intel + Nvidia) equiped computers
#!/bin/bash
export DISPLAY=:0.0
export XAUTHORITY=/home/nikku/.Xauthority
INTERN="LVDS1"
EXTERN="VIRTUAL3"
DOCK1="VIRTUAL4"
DOCK2="VIRTUAL5"
PROJECTOR="VGA1"
function p {
echo -e "\e[0;33m$1\e[m"
}
function g {
echo -e "\e[0;36m$1\e[m"
}
function echo_done {
echo -e " \e[0;32m[done]\e[m"
}
function update_displays {
local mode=${1-auto}
local XRANDR=`xrandr -q`
local VGA_MODE=${2-1024x768}
local DOCK_MODE=`xrandr -q | grep "1920x1080 60.00" | head -n 1 | cut -d" " -f4`
local DOCK_MODE="${2-$DOCK_MODE}"
case $mode in
intern)
echo "$(g [xrandr]) using $(p INTERN)"
xrandr --output $INTERN --auto --output $DOCK1 --off --output $DOCK2 --off --output $EXTERN --off --output $PROJECTOR --off
;;
auto)
if [[ $XRANDR =~ "$EXTERN connected" ]]; then
echo "$(g [xrandr]) using $(p EXTERN)"
xrandr --output $INTERN --auto --output $EXTERN --left-of $INTERN --mode $DOCK_MODE
elif [[ $XRANDR =~ "$DOCK1 connected" ]] && [[ $XRANDR =~ "$DOCK2 connected" ]]; then
echo "$(g [xrandr]) using $(p DOCKING) with mode $DOCK_MODE"
xrandr --output $INTERN --auto --output $DOCK1 --mode $DOCK_MODE --right-of $INTERN --output $DOCK2 --mode $DOCK_MODE --right-of $DOCK1
elif [[ $XRANDR =~ "$PROJECTOR connected" ]]; then
echo "$(g [xrandr]) using $(p PROJECTOR) with mode $VGA_MODE"
xrandr --output $INTERN --auto --output $PROJECTOR --mode $VGA_MODE >> /tmp/hotplug.txt 2>&1
else
update_displays intern $2
fi
;;
*)
echo "Unknown mode: $mode" >&2
exit 1
;;
esac
sleep 2
}
# update monitors according to $output $mode args
function monitors_plug {
local output=$1
if [ "$output" = "optimus" ]; then
if [ "$(pgrep intel)" = "" ]; then
echo -n "$(g [optimus]) starting"
intel-virtual-output
echo_done
echo -n "$(g [optimus]) refreshing displays"
sleep 5
echo_done
fi
else
if [ "$(pgrep intel)" != "" ]; then
update_displays intern
echo -n "$(g [optimus]) stopping"
pkill intel
until [ "$(pgrep intel)" = "" ]; do
sleep 1
echo -n " ."
done
echo_done
echo -n "$(g [optimus]) refreshing displays"
sleep 4
echo_done
exit 0
fi
fi
update_displays $2 $3
}
# parse options helper
function parse_opts {
local output=optimus
local resolution
local mode=auto
while getopts ":m:o:" opt $@; do
case $opt in
m)
mode=$OPTARG
;;
r)
resolution=$OPTARG
;;
o)
if [ "$OPTARG" = "optimus" ] || [ "$OPTARG" = "intel" ]; then
output=$OPTARG
else
echo "Invalid option -b $OPTARG; use -b (optimus|intel)" >&2
exit 1
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
monitors_plug $output $mode $resolution
}
# parse options and go
parse_opts $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment