3 # Quick-n-dirty script to turn external/internal displays on/off with xrandr
5 # 2013-14,2020,2022 by Matthias Schmidt <xhr giessen.ccc.de>
9 function choose_profile()
13 # Use classic profile mode with -p
14 if [ -n "${PROFILE}" ]; then
16 # Home profile: Internal on, external extended
21 # Work profile: Internal off, both external displays on
27 # Projector profile: Internal on, external VGA extended
32 # -----------------------------------------------------------------------
33 # Add your own profiles here.
34 # -----------------------------------------------------------------------
41 # -----------------------------------------------------------------------
44 pr "Profile $PROFILE not found"
48 # No profiles, use the connected displays instead
50 # Convert string with connected displays to array
51 local DARRAY=( ${CONNDIS} )
52 # Assume that the first display is the internal one
55 for d in "${DARRAY[@]:1}"; do
60 [ $VERBOSE -eq 1 ] && {
61 echo "Internal Display"
64 echo "External Display(s)"
70 # Set options deliberatly if toogle mode is on
71 if [ ${TFLAG} -eq 1 ]; then
92 # Only enable the internal display
93 if [ $IFLAG -eq 1 ]; then
94 build_xrandr_string "--auto" "--off"
95 # Clone screen on all displays
96 elif [ $CFLAG -eq 1 ]; then
97 RES=`xrandr -q | egrep "^ *[0-9]*x[0-9]*" | awk {'print $1'} | sort | uniq -d | head -1`
98 build_xrandr_string "--mode $RES" "--same-as $INTERN"
99 # Turn internal display off
100 elif [ $EFLAG -eq 1 ]; then
101 if [ $RIGHTOF -eq 1 ]; then
102 build_xrandr_string "--off" "--auto" "--right-of"
104 build_xrandr_string "--off" "--auto" "--left-of"
106 # Internal on and extend screen on all displays by default
108 build_xrandr_string "--auto" "--auto" "--right-of"
112 function build_xrandr_string()
118 CMD="xrandr --output $INTERN $DOINTERN "
122 for d in ${EXTERN[*]}; do
123 if [ $CFLAG -eq 1 ]; then
124 CMDEXT="${CMDEXT} --output $d ${DOEXTERN} --mode ${RES}"
125 elif [ ! -z $POSITION ]; then
126 if [ $i -eq 0 ]; then
127 CMDEXT="${CMDEXT} --output $d ${DOEXTERN} ${POSITION} $INTERN"
129 CMDEXT="${CMDEXT} --output $d ${DOEXTERN} ${POSITION} ${EXTERN[i-1]}"
132 CMDEXT="${CMDEXT} --output $d ${DOEXTERN} ${POSITION}"
138 [ $VERBOSE -eq 1 ] && {
139 echo "I'll run the following command:"
144 function run_xrandr()
146 echo "$CMD $XRANDROPTS" | bash
149 function read_config()
151 if [ -f ${DTCONF} ]; then
156 function get_last_option()
158 # Check if dtoggle saved the last state
159 if [ -f ${DTSTATE} ]; then
164 function write_last_option()
166 echo "STATE=${STATE}" > ${DTSTATE}
169 function toggle_option()
171 STATE=$(((${STATE} + 1) % 4))
176 echo "`basename $0` [-ceix] [-hmntv] [-lr] [-p profile]"
178 echo "Display Options:"
179 echo " -c Mirror screen on all displays"
180 echo " -e Enable external display(s) and disable internal"
181 echo " -i Enable only the internal display"
182 echo -n " -x Extend screen to all displays "
185 echo "General Options:"
186 echo " -h Show this help"
187 echo " -m Show all available modes"
188 echo " -n Dry run. Do not run xrand. Implies -v"
189 echo " -t Toggle different options"
190 echo " -v Be more verbose"
193 echo " -l Display n is left of display (n+1)"
194 echo -n " -r Display n is right of display (n+1) "
198 echo " -p profile Enable the specified profile"
209 echo -e "${GREEN}${1}${NC}"
214 echo -e "${RED}${1}${NC}"
226 # Clone screen on all displays
228 # Enable only external displays
230 # Enable only the internal display
232 # Toggle different modes
236 # Display n is right of display (n+1) [default]
238 # Additional options for xrandr
241 DTSTATE=$HOME/.dtoggle
243 DTCONF=$HOME/.dtoggle.conf
246 # Connected displays. Stolen from ArchLinux wiki
247 CONNDIS=$(xrandr | grep " connected" | awk '{ printf("%s ", $1) }')
249 while getopts "ciehmnrlp:vxt" opt; do
267 XRANDROPTS="--dryrun"
277 pg "Connected Displays"
285 # Do nothing here since its the default
288 [ ! -z "$OPTARG" ] && PROFILE=$OPTARG
296 pr "Option not found"
302 if [ $((CFLAG + $IFLAG + $EFLAG)) -gt 1 ]; then
304 pr "Please specify either -c or -e or -i"
310 if [ ${TFLAG} -eq 1 ]; then
316 choose_profile $PROFILE