Skip to content

Commit a03d34f

Browse files
committed
Add option to install as systemd user service
See the new words added in the README here for a complete description of this change.
1 parent 2faa798 commit a03d34f

File tree

2 files changed

+120
-19
lines changed

2 files changed

+120
-19
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ invoke libinput-gestures.desktop_. If you get this error message,
186186
install the dex package, preferably from your system packages
187187
repository, and try again.
188188

189+
#### ALTERNATIVE TO START AS SYSTEMD USER SERVICE
190+
191+
If you prefer to start the application as a [systemd user
192+
service](https://wiki.archlinux.org/index.php/Systemd/User) rather than
193+
as a desktop application started by your DE, you can use the following
194+
commands:
195+
196+
To start the application immediately in the background, instead of
197+
`libinput-gestures-setup start` as in the previous section, do the
198+
following:
199+
200+
libinput-gestures-setup start-service
201+
202+
To enable the app to start automatically in the background when you log
203+
in to your DE, instead of `libinput-gestures-setup autostart` as in the
204+
previous section, do the following:
205+
206+
libinput-gestures-setup autostart-service
207+
208+
The other commands described in the previous section work also with the
209+
systemd user service.
210+
189211
### UPGRADE
190212

191213
# cd to source dir, as above

libinput-gestures-setup

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PROG="$(basename $0)"
66
NAME=${PROG%-*}
77

88
BINDIR="/usr/bin"
9+
SYSDIR="/usr/lib/systemd/user"
910
APPDIR="/usr/share/applications"
1011
ICOBAS="/usr/share/icons/hicolor"
1112
ICODIR="$ICOBAS/128x128/apps"
@@ -18,7 +19,8 @@ AUTDIR="$HCFDIR/autostart"
1819
usage() {
1920
echo "Usage:"
2021
echo "As root: sudo $PROG install|uninstall"
21-
echo "As user: $PROG start|stop|restart|autostart|autostop|status"
22+
echo "As user: $PROG start|stop|restart|autostart|autostop|status|"
23+
echo " start-service|autostart-service"
2224
echo
2325
echo "-d <dir> (option sets DESTDIR for install/uninstall)"
2426
echo "-r (force allow root to perform user commands. PLEASE AVOID USING THIS!)"
@@ -46,7 +48,7 @@ cmd="$1"
4648

4749
# Launch given desktop app. First work out most suitable launcher.
4850
# Pretty crude at present but should work for at least GNOME and KDE.
49-
launch() {
51+
de_start() {
5052
local app="$1"
5153
local fullpath="$APPDIR/$app.desktop"
5254
local binpath="$BINDIR/$app"
@@ -88,7 +90,7 @@ launch() {
8890

8991
# Set up desktop entry link for auto start of app, if it doesn't already
9092
# exist
91-
auto_start() {
93+
de_auto_start() {
9294
if [[ ! -f $APPDIR/$NAME.desktop ]]; then
9395
if [[ -e $AUTDIR/$NAME.desktop ]]; then
9496
echo "Removed old $AUTDIR/$NAME.desktop"
@@ -107,45 +109,104 @@ auto_start() {
107109

108110
# Action given user command
109111
user_action() {
110-
local cmd=$1
112+
local cmd="$1"
113+
local has_sysd="$2"
114+
local svc_enabled="$3"
115+
local svc_running="$4"
111116

112117
if [[ $cmd == start ]]; then
118+
if [[ $svc_running == running ]]; then
119+
echo "Already running as a service."
120+
exit 1
121+
fi
113122
if [[ ! -f $APPDIR/$NAME.desktop ]]; then
114123
echo "$NAME is not installed."
115124
exit 1
116125
fi
117-
if launch "$NAME"; then
118-
echo "$NAME started."
126+
if de_start "$NAME"; then
127+
echo "$NAME started for desktop."
119128
fi
120129
elif [[ $cmd == stop ]]; then
130+
if [[ $svc_running == running ]]; then
131+
systemctl --user stop $NAME.service
132+
echo "$NAME stopped as user service."
133+
fi
134+
121135
for prog in libinput-debug-events $NAME; do
122136
if pkill -u $USER -f "$prog\$|$prog " &>/dev/null; then
123-
echo "$prog stopped."
137+
echo "$prog stopped for desktop."
124138
fi
125139
done
140+
elif [[ $cmd == start-service ]]; then
141+
if [[ $has_sysd -eq 0 ]]; then
142+
echo "Systemd is not installed."
143+
exit 1
144+
fi
145+
if systemctl --user start $NAME.service; then
146+
echo "$NAME started as user service."
147+
fi
148+
elif [[ $cmd == autostart-service ]]; then
149+
if [[ $has_sysd -eq 0 ]]; then
150+
echo "Systemd is not installed."
151+
exit 1
152+
fi
153+
if systemctl --user enable $NAME.service; then
154+
echo "$NAME enabled as user service."
155+
fi
156+
rm -fv $AUTDIR/$NAME.desktop
126157
elif [[ $cmd == autostart ]]; then
127-
if ! auto_start; then
158+
if ! de_auto_start; then
128159
echo "$NAME is not installed."
129160
exit 1
130161
fi
162+
if [[ $has_sysd -eq 1 ]]; then
163+
systemctl --user disable $NAME.service &>/dev/null
164+
fi
131165
elif [[ $cmd == autostop ]]; then
166+
if [[ $has_sysd -eq 1 ]]; then
167+
systemctl --user disable $NAME.service &>/dev/null
168+
fi
132169
rm -fv $AUTDIR/$NAME.desktop
133170
elif [[ $cmd == status ]]; then
134-
if [[ -f $APPDIR/$NAME.desktop ]]; then
171+
if [[ -f $BINDIR/$NAME ]]; then
135172
echo "$NAME is installed."
136173
else
137174
echo "$NAME is not installed."
138175
fi
176+
177+
if [[ $svc_enabled == enabled ]]; then
178+
echo "$NAME is set to autostart as user service."
179+
else
180+
echo "$NAME is not set to autostart as user service."
181+
fi
182+
139183
if [[ -f $AUTDIR/$NAME.desktop ]]; then
140-
echo "$NAME is set to autostart."
184+
echo "$NAME is set to autostart as desktop application."
141185
else
142-
echo "$NAME is not set to autostart."
186+
echo "$NAME is not set to autostart as desktop application."
143187
fi
144-
if pgrep -u $USER -f "$NAME\$|$NAME " &>/dev/null; then
145-
echo "$NAME is running."
188+
189+
local svc=0
190+
local de=0
191+
if [[ $svc_running == running ]]; then
192+
echo "$NAME is running as user service."
193+
svc=1
146194
else
147-
echo "$NAME is not running."
195+
echo "$NAME is not running as user service."
196+
svc=0
197+
fi
198+
199+
if [[ $svc -eq 0 ]]; then
200+
if pgrep -u $USER -f "$NAME\$|$NAME " &>/dev/null; then
201+
echo "$NAME is running as desktop application."
202+
de=1
203+
fi
204+
fi
205+
206+
if [[ $de -eq 0 ]]; then
207+
echo "$NAME is not running as desktop application."
148208
fi
209+
149210
if [[ -f $HCFDIR/$NAME.conf ]]; then
150211
echo "$NAME is using custom configuration."
151212
else
@@ -170,12 +231,14 @@ if [[ $cmd == install || $cmd == uninstall ]]; then
170231
if [[ $cmd == install ]]; then
171232
install -CDv -m 755 -t $DESTDIR$BINDIR $NAME-setup
172233
install -CDv -m 755 -t $DESTDIR$BINDIR $NAME
234+
install -CDv -m 644 -t $DESTDIR$SYSDIR $NAME.service
173235
install -CDv -m 644 -t $DESTDIR$APPDIR $NAME.desktop
174236
install -CDv -m 644 -t $DESTDIR$ICODIR $NAME.svg
175237
install -CDv -m 644 -t $DESTDIR$CNFDIR $NAME.conf
176238
install -CDv -m 644 -t $DESTDIR$DOCDIR README.md
177239
else
178240
rm -rfv $DESTDIR$BINDIR/$NAME
241+
rm -rfv $DESTDIR$SYSDIR/$NAME.service
179242
rm -rfv $DESTDIR$APPDIR/$NAME.desktop
180243
rm -rfv $DESTDIR$ICODIR/$NAME.svg
181244
rm -rfv $DESTDIR$CNFDIR/$NAME.conf
@@ -211,15 +274,31 @@ else
211274
echo "Removed old $AUTDIR/$NAME.desktop link"
212275
rm -f $AUTDIR/$NAME.desktop
213276
fi
214-
auto_start
277+
de_auto_start
215278
fi
216279

217-
if [[ $cmd == restart ]]; then
218-
user_action "stop"
219-
user_action "start"
280+
# Test if systemd is installed
281+
if type systemctl &>/dev/null; then
282+
has_sysd=1
283+
svc_enabled=$(systemctl --user show -P UnitFileState $NAME.service)
284+
svc_running=$(systemctl --user show -P SubState $NAME.service)
220285
else
221-
user_action $cmd
286+
has_sysd=1
287+
svc_enabled="no"
288+
svc_running="no"
289+
fi
290+
291+
if [[ $cmd == restart ]]; then
292+
if [[ $svc_enabled == enabled || $svc_running == running ]]; then
293+
cmd=start-service
294+
else
295+
cmd=start
296+
fi
297+
298+
user_action "stop" $has_sysd $svc_enabled $svc_running
222299
fi
300+
301+
user_action $cmd $has_sysd $svc_enabled $svc_running
223302
fi
224303

225304
exit 0

0 commit comments

Comments
 (0)