| 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 |
anthony / Set custom display resolution using xrandr
Last active 2 months ago
Revision 257eaeb0e851b0bac0d57dbe269de47c03a5fdfd