mabox-tools/bin/mcorners

107 lines
3.6 KiB
Plaintext
Raw Normal View History

2023-05-19 00:17:56 +02:00
#!/usr/bin/env bash
#:Usage: mcorners [-tl cmd] [-t cmd] [-tr cmd] [-l cmd] [-r cmd] [-bl cmd] [-b cmd] [-br cmd] [-iof] [-v]
#:
#: cmd: run the command when mouse reach to the edge of screen:
#:
#: -tl cmd top-left
#: -t cmd top
#: -tr cmd top-right
#: -l cmd left
#: -r cmd right
#: -bl cmd bottom-left
#: -b cmd bottom
#: -br cmd bottom-right
#: -iof, --ignore-on-fullscreen disable command when active window
#: is fullscreen.
#: --stop force stop mcorners if it's running
#: -v verbose output
#: -h show the help
#:
# same as py version before, but faster
eval $(xdotool getdisplaygeometry --shell)
G=2;MR=8 W=$((WIDTH-G)); H=$((HEIGHT-G)); DB=/tmp/mcorners.lck;
#my
E=200;
X1=$((WIDTH/2-E/2));X2=$((X1+E));Y1=$((HEIGHT/2-E/2));Y2=$((Y1+E));
#my
usage() {
grep "^#:" $0 | while read DOC; do printf '%s\n' "${DOC###:}"; done
exit
}
options() {
while [[ "$1" ]]; do
case "$1" in
"-tl") tl="$2" ;;
"-t") t="$2" ;;
"-tr") tr="$2" ;;
"-l") l="$2" ;;
"-r") r="$2" ;;
"-bl") bl="$2" ;;
"-b") b="$2" ;;
"-br") br="$2" ;;
"-iof"|"--ignore-on-fullscreen") iof=1 ;;
"--stop") kill -9 $(cat $DB) && exit ;;
"-v") verbose=1 ;;
""|"-h"|"--help") usage ;;
esac
shift
done
}
delay() {
local IFS
[[ -n "${_de:-}" ]] || exec {_de}<> <(:)
read ${1:+-t "$1"} -u $_de || :
}
invoke() {
pkill -f jgmenu
proc=$(IFS=" "; set -- $1; echo $1)
id="$(pidof -x "$proc")"
if [[ -n "$verbose" ]]; then
[[ -z "$id" ]] && (eval "$1" &) || (kill -9 $id &)
else
[[ -z "$id" ]] && (eval "$1" 2&>/dev/null &) || (kill -9 $id 2&>/dev/null &)
fi
xdotool mousemove $2 $3
delay 1.2
}
quithandler() {
rm -f $DB
exit $?
}
trap quithandler SIGINT SIGTERM EXIT
[[ -f "$DB" && -z "$(cat $DB | xargs ps -p)" ]] && rm -rf $DB
[[ -z "$@" ]] && usage || options "$@"
if [[ ! -f "$DB" ]]; then
while :;do
[[ ! -f "$DB" ]] && printf "%s\n" $$ >$DB
eval $(xdotool getmouselocation --shell)
if [[ -n "$iof" && -n "$(xdotool getactivewindow 2>/dev/null | xargs xprop -id 2>/dev/null | grep E_FULLS)" ]]; then
delay 0.8
else
[[ -n "$tl" && "$X" -lt "$G" && "$Y" -lt "$G" ]] && invoke "$tl" $((X+MR)) $((Y+MR))
[[ -n "$t" && "$X" -gt "$X1" && "$X" -lt "$X2" && "$Y" -lt "$G" ]] && invoke "$t" $((X-0)) $((Y+MR))
[[ -n "$tr" && "$X" -gt "$W" && "$X" -le "$WIDTH" && "$Y" -lt "$G" ]] && invoke "$tr" $((X-MR)) $((Y+MR))
[[ -n "$l" && "$X" -lt "$G" && "$Y" -gt "$Y1" && "$Y" -lt "$Y2" ]] && invoke "$l" $((X+MR)) $((Y+0))
[[ -n "$r" && "$X" -gt "$W" && "$X" -le "$WIDTH" && "$Y" -gt "$Y1" && "$Y" -lt "$Y2" ]] && invoke "$r" $((X-MR)) $((Y+0))
[[ -n "$bl" && "$X" -lt "$G" && "$Y" -gt "$H" ]] && invoke "$bl" $((X+MR)) $((Y-MR))
[[ -n "$b" && "$X" -gt "$X1" && "$X" -lt "$X2" && "$Y" -gt "$H" ]] && invoke "$b" $((X+0)) $((Y-MR))
[[ -n "$br" && "$X" -gt "$W" && "$X" -le "$WIDTH" && "$Y" -gt "$H" ]] && invoke "$br" $((X-MR)) $((Y-MR))
fi
delay 0.2
done
exit 0
else
printf "Lock Exists: Mcorners is running at PID $(cat $DB). run \"mcorners --stop\" to close it.\n"
exit 1
fi