sinewalker
7/14/2010 - 1:11 PM

Set X11 screen mode using xrandr

Set X11 screen mode using xrandr

#!/bin/bash
###############################################################################
#   File:       MODE
#   Language:   Bash
#   Time-stamp: <2010-07-14 21:52:07 mjl>
#   Platform:   X Window System with XRANDR
#   OS:         Linux
#   Authors:    Michael Lockhart [MJL]
#
#   Rights:     Copyright © 2010 Michael James Lockhart, B.App.Comp(HONS)
#
#     This program is free software: you can redistribute it and/or
#     modify it under the terms of the GNU General Public License as
#     published by the Free Software Foundation, either version 3 of
#     the License, or (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public
#     License along with this program.  If not, see
#     <http://www.gnu.org/licenses/>.
#
#   PURPOSE:  
#
#    Uses xrandr utility from X.org to set the screen mode. This is
#    based loosely off my old Amstrad CPC, which had a "MODE" command
#    for doing the same.
#
#    On the Amstrad, "MODE 0" selected a low-res, high colour mode
#    (160x200, 4-bit), "MODE 1" was a medium res (320x200, 2-bit) and
#    "MODE 2" was the high res, monochrome mode (640x200, 1-bit)
#
#    I have selected modes from the list that my PC's Radeon
#    processor and LCD will support. Alternatively you can enter an
#    xrandr mode name directly.
#
#  HISTORY
#
# MJL20100714 - Created
#
##

#  The script sets the maximum refresh rate that the LCD supports, and
#  sets the virtual screen panning at the LCD's native resolution. To
#  change this, (and the OUTPUT name) just change the variables below
#

OUTPUT=DVI-0
RATE=75.0
MAX_RES=1280x1024

LOW_RES=640x480
MED_RES=800x600
HI_RES=$MAX_RES

shopt -sq nocasematch
case $1 in
    0 | lo | low )
        MODE=$LOW_RES
        ;;
    1 | me | med | medium )
        MODE=$MED_RES
        ;;
    2 | hi | high )
        MODE=$HI_RES
        ;;
    *)
        if [[ -z $1 ]]; then
            echo "X Screen mode setter for screen $OUTPUT"
            echo
            echo "Usage: $0 <MODE>"
            echo " where <MODE> can be:"
            echo "  0 | low:  selects a low-res mode ($LOW_RES)"
            echo "  1 | med:  selects a med-res mode ($MED_RES)"
            echo "  2 | hi:   selects hi-res mode ($HI_RES)"
            echo " (case does not matter)"
            echo
            echo " or, enter a mode name from xrandr's list:"
            echo
            xrandr
            exit 1
        else
            MODE=$1
        fi
        ;;
esac
shopt -uq nocasematch

xrandr --output $OUTPUT --panning $MAX_RES --mode $MODE --rate $RATE