xrnd 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. # A xrandr helper
  3. # import subprocess as sb
  4. # check_current_screens = sb.check_output('xrandr -q', shell=True, universal_newlines=True)
  5. # if r'HDMI2 connected' in check_current_screens:
  6. # sb.run('xrandr --output HDMI2 --auto --output HDMI2 --auto --above eDP1', shell=True)
  7. # else:
  8. # sb.run('xrandr --output HDMI2 --off', shell=True)
  9. #If no argument is specified, ask for it and exit
  10. if [[ -z "$@" ]];
  11. then
  12. echo "An argument is needed to run this script";
  13. exit
  14. else
  15. arg="$@"
  16. #Basic check to make sure argument number is valid. If not, display error and exit
  17. if [[ $(($(echo $arg | grep -o "\s" | wc --chars) / 2 )) -ne 2 ]];
  18. then
  19. echo "Invalid Parameters. You need to specify parameters in the format \"width height refreshRate\""
  20. echo "For example setResolution \"1920 1080 60\""
  21. exit
  22. fi
  23. #Save stuff in variables and then use xrandr with those variables
  24. modename=$(echo $arg | sed 's/\s/_/g')
  25. display=$(xrandr | grep -Po '.+(?=\sconnected)')
  26. if [[ "$(xrandr|grep $modename)" = "" ]];
  27. then
  28. xrandr --newmode $modename $(gtf $(echo $arg) | grep -oP '(?<="\s\s).+') &&
  29. xrandr --addmode $display $modename
  30. fi
  31. xrandr --output $display --mode $modename
  32. #If no error occurred, display success message
  33. if [[ $? -eq 0 ]];
  34. then
  35. echo "Display changed successfully to $arg"
  36. fi
  37. fi
  38. <<COMMENT
  39. #Manual steps with explanation ahead by @debloper
  40. # First we need to get the modeline string for xrandr
  41. # Luckily, the tool "gtf" will help you calculate it.
  42. # All you have to do is to pass the resolution & the-
  43. # refresh-rate as the command parameters:
  44. gtf 1920 1080 60
  45. # In this case, the horizontal resolution is 1920px the
  46. # vertical resolution is 1080px & refresh-rate is 60Hz.
  47. # IMPORTANT: BE SURE THE MONITOR SUPPORTS THE RESOLUTION
  48. # Typically, it outputs a line starting with "Modeline"
  49. # e.g. "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync
  50. # Copy this entire string (except for the starting "Modeline")
  51. # Now, use "xrandr" to make the system recognize a new
  52. # display mode. Pass the copied string as the parameter
  53. # to the --newmode option:
  54. xrandr --newmode "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync
  55. # Well, the string within the quotes is the nick/alias
  56. # of the display mode - you can as well pass something
  57. # as "MyAwesomeHDResolution". But, careful! :-|
  58. # Then all you have to do is to add the new mode to the
  59. # display you want to apply, like this:
  60. xrandr --addmode VGA1 "1920x1080_60.00"
  61. # VGA1 is the display name, it might differ for you.
  62. # Run "xrandr" without any parameters to be sure.
  63. # The last parameter is the mode-alias/name which
  64. # you've set in the previous command (--newmode)
  65. # It should add the new mode to the display & apply it.
  66. # Usually unlikely, but if it doesn't apply automatically
  67. # then force it with this command:
  68. xrandr --output VGA1 --mode "1920x1080_60.00"
  69. # That's it... Enjoy the new awesome high-res display!
  70. COMMENT