summarylogtreecommitdiffstats
path: root/0005-Add-hook-to-manage-services.patch
blob: 0aef809b3739aa4f895b5e4d8967fd8639c4de2d (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
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
From 6fcf5b7ca0e447f92f159afae9b81381775947b9 Mon Sep 17 00:00:00 2001
From: Aren Moynihan <aren@peacevolution.org>
Date: Fri, 13 Oct 2023 17:23:28 -0400
Subject: [PATCH sxmo-utils 5/6] Add hook to manage services

This adds a layer of abstraction over superctl and systemctl, and
possibly others like s6 if someone wants to add that in the future. The
command format is mostly the same as it was with superd, but this allows
us to hide the differences between service managers.
---
 .../default_hooks/sxmo_hook_contextmenu.sh    | 12 ++--
 configs/default_hooks/sxmo_hook_service.sh    | 63 +++++++++++++++++++
 configs/default_hooks/sxmo_hook_start.sh      |  7 +--
 scripts/core/sxmo_appmenu.sh                  |  4 +-
 scripts/core/sxmo_common.sh                   |  1 +
 scripts/core/sxmo_winit.sh                    |  1 +
 scripts/core/sxmo_xinit.sh                    |  1 +
 7 files changed, 73 insertions(+), 16 deletions(-)
 create mode 100644 configs/default_hooks/sxmo_hook_service.sh

diff --git a/configs/default_hooks/sxmo_hook_contextmenu.sh b/configs/default_hooks/sxmo_hook_contextmenu.sh
index 26d21ef..1d5dd6d 100755
--- a/configs/default_hooks/sxmo_hook_contextmenu.sh
+++ b/configs/default_hooks/sxmo_hook_contextmenu.sh
@@ -14,10 +14,6 @@
 XPROPOUT="$(sxmo_wm.sh focusedwindow)"
 WMCLASS="${1:-$(printf %s "$XPROPOUT" | grep app: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')}"
 
-superd_service_isrunning() {
-	superctl status "$1" | grep -q started
-}
-
 sxmo_service_isrunning() {
 	sxmo_jobs.sh running "$1" > /dev/null
 }
@@ -44,7 +40,7 @@ case "$WMCLASS" in
 		CHOICES="
 			$icon_plk Modem PIN                ^ 0 ^ sxmo_unlocksim.sh
 			$icon_phn Modem Monitor $(
-				superd_service_isrunning sxmo_modemmonitor &&
+				sxmo_hook_service.sh is-active sxmo_modemmonitor &&
 				printf %b "$icon_ton ^ 1 ^ superctl stop sxmo_modemmonitor" ||
 				printf %b "$icon_tof ^ 1 ^ superctl start sxmo_modemmonitor"
 			) && sxmo_hook_statusbar.sh modem_monitor
@@ -72,9 +68,9 @@ case "$WMCLASS" in
 				printf %b "$icon_tof ^ 1 ^ sxmo_wm.sh inputevent stylus on"
 			)
 			$icon_cfg Gestures $(
-				superd_service_isrunning "sxmo_hook_lisgd" &&
+				sxmo_hook_service.sh is-active "sxmo_hook_lisgd" &&
 				printf "%s" "$icon_ton" || printf "%s" "$icon_tof"
-			) ^ 1 ^ supertoggle_daemon 'sxmo_hook_lisgd' && (rm $XDG_CACHE_HOME/sxmo/sxmo.nogesture 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.nogesture)
+			) ^ 1 ^ toggle_service 'sxmo_hook_lisgd' && (rm $XDG_CACHE_HOME/sxmo/sxmo.nogesture 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.nogesture)
 			$icon_cfg Toggle Bar ^ 0 ^ sxmo_wm.sh togglebar
 			$icon_bth Bluetooth $(
 				rfkill list bluetooth | grep -q "yes" &&
@@ -695,7 +691,7 @@ case "$WMCLASS" in
 		;;
 	org.gnome.maps)
 	CHOICES="
-		$icon_gps Toggle Geoclue ^ 0 ^ superctl status geoclue-agent |grep started >/dev/null && superctl stop geoclue-agent || superctl start geoclue-agent
+		$icon_gps Toggle Geoclue ^ 0 ^ toggle_service geoclue-agent
 		$icon_zmi Zoom in ^ 1 ^ sxmo_type -M ctrl =
 		$icon_zmo Zoom out ^ 1 ^ sxmo_type -M ctrl -k minus
 		$icon_fnd Explore POI ^ 0 ^ sxmo_type -M ctrl -M Shift F
diff --git a/configs/default_hooks/sxmo_hook_service.sh b/configs/default_hooks/sxmo_hook_service.sh
new file mode 100644
index 0000000..f744659
--- /dev/null
+++ b/configs/default_hooks/sxmo_hook_service.sh
@@ -0,0 +1,63 @@
+#! /bin/sh
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2023 Sxmo Contributors
+
+set -e
+
+# WARNING: we can't source sxmo_common.sh, otherwise calling sueprd will cause a
+# loop
+# TODO: this needs better detection logic, perhaps it's just a compiel time
+# flag?
+if [ -n "$SXMO_SERVICE_MANAGER" ]; then
+	MANAGER="$SXMO_SERVICE_MANAGER"
+elif command -v systemctl > /dev/null; then
+	MANAGER=systemd
+elif command -v superd > /dev/null; then
+	MANAGER=superd
+fi
+
+case "$1" in
+	restart|start|status|stop|is-active|sxmo_starting|sxmo_stopping) ;;
+	*)
+		cat <<EOF
+sxmo_hook_service.sh is a thin wrapper around the superctl / systemctl commands
+that provides a single interface for the rest of the sxmo to use.
+
+Usage: sxmo_hook_service.sh <restart|start|status|stop|is-active> [args]
+
+Unknown subcommand: $1
+EOF
+		exit 127
+		;;
+esac
+
+case "$MANAGER" in
+	superd)
+		case "$1" in
+			sxmo_starting)
+				sxmo_jobs.sh start daemon_manager superd
+				# wait for superd to start
+				while ! superctl status > /dev/null 2>&1; do
+					sleep 0.5
+				done
+				;;
+			sxmo_stopping) ;;
+			"is-active") superctl status "$1" | grep -q started ;;
+			*) superctl "$@" ;;
+		esac
+		;;
+	systemd)
+		# systemd waits for processes to start, run start asyncrounously
+		# to prevent hanging the start hook
+		case "$1" in
+			sxmo_starting)
+				printenv | cut -d '=' -f 1 | xargs systemctl --user import-environment
+				;;
+			sxmo_stopping)
+				systemctl --user unset-environment \
+					WAYLAND_DISPLAY DISPLAY SXMO_WM XDG_CURRENT_DESKTOP
+				;;
+			start) systemctl --user "$@" & ;;
+			*) systemctl --user "$@" ;;
+		esac
+esac
diff --git a/configs/default_hooks/sxmo_hook_start.sh b/configs/default_hooks/sxmo_hook_start.sh
index 50fa86e..a838e24 100755
--- a/configs/default_hooks/sxmo_hook_start.sh
+++ b/configs/default_hooks/sxmo_hook_start.sh
@@ -9,12 +9,7 @@
 # Create xdg user directories, such as ~/Pictures
 xdg-user-dirs-update
 
-sxmo_jobs.sh start daemon_manager superd
-
-# let time to superd to start correctly
-while ! superctl status > /dev/null 2>&1; do
-	sleep 0.5
-done
+sxmo_hook_service.sh sxmo_starting
 
 # Not dangerous if "locker" isn't an available state
 sxmo_state.sh set locker
diff --git a/scripts/core/sxmo_appmenu.sh b/scripts/core/sxmo_appmenu.sh
index 10c2f51..a7c7733 100755
--- a/scripts/core/sxmo_appmenu.sh
+++ b/scripts/core/sxmo_appmenu.sh
@@ -18,8 +18,8 @@ confirm() {
 	fi
 }
 
-supertoggle_daemon() {
-	if superctl status "$1" | grep -q started; then
+toggle_service() {
+	if sxmo_hook_service.sh is-active "$1"; then
 		superctl stop "$1"
 		sxmo_notify_user.sh "$1 Stopped"
 	else
diff --git a/scripts/core/sxmo_common.sh b/scripts/core/sxmo_common.sh
index 270c1f8..aa9dc8a 100755
--- a/scripts/core/sxmo_common.sh
+++ b/scripts/core/sxmo_common.sh
@@ -15,6 +15,7 @@ command -v shopt > /dev/null && shopt -s expand_aliases
 
 alias dmenu="sxmo_dmenu.sh"
 alias bemenu="sxmo_dmenu.sh"
+alias superctl=sxmo_hook_service.sh
 
 if ! command -v sxmobar > /dev/null; then
 	sxmobar() {
diff --git a/scripts/core/sxmo_winit.sh b/scripts/core/sxmo_winit.sh
index 220daac..a8c381f 100755
--- a/scripts/core/sxmo_winit.sh
+++ b/scripts/core/sxmo_winit.sh
@@ -23,6 +23,7 @@ with_dbus() {
 }
 
 cleanup() {
+	sxmo_hook_service.sh sxmo_stopping
 	sxmo_jobs.sh stop all
 	pkill bemenu
 	pkill wvkbd
diff --git a/scripts/core/sxmo_xinit.sh b/scripts/core/sxmo_xinit.sh
index cafb1ab..e6a29ed 100755
--- a/scripts/core/sxmo_xinit.sh
+++ b/scripts/core/sxmo_xinit.sh
@@ -45,6 +45,7 @@ with_dbus() {
 }
 
 cleanup() {
+	sxmo_hook_service.sh sxmo_stopping
 	sxmo_jobs.sh stop all
 	pkill svkbd
 	pkill dmenu
-- 
2.45.0