Centralize wakelock management in sxmo_wakelock.sh

Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
Signed-off-by: Peter John Hartman <peterjohnhartman@gmail.com>
This commit is contained in:
Willow Barraco
2023-02-23 13:20:07 +01:00
committed by Peter John Hartman
parent 4cfdfa3b26
commit b8e6cf3330
18 changed files with 114 additions and 85 deletions

View File

@@ -2,9 +2,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
if [ -f /sys/power/wake_lock ]; then
echo not_screenoff | doas tee -a /sys/power/wake_lock > /dev/null
fi
sxmo_wakelock.sh lock not_screenoff infinite
# Go to locker after 5 minutes of inactivity
if [ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ]; then

View File

@@ -6,8 +6,6 @@
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
if [ -f /sys/power/wake_lock ]; then
echo "stay_awake ${SXMO_UNLOCK_IDLE_TIME:-120}000000000" | doas tee -a /sys/power/wake_lock > /dev/null
fi
sxmo_wakelock.sh lock stay_awake "${SXMO_UNLOCK_IDLE_TIME:-120}000000000"
# Add here whatever you want to do

View File

@@ -11,9 +11,7 @@
sxmo_log "transitioning to stage unlock"
printf unlock > "$SXMO_STATE"
if [ -f /sys/power/wake_lock ]; then
echo "stay_awake ${SXMO_UNLOCK_IDLE_TIME:-120}000000000" | doas tee -a /sys/power/wake_lock > /dev/null
fi
sxmo_wakelock.sh lock stay_awake "${SXMO_UNLOCK_IDLE_TIME:-120}000000000"
sxmo_hook_statusbar.sh state_change &
@@ -27,6 +25,6 @@ sxmo_daemons.sh start periodic_state_mutex_check sxmo_run_periodically.sh 10 sxm
# suspend after if no activity after 120s
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
timeout "1" '' \
resume "echo \"stay_awake ${SXMO_UNLOCK_IDLE_TIME:-120}000000000\" | doas tee -a /sys/power/wake_lock > /dev/null"
resume "sxmo_wakelock.sh lock stay_awake \"${SXMO_UNLOCK_IDLE_TIME:-120}000000000\""
wait

View File

@@ -9,39 +9,31 @@
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
lock_suspend_mutex() {
echo "$1" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_debug "lock \"$1\""
}
free_suspend_mutex() {
echo "$1" | doas tee -a /sys/power/wake_unlock > /dev/null 2>&1
sxmo_debug "free \"$1\""
}
cleanup_main_mutex() {
free_suspend_mutex "checking_mutexes"
sxmo_wakelock.sh unlock checking_mutexes
exit 0
}
exec 3<> "${XDG_RUNTIME_DIR:-$HOME}/sxmo.checkstatemutexes.lock"
flock -x 3
lock_suspend_mutex "checking_mutexes"
DEFAULT_DURATION=30000000000 # 30s to be sure to not lock indefinitely
sxmo_wakelock.sh lock checking_mutexes "$DEFAULT_DURATION"
trap 'cleanup_main_mutex' TERM INT EXIT
# ongoing_call
if pgrep -f sxmo_modemcall.sh > /dev/null; then
lock_suspend_mutex "ongoing_call"
sxmo_wakelock.sh lock ongoing_call "$DEFAULT_DURATION"
else
free_suspend_mutex "ongoing_call"
sxmo_wakelock.sh unlock ongoing_call
fi
# hotspot active
if nmcli -t c show --active | grep ^Hotspot; then
lock_suspend_mutex "hotspot_active"
sxmo_wakelock.sh lock hotspot_active "$DEFAULT_DURATION"
else
free_suspend_mutex "hotspot_active"
sxmo_wakelock.sh unlock hotspot_active
fi
ssh_connected() {
@@ -54,44 +46,44 @@ ssh_connected() {
# active_ssh
if ssh_connected; then
lock_suspend_mutex "ssh_connected"
sxmo_wakelock.sh lock ssh_connected "$DEFAULT_DURATION"
else
free_suspend_mutex "ssh_connected"
sxmo_wakelock.sh unlock ssh_connected
fi
# active_mosh
if command -v mosh-server > /dev/null && pgrep -f mosh-server > /dev/null; then
lock_suspend_mutex "mosh_listening"
sxmo_wakelock.sh lock mosh_listening "$DEFAULT_DURATION"
else
free_suspend_mutex "mosh_listening"
sxmo_wakelock.sh unlock mosh_listening
fi
# playing_mpc
if command -v mpc > /dev/null && mpc status 2>/dev/null | grep -q '\[playing\]'; then
lock_suspend_mutex "mpd_playing"
sxmo_wakelock.sh lock mpd_playing "$DEFAULT_DURATION"
else
free_suspend_mutex "mpd_playing"
sxmo_wakelock.sh unlock mpd_playing
fi
# mpris compatible media player
if command -v playerctl > /dev/null; then
if test "$(playerctl status 2>/dev/null)" = "Playing"; then
lock_suspend_mutex "mpris_playing"
sxmo_wakelock.sh lock mpris_playing "$DEFAULT_DURATION"
else
free_suspend_mutex "mpris_playing"
sxmo_wakelock.sh unlock mpris_playing
fi
fi
# photos_processing
if pgrep -f postprocess > /dev/null; then
lock_suspend_mutex "camera_postprocessing"
sxmo_wakelock.sh lock camera_postprocessing "$DEFAULT_DURATION"
else
free_suspend_mutex "camera_postprocessing"
sxmo_wakelock.sh unlock camera_postprocessing
fi
# auto_suspend
if [ -e "$XDG_CACHE_HOME/sxmo/sxmo.nosuspend" ]; then
lock_suspend_mutex "manually_disabled"
sxmo_wakelock.sh lock manually_disabled "$DEFAULT_DURATION"
else
free_suspend_mutex "manually_disabled"
sxmo_wakelock.sh unlock manually_disabled
fi

View File

@@ -99,7 +99,7 @@ case "$WMCLASS" in
$icon_sfl Migrate configuration ^ 0 ^ sxmo_terminal.sh sxmo_migrate.sh
$icon_cfg Edit configuration ^ 0 ^ sxmo_terminal.sh $EDITOR $XDG_CONFIG_HOME/sxmo/$(test "$SXMO_WM" = sway && printf sway || printf xinit)
$(command -v pmos-tweaks >/dev/null && echo "$icon_cfg PostmarketOS Tweaks ^ 0 ^ GDK_SCALE=1 pmos-tweaks")
$icon_cfg Suspend Blockers ^ 0 ^ sxmo_terminal.sh sxmo_mutex_debug.sh
$icon_cfg Suspend Blockers ^ 0 ^ sxmo_terminal.sh sxmo_wakelock.sh debug
$icon_inf Version ^ 0 ^ sxmo_terminal.sh sxmo_version.sh --block
"
WINNAME=Config

View File

@@ -12,9 +12,7 @@ flock -x 3
sxmo_log "transitioning to stage lock"
printf lock > "$SXMO_STATE"
if [ -f /sys/power/wake_lock ]; then
echo not_screenoff | doas tee -a /sys/power/wake_lock > /dev/null
fi
sxmo_wakelock.sh lock not_screenoff infinite
# This hook is called when the system reaches a locked state

View File

@@ -6,25 +6,10 @@
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
UNSUSPENDREASON="$1"
#The UNSUSPENDREASON can be "usb power", "modem", "rtc" (real-time clock
#periodic wakeup) or "button". You will likely want to check against this and
#decide what to do
# stay awake if modem has crashed for 30s to give modem time to recover
modemloop() {
echo "modem_crashed" | doas tee -a /sys/power/wake_lock > /dev/null
sleep 30s
echo "modem_crashed" | doas tee -a /sys/power/wake_unlock > /dev/null
}
if [ "$UNSUSPENDREASON" = "modem" ]; then
MMCLI="$(mmcli -m any -J 2>/dev/null)"
if [ -z "$MMCLI" ]; then
sxmo_notify_user.sh --urgency=critical "Modem crashed! 30s recovery."
modemloop &
fi
sxmo_wakelock.sh lock modem_crashed 30000000000
fi
# see the comments in sxmo_hook_lock.sh

View File

@@ -45,5 +45,5 @@ sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
wait
if [ -f /sys/power/wake_unlock ]; then
echo not_screenoff | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock not_screenoff
fi

View File

@@ -14,9 +14,7 @@ flock -x 3
sxmo_log "transitioning to stage unlock"
printf unlock > "$SXMO_STATE"
if [ -f /sys/power/wake_lock ]; then
echo not_screenoff | doas tee -a /sys/power/wake_lock > /dev/null
fi
sxmo_wakelock.sh lock not_screenoff infinite
sxmo_led.sh blink red green &
sxmo_hook_statusbar.sh state_change &

View File

@@ -28,7 +28,7 @@ autosuspend() {
mnc="$(sxmo_hook_mnc.sh)"
if [ -n "$mnc" ] && [ "$mnc" -gt 0 ] && [ "$mnc" -lt "$YEARS8_TO_SEC" ]; then
if [ "$mnc" -le 15 ]; then # cronjob imminent
echo "waiting_cronjob" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock waiting_cronjob infinite
suspend_time=$((mnc + 1)) # to arm the following one
else
suspend_time=$((mnc - 10))
@@ -46,7 +46,7 @@ autosuspend() {
wokeup() {
# 10s basic hold
echo "woke_up 10000000000" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock woke_up 10000000000
sxmo_hook_postwake.sh
}

View File

@@ -6,12 +6,12 @@
. sxmo_common.sh
free_mutex() {
echo "playing_with_leds" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock playing_with_leds
exit
}
ensure_mutex() {
echo "playing_with_leds" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock playing_with_leds 2000000000
trap 'free_mutex' TERM INT
}

View File

@@ -1,5 +0,0 @@
#! /bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
watch 'cat /sys/power/wake_lock | tr " " "\n"'

View File

@@ -19,7 +19,7 @@ finish() {
# (see static const int stk3310_ps_max[4])
printf 6553 > "$prox_path/events/in_proximity_thresh_rising_value"
echo "proximity_lock_running" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock proximity_lock_running
sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
exec sxmo_hook_"$INITIALSTATE".sh
@@ -50,7 +50,7 @@ trap 'finish' TERM INT
sxmo_daemons.sh stop idle_locker
echo "proximity_lock_running" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock proximity_lock_running infinite
sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
# find the device

View File

@@ -8,11 +8,11 @@
. sxmo_common.sh
# We can have multiple cronjobs at the same time
echo "executing_cronjob" | doas tee -a /sys/power/wake_lock > /dev/null
echo "waiting_cronjob" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh lock executing_cronjob infinite
sxmo_wakelock.sh unlock waiting_cronjob
finish() {
echo "executing_cronjob" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock executing_cronjob
exit 0
}

View File

@@ -34,7 +34,7 @@ update_nixos() {
echo "Upgrade complete - reboot for all changes to take effect"
}
echo "upgrading" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock upgrading infinite
case "$OS" in
alpine|postmarketos) update_apk;;
@@ -42,4 +42,4 @@ case "$OS" in
nixos) update_nixos;;
esac
echo "upgrading" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock upgrading

View File

@@ -0,0 +1,67 @@
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
usage() {
cat >&2 <<EOF
Usage: $(basename "$0") ACTION
lock <lock-name> <nanosec|infinite>
unlock <lock-name>
EOF
}
lock() {
if [ "$#" -ne 2 ]; then
usage
exit 1
fi
if [ ! -f /sys/power/wake_lock ]; then
exit # we swallow when the system doesn't support it
fi
if [ "$2" = "infinite" ]; then
sxmo_debug "lock $1 infinite"
echo "$1" | doas tee -a /sys/power/wake_lock > /dev/null
exit
fi
if [ "$2" -ne "$2" ]; then
echo "$2 isn't a duration" >&2
exit 1
fi
sxmo_debug "lock $1 $2"
echo "$1 $2" | doas tee -a /sys/power/wake_lock > /dev/null
}
unlock() {
if [ "$#" -ne 1 ]; then
usage
exit 1
fi
if [ ! -f /sys/power/wake_unlock ]; then
exit # we swallow when the system doesn't support it
fi
sxmo_debug "unlock $1"
echo "$1" | doas tee -a /sys/power/wake_unlock > /dev/null 2>&1
}
debug() {
tr " " "\n" < /sys/power/wake_lock | grep .
tail -f "$XDG_STATE_HOME"/sxmo.log | grep "${0##*/}"
}
cmd="$1"
shift
case "$cmd" in
lock) lock "$@";;
unlock) unlock "$@";;
debug) debug "$@";;
*) usage; exit 1;;
esac

View File

@@ -286,6 +286,6 @@ processmms() {
fi
}
echo "mms_processing" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock mms_processing 30000000000
"$@"
echo "mms_processing" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock mms_processing 30000000000

View File

@@ -260,6 +260,6 @@ checkfornewtexts() {
done
}
echo "modem_used" | doas tee -a /sys/power/wake_lock > /dev/null
sxmo_wakelock.sh lock modem_used 30000000000
"$@"
echo "modem_used" | doas tee -a /sys/power/wake_unlock > /dev/null
sxmo_wakelock.sh unlock modem_used