blackhat1337

ran

Feb 28th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.80 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Copyright © 2021 Chirag Bhatia
  4.  
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  6.  
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8.  
  9. # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10.  
  11. #If no argument is specified, ask for it and exit
  12. if [[ -z "$@" ]];
  13. then
  14.     echo "An argument is needed to run this script";
  15.     exit
  16. else
  17.     arg="$@"
  18.     #Basic check to make sure argument number is valid. If not, display error and exit
  19.     if [[ $(($(echo $arg | grep -o "\s" | wc --chars) / 2 )) -ne 2 ]];
  20.     then
  21.         echo "Invalid Parameters. You need to specify parameters in the format \"width height refreshRate\""
  22.         echo "For example setResolution \"1920 1080 60\""
  23.         exit
  24.     fi
  25.    
  26.     #Save stuff in variables and then use xrandr with those variables
  27.     modename=$(echo $arg | sed 's/\s/_/g')
  28.     display=$(xrandr | grep -Po '.+(?=\sconnected)')
  29.     if [[ "$(xrandr|grep $modename)" = "" ]];
  30.     then
  31.         xrandr --newmode $modename $(gtf $(echo $arg) | grep -oP '(?<="\s\s).+') &&
  32.         xrandr --addmode $display $modename    
  33.     fi
  34.     xrandr --output $display --mode $modename
  35.  
  36.     #If no error occurred, display success message
  37.     if [[ $? -eq 0 ]];
  38.     then
  39.         echo "Display changed successfully to $arg"
  40.     fi
  41. fi
  42.  
  43. <<COMMENT
  44. #Manual steps with explanation ahead by @debloper
  45. # First we need to get the modeline string for xrandr
  46. # Luckily, the tool "gtf" will help you calculate it.
  47. # All you have to do is to pass the resolution & the-
  48. # refresh-rate as the command parameters:
  49. gtf 1920 1080 60
  50.  
  51. # In this case, the horizontal resolution is 1920px the
  52. # vertical resolution is 1080px & refresh-rate is 60Hz.
  53. # IMPORTANT: BE SURE THE MONITOR SUPPORTS THE RESOLUTION
  54.  
  55. # Typically, it outputs a line starting with "Modeline"
  56. # e.g. "1920x1080_60.00"  172.80  1920 2040 2248 2576  1080 1081 1084 1118  -HSync +Vsync
  57. # Copy this entire string (except for the starting "Modeline")
  58.  
  59. # Now, use "xrandr" to make the system recognize a new
  60. # display mode. Pass the copied string as the parameter
  61. # to the --newmode option:
  62. xrandr --newmode "1920x1080_60.00"  172.80  1920 2040 2248 2576  1080 1081 1084 1118  -HSync +Vsync
  63.  
  64. # Well, the string within the quotes is the nick/alias
  65. # of the display mode - you can as well pass something
  66. # as "MyAwesomeHDResolution". But, careful! :-|
  67.  
  68. # Then all you have to do is to add the new mode to the
  69. # display you want to apply, like this:
  70. xrandr --addmode VGA1 "1920x1080_60.00"
  71.  
  72. # VGA1 is the display name, it might differ for you.
  73. # Run "xrandr" without any parameters to be sure.
  74. # The last parameter is the mode-alias/name which
  75. # you've set in the previous command (--newmode)
  76.  
  77. # It should add the new mode to the display & apply it.
  78. # Usually unlikely, but if it doesn't apply automatically
  79. # then force it with this command:
  80. xrandr --output VGA1 --mode "1920x1080_60.00"
  81.  
  82. # That's it... Enjoy the new awesome high-res display!
  83. COMMENT
Add Comment
Please, Sign In to add comment