1. #!/bin/bash
  2.  
  3. # ------------------------------------------------------------------------------
  4. #
  5. # This Script writes the (hopefully) correct section in your xorg.conf
  6. # You must run it as root.
  7. #
  8. # All buttons work as expected!
  9. # This means: thumbbuttons to navigate in browsers and the two wheels scroll horizontal/vertical
  10. # ans all the others too. Except of the Mode-Button of course ...
  11. #
  12. # Created by yokmp on Friday, 13th January 2012
  13. # Yes rly, so use with CARE!
  14. #
  15. # this is version 0.2
  16. # ------------------------------------------------------------------------------
  17.  
  18. # lets define some vars 'n' arrays
  19. RED='\e[1;31m'
  20. GREEN='\e[1;32m'
  21. yellow='\e[1;33m'
  22.  
  23. fat='\033[1m'
  24. nf='\033[0m'
  25.  
  26. # files to look for
  27. files_to_search=( "x11-apps/xinput" "x11-apps/xev" )
  28. files_to_install=( "xinput" "xev" )
  29.  
  30.  
  31. function center() { printf "%*s\n" $((( ${#center} + $COLUMNS) / 2 )) "$center" ; }
  32.  
  33. function hr()
  34. {
  35.   echo -en "${yellow}"
  36.   for (( l = 1 ; l <= $COLUMNS ; l++ )); do
  37.     echo -n "="
  38.   done
  39.   echo -e "${nf}"
  40. }
  41.  
  42. function shr()
  43. {
  44.   echo -en "${yellow}"
  45.   for (( l = 1 ; l <= $COLUMNS ; l++ )); do
  46.     echo -n "~"
  47.   done
  48.   echo -e "${nf}"
  49. }
  50.  
  51. # ------------------------------------------------------------------------------
  52. # lets check for xinput and xev and yes i love loops
  53. function chk_stuff()
  54. {
  55.   echo -e "\nSniffin your Packets ..."
  56.   shr
  57.  
  58.   for (( packet = 0 ; packet <= $(((${#files_to_search[*]} - 1 ))) ; packet++ )); do
  59.     equo query installed ${files_to_install[$packet]}|grep "${files_to_search[$packet]}" 1> /dev/null
  60.     if [ $? = 0 ]; then
  61.       echo -e "${fat}${files_to_search[$packet]}\t[ ${GREEN}OK${nf} ]"
  62.     else
  63.       echo -e "${fat}${files_to_search[$packet]}\t[${RED}FAIL${nf}]"
  64.       equo install ${files_to_install[$packet]} --ask
  65.     fi
  66.   done
  67.  
  68. }
  69.  
  70. function sed_FTW()  # we dont need sed )=
  71. {                   # and we will not check if the user rly has a RAT plugged in ...
  72.   mouse_name=$(xinput list|grep -o "R.A.T.*Mouse\|R.A.T.*Albino\|R.A.T.*Contagion")
  73.   mouse_model=$(xinput list|grep -o "R.A.T.*Mouse\|R.A.T.*Albino\|R.A.T.*Contagion"|head -c7|tail -c1)
  74.  
  75.   [ -e /etc/X11/xorg.conf ] || echo "" > /etc/X11/xorg.conf
  76.   mkdir -p /home/$username/Desktop # just to be shure ...
  77.   cp /etc/X11/xorg.conf /etc/X11/xorg.conf.bakup
  78.   cp /etc/X11/xorg.conf /home/$username/Desktop
  79.   chown $username /home/$username/Desktop/xorg.conf
  80.  
  81.   button_mapping() {
  82.  
  83.     echo "
  84. Section \"InputClass\"
  85.    Identifier \"Mouse Remap\"
  86.    MatchProduct \"Saitek Cyborg $mouse_name\"
  87.    MatchDevicePath \"/dev/input/event*\"
  88. " >> /home/$username/Desktop/xorg.conf
  89.  
  90.   case $mouse_model in
  91.     3)
  92.       echo '    Option "ButtonMapping" "1 2 3 4 5 6 7 8 9 0 0 0 0 0 0"
  93. EndSection' >> /home/$username/Desktop/xorg.conf
  94.     ;;
  95.     5)
  96.       echo '  Option "Buttons" "17"
  97.  Option "ButtonMapping" "1 2 3 4 5 0 0 8 9 7 6 12 0 0 0 16 17"
  98.  Option "AutoReleaseButtons" "13 14 15"
  99.  Option "ZAxisMapping" "4 5 6 7"
  100.  Option "YAxisMapping" "10 11"
  101. EndSection' >> /home/$username/Desktop/xorg.conf
  102.     ;;
  103.     7)
  104.       echo '  Option "Buttons" "17"
  105.  Option "ButtonMapping" "1 2 3 4 5 0 0 8 9 7 6 12 0 0 0 16 17"
  106.  Option "AutoReleaseButtons" "13 14 15"
  107.  Option "ZAxisMapping" "4 5 6 7"
  108.  Option "YAxisMapping" "10 11"
  109. EndSection' >> /home/$username/Desktop/xorg.conf
  110.     ;;
  111.     9)
  112.       echo '  Option "Buttons" "17"
  113.  Option "ButtonMapping" "1 2 3 4 5 0 0 8 9 7 6 12 0 0 0 16 17"
  114.  Option "AutoReleaseButtons" "13 14 15"
  115.  Option "ZAxisMapping" "4 5 6 7"
  116.  Option "YAxisMapping" "10 11"
  117. EndSection' >> /home/$username/Desktop/xorg.conf
  118.     ;;
  119.   esac
  120.   }
  121.  
  122.  
  123.   echo -ne "\nIs ${fat}$mouse_name${nf} your mouse? (y/n) "
  124.   read -n1 ; echo
  125.  
  126.   if ( [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ] ); then
  127.     echo "Now we will write some stuff in your xorg.conf!"
  128.     button_mapping
  129.   else
  130.     echo -e "Note: In some cases you must use 7 instead of 7 Albion or 9 etc...\nPlay arond and try yourself ;)"
  131.     read -p "Please enter you model (3, 5, 7, 7 Contagion, 7 Albino, 9): R.A.T." mouse_user
  132.     mouse_name="R.A.T.$mouse_user" ; button_mapping
  133.   fi
  134.  
  135. }
  136.  
  137.  
  138. # ------------------------------------------------------------------------------
  139. clear
  140. echo -e "${fat}" ; center="This Script will modify your xorg.conf!" ; center ; echo -e "${nf}"
  141.  
  142. if [ "$(id -u)" != "0" ]; then
  143.   echo -e "${fat}" ; center="... But you must be root ..." ; center ; echo -e "${nf}"
  144.   return 1
  145. else
  146.   read -p "Please enter your username (not root -.-): "
  147.   username=$REPLY
  148. fi
  149.  
  150. hr
  151. echo -e "\nThe Script needs ${fat}xinput${nf} and optionally ${fat}xev${nf}"
  152. echo "and must also have you ${fat}R.A.T. Mouse plugged in${nf} and of course ${fat}running Sabayon${nf}!"
  153. echo -e "\nYou can let the Script handle the installation of both (you will be asked for this)"
  154. echo -e "\n\tAnd just in case:\n\tIm not responsible for any wired things that may can happen!"
  155. echo -e "\tBut you can leave me a messeage at the discussion page:"
  156. echo -e "\thttp://wiki.sabayon.org/index.php?title=User:Yokmp"
  157. hr
  158. echo -e "\nFirst let me refresh the Repository ..."
  159. shr
  160. equo update
  161. echo -e "\nDone here. Starting search now."
  162. hr
  163. chk_stuff
  164. shr
  165. echo -e "Now lets do something useful.\n${fat}If you aborted the installation of xinput,"
  166. echo -e "then you should press [CTRL]+[C] now and rerun the script!${nf}"
  167. echo "Else press enter"
  168. read
  169. hr
  170. sed_FTW
  171. hr
  172. echo -e "\nDone so far.\nWeird things can happen in scripts so you should check your new xorg.conf!"
  173. shr
  174. cat /home/$username/Desktop/xorg.conf
  175. shr
  176. read -p "Replace your xorg.conf now? (y/n) "
  177. case $REPLY in
  178.   y|Y|yes|Yes|YES)
  179.     cp -vi /home/$username/Desktop/xorg.conf /etc/X11/ # Very Interactive
  180.   ;;
  181.   *) echo "You must copy the new file yourself!" ;;
  182. esac
  183. hr
  184. echo -e "${fat}"
  185. center="FINISH! Easy thing right?"
  186. center
  187. center="Don't forget to restart Xorg or nothing will happen!"
  188. center
  189. echo -e "${nf}"
  190. hr