shtools/zclick/zclick
2020-10-26 11:25:36 +01:00

103 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
fname=$(basename "$0")
usage()
{
echo "$fname [options] [clickID]
Automatic clicker. Click ID is: 1=left , 2=middle , 3=right
To run the clicker, you need to start the daemon and then run the commands
[Daemon Options]
-d Run daemon
-b <ms> Time per block of operation in ms. Default 100
[Client options]
-h Display this help
-i <ms> Interval between clicks in ms, 0 for no clicking. Default 20
-t Toggle clicking. Sets interval to 0 if current value was != 0"
}
error () {
printf "\033[1;31m%s\033[0m\n" "$1" >&2
}
arg_i=20
block_time=100
[ -z "$FOLDER" ] && {
if [ -n "$XDG_DATA_HOME" ]
then
FOLDER="$XDG_DATA_HOME/zclick"
else
FOLDER="$HOME/.local/share/zclick"
fi
}
while getopts ":hi:b:dt" opt;
do
case $opt in
h) usage && exit 0 ;;
i) [ "$OPTARG" -ge 0 ] 2>/dev/null || { error "i argument has to be positive" ; exit 2; }
arg_i=$OPTARG ;;
b) [ "$OPTARG" -ge 1 ] 2>/dev/null || { error "b argument has to be strictly positive" ; exit 2; }
block_time=$OPTARG ;;
d) opt_d=y ;;
t) opt_t=y ;;
\?) echo "Unknown option: $OPTARG" >&2 ; usage ; exit 1 ;;
esac
done
shift $((OPTIND-1))
mkdir -p "$FOLDER"
if [ -n "$opt_d" ]
then
## DAEMON
# reset status of all clicks
which bc >/dev/null || { echo "bc not installed" >&2 && exit 1; }
which xdotool >/dev/null || { echo "xdotool not installed" >&2 && exit 1; }
stime=$(echo "scale=2;$block_time/1000.0" | bc)
for I in "$FOLDER"/*
do
echo "0" > "$I" 2>/dev/null
done
# DAEMON
while true
do
sleep $stime &
for I in "$FOLDER"/*
do
interval=$(cat "$I")
click_id=$(echo "$I" | rev | cut -d'/' -f1 | rev)
if [ "$interval" != "0" ]
then
N=$(($block_time / $interval))
[ "$N" -lt 1 ] && N=1
xdotool click --repeat $N --delay $interval $click_id &
fi
done
wait $(jobs -p)
done
else
## CONTROL
[ $# -le 0 ] && usage && exit 1
[ -n "$1" ] && ! [ "$1" -gt 0 ] 2>/dev/null && { error "Click ID has to be a strictly positive integer" ; exit 2; }
cid=$1
if [ -n "$opt_t" ]
then
#toggle
if [ "$(cat "$FOLDER/$cid")" != "0" ] ; then
echo 0 > "$FOLDER/$cid"
else
echo "$arg_i" > "$FOLDER/$cid"
fi
else
#set
echo "$arg_i" > "$FOLDER/$cid"
fi
fi