diff --git a/configs/default_hooks/desktop/sxmo_hook_unlock.sh b/configs/default_hooks/desktop/sxmo_hook_unlock.sh index d2c08c0..3e30697 100755 --- a/configs/default_hooks/desktop/sxmo_hook_unlock.sh +++ b/configs/default_hooks/desktop/sxmo_hook_unlock.sh @@ -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 diff --git a/configs/default_hooks/one_button_e_reader/sxmo_hook_postwake.sh b/configs/default_hooks/one_button_e_reader/sxmo_hook_postwake.sh index 1110804..6fdc55f 100755 --- a/configs/default_hooks/one_button_e_reader/sxmo_hook_postwake.sh +++ b/configs/default_hooks/one_button_e_reader/sxmo_hook_postwake.sh @@ -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 diff --git a/configs/default_hooks/one_button_e_reader/sxmo_hook_unlock.sh b/configs/default_hooks/one_button_e_reader/sxmo_hook_unlock.sh index 280071c..a6618ad 100755 --- a/configs/default_hooks/one_button_e_reader/sxmo_hook_unlock.sh +++ b/configs/default_hooks/one_button_e_reader/sxmo_hook_unlock.sh @@ -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 diff --git a/configs/default_hooks/sxmo_hook_check_state_mutexes.sh b/configs/default_hooks/sxmo_hook_check_state_mutexes.sh index e96396f..d812def 100755 --- a/configs/default_hooks/sxmo_hook_check_state_mutexes.sh +++ b/configs/default_hooks/sxmo_hook_check_state_mutexes.sh @@ -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 diff --git a/configs/default_hooks/sxmo_hook_contextmenu.sh b/configs/default_hooks/sxmo_hook_contextmenu.sh index 4471cf7..9743176 100755 --- a/configs/default_hooks/sxmo_hook_contextmenu.sh +++ b/configs/default_hooks/sxmo_hook_contextmenu.sh @@ -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 diff --git a/configs/default_hooks/three_button_touchscreen/sxmo_hook_lock.sh b/configs/default_hooks/three_button_touchscreen/sxmo_hook_lock.sh index e52c07e..8ec4a2a 100755 --- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_lock.sh +++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_lock.sh @@ -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 diff --git a/configs/default_hooks/three_button_touchscreen/sxmo_hook_postwake.sh b/configs/default_hooks/three_button_touchscreen/sxmo_hook_postwake.sh index 5eacb3f..5c98e65 100755 --- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_postwake.sh +++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_postwake.sh @@ -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 +MMCLI="$(mmcli -m any -J 2>/dev/null)" +if [ -z "$MMCLI" ]; then + sxmo_notify_user.sh --urgency=critical "Modem crashed! 30s recovery." + sxmo_wakelock.sh lock modem_crashed 30000000000 fi # see the comments in sxmo_hook_lock.sh diff --git a/configs/default_hooks/three_button_touchscreen/sxmo_hook_screenoff.sh b/configs/default_hooks/three_button_touchscreen/sxmo_hook_screenoff.sh index 8d0a32b..5a0fbfc 100755 --- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_screenoff.sh +++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_screenoff.sh @@ -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 diff --git a/configs/default_hooks/three_button_touchscreen/sxmo_hook_unlock.sh b/configs/default_hooks/three_button_touchscreen/sxmo_hook_unlock.sh index b07e167..3486b50 100755 --- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_unlock.sh +++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_unlock.sh @@ -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 & diff --git a/scripts/core/sxmo_autosuspend.sh b/scripts/core/sxmo_autosuspend.sh index ebc7e39..c7333e3 100755 --- a/scripts/core/sxmo_autosuspend.sh +++ b/scripts/core/sxmo_autosuspend.sh @@ -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 } diff --git a/scripts/core/sxmo_led.sh b/scripts/core/sxmo_led.sh index 58d940c..cad0088 100755 --- a/scripts/core/sxmo_led.sh +++ b/scripts/core/sxmo_led.sh @@ -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 } diff --git a/scripts/core/sxmo_mutex_debug.sh b/scripts/core/sxmo_mutex_debug.sh deleted file mode 100644 index 18e729c..0000000 --- a/scripts/core/sxmo_mutex_debug.sh +++ /dev/null @@ -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"' diff --git a/scripts/core/sxmo_proximitylock.sh b/scripts/core/sxmo_proximitylock.sh index b463a5d..f8e779c 100755 --- a/scripts/core/sxmo_proximitylock.sh +++ b/scripts/core/sxmo_proximitylock.sh @@ -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 diff --git a/scripts/core/sxmo_rtcwake.sh b/scripts/core/sxmo_rtcwake.sh index a7e0c6f..ec3620b 100755 --- a/scripts/core/sxmo_rtcwake.sh +++ b/scripts/core/sxmo_rtcwake.sh @@ -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 } diff --git a/scripts/core/sxmo_upgrade.sh b/scripts/core/sxmo_upgrade.sh index 8b21688..714d23f 100755 --- a/scripts/core/sxmo_upgrade.sh +++ b/scripts/core/sxmo_upgrade.sh @@ -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 diff --git a/scripts/core/sxmo_wakelock.sh b/scripts/core/sxmo_wakelock.sh new file mode 100644 index 0000000..8b019b5 --- /dev/null +++ b/scripts/core/sxmo_wakelock.sh @@ -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 < + unlock +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 diff --git a/scripts/modem/sxmo_mms.sh b/scripts/modem/sxmo_mms.sh index c95bf32..2f63cc6 100755 --- a/scripts/modem/sxmo_mms.sh +++ b/scripts/modem/sxmo_mms.sh @@ -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 diff --git a/scripts/modem/sxmo_modem.sh b/scripts/modem/sxmo_modem.sh index 016ca30..1cf887a 100755 --- a/scripts/modem/sxmo_modem.sh +++ b/scripts/modem/sxmo_modem.sh @@ -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