Guest User

autoscroll

a guest
Jan 6th, 2020
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.13 KB | None | 0 0
  1. #!/bin/bash
  2. # Autoscroll (Full)
  3.  
  4. enableverticalscroll=1
  5. enablehorizontalscroll=1
  6. enableclickscroll=0
  7. clickdelay=0.2 #The grace period for you to release the middle mouse button to activate clickscrolling before it assumes you're holding it down (in seconds), it is inadvisable to set this setting much higher than this.
  8. paddingtop=50 #The closest the cursor can go (in pixels) to the top of your screen before being stopped. May need to be optimized based on your screen resolution.
  9. paddingbot=40 #The closest your cursor can go (in pixels) to the bottom of before being stopped. May need to be optimized based on your screen resolution.
  10.  
  11. middlemousebutton=2
  12. mousescrollup=4
  13. mousescrolldown=5
  14. mousescrollleft=6
  15. mousescrollright=7
  16.  
  17. ## Danger below :D
  18.  
  19. filename=`basename "$0"` #Required for autoscroll
  20. echo -n | xsel -n -i
  21. eval $(xdotool getmouselocation --shell)
  22. starty=$Y
  23. startx=$X
  24. toggle=$middlemousebutton
  25. firstloop=1
  26.  
  27. #A horrible hack to find the device ID of your mouse and screen height which are required to create the workaround to prevent accidental tab and toolbar scrolling.
  28. devcount=$(xinput | sed '/keyboard/d' | sed '/Virtual/d'| wc -l)
  29. for i in $(seq 1 $devcount)
  30. do
  31.     mouseid=$(xinput | sed '/keyboard/d' | sed '/Virtual/d'|cut -d '=' -f2 | cut -d '[' -f1 | head -n $i | tail -1)
  32.     buffer=$(xinput --list $mouseid | grep -i -m 1 "Button state:" | grep -o "[$middlemousebutton]\+")
  33.     if [ "$buffer" = "$middlemousebutton" ]
  34.     then
  35.         break
  36.     fi
  37. done
  38. screenheight=$(( $(xrandr | grep '*' | awk '{ print $1 }' | cut -d 'x' -f2) - $paddingbot ))
  39.  
  40. if [ "$(pidof -x $filename | wc -w)" -gt "2" ]
  41. then
  42.     xinput set-prop $mouseid "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
  43.     pkill autoscroll #Disables autoscrolling on second click when clickscrolling is enabled.
  44. fi
  45.  
  46.  
  47. while [ "$toggle" = "$middlemousebutton" ]
  48. do
  49.     eval $(xdotool getmouselocation --shell)
  50.     curry=$Y
  51.     currx=$X
  52.     if [ $curry -lt $paddingtop ] #Prevent accidental browsertab scroll
  53.     then
  54.     xinput set-prop $mouseid "Coordinate Transformation Matrix" 0 0 0 0 0 0 0 0 100000
  55.     xdotool mousemove_relative 0 $paddingtop
  56.     elif [ $curry -gt $screenheight ] #Prevent accidental taskbar scroll
  57.     then
  58.     xinput set-prop $mouseid "Coordinate Transformation Matrix" 0 0 0 0 0 0 0 0 100000
  59.     xdotool mousemove_relative 0 -$paddingbot
  60.     fi
  61.     if [ $enableverticalscroll -eq 1 ]
  62.     then
  63.         if [ $curry -gt $starty ]
  64.         then
  65.             speedy=$(expr $curry / 100 - $starty / 100)
  66.             if [ $speedy -gt 0 ]
  67.             then
  68.                 xdotool click --repeat $speedy --delay 1 $mousescrolldown
  69.             fi
  70.         else
  71.             speedy=$(expr $curry / 100  - $starty / 100  | sed 's:-::')
  72.             if [ $speedy -gt 0 ]
  73.             then
  74.                 xdotool click --repeat $speedy --delay 1 $mousescrollup
  75.             fi
  76.         fi
  77.     fi
  78.  
  79.     if [ $enablehorizontalscroll -eq 1 ]
  80.     then
  81.         if [ $currx -gt $startx ]
  82.         then
  83.             speedx=$(expr $currx / 100 - $startx / 100)
  84.             if [ $speedx -gt 0 ]
  85.             then
  86.                 xdotool click --repeat $speedx --delay 1 $mousescrollright
  87.             fi
  88.         else
  89.             speedx=$(expr $currx / 100  - $startx / 100  | sed 's:-::')
  90.             if [ $speedx -gt 0 ]
  91.             then
  92.                 xdotool click --repeat $speedx --delay 1 $mousescrollleft
  93.             fi
  94.         fi
  95.     fi
  96.     if [ $firstloop = 1 ]
  97.     then
  98.         sleep $clickdelay
  99.         firstloop=0
  100.     fi
  101.     if [ $enableclickscroll -eq 1 ] && [ -z $(xinput --list "Virtual core pointer" | grep -i -m 1 "Button state:" | grep -o "[$middlemousebutton]\+") ]
  102.     then
  103.         if [ $(xinput --query-state $mouseid | grep 'button\[' | sort | grep -c down) -gt 0 ] #Enables mouse buttons other than middle mouse to stop clickscrolls.
  104.         then
  105.             xinput set-prop $mouseid "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
  106.             exit
  107.         fi
  108.     else
  109.         enableclickscroll=0
  110.         toggle=$(xinput --list "Virtual core pointer" | grep -i -m 1 "Button state:" | grep -o "[$middlemousebutton]\+")
  111.     fi
  112.     sleep 0.02
  113. done
  114. xinput set-prop $mouseid "Coordinate Transformation Matrix" 1 0 0 0 1 0 0 0 1
Add Comment
Please, Sign In to add comment