Skip to content

Instantly share code, notes, and snippets.

@jcaesar
Forked from anonymous/barkdog
Created September 27, 2016 14:29
Show Gist options
  • Save jcaesar/4e9a5078427e759477e54f9ef0bc913b to your computer and use it in GitHub Desktop.
Save jcaesar/4e9a5078427e759477e54f9ef0bc913b to your computer and use it in GitHub Desktop.
barkdog
#!/bin/bash
set -e -u -x
oldlist=""
declare -A idi
current='' # '' codes for "do nothing with that window"
xprop -spy -root _NET_CLIENT_LIST _NET_ACTIVE_WINDOW |\
while read info; do
if [[ "$info" == _NET_CLIENT_LIST* ]]; then
if [[ "$info" == "$oldlist" ]]; then
continue;
fi
oldlist="$info"
unset nidi
declare -A nidi
for id in $info; do
if [[ "$id" != 0x* ]]; then
continue;
fi
id=${id%,}
id=$((16#${id/0x/}))
if [[ ${idi["$id"]+_} ]]; then
nidi["$id"]="${idi["$id"]}"
else
# unknown id, get info
nidi["$id"]=$(xprop -id "$id" WM_CLASS _NET_WM_PID | sed -n '/^WM_CLASS.* = .*\(\"pavucontrol\"\|\"Firefox\"\)/ { n; s/_NET_WM_PID.* = //p; }')
fi
done
unset idi
declare -A idi
for k in ${!nidi[@]}; do
idi["$k"]="${nidi["$k"]}"
done
echo ${!idi[@]}
elif [[ "$info" == _NET_ACTIVE_WINDOW* ]]; then
id=$((16#${info/*\# 0x/}))
if [[ -n "$current" ]]; then
# only stop it if it still has a window to go back to
haswnd=false
for wid in $(xdotool search --pid="$current" --maxdepth=2); do
if [[ ${idi["$wid"]+_} ]]; then
haswnd=true
break
fi
done
if $haswnd; then
kill -s STOP "$current" # TODO: This will fail if I have a process with the wrong user. Should I just accept my fate?
fi
fi
current="${idi[$id]}"
if [[ -n "$current" ]]; then
kill -s CONT "$current"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment