aboutsummaryrefslogtreecommitdiff
path: root/bspswallow
blob: 7525013ed593f039dad5bc48a0f1a6c4c11f3c1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh

# Get class of a wid
get_class() {
	id=$1
  if [ -z "$id" ]; then
    echo ""
  else
	  xprop -id "$id" | sed -n '/WM_CLASS\|WM_COMMAND/s/.*"\(.*\)".*/\1/p'
  fi
}

get_pid() {
	xprop _NET_WM_PID -id "$1" | awk '/[0-9]{4}/ {print $3}'
}

get_ppid() {
	ps -o ppid= -p $1
}

check_swallowing_process() {
	[ -n $(get_pid $1) ] && return 0
	[ "$(get_ppid "$(get_ppid "$(get_pid "$1")")")" = "$(get_pid "$2")" ] && return 0 || return 1
}

swallow() {
	addedtodesktop=$2
	lasttermdesktop=$(bspc query -D -n last)

	swallowerid=$1
	swallowingid=$(bspc query -N -n last)
	if [ "$addedtodesktop" = "$lasttermdesktop" ] && check_swallowing_process "$swallowerid" "$swallowingid"; then
		cat ~/.config/bspwm/noswallow ~/.config/bspwm/terminals | grep "^$(get_class "$swallowerid")$" && return
		grep "^$(get_class "$swallowingid")$" ~/.config/bspwm/terminals || return
		echo "$swallowerid $swallowingid" >> /tmp/swallowids
		bspc node "$swallowingid" --flag hidden=on
	fi
}

spit() {
	spitterid=$1
	spitterdesktop=$2
	grep "^$spitterid" /tmp/swallowids || return
	spittingid=$(grep "^$spitterid" /tmp/swallowids | head -n1 | awk '{print $2}')

	bspc node "$spittingid" --flag hidden=off

	termdesktop=$(bspc query -D -n "$spittingid")
	[ "$termdesktop" = "$spitterdesktop" ] || bspc node "$spittingid" -d "$spitterdesktop"

	bspc node "$spittingid" -f
	sed -i "/^$spitterid/d" /tmp/swallowids
}

bspc subscribe node_add node_remove | while read -r event
do
	case $(echo "$event" | awk '{ print $1 }') in
		node_add)
            		swallow $(echo "$event" | awk '{print $5 " " $3}')
			;;
		node_remove)
			spit $(echo "$event" | awk '{print $4 " " $3}')
			;;
	esac
done