-
Notifications
You must be signed in to change notification settings - Fork 2
/
tofc
executable file
·242 lines (201 loc) · 5.54 KB
/
tofc
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/sh -e
# A window controller for Porkbelly.
die () {
printf '%s\n' "${0##*/}: $*" >&2
exit 1
}
usage () {
cat << EOF
${0##*/}: control windows via x11fs
usage: tofc [opt] args <wid>
ev_subscribe subscribe to the event stream
ev_send send events to the event stream
relocate move a window to coordinates
resize reshape a window to geometry
killw kill a window
focus focus a window
shove move a window by amount
stretch resize a window by amount
centre centre a window on a coordinate
swap swap a window with another
map map a window
unmap unmap a window
ignore ignore(override_redirect) a window
unignore unignore(override_redirect) a window
border_colour set a window's border colour
border_width set a window's border width
query query a window's properties
Installed extensions
$(find_exts)
if a wid is not given, the focused window will be used
extensions override main functions
EOF
}
find_exts () {
# Find extensions, and print a name for them, and their usage info.
for file in "$exts"/*; do
i=0
while read -r line; do
[ "$i" -gt 2 ] && break
[ "$i" -eq 2 ] && printf '%-19s %s\n' " ${file##*/}" "${line#??}"
: $(( i = i + 1 ))
done < "$file"
done
}
is_int () {
c=$1
shift 1
t=$*
set -- ${t%0x????????}
[ $# -ne "$c" ] && die "incorrect argument count"
for w
do printf '%d' "$w" >/dev/null 2>&1 || die "$w: not an integer"
done
}
is_hex () {
c=$1
shift 1
t=$*
set -- ${t%0x????????}
[ $# -ne "$c" ] && die "incorrect argument count"
for w; do
case ${w#\#} in
[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]) ;;
*) die "$w: not a hex code"
esac
done
}
ev_subscribe () {
[ -f "$WM/event" ] || die "$WM/event doesn't exist"
tail -f "$WM/event"
}
ev_send () {
[ -f "$WM/event" ] || die "$WM/event doesn't exist"
printf '%s\n' "$*" >> "$WM/event"
}
# Window manipulation definitions.
relocate () {
is_int 2 "$@"
printf '%s' "$1 $2" > "$WM/fs/$wid/geometry/position"
ev_send "RELOCATE $wid"
}
resize () {
is_int 2 "$@"
printf '%s' "$1 $2" > "$WM/fs/$wid/geometry/size"
ev_send "RESIZE $wid"
}
killw () {
rmdir "$WM/fs/$wid"
ev_send "KILL $wid"
}
focus () {
printf '%s' "$wid" > "$WM/fs/focused"
ev_send "FOCUS $wid"
}
shove () {
is_int 2 "$@"
read -r x y < "$WM/fs/$wid/geometry/position"
printf '%s' "$(( x + $1 )) $(( y + $2 ))" > "$WM/fs/$wid/geometry/position"
ev_send "SHOVE $wid"
}
stretch () {
is_int 2 "$@"
read -r w h < "$WM/fs/$wid/geometry/size"
printf '%s' "$(( w + $1 )) $(( h + $2 ))" > "$WM/fs/$wid/geometry/size"
ev_send "STRETCH $wid"
}
centre () {
# Centre a window on a set of coordinates.
is_int 2 "$@"
read -r w h < "$WM/fs/$wid/geometry/size"
printf '%s' "$(( $1 - ( w / 2 ))) $(( $2 - ( h / 2 )))" > "$WM/fs/$wid/geometry/position"
ev_send "CENTRE $wid"
}
swap () {
[ "$1" ] || die "not enough arguments"
[ -d "$WM/fs/$1" ] || die "$1: not a window"
# We have to get the focused window ourselves.
[ "$2" ] || read -r wid < "$WM/fs/focused"
read -r all1 < "$WM/fs/$1/geometry/all"
read -r all2 < "$WM/fs/$wid/geometry/all"
printf '%s\n' "$all1" > "$WM/fs/$wid/geometry/all"
printf '%s\n' "$all2" > "$WM/fs/$1/geometry/all"
}
morph () {
is_int 4 "$@"
printf '%s\n' "$1 $2 $3 $4" > "$WM/fs/$wid/geometry/all"
ev_send "MORPH $wid"
}
border_colour () {
is_hex 1 "$1"
printf '%s' "$1" > "$WM/fs/$wid/border/color"
ev_send "BORDER_COLOUR $wid"
}
border_width () {
is_int 1 "$@"
printf '%s' "$1" > "$WM/fs/$wid/border/width"
ev_send "BORDER_WIDTH $wid"
}
map () {
printf '%s\n' "true" > "$WM/fs/$wid/mapped" &
}
unmap () {
printf '%s\n' "false" > "$WM/fs/$wid/mapped" &
}
ignore () {
printf '%s\n' "true" > "$WM/fs/$wid/mapped"
}
unignore () {
printf '%s\n' "false" > "$WM/fs/$wid/mapped"
}
query () {
case $1 in
mapped) sh "$WM/fs/$wid/mapped" ;;
ignored) sh "$WM/fs/$wid/ignored" ;;
x|y|width|height|position|size|all)
read -r a < "$WM/fs/$wid/geometry/$1"
printf '%s\n' "$a"
;;
title|class)
cat "$WM/fs/$wid/$1"
;;
*) die "$1: not found" ;;
esac
}
get_wid () {
# To make this easy for end users to write extensions and such, we use $wid.
# This function sets up $wid.
for _wid; do :; done
read -r focused < "$WM/fs/focused"
case $_wid in
0x????????) : ;;
*) _wid=$focused ;;
esac
wid="$_wid"
export wid
}
main () {
: "${XDG_CONFIG_HOME:=$HOME/.config}" "${WM:=$HOME/.tofu}"
conf="$XDG_CONFIG_HOME/tofu"
exts="$conf/exts"
opt="$1"
shift 1
if [ -x "$exts/$opt" ]; then
get_wid "$@"
[ -x "$conf/hooks/pre-$opt" ] && "$conf/hooks/pre-$opt" "$@"
"$exts/$opt" "$@"
elif [ "$(command -V "$opt" 2>&1 )" = "$opt is a shell builtin" ]; then
usage
die "$opt is a shell builtin"
elif [ "$(command -v "$opt")" = "$opt" ]; then
get_wid "$@"
[ -x "$conf/hooks/pre-$opt" ] && "$conf/hooks/pre-$opt" "$@"
"$opt" "$@"
elif [ -z "$opt" ] || [ "$opt" = help ]; then
usage
else
usage
die "$opt is not a function or extension."
fi
}
main "$@"