Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #!/bin/bash
  2. # -------------------------------------------------------
  3. # A shell script that fixes screen tearing on intel video cards and switches between nvidia and intel graphic cards
  4. # Written by: Bruno Assis
  5. # Created on: 25/03/2017
  6. # -------------------------------------------------------
  7.  
  8. #asks for sudo if the user uid != 0 then calls the script again with the current user name
  9. (( EUID != 0 )) && exec sudo -- "$0" "$@$USER"
  10.  
  11. #-----------------
  12. #----Settings-----
  13. #-----------------
  14.  
  15. PREVIOUS_USER=$1
  16. SCRIPT_NAME="20-intel.conf"
  17. CONF_BASE_PATH="/etc/X11/"
  18. CONF_BASE_DIR="xorg.conf.d"
  19. CONF_PATH="/etc/X11/$CONF_BASE_DIR/"
  20. CONF_FILE_PATH="$CONF_PATH$SCRIPT_NAME"
  21. CURRENT_VIDEO_CARD=""
  22.  
  23. #-----------------
  24. #----Functions----
  25. #-----------------
  26.  
  27. #creates the config file.
  28. function CreateTearingFreeFile {
  29. echo 'Section "Device"' >> $1
  30. echo ' Identifier "Intel Graphics"' >> $1
  31. echo ' Driver "intel"' >> $1
  32. echo ' Option "TearFree" "true"' >> $1
  33. echo "EndSection" >> $1
  34. }
  35.  
  36. #switches the environment to use nvidia graphics card
  37. function SwitchToNvdia {
  38. echo -e "n Switching to nvidia."
  39. rm $CONF_FILE_PATH
  40. prime-select nvidia
  41. }
  42.  
  43. #switches the environment to use intel's graphics card
  44. function SwitchToIntel {
  45. echo -e "n Switching to Intel."
  46. cd $CONF_PATH
  47. CreateTearingFreeFile $SCRIPT_NAME
  48. prime-select intel
  49. }
  50.  
  51. function LogoutFromCurrentSession {
  52. echo -e "n You'll need to logout in order to changes take effect."
  53. echo -e "n Do you wish to logout now? (y | n)"
  54. read -n 1 option
  55. if [[ "$option" = "y" || "$option" = "Y" ]]; then
  56. #log out from the current session for the current user
  57. su -c "kill -9 -1" "$PREVIOUS_USER"
  58. fi
  59. }
  60.  
  61. function SetupEnvironment {
  62.  
  63. if [ ! -d "$CONF_BASE_PATH" ]; then
  64. echo -e "n error: could not find $CONF_BASE_PATH."
  65. exit 1
  66. else
  67. if [ ! -d "$CONF_PATH" ]; then
  68. echo -e "n could not find $CONF_BASE_DIR."
  69. echo -e "n trying to create it."
  70. cd $CONF_BASE_PATH
  71. mkdir $CONF_BASE_DIR
  72. if [ ! -d "$CONF_PATH" ]; then
  73. echo -e "n could not create $CONF_BASE_DIR folder."
  74. exit 1
  75. else
  76. echo -e "n $CONF_BASE_DIR folder created successfully."
  77. fi
  78. fi
  79. fi
  80. }
  81.  
  82. function CheckForCurrentVideoCardInUse {
  83. local _VIDEO_CARD=`glxinfo|egrep "OpenGL vendor|OpenGL renderer*"`
  84. if [[ $_VIDEO_CARD == *"NVIDIA"* && $_VIDEO_CARD == *"GeForce"* ]]; then
  85. CURRENT_VIDEO_CARD="NVIDIA"
  86. elif [[ $_VIDEO_CARD == *"Intel"* && $_VIDEO_CARD == *"Intel"* ]]; then
  87. CURRENT_VIDEO_CARD="INTEL"
  88. else
  89. ErrorHandler
  90. fi
  91. }
  92.  
  93. function ErrorHandler {
  94. echo -e "n Could not find your video card"
  95. echo -e "n Please use glxinfo to check if the name is correct or if you are using NVIDIA's or Intel's card"
  96. exit 1
  97. }
  98.  
  99. #------------------
  100. #--Program Flow ---
  101. #------------------
  102.  
  103. #Setups the environment if it hasn't already been set.
  104.  
  105. CheckForCurrentVideoCardInUse
  106. SetupEnvironment
  107.  
  108. echo -e " current video card in use: $CURRENT_VIDEO_CARD"
  109.  
  110. #If the file exists then we should delete it and start nvidia
  111.  
  112. if [ $CURRENT_VIDEO_CARD == "INTEL" ]; then
  113. echo -e "n Switch graphics card to NVIDIA? (y | n)"
  114. read -n 1 option
  115. if [[ "$option" == "y" || "$option" == "Y" ]]; then
  116. SwitchToNvdia
  117. LogoutFromCurrentSession
  118. fi
  119. elif [ $CURRENT_VIDEO_CARD == "NVIDIA" ]; then
  120. echo -e "n Switch graphics card to INTEL? (y | n)"
  121. read -n 1 option
  122. if [[ "$option" == "y" || "$option" == "Y" ]]; then
  123. SwitchToIntel
  124. LogoutFromCurrentSession
  125. fi
  126. else
  127. ErrorHandler
  128. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement