If two power handler trigger at the same time, the current behavior is
that we switch two time to the same target state.
This move the flock to a central script that make sure only one switch
is ocurring at the same time. Concurrent switch will wait for the
previous one to finish before guessing the new target state.
This also refactorise the sxmo_proximitylock.sh to make it simple,
stripping the two related proximitylock and proximityunlock states.
Signed-off-by: Willow Barraco <contact@willowbarraco.fr>
---
.../default_hooks/desktop/sxmo_hook_locker.sh | 2 +-
.../one_button_e_reader/sxmo_hook_unlock.sh | 3 -
.../default_hooks/sxmo_hook_contextmenu.sh | 6 +-
configs/default_hooks/sxmo_hook_start.sh | 2 +-
configs/default_hooks/sxmo_hook_statusbar.sh | 8 +-
.../sxmo_hook_inputhandler.sh | 37 ++-------
.../sxmo_hook_lock.sh | 9 +--
.../sxmo_hook_screenoff.sh | 7 --
.../sxmo_hook_unlock.sh | 11 +--
scripts/core/sxmo_proximitylock.sh | 33 ++++----
scripts/core/sxmo_state_switch.sh | 76 +++++++++++++++++++
scripts/modem/sxmo_mms.sh | 2 +-
scripts/modem/sxmo_modem.sh | 2 +-
scripts/modem/sxmo_vvm.sh | 2 +-
14 files changed, 114 insertions(+), 86 deletions(-)
create mode 100644 scripts/core/sxmo_state_switch.sh
diff --git a/configs/default_hooks/desktop/sxmo_hook_locker.sh b/configs/default_hooks/desktop/sxmo_hook_locker.sh
index b72c2f7..63e9e1e 100755
--- a/configs/default_hooks/desktop/sxmo_hook_locker.sh
+++ b/configs/default_hooks/desktop/sxmo_hook_locker.sh
@@ -15,4 +15,4 @@ case "$SXMO_WM" in
;;
esac
-sxmo_hook_unlock.sh
+sxmo_state_switch.sh set unlock
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 ab19a50..8fedc32 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
@@ -8,9 +8,6 @@
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
-sxmo_log "transitioning to stage unlock"
-printf unlock > "$SXMO_STATE"
-
sxmo_wakelock.sh lock sxmo_stay_awake "${SXMO_UNLOCK_IDLE_TIME:-120}s"
sxmo_hook_statusbar.sh state_change &
diff --git a/configs/default_hooks/sxmo_hook_contextmenu.sh b/configs/default_hooks/sxmo_hook_contextmenu.sh
index cc3273f..f3e9b0d 100755
--- a/configs/default_hooks/sxmo_hook_contextmenu.sh
@@ -88,7 +88,7 @@ case "$WMCLASS" in
) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.nosuspend 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.nosuspend)
$icon_zzz Auto-screen-off $(
[ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ] && printf "%s" "$icon_tof" || printf "%s" "$icon_ton"
- ) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.noidle 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.noidle) && sxmo_hook_unlock.sh
+ ) ^ 1 ^ (rm $XDG_CACHE_HOME/sxmo/sxmo.noidle 2>/dev/null || touch $XDG_CACHE_HOME/sxmo/sxmo.noidle) && sxmo_state_switch.sh set unlock
$icon_ror Autorotate $(
sxmo_daemons.sh running autorotate -q &&
printf "%s" "$icon_ton" || printf "%s" "$icon_tof"
@@ -107,8 +107,8 @@ case "$WMCLASS" in
power)
# Power menu
CHOICES="
- $icon_lck Lock ^ 0 ^ sxmo_hook_lock.sh
- $icon_lck Lock (Screen off) ^ 0 ^ sxmo_hook_screenoff.sh
+ $icon_lck Lock ^ 0 ^ sxmo_state_switch.sh set lock
+ $icon_lck Lock (Screen off) ^ 0 ^ sxmo_state_switch.sh set screenoff
$icon_out Logout ^ 0 ^ confirm Logout && sxmo_power.sh logout
$([ -f "$(xdg_data_path xsessions/sxmo.desktop)" ] &&
[ -f "$(xdg_data_path wayland-sessions/swmo.desktop)" ] &&
diff --git a/configs/default_hooks/sxmo_hook_start.sh b/configs/default_hooks/sxmo_hook_start.sh
index 5064777..728035e 100755
--- a/configs/default_hooks/sxmo_hook_start.sh
+++ b/configs/default_hooks/sxmo_hook_start.sh
@@ -66,7 +66,7 @@ case "$SXMO_WM" in
esac
# To setup initial lock state
-sxmo_hook_unlock.sh
+sxmo_state_switch.sh set unlock
# Turn on auto-suspend
if [ -w "/sys/power/wakeup_count" ] && [ -f "/sys/power/wake_lock" ]; then
diff --git a/configs/default_hooks/sxmo_hook_statusbar.sh b/configs/default_hooks/sxmo_hook_statusbar.sh
index d5c3ac1..a2a04fe 100755
--- a/configs/default_hooks/sxmo_hook_statusbar.sh
+++ b/configs/default_hooks/sxmo_hook_statusbar.sh
@@ -27,11 +27,13 @@ set_state() {
return
fi
+ if sxmo_daemons.sh running proximity_lock; then
+ sxmobar -a -e bold -f orange state 90 "$icon_state_proximity" # circle with dot
+ return
+ fi
+
STATE_LABEL="$(cat "$SXMO_STATE")"
case "$STATE_LABEL" in
- proximity*)
- sxmobar -a -e bold -f orange state 90 "$icon_state_proximity" # circle with dot
- ;;
screenoff)
sxmobar -a -e bold -f red state 90 "$icon_state_screenoff" # filled circle
;;
diff --git a/configs/default_hooks/three_button_touchscreen/sxmo_hook_inputhandler.sh b/configs/default_hooks/three_button_touchscreen/sxmo_hook_inputhandler.sh
index 8747dce..957de4a 100755
--- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_inputhandler.sh
+++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_inputhandler.sh
@@ -15,29 +15,6 @@ stop_proximity_lock() {
sxmo_daemons.sh stop proximity_lock
}
-# this action will move the lock state $1 levels higher
-lock_screen_action() {
- count="${1:-1}"
-
- state="$(cat "$SXMO_STATE")"
- while [ "$count" -gt 0 ]; do
- case "$state" in
- unlock)
- state=screenoff
- ;;
- screenoff)
- state=lock
- ;;
- lock)
- state=unlock
- ;;
- esac
- count=$((count-1))
- done
-
- sxmo_hook_"$state".sh
-}
-
XPROPOUT="$(sxmo_wm.sh focusedwindow)"
WMCLASS="$(printf %s "$XPROPOUT" | grep app: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')"
WMNAME="$(printf %s "$XPROPOUT" | grep title: | cut -d" " -f2- | tr '[:upper:]' '[:lower:]')"
@@ -61,16 +38,16 @@ if ! grep -q unlock "$SXMO_STATE"; then
esac
case "$ACTION" in
"powerbutton_one")
- lock_screen_action
+ sxmo_state_switch.sh up
;;
"powerbutton_two")
- lock_screen_action 2
+ sxmo_state_switch.sh up 2
;;
"powerbutton_three")
if grep -q proximity "$SXMO_STATE"; then
stop_proximity_lock
else
- lock_screen_action 2
+ sxmo_state_switch.sh up 2
fi
;;
"voldown_one")
@@ -356,12 +333,12 @@ case "$ACTION" in
if echo "$WMCLASS" | grep -i "megapixels"; then
sxmo_type.sh -k space
else
- lock_screen_action
+ sxmo_state_switch.sh up
fi
exit 0
;;
"powerbutton_two")
- lock_screen_action 2
+ sxmo_state_switch.sh up 2
exit 0
;;
"powerbutton_three")
@@ -496,9 +473,9 @@ case "$ACTION" in
sxmo_dmenu.sh close
sxmo_keyboard.sh close
if [ -n "$WMCLASS" ]; then
- sxmo_hook_lock.sh
+ sxmo_state_switch.sh set lock
else
- sxmo_hook_screenoff.sh
+ sxmo_state_switch.sh set screenoff
fi
exit 0
;;
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 efbfaf5..11ebb42 100755
--- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_lock.sh
+++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_lock.sh
@@ -14,18 +14,11 @@
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
-exec 3<> "$SXMO_STATE.lock"
-flock -x 3
-
-sxmo_log "transitioning to stage lock"
-printf lock > "$SXMO_STATE"
-
sxmo_wakelock.sh lock sxmo_not_screenoff infinite
# This hook is called when the system reaches a locked state
sxmo_led.sh blink blue &
-sxmo_hook_statusbar.sh state_change &
[ "$SXMO_WM" = "sway" ] && swaymsg mode default
sxmo_wm.sh dpms off
@@ -37,7 +30,7 @@ sxmo_daemons.sh stop periodic_wakelock_check
# Go to screenoff after 8 seconds of inactivity
if ! [ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ]; then
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
- timeout "${SXMO_LOCK_IDLE_TIME:-8}" "sxmo_hook_screenoff.sh"
+ timeout "${SXMO_LOCK_IDLE_TIME:-8}" "sxmo_state_switch.sh set screenoff"
fi
wait
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 5c45c42..84ad692 100755
--- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_screenoff.sh
+++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_screenoff.sh
@@ -16,13 +16,6 @@
# This hook is called when the system reaches a off state (screen off)
-exec 3<> "$SXMO_STATE.lock"
-flock -x 3
-
-sxmo_log "transitioning to stage off"
-printf screenoff > "$SXMO_STATE"
-sxmo_hook_statusbar.sh state_change &
-
[ "$SXMO_WM" = "sway" ] && swaymsg mode default
sxmo_wm.sh dpms on
sxmo_wm.sh inputevent touchscreen off
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 3b40f27..7815d4b 100755
--- a/configs/default_hooks/three_button_touchscreen/sxmo_hook_unlock.sh
+++ b/configs/default_hooks/three_button_touchscreen/sxmo_hook_unlock.sh
@@ -8,16 +8,9 @@
# This hook is called when the system becomes unlocked again
-exec 3<> "$SXMO_STATE.lock"
-flock -x 3
-
-sxmo_log "transitioning to stage unlock"
-printf unlock > "$SXMO_STATE"
-
sxmo_wakelock.sh lock sxmo_not_screenoff infinite
sxmo_led.sh blink red green &
-sxmo_hook_statusbar.sh state_change &
sxmo_wm.sh dpms off
sxmo_wm.sh inputevent touchscreen on
@@ -34,12 +27,12 @@ else
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" 'sh -c "
swaymsg mode default;
- exec sxmo_hook_lock.sh
+ exec sxmo_state_switch.sh set lock
"'
;;
dwm)
sxmo_daemons.sh start idle_locker sxmo_idle.sh -w \
- timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" "sxmo_hook_lock.sh"
+ timeout "${SXMO_UNLOCK_IDLE_TIME:-120}" "sxmo_state_switch.sh set lock"
;;
esac
fi
diff --git a/scripts/core/sxmo_proximitylock.sh b/scripts/core/sxmo_proximitylock.sh
index c477011..0a11251 100755
--- a/scripts/core/sxmo_proximitylock.sh
+++ b/scripts/core/sxmo_proximitylock.sh
@@ -20,38 +20,35 @@ finish() {
printf 6553 > "$prox_path/events/in_proximity_thresh_rising_value"
sxmo_wakelock.sh unlock sxmo_proximity_lock_running
- sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
- exec sxmo_hook_"$INITIALSTATE".sh
+ if [ -n "$INITIALSTATE" ]; then
+ sxmo_state_switch.sh set "$INITIALSTATE"
+ fi
+
+ exit
}
near() {
+ if [ -z "$INITIALSTATE" ]; then
+ INITIALSTATE="$(cat "$SXMO_STATE")"
+ fi
+
sxmo_debug "near"
- sxmo_wm.sh dpms on
- sxmo_wm.sh inputevent touchscreen off
- printf proximitylock > "$SXMO_STATE"
+ sxmo_state_switch.sh set screenoff
}
far() {
+ if [ -z "$INITIALSTATE" ]; then
+ INITIALSTATE="$(cat "$SXMO_STATE")"
+ fi
+
sxmo_debug "far"
- sxmo_wm.sh dpms off
- sxmo_wm.sh inputevent touchscreen on
- printf proximityunlock > "$SXMO_STATE"
+ sxmo_state_switch.sh set unlock
}
-exec 3<> "$SXMO_STATE.lock"
-flock -x 3
-
-sxmo_log "transitioning to stage proximitylock"
-INITIALSTATE="$(cat "$SXMO_STATE")"
-printf proximitylock > "$SXMO_STATE"
-
trap 'finish' TERM INT
-sxmo_daemons.sh stop idle_locker
-
sxmo_wakelock.sh lock sxmo_proximity_lock_running infinite
-sxmo_daemons.sh start state_change_bar sxmo_hook_statusbar.sh state_change
# find the device
if [ -z "$SXMO_PROX_RAW_BUS" ]; then
diff --git a/scripts/core/sxmo_state_switch.sh b/scripts/core/sxmo_state_switch.sh
new file mode 100644
index 0000000..ef72971
--- /dev/null
+++ b/scripts/core/sxmo_state_switch.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+# include common definitions
+# shellcheck source=scripts/core/sxmo_common.sh
+. sxmo_common.sh
+
+transition() {
+ sxmo_log "transitioning to stage $state"
+ printf %s "$state" > "$SXMO_STATE"
+
+ sxmo_hook_"$state".sh &
+ sxmo_hook_statusbar.sh state_change &
+ wait
+}
+
+up() {
+ count="${1:-1}"
+ while [ "$count" -gt 0 ]; do
+ case "$state" in
+ unlock)
+ state=screenoff
+ ;;
+ screenoff)
+ state=lock
+ ;;
+ lock)
+ state=unlock
+ ;;
+ esac
+ count=$((count-1))
+ done
+ transition
+}
+
+down() {
+ count="${1:-1}"
+ while [ "$count" -gt 0 ]; do
+ case "$state" in
+ unlock)
+ state=lock
+ ;;
+ screenoff)
+ state=unlock
+ ;;
+ lock)
+ state=screenoff
+ ;;
+ esac
+ count=$((count-1))
+ done
+ transition
+}
+
+exec 3<> "$SXMO_STATE.lock"
+flock -x 3
+
+state="$(cat "$SXMO_STATE")"
+
+action="$1"
+shift
+case "$action" in
+ up)
+ up "$@"
+ ;;
+ down)
+ down "$@"
+ ;;
+ set)
+ case "$1" in
+ lock|unlock|screenoff)
+ state="$1"
+ transition
+ ;;
+ esac
+ ;;
+esac
diff --git a/scripts/modem/sxmo_mms.sh b/scripts/modem/sxmo_mms.sh
index 00a0764..8ae6d88 100755
--- a/scripts/modem/sxmo_mms.sh
+++ b/scripts/modem/sxmo_mms.sh
@@ -199,7 +199,7 @@ processmms() {
fi
if grep -q screenoff "$SXMO_STATE"; then
- sxmo_hook_lock.sh
+ sxmo_state_switch.sh set lock
fi
if [ "$NUM_RECIPIENTS" -gt 1 ]; then
diff --git a/scripts/modem/sxmo_modem.sh b/scripts/modem/sxmo_modem.sh
index af5b494..6f22c20 100755
--- a/scripts/modem/sxmo_modem.sh
+++ b/scripts/modem/sxmo_modem.sh
@@ -266,7 +266,7 @@ checkfornewtexts() {
fi
if grep -q screenoff "$SXMO_STATE"; then
- sxmo_hook_lock.sh
+ sxmo_state_switch.sh set lock
fi
sxmo_hook_sms.sh "$CONTACTNAME" "$TEXT"
diff --git a/scripts/modem/sxmo_vvm.sh b/scripts/modem/sxmo_vvm.sh
index 3aeb929..b0acb24 100755
--- a/scripts/modem/sxmo_vvm.sh
+++ b/scripts/modem/sxmo_vvm.sh
@@ -44,7 +44,7 @@ processvvm() {
fi
if grep -q screenoff "$SXMO_STATE"; then
- sxmo_hook_lock.sh
+ sxmo_state_switch.sh set lock
fi
sxmo_hook_sms.sh "$VVM_SENDER" "VVM" "$VVM_ID"
--
2.42.0