Skip to content

Instantly share code, notes, and snippets.

/sandman.sh

Created Nov 27, 2015
Embed
What would you like to do?
#!/usr/bin/zsh
#xdotool getactivewindow getwindowpid
getwindowpid () {
if wmpid=$(xprop |grep ^_NET_WM_PID); then
pid=${wmpid##* = }
if [[ $pid = 1 ]]; then
pgrep -o iceweasel # HACK - I run iceweasel in a sandbox with its own pid namespace
else
echo $pid
fi
else
exit 1
fi
}
awake () {
case $(ps ho state $1) in
T)
return 1
;;
S)
return 0
;;
esac
}
wake () {
if [[ -n $1 ]]; then
if [[ $1 = <-> ]]; then
kill -s CONT $1
else
killall -SIGCONT $1
fi
else
if pid=$(getwindowpid); then
kill -s CONT $(getwindowpid)
fi
fi
}
sleep () {
timeout 1s xclip -o | xclip # a stopped firefox will not respond to paste requests
if [[ -n $1 ]]; then
if [[ $1 = <-> ]]; then
kill -s STOP $1
else
killall -SIGSTOP $1
fi
else
if pid=$(getwindowpid); then
kill -s STOP $pid
fi
fi
}
toggle () {
if ! pid=$(getwindowpid); then
exit 2
fi
if awake $pid; then
sleep $pid
else
wake $pid
fi
}
usage () {
cat <<USAGE
sandman wake|sleep|toggle [procname]
by default, sandman will send SIGSTOP|SIGCONT to the specified process.
if no procname is given, it'll ask xprop to get a pid.
toggle takes no arguments.
USAGE
}
case $1 in
wake)
wake $2
;;
sleep)
sleep $2
;;
toggle)
toggle
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment