set-resolution.sh
· 2.4 KiB · Bash
Brut
#!/bin/bash
#########################################################################
# #
# Set display resolution #
# #
# Author: Anthony Axenov (Антон Аксенов) #
# Version: 1.0 #
# License: WTFPL #
# #
#########################################################################
# #
# Using this script you can change your display resolution #
# to any one you need. Just adjust some vars below and run script #
# (chmod +x needed). #
# #
#########################################################################
# Set display name to work with. You can get it via 'xrandr --listactivemonitors'
display="HDMI-2"
# Set width of this display in px
width=1600
# Set height of this display in px
height=900
# Sometimes cvt and gtf generates different modelines.
# You can play around and look which of them gives best result:
modeline=$(cvt ${width} ${height} | grep "Modeline")
# modeline=$(gtf ${width} ${height} 60 | grep "Modeline")
# Some important data needed to xrandr:
modename="${width}x${height}_my"
params=$(echo "$modeline" | sed "s|^\s*Modeline\s*\"[0-9x_.]*\"\s*||")
echo "Set resolution ${width}x${height} on display $display:"
echo "$modename $params"
# Simple logic:
# 1. Switch display to safe mode which always exists (I believe) to avoid errors
xrandr --output $display --mode 640x480
# 2. If display aready have our mode -- we must delete it to avoid errors
if $(xrandr | grep -q "$modename"); then
# 2.1. Detach mode from display
xrandr --delmode $display $modename
# 2.2. Remove mode itself
xrandr --rmmode $modename
fi
# 3. Create new mode with freshly generated parameters
xrandr --newmode $modename $params
# 4. Attach mode to our display
xrandr --addmode $display $modename
# 5. Switch display to this mode immidiately
xrandr --output $display --mode $modename
| 1 | #!/bin/bash |
| 2 | ######################################################################### |
| 3 | # # |
| 4 | # Set display resolution # |
| 5 | # # |
| 6 | # Author: Anthony Axenov (Антон Аксенов) # |
| 7 | # Version: 1.0 # |
| 8 | # License: WTFPL # |
| 9 | # # |
| 10 | ######################################################################### |
| 11 | # # |
| 12 | # Using this script you can change your display resolution # |
| 13 | # to any one you need. Just adjust some vars below and run script # |
| 14 | # (chmod +x needed). # |
| 15 | # # |
| 16 | ######################################################################### |
| 17 | |
| 18 | # Set display name to work with. You can get it via 'xrandr --listactivemonitors' |
| 19 | display="HDMI-2" |
| 20 | # Set width of this display in px |
| 21 | width=1600 |
| 22 | # Set height of this display in px |
| 23 | height=900 |
| 24 | |
| 25 | # Sometimes cvt and gtf generates different modelines. |
| 26 | # You can play around and look which of them gives best result: |
| 27 | modeline=$(cvt ${width} ${height} | grep "Modeline") |
| 28 | # modeline=$(gtf ${width} ${height} 60 | grep "Modeline") |
| 29 | |
| 30 | # Some important data needed to xrandr: |
| 31 | modename="${width}x${height}_my" |
| 32 | params=$(echo "$modeline" | sed "s|^\s*Modeline\s*\"[0-9x_.]*\"\s*||") |
| 33 | |
| 34 | echo "Set resolution ${width}x${height} on display $display:" |
| 35 | echo "$modename $params" |
| 36 | |
| 37 | # Simple logic: |
| 38 | # 1. Switch display to safe mode which always exists (I believe) to avoid errors |
| 39 | xrandr --output $display --mode 640x480 |
| 40 | # 2. If display aready have our mode -- we must delete it to avoid errors |
| 41 | if $(xrandr | grep -q "$modename"); then |
| 42 | # 2.1. Detach mode from display |
| 43 | xrandr --delmode $display $modename |
| 44 | # 2.2. Remove mode itself |
| 45 | xrandr --rmmode $modename |
| 46 | fi |
| 47 | # 3. Create new mode with freshly generated parameters |
| 48 | xrandr --newmode $modename $params |
| 49 | # 4. Attach mode to our display |
| 50 | xrandr --addmode $display $modename |
| 51 | # 5. Switch display to this mode immidiately |
| 52 | xrandr --output $display --mode $modename |