summaryrefslogtreecommitdiff
path: root/dwmswallow
blob: eaab3bb937c229c5a166038704fc2e3ac74de843 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env sh

# Separator and command prefix, as defined in dwm.c:fakesignal()
SEP='###'
PREFIX='#!'

# Asserts that all arguments are valid X11 window IDs, i.e. positive integers.
# For the purpose of this script 0 is declared invalid.
is_winid() {
	while :; do
		# Given input incompatible to %d, some implementations of printf return
		# an error while others silently evaluate the expression to 0.
		if ! wid=$(printf '%d' "$1" 2>/dev/null) || [ "$wid" -le 0 ]; then
			return 1
		fi

		[ -n "$2" ] && shift || break
	done
}

# Prints usage help. If "$1" is provided, function exits script after
# execution.
usage() {
	[ -t 1 ] && myprintf=printf || myprintf=true
	msg="$(cat <<-EOF
	dwm window swallowing command-line interface. Usage:

	  $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWER [-c CLASS] [-i INSTANCE] [-t TITLE]$($myprintf "\033[0m")
	    Register window $($myprintf "\033[3m")SWALLOWER$($myprintf "\033[0m") to swallow the next future window whose attributes
	    match the $($myprintf "\033[3m")CLASS$($myprintf "\033[0m") name, $($myprintf "\033[3m")INSTANCE$($myprintf "\033[0m") name and window $($myprintf "\033[3m")TITLE$($myprintf "\033[0m") filters using basic
	    string-matching. An omitted filter will match anything.

	  $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWER -d$($myprintf "\033[0m")
	    Deregister queued swallow for window $($myprintf "\033[3m")SWALLOWER$($myprintf "\033[0m"). Inverse of above signature.

	  $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWER SWALLOWEE$($myprintf "\033[0m")
	    Perform immediate swallow of window $($myprintf "\033[3m")SWALLOWEE$($myprintf "\033[0m") by window $($myprintf "\033[3m")SWALLOWER$($myprintf "\033[0m").

	  $($myprintf "\033[1m")dwmswallow $($myprintf "\033[3m")SWALLOWEE -s$($myprintf "\033[0m")
	    Stop swallow of window $($myprintf "\033[3m")SWALLOWEE$($myprintf "\033[0m"). Inverse of the above signature. Visible
	    windows only.

	  $($myprintf "\033[1m")dwmswallow -h$($myprintf "\033[0m")
	    Show this usage information.
	EOF
	)"

	if [ -n "$1" ]; then
		echo "$msg" >&2
		exit "$1"
	else
		echo "$msg"
	fi
}

# Determine number of leading positional arguments
arg1="$1" # save for later
arg2="$2" # save for later
num_pargs=0
while :; do
	case "$1" in
	-*|"") break ;;
	*) num_pargs=$((num_pargs + 1)); shift ;;
	esac
done

case "$num_pargs" in
1)
	! is_winid "$arg1" && usage 1

	widswer="$arg1"
	if [ "$1" = "-d" ] && [ "$#" -eq 1 ]; then
		if name="$(printf "${PREFIX}swalunreg${SEP}%u" "$widswer" 2>/dev/null)"; then
			xsetroot -name "$name"
		else
			usage 1
		fi
	elif [ "$1" = "-s" ] && [ "$#" -eq 1 ]; then
		widswee="$arg1"
		if name="$(printf "${PREFIX}swalstop${SEP}%u" "$widswee" 2>/dev/null)"; then
			xsetroot -name "$name"
		else
			usage 1
		fi
	else
		while :; do
			case "$1" in
			-c) [ -n "$2" ] && { class="$2"; shift 2; } || usage 1 ;;
			-i) [ -n "$2" ] && { instance="$2"; shift 2; } || usage 1 ;;
			-t) [ -n "$2" ] && { title="$2"; shift 2; } || usage 1 ;;
			"") break ;;
			*)	usage 1 ;;
			esac
		done
		widswer="$arg1"
		if name="$(printf "${PREFIX}swalreg${SEP}%u${SEP}%s${SEP}%s${SEP}%s" "$widswer" "$class" "$instance" "$title" 2>/dev/null)"; then
			xsetroot -name "$name"
		else
			usage 1
		fi
	fi
	;;
2)
	! is_winid "$arg1" "$arg2" || [ -n "$1" ] && usage 1

	widswer="$arg1"
	widswee="$arg2"
	if name="$(printf "${PREFIX}swal${SEP}%u${SEP}%u" "$widswer" "$widswee" 2>/dev/null)"; then
		xsetroot -name "$name"
	else
		usage 1
	fi
	;;
*)
	if [ "$arg1" = "-h" ] && [ $# -eq 1 ]; then
		usage
	else
		usage 1
	fi
esac