forked from bulletmark/libinput-gestures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libinput-gestures-setup
executable file
·373 lines (334 loc) · 9.13 KB
/
libinput-gestures-setup
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/bash
# User setup script.
# (C) Mark Blakeney, Aug 2016.
PROG="$(basename $0)"
NAME=${PROG%-*}
BINDIR="/usr/bin"
SYSDIR="/usr/lib/systemd/user"
APPDIR="/usr/share/applications"
ICOBAS="/usr/share/icons/hicolor"
ICODIR="$ICOBAS/128x128/apps"
OCODIR="/usr/share/pixmaps"
DOCDIR="/usr/share/doc/$NAME"
CNFDIR="/etc"
HCFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
AUTDIR="$HCFDIR/autostart"
SVCFLG="$HCFDIR/.$NAME-is-service"
CHECKSECS=6
usage() {
echo "Usage:"
echo "As root: sudo $PROG install|uninstall"
echo " -d <dir> (option sets DESTDIR for install/uninstall)"
echo
echo "As user: $PROG command [command ..]"
echo " where command is any of:"
echo " start|stop|restart|autostart|autostop|status"
echo
exit 1
}
# Process command line options
DESTDIR=""
while getopts d: c; do
case $c in
d) DESTDIR="$OPTARG";;
\?) usage;;
esac
done
shift $((OPTIND - 1))
# Test if given systemd property is set for given unit
sysd_prop() {
if systemctl --user show -p "$2" "$1" 2>/dev/null | grep -q "=$3$"; then
echo 1
else
echo 0
fi
}
# Launch given desktop app. First work out most suitable launcher.
# Pretty crude at present but should work for at least GNOME and KDE.
de_start() {
local app="$1"
local fullpath="$APPDIR/$app.desktop"
local binpath="$BINDIR/$app"
# All the commands we will potentially try ..
local cmds=(
"kde kioclient5 exec $fullpath"
"kde kioclient exec $fullpath"
"all gtk-launch $app"
"all i3-msg exec $binpath"
"all exo-open $fullpath"
"all dex $fullpath"
)
local cmdline
for cmdline in "${cmds[@]}" ; do
IFS=' ' read de cmd args <<< "$cmdline"
# Skip if the command does not exist
if ! hash $cmd &>/dev/null; then
continue
fi
# Only try KDE commands on KDE
if ! echo $XDG_CURRENT_DESKTOP | grep -q KDE; then
if [[ $de == kde ]]; then
continue
fi
fi
# Execute this command
$cmd $args &>/dev/null
return $?
done
echo "Don't know how to invoke $app.desktop" >&2
return 1
}
# Set up desktop entry link for auto start of app, if it doesn't already
# exist
de_auto_start() {
if [[ ! -f $APPDIR/$NAME.desktop ]]; then
if [[ -e $AUTDIR/$NAME.desktop ]]; then
echo "Removed old $AUTDIR/$NAME.desktop"
rm -f $AUTDIR/$NAME.desktop
fi
return 1
fi
if ! cmp -s $APPDIR/$NAME.desktop $AUTDIR/$NAME.desktop; then
if mkdir -p $AUTDIR && cp $APPDIR/$NAME.desktop $AUTDIR; then
echo "installed or updated $AUTDIR/$NAME.desktop"
fi
fi
return 0
}
# Action given user command
user_action() {
local cmd="$1"
local pidfile=/tmp/$NAME-$USER.pid
# Test if systemd is enabled/running
if [[ $HAS_SYSD -eq 1 ]]; then
svc_enabled=$(sysd_prop $NAME.service UnitFileState enabled)
svc_running=$(sysd_prop $NAME.service SubState running)
else
svc_enabled=0
svc_running=0
fi
if [[ $cmd == service ]]; then
if [[ $HAS_SYSD -eq 0 ]]; then
echo "Systemd not available, can not run as service."
exit 1
fi
mkdir -p "$(dirname $SVCFLG)"
echo "# This file created by \"$NAME-setup $cmd\" command." >$SVCFLG
rm -fv $AUTDIR/$NAME.desktop
elif [[ $cmd == desktop ]]; then
rm -f $SVCFLG
if [[ $HAS_SYSD -eq 1 ]]; then
systemctl --user disable $NAME.service &>/dev/null
fi
elif [[ $cmd == start ]]; then
STARTAS=""
if [[ -f $SVCFLG ]]; then
if [[ $HAS_SYSD -eq 0 ]]; then
echo "Systemd service is not available."
exit 1
fi
if systemctl --user start $NAME.service; then
STARTAS="user service"
fi
else
if [[ ! -f $APPDIR/$NAME.desktop ]]; then
echo "$NAME is not installed."
exit 1
fi
if de_start "$NAME"; then
STARTAS="desktop application"
fi
fi
if [[ -n $STARTAS ]]; then
# Wait some time to start ..
done=0
for _ in $(seq $CHECKSECS); do
sleep 1
if ps "$(head -1 $pidfile 2>/dev/null)" &>/dev/null; then
done=1
break
fi
done
if [[ $done -eq 1 ]]; then
echo "$NAME started as a $STARTAS".
else
echo "$NAME failed to start as a $STARTAS". >&2
fi
fi
elif [[ $cmd == stop ]]; then
STARTAS=""
if [[ $svc_running -eq 1 ]]; then
systemctl --user stop $NAME.service
STARTAS="user service"
else
if [[ -f $pidfile ]]; then
local killed=0
while read pid; do
if kill $pid &>/dev/null; then
killed=1
fi
done <$pidfile
if [[ $killed -ne 0 ]]; then
STARTAS="desktop application"
fi
fi
fi
if [[ -n $STARTAS ]]; then
# Wait some time to stop ..
done=0
for _ in $(seq $CHECKSECS); do
sleep 1
if ! ps "$(head -1 $pidfile 2>/dev/null)" &>/dev/null; then
done=1
break
fi
done
if [[ $done -eq 1 ]]; then
echo "$NAME stopped $STARTAS".
else
echo "$NAME failed to stop $STARTAS". >&2
fi
fi
elif [[ $cmd == autostart ]]; then
if [[ $HAS_SYSD -eq 1 ]]; then
# Be sure to remove any old systemd links ..
systemctl --user disable $NAME.service &>/dev/null
fi
if [[ -f $SVCFLG ]]; then
if [[ $HAS_SYSD -eq 0 ]]; then
echo "Systemd service is not available."
exit 1
fi
if systemctl --user enable $NAME.service; then
echo "$NAME enabled as a user service."
fi
rm -fv $AUTDIR/$NAME.desktop
elif ! de_auto_start; then
echo "$NAME is not installed."
exit 1
fi
elif [[ $cmd == autostop ]]; then
if [[ $HAS_SYSD -eq 1 ]]; then
systemctl --user disable $NAME.service &>/dev/null
fi
rm -fv $AUTDIR/$NAME.desktop
elif [[ $cmd == status ]]; then
if [[ -f $BINDIR/$NAME ]]; then
echo "$NAME is installed."
else
echo "$NAME is not installed."
fi
if [[ -f $SVCFLG ]]; then
echo "$NAME is set up as a user service."
else
echo "$NAME is set up as a desktop application."
fi
if [[ $svc_running -eq 1 ]]; then
echo "$NAME is currently running as a user service."
elif ps "$(head -1 $pidfile 2>/dev/null)" &>/dev/null; then
echo "$NAME is currently running as a desktop application."
else
echo "$NAME is not currently running."
fi
if [[ $svc_enabled -eq 1 ]]; then
echo "$NAME is set to autostart as a user service."
rm -fv $AUTDIR/$NAME.desktop
else
if [[ -f $AUTDIR/$NAME.desktop ]]; then
echo "$NAME is set to autostart as a desktop application."
else
echo "$NAME is not set to autostart."
fi
fi
if [[ -f $HCFDIR/$NAME.conf ]]; then
echo "$NAME is using custom configuration file."
else
echo "$NAME is using default configuration file."
fi
else
echo "ERROR: \"$cmd\" is not a valid user command" >&2
fi
}
cmd="$1"
if [[ $cmd == install || $cmd == uninstall ]]; then
DESTDIR="${DESTDIR%%+(/)}"
if [[ -z $DESTDIR && $(id -un) != root ]]; then
echo "Install or uninstall must be run as sudo/root."
exit 1
fi
if [[ $# -ne 1 ]]; then
usage
fi
# Remove any old files from earlier versions of program
rm -f $DESTDIR$OCODIR/$NAME.png
rm -f $DESTDIR$ICODIR/$NAME.png
if [[ $cmd == install ]]; then
install -CDv -m 755 -t $DESTDIR$BINDIR $NAME-setup
install -CDv -m 755 -t $DESTDIR$BINDIR $NAME
install -CDv -m 644 -t $DESTDIR$SYSDIR $NAME.service
install -CDv -m 644 -t $DESTDIR$APPDIR $NAME.desktop
install -CDv -m 644 -t $DESTDIR$ICODIR $NAME.svg
install -CDv -m 644 -t $DESTDIR$CNFDIR $NAME.conf
install -CDv -m 644 -t $DESTDIR$DOCDIR README.md
else
rm -rfv $DESTDIR$BINDIR/$NAME
rm -rfv $DESTDIR$SYSDIR/$NAME.service
rm -rfv $DESTDIR$APPDIR/$NAME.desktop
rm -rfv $DESTDIR$ICODIR/$NAME.svg
rm -rfv $DESTDIR$CNFDIR/$NAME.conf
rm -rfv $DESTDIR$DOCDIR
rm -rfv $DESTDIR$BINDIR/$NAME-setup
fi
if [[ -z $DESTDIR ]]; then
if [[ -x /usr/bin/update-desktop-database ]]; then
/usr/bin/update-desktop-database -q
fi
if [[ -x /usr/bin/gtk-update-icon-cache ]]; then
/usr/bin/gtk-update-icon-cache $ICOBAS
fi
fi
else
if [[ $(id -un) == root ]]; then
echo "Non-installation commands must be run as your own user."
exit 1
fi
# Test if systemd is installed
if type systemctl &>/dev/null; then
HAS_SYSD=$(sysd_prop graphical-session.target ActiveState active)
else
HAS_SYSD=0
fi
if [[ $HAS_SYSD -eq 1 ]]; then
# Reload systemd if service file has been updated
if systemctl --user status $NAME.service 2>&1 |
grep -q ' changed on disk'; then
echo "$NAME.service has changed, reloading systemd user daemon .."
systemctl --user daemon-reload
sleep 2
fi
fi
# Remove any old configuration from earlier versions of program
rm -fv ~/bin/$NAME 2>/dev/null
rm -fv ~/.local/bin/$NAME 2>/dev/null
rm -fv ~/.local/share/applications/$NAME.desktop 2>/dev/null
rm -fv ~/.local/share/icons/$NAME.png 2>/dev/null
# Look for and update any autostart file if it is a link or not
# pointing to the latest desktop entry. Apparently user autostart
# files should not be symlinks to system dir files.
if [[ -e $AUTDIR/$NAME.desktop ]]; then
if [[ -L $AUTDIR/$NAME.desktop ]]; then
echo "Removed old $AUTDIR/$NAME.desktop link"
rm -f $AUTDIR/$NAME.desktop
fi
de_auto_start
fi
# Execute each command given on command line ..
for cmd in "$@"; do
if [[ $cmd == restart ]]; then
user_action "stop"
cmd=start
fi
user_action "$cmd"
done
fi
exit 0