1
0
Fork 0
src/xjiggler/xjiggler

101 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-07-09 09:32:03 +00:00
#!/bin/sh
set -e
# based on https://github.com/carrot69/keep-presence/
2023-07-09 15:30:11 +00:00
test -n "$XJIGGLER_DELAY_ACTION" \
|| XJIGGLER_DELAY_ACTION=300 # seconds
test -n "$XJIGGLER_DELAY_DETECTION" \
|| XJIGGLER_DELAY_DETECTION=60 # seconds
test -n "$XJIGGLER_MOUSE_DIRECTION" \
|| XJIGGER_MOUSE_DIRECTION=CIRCULAR # "CIRCULAR" "DIAGONAL"
2023-07-09 09:32:03 +00:00
test -n "$XJIGGLER_MOUSE_DISTANCE" \
|| XJIGGLER_MOUSE_DISTANCE=1 # pixels
# eval is scary but this use was mentioned in the man page
getmouselocation(){
eval $(xdotool getmouselocation --shell | sed 's/^/XJIGGLER_/g')
XJIGGLER_T="$(date '+%s')"
}
jiggle(){
2023-07-09 15:30:11 +00:00
case "$XJIGGLER_MOUSE_DIRECTION" in
2023-07-09 09:32:03 +00:00
up) xdotool mousemove_relative 0 -"$XJIGGLER_MOUSE_DISTANCE"
return ;;
right) xdotool mousemove_relative "$XJIGGLER_MOUSE_DISTANCE" 0
return ;;
down) xdotool mousemove_relative 0 "$XJIGGLER_MOUSE_DISTANCE"
return ;;
left) xdotool mousemove_relative -"$XJIGGLER_MOUSE_DISTANCE" 0
return ;;
2023-07-09 15:30:11 +00:00
DIAGONAL) xdotooltool mousemove_relative
"$XJIGGLER_MOUSE_DISTANCE"
"$XJIGGLER_MOUSE_DISTANCE"
return ;;
2023-07-09 09:32:03 +00:00
*)
false ;;
esac
}
pushmouselocation(){
XJIGGLER_lastT="$XJIGGLER_T"
XJIGGLER_lastX="$XJIGGLER_X"
XJIGGLER_lastY="$XJIGGLER_Y"
}
printdebug(){
printf "XJIGGLER_T=%s\n" "$XJIGGLER_T" # seconds since epoch
printf "XJIGGLER_X=%s\n" "$XJIGGLER_X" # pixels
printf "XJIGGLER_Y=%s\n" "$XJIGGLER_Y" # pixels
printf "XJIGGLER_lastT=%s\n" "$XJIGGLER_lastT"
printf "XJIGGLER_lastX=%s\n" "$XJIGGLER_lastX"
printf "XJIGGLER_lastY=%s\n" "$XJIGGLER_lastY"
}
rotatemousedirection(){
2023-07-09 15:30:11 +00:00
case "$XJIGGLER_MOUSE_DIRECTION" in
up) XJIGGLER_MOUSE_DIRECTION=right; return; ;;
right) XJIGGLER_MOUSE_DIRECTION=down; return; ;;
down) XJIGGLER_MOUSE_DIRECTION=left; return; ;;
DIAGONAL) return; ;;
*) XJIGGLER_MOUSE_DIRECTION=up; return; ;;
2023-07-09 09:32:03 +00:00
esac
}
2023-07-09 15:30:11 +00:00
usage(){
printf 'Usage: %s (-ch)\n' "$0">&2
exit 64 # sysexits(3) EX_USAGE
}
while getopts :ch OPTION
do
case "$OPTION" in
c) XJIGGLER_MOUSE_DIRECTION=circular ;;
*) usage ;;
esac
done
2023-07-09 09:32:03 +00:00
getmouselocation; pushmouselocation
while true
do
2023-07-09 15:30:11 +00:00
sleep "$XJIGGLER_DELAY_DETECTION"
2023-07-09 09:32:03 +00:00
getmouselocation
if test "$XJIGGLER_lastX" = "$XJIGGLER_X" \
&& test "$XJIGGLER_lastY" = "$XJIGGLER_Y"
then # no movement
2023-07-09 14:48:49 +00:00
test "$(printf '%s\n%s\n-\np\n' "$XJIGGLER_T" \
"$XJIGGLER_lastT" \
2023-07-09 15:30:11 +00:00
| dc)" -gt "$XJIGGLER_DELAY_ACTION" \
2023-07-09 09:32:03 +00:00
|| continue # hasn't been long enough
rotatemousedirection; jiggle
printf '%s: Jiggled:\n' "$0"
printdebug | sed 's/^/\t/g'
else # movement
pushmouselocation
printf '%s: Movement detected:\n' "$0"
printdebug | sed 's/^/\t/g'
fi
done >&2