Created
November 27, 2015 18:21
-
-
Save anonymous/a89fbe442b71cbda2b40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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