vexe

mouseclicks_counter.sh

Apr 2nd, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.72 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # our script takes one arg which could be either (currently): -log, -process, -stop
  4. # -log starts logging, dumping output from xinput to a mouse.log file.
  5. # -stop will stop logging.
  6. # -process will process the log file and get the actual number of clicks.
  7. # This attempt isn't the very best, as you could just make another script, which will do the processing of whatever
  8. # this dumps to the log file, but since we're only using one script, you need to kill it first then process the log.
  9. # the 2nd and 3rd steps might be redundant, they could be mixed as one, it's up to you.
  10. # you might find some use of that separation later on, if you don't, then just mix them.
  11.  
  12. # here we go, catching the arg
  13. arg="$1";
  14.  
  15. # we're gonna put the log file in a .log dir in $HOME
  16. test -d $HOME/.log
  17. if [ $? -ne 0 ]; then
  18.     mkdir $HOME/.log
  19. fi
  20.  
  21. # getting in our log dir so that we don't have to re-type the whole path over and over again.
  22. cd $HOME/.log
  23.  
  24. # use your own mouse -_-
  25. mouse="SynPS/2 Synaptics TouchPad"
  26.  
  27. # using case branching :)
  28. case $arg in
  29.  
  30. # don't forget the '&' to run it in the background (daemonize it) I'm not gonna do it here,
  31. # you do it when you run the script.
  32. # I didn't include the 2nd mouse button (middle click), because I have it disabled :) (annoying 2nd Linux clipboard)
  33. # we'll use nohup as well, so that we can safely close the terminal we ran the script from safely,
  34. # not worrying about the process
  35. # dying, cuz, postfixing the thing with '&' isn't enough, it's true if you 'close' the terminal it will
  36. # remain there runnnig, but not if you KILL the terminal (with a stronger signal, which is what my Win+Q keybinding # does in awesome window manager)
  37. # PLEASE note that the below pattern of egrep is so sensetive, you might wanna check what xinput test
  38. # mouse_name gives you first
  39. # before you attempt this, it could be 'mouse press' or 'mouse button' instead
  40. # of 'button press' or one space instead of 3, etc.
  41. # Since we're depeding on the mouse' name, it's limitation is that it's mouse-dependent, meaning that if you use
  42. # another mouse, it won't work unless you give it the right name.
  43. # Note that I can't just grep things out from xinput list output, cuz that output differs from one computer to
  44. # another, so no way of trying to be a smarty pants there ~_~
  45.  
  46. "-log" )    nohup unbuffer xinput test "$mouse" \
  47.                 | unbuffer -p egrep "button press   1|button press   3" \
  48.             > mouseclicks.log &
  49.             ;;
  50. "-stop" )       pkill xinput
  51.         ;;
  52. "-process" )    n_clicks=$(wc -l mouseclicks.log | cut -d\  -f1)
  53.         # let's make it pretty, some time/date formatting... :)
  54.         report="$n_clicks mouse clicks in total @ $(date)"
  55.         echo "$report" >> mouseclicks.report
  56. esac
  57.  
  58. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment