-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathawmtt.sh
245 lines (225 loc) · 7.46 KB
/
awmtt.sh
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
243
244
245
#!/usr/bin/env bash
# awmtt: awesomewm testing tool
# https://github.com/mikar/awmtt
#{{{ Usage
usage() {
cat <<EOF
awmtt start [-B <path>] [-C <path>] [-D <int>] [-S <size>] [-a <opt>]... [-x <opts>]
awmtt (stop [all] | restart)
awmtt run [-D <int>] <command>
awmtt theme (get | set <theme> | list | random) [-N]
Arguments:
start Spawn nested Awesome via Xephyr
stop Stops the last Xephyr process
all Stop all instances of Xephyr
restart Restart all instances of Xephyr
run <cmd> Run a command inside a Xephyr instance (specify which one with -D)
theme Some basic theming control via:
get Get current theme name
set <theme> Set theme to <theme>
list List available themes
random Set a random theme
Options:
-B|--binary <path> Specify path to awesome binary (for testing custom awesome builds)
-C|--config <path> Specify configuration file
-D|--display <int> Specify the display to use (e.g. 1)
-N|--notest Don't use a testfile but your actual rc.lua (i.e. $HOME/.config/awesome/rc.lua)
This happens by default if there is no rc.lua.test file.
-S|--size <size> Specify the window size
-a|--aopt <opt> Pass option to awesome binary (e.g. --no-argb or --check). Can be repeated.
-x|--xopts <opts> Pass options to xephyr binary (e.g. -keybd ephyr,,,xkblayout=de). Needs to be last.
-h|--help Show this help text and exit
Examples:
awmtt start (uses defaults: -C $HOME/.config/awesome/rc.lua.test -D 1 -S 1024x640)
awmtt start -C /etc/xdg/awesome/rc.lua -D 3 -S 1280x800
awmtt theme set zenburn -N
EOF
exit 0
}
[ "$#" -lt 1 ] && usage
#}}}
#{{{ Utilities
awesome_pid() { pgrep -n "awesome"; }
xephyr_pid() { pgrep -f xephyr_$D; }
errorout() { echo "error: $*" >&2; exit 1; }
#}}}
#{{{ Executable check
AWESOME=$(which awesome)
XEPHYR=$(which Xephyr)
[[ -x "$AWESOME" ]] || errorout 'Please install Awesome first'
[[ -x "$XEPHYR" ]] || errorout 'Please install Xephyr first'
#}}}
#{{{ Default Variables
# Display and window size
D=1
SIZE="1024x640"
AWESOME_OPTIONS=""
XEPHYR_OPTIONS=""
# Path to rc.lua
if [[ "$XDG_CONFIG_HOME" ]]; then
RC_FILE="$XDG_CONFIG_HOME"/awesome/rc.lua.test
else
RC_FILE="$HOME"/.config/awesome/rc.lua.test
fi
[[ ! -f "$RC_FILE" ]] && RC_FILE="$HOME"/.config/awesome/rc.lua
#}}}
#{{{ Hostname Check - this is probably only useful for me. I have the same rc.lua running on two different machines
HOSTNAME=$(uname -n)
#}}}
#{{{ Functions
#{{{ Start function
start() {
# check for free $DISPLAYs
for ((i=0;;i++)); do
if [[ ! -f "/tmp/.X${i}-lock" ]]; then
D=$i;
break;
fi;
done
"$XEPHYR" :$D -name xephyr_$D -ac -br -noreset -screen "$SIZE" $XEPHYR_OPTIONS >/dev/null 2>&1 &
sleep 1
DISPLAY=:$D.0 "$AWESOME" -c "$RC_FILE" $AWESOME_OPTIONS &
sleep 1
# print some useful info
if [[ "$RC_FILE" =~ .test$ ]]; then
echo "Using a test file ($RC_FILE)"
else
echo "Caution: NOT using a test file ($RC_FILE)"
fi
echo "Display: $D, Awesome PID: $(awesome_pid), Xephyr PID: $(xephyr_pid)"
}
#}}}
#{{{ Stop function
stop() {
if [[ "$1" == all ]]; then
echo "Stopping all instances of Xephyr"
kill $(pgrep Xephyr) >/dev/null 2>&1
elif [[ $(xephyr_pid) ]]; then
echo "Stopping Xephyr for display $D"
kill $(xephyr_pid)
else
echo "Xephyr is not running or you did not specify the correct display with -D"
exit 0
fi
}
#}}}
#{{{ Restart function
restart() {
# TODO: (maybe use /tmp/.X{i}-lock files) Find a way to uniquely identify an awesome instance
# (without storing the PID in a file). Until then all instances spawned by this script are restarted...
echo -n "Restarting Awesome... "
for i in $(pgrep -f "awesome -c"); do
kill -s SIGHUP $i;
done
}
#}}}
#{{{ Run function
run() {
[[ -z "$D" ]] && D=1
DISPLAY=:$D.0 "$@" &
LASTPID=$!
echo "PID is $LASTPID"
}
#}}}
#{{{ Theme function
theme() {
# List themes
theme_list() { #TODO: list only directories
if [[ -d $(dirname "$RC_FILE")/themes ]]; then
ls /usr/share/awesome/themes $(dirname "$RC_FILE")/themes
else
ls /usr/share/awesome/themes "$HOME"/.config/awesome/themes
fi
}
case "$1" in
l|list) theme_list
exit 0
;;
esac
# Check for Beautiful library
BEAUTIFUL=$(grep -c 'beautiful.init' "$RC_FILE")
[[ "$BEAUTIFUL" -ge 1 ]] || errorout 'Could not detect theme library "beautiful". Exiting.'
if [[ "$HOSTNAME" == laptop ]]; then
curtheme=$(grep "^themelap" "$RC_FILE" | awk -F\/ '{print $2}')
elif [[ "$HOSTNAME" == htpc ]]; then
curtheme=$(grep "^themehtpc" "$RC_FILE" | awk -F\/ '{print $2}')
else
curtheme=$(grep -oP "[^\/]+(?=\/theme.lua)" "$RC_FILE")
fi
# Change theme
theme_set() {
if [[ "$HOSTNAME" == laptop ]]; then
theme=themelap
elif [[ "$HOSTNAME" == htpc ]]; then
theme=themehtpc
else
theme="^beautiful\.init"
fi
if [[ "$file" ]]; then
[[ $(theme_list | grep -c $file) -lt 1 ]] && errorout 'No such theme.'
echo "changing $curtheme to $file in $RC_FILE"
sed -i "/$theme.*\/theme\.lua\"/s/[^/]*\(\/theme\.lua\)/$file\1/" "$RC_FILE"
else
[[ $(theme_list | grep -c $2) -lt 1 ]] && errorout 'No such theme.'
echo "changing $curtheme to $2 in $RC_FILE"
sed -i "/$theme.*\/theme\.lua\"/s/[^/]*\(\/theme\.lua\)/$2\1/" "$RC_FILE"
fi
}
# Print themename
theme_get() {
echo "$curtheme"
}
# Select random theme and start Xephyr instance
theme_random() {
themes=$(ls -1 $(dirname "$RC_FILE")/themes /usr/share/awesome/themes | grep -vE '/home/|/usr/|icons|README')
file=$(echo "$themes" | sort --random-sort | head -1)
theme_set
D=11 && start
}
case "$1" in
g|get) theme_get;;
s|set) theme_set "${args[@]}";;
r|random) theme_random;;
*) errorout "unrecognized argument. Use theme (list | random | get | set <theme>)";;
esac
}
#}}}
#{{{ Parse options
parse_options() {
while [[ -n "$1" ]]; do
case "$1" in
start) input=start;;
stop) input=stop;;
restart) input=restart;;
run) input=run;;
theme) input=theme;;
-B|--binary) shift; AWESOME="$1";;
-C|--config) shift; RC_FILE="$1";;
-D|--display) shift; D="$1"
[[ ! "$D" =~ ^[0-9] ]] && errorout "$D is not a valid display number";;
-N|--notest) RC_FILE="$HOME"/.config/awesome/rc.lua;;
-S|--size) shift; SIZE="$1";;
-a|--aopt) shift; AWESOME_OPTIONS+="$1";;
-x|--xopts) shift; XEPHYR_OPTIONS="$@";;
-h|--help) usage;;
*) args+=("$1");;
esac
shift
done
}
#}}}
#}}}
#{{{ Main
main() {
case "$input" in
start) start "${args[@]}";;
stop) stop "${args[@]}";;
restart) restart "${args[@]}";;
run) run "${args[@]}";;
theme) theme "${args[@]}";;
*) echo "Option missing or not recognized";;
esac
}
#}}}
parse_options "$@"
main