~mil/sxmo-utils

sxmo-utils/scripts/core/sxmo_appmenu.sh -rwxr-xr-x 3.2 KiB
c0e64920Maarten van Gompel improve detection of active app_id and title for xwayland apps 4 days ago
                                                                                
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
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

# include common definitions
# shellcheck source=configs/default_hooks/sxmo_hook_icons.sh
. sxmo_hook_icons.sh
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh

confirm() {
	PICKED="$(printf "Yes\nNo\n" | sxmo_dmenu.sh -p "Confirm $1")"

	if [ "$PICKED" = "Yes" ]; then
		return 0
	else
		return 1
	fi
}

supertoggle_daemon() {
	if superctl status "$1" | grep -q started; then
		superctl stop "$1"
		sxmo_notify_user.sh "$1 Stopped"
	else
		superctl start "$1"
		sxmo_notify_user.sh "$1 Started"
	fi

}

toggle_daemon() {
	name="$1"
	shift

	if sxmo_daemons.sh running "$1" -q; then
		sxmo_daemons.sh stop "$@"
		notify-send "$name Stopped"
	else
		sxmo_daemons.sh start "$@" &
		notify-send "$name Started"
	fi
}

sxmo_type() {
	sxmo_type.sh -s 200 "$@" # dunno why this is necessary but it sucks without
}

call_entries() {
	shown_incall_menu=
	sxmo_modemcall.sh list_active_calls | while read -r line; do
		case "$line" in
			*"(ringing-in)")
				CALLID="$(printf %s "$line" | cut -d" " -f1 | xargs basename)"
				NUMBER="$(sxmo_modemcall.sh vid_to_number "$CALLID")"
				CONTACT="$(sxmo_contacts.sh --name "$NUMBER")"

				printf "%s Incoming call %s ^ 0 ^ sxmo_daemons.sh start incall_menu sxmo_modemcall.sh incoming_call_menu %s\n" \
					"$icon_phn" "$CONTACT" "$CALLID"
				;;
			*)
				[ -n "$shown_incall_menu" ] && continue
				shown_incall_menu=1
				printf "%s Incall Menu ^ 0 ^ sxmo_daemons.sh start incall_menu sxmo_modemcall.sh incall_menu\n" \
					"$icon_phn"
				;;
		esac
	done

}

getprogchoices() {
	RES="$(sxmo_hook_contextmenu.sh "$1")"
	if [ -n "$RES" ]; then
		WINNAME="$(printf %s "$RES" | head -n1)"
		CHOICES="$(printf %s "$RES" | tail -n+2)"
	fi

	# For the Sys menu decorate at top with notifications if >1 notification
	if [ "$WINNAME" = "Sys" ]; then
		NNOTIFICATIONS="$(find "$SXMO_NOTIFDIR" -type f | wc -l)"
		if [ "$NNOTIFICATIONS" -gt 0 ]; then
			CHOICES="
				$icon_bel Notifications ($NNOTIFICATIONS) ^ 0 ^ sxmo_notificationsmenu.sh
				$CHOICES
			"
		fi
	fi

	CHOICES="
		$(call_entries)
		$CHOICES
	"

	# Decorate menu at bottom w/ system menu entry if not system menu
	echo "$WINNAME" | grep -qv Sys && CHOICES="
		$CHOICES
		$icon_mnu System Menu   ^ 0 ^ sxmo_appmenu.sh sys
	"

	# Decorate menu at bottom w/ close menu entry
	CHOICES="
		$CHOICES
		$icon_cls Close Menu    ^ 0 ^ quit
	"

	CHOICES="$(printf "%s\n" "$CHOICES" | xargs -0 echo | sed '/^[[:space:]]*$/d' | awk '{$1=$1};1')"
}

quit() {
	exit 0
}

mainloop() {
	getprogchoices "$@"
	PICKED="$(
		printf "%s\n" "$CHOICES" |
		cut -d'^' -f1 |
		sxmo_dmenu.sh -i -p "$WINNAME"
	)" || quit

	CHOICE="$(echo "$CHOICES" | awk -F '^' -v picked="$PICKED" \
		'$1 == picked {print $2 "^" $3}'
	)"

	LOOP="$(echo "$CHOICE" | cut -d '^' -f1)"
	CMD="$(echo "$CHOICE" | cut -d '^' -f2)"

	if [ -z "$CMD" ]; then
		printf "%s\n" "sxmo_appmenu: Fallback: unknown choice <$PICKED> to contextmenu_fallback hook">&2
		sxmo_hooks.sh contextmenu_fallback "$WINNAME" "$PICKED"
		quit
	fi

	printf 'sxmo_appmenu: Eval: <%s> from picked <%s> with loop <%s>\n' \
		"$CMD" "$PICKED" "$LOOP" >&2

	if printf %s "$LOOP" | grep -q 1; then
		eval "$CMD"
		mainloop "$@"
	else
		eval "$CMD"
		quit
	fi
}

mainloop "$@"